001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/file/collectors/A_CmsResourceCollector.java,v $
003: * Date : $Date: 2008-02-27 12:05:43 $
004: * Version: $Revision: 1.14 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.file.collectors;
033:
034: import org.opencms.file.CmsDataAccessException;
035: import org.opencms.file.CmsObject;
036: import org.opencms.file.CmsResource;
037: import org.opencms.file.CmsResourceFilter;
038: import org.opencms.main.CmsException;
039: import org.opencms.main.CmsIllegalArgumentException;
040: import org.opencms.util.CmsMacroResolver;
041: import org.opencms.util.PrintfFormat;
042: import org.opencms.workplace.CmsWorkplace;
043:
044: import java.util.ArrayList;
045: import java.util.List;
046:
047: /**
048: * Provides some helpful base implementations for resource collector classes.<p>
049: *
050: * @author Alexander Kandzior
051: *
052: * @version $Revision: 1.14 $
053: *
054: * @since 6.0.0
055: */
056: public abstract class A_CmsResourceCollector implements
057: I_CmsResourceCollector {
058:
059: /** The "number" macro. */
060: private static final String MACRO_NUMBER = "number";
061:
062: /** Format for file create parameter. */
063: private static final PrintfFormat NUMBER_FORMAT = new PrintfFormat(
064: "%0.4d");
065:
066: /** The collector order of this collector. */
067: protected int m_order;
068:
069: /** The name of the configured default collector. */
070: private String m_defaultCollectorName;
071:
072: /** The default collector parameters. */
073: private String m_defaultCollectorParam;
074:
075: /** The hash code of this collector. */
076: private int m_hashcode;
077:
078: /**
079: * Constructor to initialize some default values.<p>
080: */
081: public A_CmsResourceCollector() {
082:
083: m_hashcode = getClass().getName().hashCode();
084: }
085:
086: /**
087: * @see java.lang.Comparable#compareTo(java.lang.Object)
088: */
089: public int compareTo(Object obj) {
090:
091: if (obj == this ) {
092: return 0;
093: }
094: if (obj instanceof I_CmsResourceCollector) {
095: return getOrder()
096: - ((I_CmsResourceCollector) obj).getOrder();
097: }
098: return 0;
099: }
100:
101: /**
102: * Two collectors are considered to be equal if they are sharing the same
103: * implementation class.<p>
104: *
105: * @see java.lang.Object#equals(java.lang.Object)
106: */
107: public boolean equals(Object obj) {
108:
109: if (obj == this ) {
110: return true;
111: }
112: if (obj instanceof I_CmsResourceCollector) {
113: return getClass().getName()
114: .equals(obj.getClass().getName());
115: }
116: return false;
117: }
118:
119: /**
120: * @see org.opencms.file.collectors.I_CmsResourceCollector#getCreateLink(org.opencms.file.CmsObject)
121: */
122: public String getCreateLink(CmsObject cms) throws CmsException,
123: CmsDataAccessException {
124:
125: checkParams();
126: return getCreateLink(cms, getDefaultCollectorName(),
127: getDefaultCollectorParam());
128: }
129:
130: /**
131: * @see org.opencms.file.collectors.I_CmsResourceCollector#getCreateParam(org.opencms.file.CmsObject)
132: */
133: public String getCreateParam(CmsObject cms)
134: throws CmsDataAccessException {
135:
136: checkParams();
137: return getCreateParam(cms, getDefaultCollectorName(),
138: getDefaultCollectorParam());
139: }
140:
141: /**
142: * @see org.opencms.file.collectors.I_CmsResourceCollector#getDefaultCollectorName()
143: */
144: public String getDefaultCollectorName() {
145:
146: return m_defaultCollectorName;
147: }
148:
149: /**
150: * @see org.opencms.file.collectors.I_CmsResourceCollector#getDefaultCollectorParam()
151: */
152: public String getDefaultCollectorParam() {
153:
154: return m_defaultCollectorParam;
155: }
156:
157: /**
158: * @see org.opencms.file.collectors.I_CmsResourceCollector#getOrder()
159: */
160: public int getOrder() {
161:
162: return m_order;
163: }
164:
165: /**
166: * @see org.opencms.file.collectors.I_CmsResourceCollector#getResults(org.opencms.file.CmsObject)
167: */
168: public List getResults(CmsObject cms)
169: throws CmsDataAccessException, CmsException {
170:
171: checkParams();
172: return getResults(cms, getDefaultCollectorName(),
173: getDefaultCollectorParam());
174: }
175:
176: /**
177: * @see java.lang.Object#hashCode()
178: */
179: public int hashCode() {
180:
181: return m_hashcode;
182: }
183:
184: /**
185: * @see org.opencms.file.collectors.I_CmsResourceCollector#setDefaultCollectorName(java.lang.String)
186: */
187: public void setDefaultCollectorName(String collectorName) {
188:
189: m_defaultCollectorName = collectorName;
190: }
191:
192: /**
193: * @see org.opencms.file.collectors.I_CmsResourceCollector#setDefaultCollectorParam(java.lang.String)
194: */
195: public void setDefaultCollectorParam(String param) {
196:
197: m_defaultCollectorParam = param;
198: }
199:
200: /**
201: * @see org.opencms.file.collectors.I_CmsResourceCollector#setOrder(int)
202: */
203: public void setOrder(int order) {
204:
205: m_order = order;
206: }
207:
208: /**
209: * Checks if the required parameters have been set.<p>
210: *
211: * @see #setDefaultCollectorName(String)
212: * @see #setDefaultCollectorParam(String)
213: */
214: protected void checkParams() {
215:
216: if ((m_defaultCollectorName == null)
217: || (m_defaultCollectorParam == null)) {
218: throw new CmsIllegalArgumentException(Messages.get()
219: .container(
220: Messages.ERR_COLLECTOR_DEFAULTS_INVALID_2,
221: m_defaultCollectorName,
222: m_defaultCollectorParam));
223: }
224: }
225:
226: /**
227: * Returns the link to create a new XML content item in the folder pointed to by the parameter.<p>
228: *
229: * @param cms the current CmsObject
230: * @param data the collector data to use
231: *
232: * @return the link to create a new XML content item in the folder
233: *
234: * @throws CmsException if something goes wrong
235: *
236: * @since 7.0.2
237: */
238: protected String getCreateInFolder(CmsObject cms,
239: CmsCollectorData data) throws CmsException {
240:
241: String foldername = CmsResource.getFolderPath(data
242: .getFileName());
243:
244: // must check ALL resources in folder because name doesn't care for type
245: List resources = cms.readResources(foldername,
246: CmsResourceFilter.ALL, false);
247:
248: // now create a list of all resources that just contains the file names
249: List result = new ArrayList(resources.size());
250: for (int i = 0; i < resources.size(); i++) {
251: CmsResource resource = (CmsResource) resources.get(i);
252: result.add(resource.getRootPath());
253: }
254:
255: String fileName = cms.getRequestContext().addSiteRoot(
256: data.getFileName());
257: String checkFileName, checkTempFileName, number;
258: CmsMacroResolver resolver = CmsMacroResolver.newInstance();
259:
260: int j = 0;
261: do {
262: number = NUMBER_FORMAT.sprintf(++j);
263: resolver.addMacro(MACRO_NUMBER, number);
264: // resolve macros in file name
265: checkFileName = resolver.resolveMacros(fileName);
266: // get name of the resolved temp file
267: checkTempFileName = CmsWorkplace
268: .getTemporaryFileName(checkFileName);
269: } while (result.contains(checkFileName)
270: || result.contains(checkTempFileName));
271:
272: return cms.getRequestContext().removeSiteRoot(checkFileName);
273: }
274:
275: /**
276: * Returns the link to create a new XML content item in the folder pointed to by the parameter.<p>
277: *
278: * @param cms the current CmsObject
279: * @param param the folder name to use
280: *
281: * @return the link to create a new XML content item in the folder
282: *
283: * @throws CmsException if something goes wrong
284: */
285: protected String getCreateInFolder(CmsObject cms, String param)
286: throws CmsException {
287:
288: return getCreateInFolder(cms, new CmsCollectorData(param));
289: }
290:
291: /**
292: * Shrinks a List to fit a maximum size.<p>
293: *
294: * @param result a List
295: * @param maxSize the maximum size of the List
296: *
297: * @return the shrinked list
298: */
299: protected List shrinkToFit(List result, int maxSize) {
300:
301: if ((maxSize > 0) && (result.size() > maxSize)) {
302: // cut off all items > count
303: result = result.subList(0, maxSize);
304: }
305:
306: return result;
307: }
308: }
|