001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.file.collectors;
029
030import org.opencms.file.types.I_CmsResourceType;
031import org.opencms.loader.CmsLoaderException;
032import org.opencms.main.CmsIllegalArgumentException;
033import org.opencms.main.CmsLog;
034import org.opencms.main.CmsRuntimeException;
035import org.opencms.main.OpenCms;
036import org.opencms.util.CmsStringUtil;
037
038import java.util.List;
039
040import org.apache.commons.logging.Log;
041
042/**
043 * Extended data structure for the collector, parsed from the collector parameters.<p>
044 *
045 * The input data String must have the following format:<br>
046 * <code>"{VFS URI}|{Resource type}|{Count}|excludeTimerange|{AddParam1}|{AddParam2}..."</code>, for example:<br>
047 * <code>"/my/folder/|xmlcontent|5|excludeTimerange|p1|p2|p3|p4"</code> or <code>"/my/folder/|xmlcontent|5|p1|p2|p3|p4"</code>.<p>
048 *
049 * This extends the basic {@link CmsCollectorData} by allowing to append additional
050 * parameters to the input String. The parameters can then be obtained by the collector
051 * using {@link #getAdditionalParams()}. It will depend on the collector implementation
052 * how these additional parameters are used.<p>
053 *
054 * @since 7.0.2
055 *
056 * @see CmsCollectorData
057 */
058public class CmsExtendedCollectorData extends CmsCollectorData {
059
060    /** The log object for this class. */
061    private static final Log LOG = CmsLog.getLog(CmsExtendedCollectorData.class);
062
063    /** The list of additional parameters. */
064    private List<String> m_additionalParams;
065
066    /**
067     * Required constructor for subclasses.<p>
068     */
069    protected CmsExtendedCollectorData() {
070
071        // NOOP
072    }
073
074    /**
075     * Creates a new extended collector data set.<p>
076     *
077     * The input data String must have the following format:<br>
078     * <code>"{VFS URI}|{Resource type}|{Count}|{AddParam1}|{AddParam2}..."</code>, for example:<br>
079     * <code>"/my/folder/|xmlcontent|5|excludeTimerange|p1|p2|p3|p4"</code> or <code>"/my/folder/|xmlcontent|5|p1|p2|p3|p4"</code>.<p>
080     *
081     * @param data the data to parse
082     */
083    public CmsExtendedCollectorData(String data) {
084
085        if (data == null) {
086            throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_COLLECTOR_PARAM_EMPTY_0));
087        }
088        List<String> args = CmsStringUtil.splitAsList(data, '|', true);
089        if (args.size() < 3) {
090            // we need at least 2 arguments: VFS URI and Resource Type
091            throw new CmsIllegalArgumentException(
092                Messages.get().container(Messages.ERR_COLLECTOR_PARAM_INVALID_1, data));
093        }
094        setFileName(args.get(0));
095        String type = args.get(1);
096        try {
097            // try to look up the resource type
098            I_CmsResourceType resourceType = OpenCms.getResourceManager().getResourceType(type);
099            setType(resourceType.getTypeId());
100        } catch (CmsLoaderException e1) {
101            // maybe the int id is directly used?
102            try {
103                int typeInt = Integer.parseInt(type);
104                I_CmsResourceType resourceType = OpenCms.getResourceManager().getResourceType(typeInt);
105                setType(resourceType.getTypeId());
106                if (LOG.isWarnEnabled()) {
107                    LOG.warn(
108                        Messages.get().getBundle().key(
109                            Messages.LOG_RESTYPE_INTID_2,
110                            resourceType.getTypeName(),
111                            Integer.valueOf(resourceType.getTypeId())));
112                }
113            } catch (NumberFormatException e2) {
114                // bad number format used for type
115                throw new CmsRuntimeException(
116                    Messages.get().container(Messages.ERR_COLLECTOR_PARAM_INVALID_1, data),
117                    e2);
118            } catch (CmsLoaderException e2) {
119                // this resource type does not exist
120                throw new CmsRuntimeException(Messages.get().container(Messages.ERR_UNKNOWN_RESTYPE_1, type), e2);
121            }
122        }
123        setCount(5);
124        if (args.size() > 2) {
125            String count = args.get(2);
126            if (CmsStringUtil.isNotEmpty(count)) {
127                try {
128                    setCount(Integer.parseInt(count));
129                } catch (NumberFormatException e) {
130                    // bad number format used for count
131                    throw new CmsRuntimeException(
132                        Messages.get().container(Messages.ERR_COLLECTOR_PARAM_INVALID_1, data),
133                        e);
134                }
135            }
136        }
137        if (args.size() > 3) {
138            if (PARAM_EXCLUDETIMERANGE.equalsIgnoreCase(args.get(3))) {
139                setExcludeTimerange(true);
140                if (args.size() > 4) {
141                    m_additionalParams = args.subList(4, args.size());
142                }
143            } else {
144                m_additionalParams = args.subList(3, args.size());
145            }
146        }
147    }
148
149    /**
150     * Returns the List of additional parameters (String objects).<p>
151     *
152     * @return the List of additional parameters (String objects)
153     */
154    public List<String> getAdditionalParams() {
155
156        return m_additionalParams;
157    }
158
159    /**
160     * Sets the List of additional parameters (String objects).<p>
161     *
162     * @param additionalParams the List of additional parameters (String objects) to set
163     */
164    protected void setAdditionalParams(List<String> additionalParams) {
165
166        m_additionalParams = additionalParams;
167    }
168}