KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > db > CmsPublishList


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/db/CmsPublishList.java,v $
3  * Date : $Date: 2006/03/27 14:52:27 $
4  * Version: $Revision: 1.25 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.db;
33
34 import org.opencms.file.CmsProject;
35 import org.opencms.file.CmsResource;
36 import org.opencms.main.CmsIllegalArgumentException;
37 import org.opencms.util.CmsFileUtil;
38 import org.opencms.util.CmsUUID;
39
40 import java.util.ArrayList JavaDoc;
41 import java.util.Collections JavaDoc;
42 import java.util.Iterator JavaDoc;
43 import java.util.List JavaDoc;
44
45 /**
46  * A container for all new/changed/deteled Cms resources that are published together.<p>
47  *
48  * Only classes inside the org.opencms.db package can add or remove elements to or from this list.
49  * This allows the OpenCms API to pass the list around between classes, but with restricted access to
50  * create this list.<p>
51  *
52  * To create a publish list, one of the public constructors must be used in order to set the basic operation mode
53  * (project publish or direct publish).
54  * After this, use <code>{@link org.opencms.db.CmsDriverManager#fillPublishList(CmsDbContext, CmsPublishList)}</code>
55  * to fill the actual values of the publish list.<p>
56  *
57  * @author Alexander Kandzior
58  * @author Thomas Weckert
59  *
60  * @version $Revision: 1.25 $
61  *
62  * @since 6.0.0
63  *
64  * @see org.opencms.db.CmsDriverManager#fillPublishList(CmsDbContext, CmsPublishList)
65  */

66 public class CmsPublishList {
67
68     /** The list of deleted Cms folder resources to be published.<p> */
69     private List JavaDoc m_deletedFolderList;
70
71     /** The list of direct publish resources. */
72     private List JavaDoc m_directPublishResources;
73
74     /** The list of new/changed/deleted Cms file resources to be published.<p> */
75     private List JavaDoc m_fileList;
76
77     /** The list of new/changed Cms folder resources to be published.<p> */
78     private List JavaDoc m_folderList;
79
80     /** The id of the project that is to be published. */
81     private int m_projectId;
82
83     /** The publish history ID.<p> */
84     private CmsUUID m_publishHistoryId;
85
86     /** Indicates if siblings of the resources in the list should also be published. */
87     private boolean m_publishSiblings;
88
89     /** Indicates if sub-resources in folders should be published (for direct publish only). */
90     private boolean m_publishSubResources;
91
92     /**
93      * Constructs a publish list for a given project.<p>
94      *
95      * @param project the project to publish, this should always be the id of the current project
96      */

97     public CmsPublishList(CmsProject project) {
98
99         this(project, null, false, true);
100     }
101
102     /**
103      * Constructs a publish list for a single direct publish resource.<p>
104      *
105      * @param directPublishResource a VFS resource to be published directly
106      * @param publishSiblings indicates if all siblings of the selected resources should be published
107      */

108     public CmsPublishList(CmsResource directPublishResource, boolean publishSiblings) {
109
110         this(null, Collections.singletonList(directPublishResource), publishSiblings, true);
111     }
112
113     /**
114      * Constructs a publish list for a list of direct publish resources.<p>
115      *
116      * @param directPublishResources a list of <code>{@link CmsResource}</code> instances to be published directly
117      * @param publishSiblings indicates if all siblings of the selected resources should be published
118      */

119     public CmsPublishList(List JavaDoc directPublishResources, boolean publishSiblings) {
120
121         this(null, directPublishResources, publishSiblings, true);
122     }
123
124     /**
125      * Constructs a publish list for a list of direct publish resources.<p>
126      *
127      * @param directPublishResources a list of <code>{@link CmsResource}</code> instances to be published directly
128      * @param publishSiblings indicates if all siblings of the selected resources should be published
129      * @param publishSubResources indicates if sub-resources in folders should be published (for direct publish only)
130      */

131     public CmsPublishList(List JavaDoc directPublishResources, boolean publishSiblings, boolean publishSubResources) {
132
133         this(null, directPublishResources, publishSiblings, publishSubResources);
134     }
135
136     /**
137      * Internal constructor for a publish list.<p>
138      *
139      * @param project the project to publish
140      * @param directPublishResources the list of direct publish resources
141      * @param publishSiblings indicates if all siblings of the selected resources should be published
142      * @param publishSubResources indicates if sub-resources in folders should be published (for direct publish only)
143      */

144     private CmsPublishList(
145         CmsProject project,
146         List JavaDoc directPublishResources,
147         boolean publishSiblings,
148         boolean publishSubResources) {
149
150         m_fileList = new ArrayList JavaDoc();
151         m_folderList = new ArrayList JavaDoc();
152         m_deletedFolderList = new ArrayList JavaDoc();
153         m_publishHistoryId = new CmsUUID();
154         m_publishSiblings = publishSiblings;
155         m_publishSubResources = publishSubResources;
156         m_projectId = (project != null) ? project.getId() : -1;
157         if (directPublishResources != null) {
158             // reduce list of folders to minimum
159
m_directPublishResources = Collections.unmodifiableList(CmsFileUtil.removeRedundantResources(directPublishResources));
160         }
161     }
162
163     /**
164      * Returns a list of folder resources with the given state.<p>
165      *
166      * @return a list of folder resources with the desired state
167      */

168     public List JavaDoc getDeletedFolderList() {
169
170         return m_deletedFolderList;
171     }
172
173     /**
174      * Returns the list of resources that should be published for a "direct" publish operation.<p>
175      *
176      * Will return <code>null</code> if this publish list was not initilaized for a "direct publish" but
177      * for a project publish.<p>
178      *
179      * @return the list of resources that should be published for a "direct" publish operation, or <code>null</code>
180      */

181     public List JavaDoc getDirectPublishResources() {
182
183         return m_directPublishResources;
184     }
185
186     /**
187      * Returns an unmodifiable list of the Cms file resources in this publish list.<p>
188      *
189      * @return the list with the Cms file resources in this publish list
190      */

191     public List JavaDoc getFileList() {
192
193         return Collections.unmodifiableList(m_fileList);
194     }
195
196     /**
197      * Returns an unmodifiable list of the new/changed Cms folder resources in this publish list.<p>
198      *
199      * @return the list with the new/changed Cms file resources in this publish list
200      */

201     public List JavaDoc getFolderList() {
202
203         return Collections.unmodifiableList(m_folderList);
204     }
205
206     /**
207      * Returns the id of the project that should be published, or <code>-1</code> if this publish list
208      * is initialized for a "direct publish" operation.<p>
209      *
210      * @return the id of the project that should be published, or <code>-1</code>
211      */

212     public int getProjectId() {
213
214         return m_projectId;
215     }
216
217     /**
218      * Returns the publish history Id for this publish list.<p>
219      *
220      * @return the publish history Id
221      */

222     public CmsUUID getPublishHistoryId() {
223
224         return m_publishHistoryId;
225     }
226
227     /**
228      * Checks if this is a publish list is used for a "direct publish" operation.<p>
229      *
230      * @return true if this is a publish list is used for a "direct publish" operation
231      */

232     public boolean isDirectPublish() {
233
234         return m_projectId < 0;
235     }
236
237     /**
238      * Returns <code>true</code> if all siblings of the project resources are to be published.<p>
239      *
240      * @return <code>true</code> if all siblings of the project resources are to be publisheds
241      */

242     public boolean isPublishSiblings() {
243
244         return m_publishSiblings;
245     }
246
247     /**
248      * Returns <code>true</code> if sub-resources in folders should be published (for direct publish only).<p>
249      *
250      * @return <code>true</code> if sub-resources in folders should be published (for direct publish only)
251      */

252     public boolean isPublishSubResources() {
253
254         return m_publishSubResources;
255     }
256
257     /**
258      * @see java.lang.Object#toString()
259      */

260     public String JavaDoc toString() {
261
262         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
263         result.append("\n[\n");
264         if (isDirectPublish()) {
265             result.append("direct publish of resources: ").append(m_directPublishResources.toString()).append("\n");
266         } else {
267             result.append("publish of project: ").append(m_projectId).append("\n");
268         }
269         result.append("publish history ID: ").append(m_publishHistoryId.toString()).append("\n");
270         result.append("resources: ").append(m_fileList.toString()).append("\n");
271         result.append("folders: ").append(m_folderList.toString()).append("\n");
272         result.append("]\n");
273         return result.toString();
274     }
275
276     /**
277      * Adds a new/changed/deleted Cms file resource to the publish list.<p>
278      *
279      * @param resource a new/changed/deleted Cms file resource
280      * @throws CmsIllegalArgumentException if the specified resource is not a file or unchanged
281      */

282     protected void addFile(CmsResource resource) throws CmsIllegalArgumentException {
283
284         // it is essential that this method is only visible within the db package!
285

286         if (resource.isFolder()) {
287             throw new CmsIllegalArgumentException(Messages.get().container(
288                 Messages.ERR_PUBLISH_NO_CMS_FILE_1,
289                 resource.getRootPath()));
290         }
291
292         if (resource.getState() == CmsResource.STATE_UNCHANGED) {
293             throw new CmsIllegalArgumentException(Messages.get().container(
294                 Messages.ERR_PUBLISH_UNCHANGED_RESOURCE_1,
295                 resource.getRootPath()));
296         }
297
298         if (!m_fileList.contains(resource)) {
299             // only add files not already contained in the list
300
// this is required to make sure no siblings are duplicated
301
m_fileList.add(resource);
302         }
303     }
304
305     /**
306      * Appends all of the new/changed/deleted Cms file resources in the specified list to the end
307      * of this publish list.<p>
308      *
309      * @param list a list with new/changed/deleted Cms file resources to be added to this publish list
310      * @throws IllegalArgumentException if one of the resources is not a file or unchanged
311      */

312     protected void addFiles(List JavaDoc list) throws IllegalArgumentException JavaDoc {
313
314         // it is essential that this method is only visible within the db package!
315

316         Iterator JavaDoc i = list.iterator();
317         while (i.hasNext()) {
318             addFile((CmsResource)i.next());
319         }
320     }
321
322     /**
323      * Adds a new/changed Cms folder resource to the publish list.<p>
324      *
325      * @param resource a new/changed Cms folder resource
326      * @throws IllegalArgumentException if the specified resource is not a folder or unchanged
327      */

328     protected void addFolder(CmsResource resource) throws IllegalArgumentException JavaDoc {
329
330         // it is essential that this method is only visible within the db package!
331

332         if (resource.isFile()) {
333             throw new CmsIllegalArgumentException(Messages.get().container(
334                 Messages.ERR_PUBLISH_NO_FOLDER_1,
335                 resource.getRootPath()));
336         }
337
338         if (resource.getState() == CmsResource.STATE_UNCHANGED) {
339             throw new CmsIllegalArgumentException(Messages.get().container(
340                 Messages.ERR_PUBLISH_UNCHANGED_RESOURCE_1,
341                 resource.getRootPath()));
342         }
343
344         if (resource.getState() == CmsResource.STATE_DELETED) {
345             m_deletedFolderList.add(resource);
346         } else {
347             m_folderList.add(resource);
348         }
349     }
350
351     /**
352      * Appends all of the new/changed Cms folder resources in the specified list to the end
353      * of this publish list.<p>
354      *
355      * @param list a list with new/changed Cms folder resources to be added to this publish list
356      * @throws IllegalArgumentException if one of the resources is not a folder or unchanged
357      */

358     protected void addFolders(List JavaDoc list) throws IllegalArgumentException JavaDoc {
359
360         // it is essential that this method is only visible within the db package!
361

362         Iterator JavaDoc i = list.iterator();
363         while (i.hasNext()) {
364             addFolder((CmsResource)i.next());
365         }
366     }
367
368     /**
369      * @see java.lang.Object#finalize()
370      */

371     protected void finalize() throws Throwable JavaDoc {
372
373         try {
374             if (m_fileList != null) {
375                 m_fileList.clear();
376             }
377             m_fileList = null;
378
379             if (m_folderList != null) {
380                 m_folderList.clear();
381             }
382             m_folderList = null;
383
384             if (m_deletedFolderList != null) {
385                 m_deletedFolderList.clear();
386             }
387             m_deletedFolderList = null;
388         } catch (Throwable JavaDoc t) {
389             // ignore
390
}
391
392         super.finalize();
393     }
394
395     /**
396      * Returns the list with the new/changed Cms folder resources.<p>
397      *
398      * @return the list with the new/changed Cms folder resources
399      */

400     protected List JavaDoc getFolderListInstance() {
401
402         // it is essential that this method is only visible within the db package!
403
return m_folderList;
404     }
405
406     /**
407      * Initializes the publish list, ensuring all internal lists are in the right order.<p>
408      */

409     protected void initialize() {
410
411         if (m_folderList != null) {
412             // ensure folders are sorted starting with parent folders
413
Collections.sort(m_folderList, CmsResource.COMPARE_ROOT_PATH);
414         }
415
416         if (m_fileList != null) {
417             // ensure files are sorted starting with files in parent folders
418
Collections.sort(m_fileList, CmsResource.COMPARE_ROOT_PATH);
419         }
420
421         if (m_deletedFolderList != null) {
422             // ensure deleted folders are sorted starting with child folders
423
Collections.sort(m_deletedFolderList, CmsResource.COMPARE_ROOT_PATH);
424             Collections.reverse(m_deletedFolderList);
425         }
426     }
427
428     /**
429      * Removes a Cms resource from the publish list.<p>
430      *
431      * @param resource a Cms resource
432      * @return true if this publish list contains the specified resource
433      * @see List#remove(java.lang.Object)
434      */

435     protected boolean remove(CmsResource resource) {
436
437         // it is essential that this method is only visible within the db package!
438
return m_fileList.remove(resource);
439     }
440 }
Popular Tags