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 * 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"</code>.<br>
047 * The <code>{Count}</code> and <code>excludeTimerange</code> values are optional.<br>
048 * Example:<br>
049 * <code>"/my/folder/|xmlcontent|5|excludeTimerange"</code>.<p>
050 *
051 * @since 6.0.0
052 *
053 * @see CmsExtendedCollectorData
054 */
055public class CmsCollectorData {
056
057    /** The value of the optional parameter to exclude the time range. */
058    public static final String PARAM_EXCLUDETIMERANGE = "excludeTimerange";
059
060    /** The log object for this class. */
061    private static final Log LOG = CmsLog.getLog(CmsCollectorData.class);
062
063    /** The display count. */
064    private int m_count;
065
066    /** The flag to exclude the time range in an offline project. */
067    private boolean m_excludeTimerange;
068
069    /** The absolute file name. */
070    private String m_fileName;
071
072    /** The file type id. */
073    private int m_type;
074
075    /**
076     * Creates a new extended collector data set.<p>
077     *
078     * The input data String must have the following format:<br>
079     * <code>"{VFS URI}|{Resource type}|{Count}|excludeTimerange"</code>, for example:<br>
080     * <code>"/my/folder/|xmlcontent|5|excludeTimerange"</code>.<p>
081     *
082     * @param data the data to parse
083     */
084    public CmsCollectorData(String data) {
085
086        if (data == null) {
087            throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_COLLECTOR_PARAM_EMPTY_0));
088        }
089
090        if (!data.contains("|")) {
091            throw new CmsIllegalArgumentException(
092                Messages.get().container(Messages.ERR_COLLECTOR_PARAM_INVALID_1, data));
093        }
094
095        List<String> args = CmsStringUtil.splitAsList(data, '|', true);
096
097        m_fileName = args.get(0);
098        String type = args.get(1);
099        m_count = 0;
100        if (args.size() >= 3) {
101            String value = args.get(2);
102            if (PARAM_EXCLUDETIMERANGE.equalsIgnoreCase(value)) {
103                m_excludeTimerange = true;
104            } else {
105                try {
106                    m_count = Integer.parseInt(value);
107                } catch (NumberFormatException e) {
108                    throw new CmsIllegalArgumentException(
109                        Messages.get().container(Messages.ERR_COLLECTOR_PARAM_INVALID_1, data));
110                }
111            }
112            if ((args.size() == 4) && PARAM_EXCLUDETIMERANGE.equalsIgnoreCase(args.get(3))) {
113                m_excludeTimerange = true;
114            }
115        }
116
117        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(type)) {
118            try {
119                // try to look up the resource type
120                I_CmsResourceType resourceType = OpenCms.getResourceManager().getResourceType(type);
121                m_type = resourceType.getTypeId();
122            } catch (CmsLoaderException e) {
123                // maybe the int id is directly used?
124                try {
125                    int typeInt = Integer.valueOf(type).intValue();
126                    I_CmsResourceType resourceType = OpenCms.getResourceManager().getResourceType(typeInt);
127                    m_type = resourceType.getTypeId();
128                    if (LOG.isWarnEnabled()) {
129                        LOG.warn(
130                            Messages.get().getBundle().key(
131                                Messages.LOG_RESTYPE_INTID_2,
132                                resourceType.getTypeName(),
133                                Integer.valueOf(resourceType.getTypeId())));
134                    }
135                } catch (CmsLoaderException | NumberFormatException e1) {
136                    // this resource type does not exist
137                    throw new CmsRuntimeException(Messages.get().container(Messages.ERR_UNKNOWN_RESTYPE_1, type), e1);
138                }
139            }
140        } else {
141            m_type = -1;
142        }
143    }
144
145    /**
146     * Required constructor for subclasses.<p>
147     */
148    protected CmsCollectorData() {
149
150        // NOOP
151    }
152
153    /**
154     * Returns the count.<p>
155     *
156     * @return the count
157     */
158    public int getCount() {
159
160        return m_count;
161    }
162
163    /**
164     * Returns the file name.<p>
165     *
166     * @return the file name
167     */
168    public String getFileName() {
169
170        return m_fileName;
171    }
172
173    /**
174     * Returns the type.<p>
175     *
176     * @return the type
177     */
178    public int getType() {
179
180        return m_type;
181    }
182
183    /**
184     * Returns the flag to exclude the time range in an offline project.<p>
185     *
186     * @return the flag to exclude the time range in an offline project
187     */
188    public boolean isExcludeTimerange() {
189
190        return m_excludeTimerange;
191    }
192
193    /**
194     * Sets the flag to exclude the time range in an offline project.<p>
195     *
196     * @param excludeTimerange the flag to exclude the time range in an offline project
197     */
198    public void setExcludeTimerange(boolean excludeTimerange) {
199
200        m_excludeTimerange = excludeTimerange;
201    }
202
203    /**
204     * Sets the fileName.<p>
205     *
206     * @param fileName the file name to set
207     */
208    public void setFileName(String fileName) {
209
210        m_fileName = fileName;
211    }
212
213    /**
214     * Sets the type.<p>
215     *
216     * @param type the type to set
217     */
218    public void setType(int type) {
219
220        m_type = type;
221    }
222
223    /**
224     * Sets the count.<p>
225     *
226     * @param count the count
227     */
228    protected void setCount(int count) {
229
230        m_count = count;
231    }
232}