KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > file > types > A_CmsResourceTypeFolderBase


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/file/types/A_CmsResourceTypeFolderBase.java,v $
3  * Date : $Date: 2006/03/27 14:52:48 $
4  * Version: $Revision: 1.16 $
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.file.types;
33
34 import org.opencms.db.CmsSecurityManager;
35 import org.opencms.file.CmsDataNotImplementedException;
36 import org.opencms.file.CmsObject;
37 import org.opencms.file.CmsResource;
38 import org.opencms.file.CmsResourceFilter;
39 import org.opencms.file.CmsVfsException;
40 import org.opencms.main.CmsException;
41 import org.opencms.main.CmsIllegalArgumentException;
42 import org.opencms.main.OpenCms;
43 import org.opencms.security.CmsPermissionSet;
44 import org.opencms.util.CmsStringUtil;
45
46 import java.util.HashSet JavaDoc;
47 import java.util.List JavaDoc;
48 import java.util.Set JavaDoc;
49
50 /**
51  * Resource type descriptor for the type "folder".<p>
52  *
53  * @author Alexander Kandzior
54  *
55  * @version $Revision: 1.16 $
56  *
57  * @since 6.0.0
58  */

59 public abstract class A_CmsResourceTypeFolderBase extends A_CmsResourceType {
60
61     /**
62      * Default constructor, used to initialize member variables.<p>
63      */

64     public A_CmsResourceTypeFolderBase() {
65
66         super();
67     }
68
69     /**
70      * @see org.opencms.file.types.I_CmsResourceType#changeLastModifiedProjectId(org.opencms.file.CmsObject, CmsSecurityManager, CmsResource)
71      */

72     public void changeLastModifiedProjectId(CmsObject cms, CmsSecurityManager securityManager, CmsResource resource)
73     throws CmsException {
74
75         // collect all resources in the folder (include deleted ones)
76
List JavaDoc resources = securityManager.readChildResources(
77             cms.getRequestContext(),
78             resource,
79             CmsResourceFilter.ALL,
80             true,
81             true);
82
83         // handle the folder itself
84
super.changeLastModifiedProjectId(cms, securityManager, resource);
85
86         // now walk through all sub-resources in the folder
87
for (int i = 0; i < resources.size(); i++) {
88             CmsResource childResource = (CmsResource)resources.get(i);
89             if (childResource.isFolder()) {
90                 // recurse into this method for subfolders
91
changeLastModifiedProjectId(cms, securityManager, childResource);
92             } else {
93                 // handle child resources
94
getResourceType(childResource.getTypeId()).changeLastModifiedProjectId(
95                     cms,
96                     securityManager,
97                     childResource);
98             }
99         }
100     }
101
102     /**
103      * @see org.opencms.file.types.I_CmsResourceType#chtype(org.opencms.file.CmsObject, CmsSecurityManager, CmsResource, int)
104      */

105     public void chtype(CmsObject cms, CmsSecurityManager securityManager, CmsResource filename, int newType)
106     throws CmsException, CmsDataNotImplementedException {
107
108         if (!OpenCms.getResourceManager().getResourceType(newType).isFolder()) {
109             // it is not possible to change the type of a folder to a file type
110
throw new CmsDataNotImplementedException(Messages.get().container(
111                 Messages.ERR_CHTYPE_FOLDER_1,
112                 cms.getSitePath(filename)));
113         }
114         super.chtype(cms, securityManager, filename, newType);
115     }
116
117     /**
118      * @see org.opencms.file.types.I_CmsResourceType#copyResource(org.opencms.file.CmsObject, CmsSecurityManager, CmsResource, java.lang.String, int)
119      */

120     public void copyResource(
121         CmsObject cms,
122         CmsSecurityManager securityManager,
123         CmsResource source,
124         String JavaDoc destination,
125         int siblingMode) throws CmsIllegalArgumentException, CmsException {
126
127         // first validate the destination name
128
destination = validateFoldername(destination);
129
130         // collect all resources in the folder (but exclude deleted ones)
131
List JavaDoc resources = securityManager.readChildResources(
132             cms.getRequestContext(),
133             source,
134             CmsResourceFilter.IGNORE_EXPIRATION,
135             true,
136             true);
137
138         // handle the folder itself
139
super.copyResource(cms, securityManager, source, destination, siblingMode);
140
141         // now walk through all sub-resources in the folder
142
for (int i = 0; i < resources.size(); i++) {
143             CmsResource childResource = (CmsResource)resources.get(i);
144             String JavaDoc childDestination = destination.concat(childResource.getName());
145             if (childResource.isFolder()) {
146                 // recurse into this method for subfolders
147
copyResource(cms, securityManager, childResource, childDestination, siblingMode);
148             } else {
149                 // handle child resources
150
getResourceType(childResource.getTypeId()).copyResource(
151                     cms,
152                     securityManager,
153                     childResource,
154                     childDestination,
155                     siblingMode);
156             }
157         }
158     }
159
160     /**
161      * @see org.opencms.file.types.I_CmsResourceType#createResource(org.opencms.file.CmsObject, CmsSecurityManager, java.lang.String, byte[], List)
162      */

163     public CmsResource createResource(
164         CmsObject cms,
165         CmsSecurityManager securityManager,
166         String JavaDoc resourcename,
167         byte[] content,
168         List JavaDoc properties) throws CmsException {
169
170         resourcename = validateFoldername(resourcename);
171         return super.createResource(cms, securityManager, resourcename, content, properties);
172     }
173
174     /**
175      * @see org.opencms.file.types.I_CmsResourceType#deleteResource(org.opencms.file.CmsObject, CmsSecurityManager, CmsResource, int)
176      */

177     public void deleteResource(CmsObject cms, CmsSecurityManager securityManager, CmsResource resource, int siblingMode)
178     throws CmsException {
179
180         // collect all resources in the folder (but exclude deleted ones)
181
List JavaDoc resources = securityManager.readChildResources(
182             cms.getRequestContext(),
183             resource,
184             CmsResourceFilter.IGNORE_EXPIRATION,
185             true,
186             true);
187
188         Set deletedResources = new HashSet JavaDoc();
189
190         // now walk through all sub-resources in the folder
191
for (int i = 0; i < resources.size(); i++) {
192             CmsResource childResource = (CmsResource)resources.get(i);
193
194             if (siblingMode == CmsResource.DELETE_REMOVE_SIBLINGS
195                 && deletedResources.contains(childResource.getResourceId())) {
196                 // sibling mode is "delete all siblings" and another sibling of the current child resource has already
197
// been deleted- do nothing and continue with the next child resource.
198
continue;
199             }
200
201             if (childResource.isFolder()) {
202                 // recurse into this method for subfolders
203
deleteResource(cms, securityManager, childResource, siblingMode);
204             } else {
205                 // handle child resources
206
getResourceType(childResource.getTypeId()).deleteResource(
207                     cms,
208                     securityManager,
209                     childResource,
210                     siblingMode);
211             }
212
213             deletedResources.add(childResource.getResourceId());
214         }
215
216         deletedResources.clear();
217
218         // handle the folder itself
219
super.deleteResource(cms, securityManager, resource, siblingMode);
220     }
221
222     /**
223      * @see org.opencms.file.types.I_CmsResourceType#getLoaderId()
224      */

225     public int getLoaderId() {
226
227         // folders have no loader
228
return -1;
229     }
230
231     /**
232      * @see org.opencms.file.types.A_CmsResourceType#isFolder()
233      */

234     public boolean isFolder() {
235
236         return true;
237     }
238
239     /**
240      * @see org.opencms.file.types.I_CmsResourceType#moveResource(org.opencms.file.CmsObject, CmsSecurityManager, CmsResource, java.lang.String)
241      */

242     public void moveResource(CmsObject cms, CmsSecurityManager securityManager, CmsResource resource, String JavaDoc destination)
243     throws CmsException, CmsIllegalArgumentException {
244
245         String JavaDoc dest = cms.getRequestContext().addSiteRoot(destination);
246         if (!CmsResource.isFolder(dest)) {
247             // ensure folder name end's with a / (required for the following comparison)
248
dest = dest.concat("/");
249         }
250         if (resource.getRootPath().equals(dest)) {
251             // move to target with same name is not allowed
252
throw new CmsVfsException(org.opencms.file.Messages.get().container(
253                 org.opencms.file.Messages.ERR_MOVE_SAME_NAME_1,
254                 destination));
255         }
256         if (dest.startsWith(resource.getRootPath())) {
257             // move of folder inside itself is not allowed
258
throw new CmsVfsException(org.opencms.file.Messages.get().container(
259                 org.opencms.file.Messages.ERR_MOVE_SAME_FOLDER_2,
260                 cms.getSitePath(resource),
261                 destination));
262         }
263
264         // check if the user has write access and if resource is locked
265
// done here since copy is ok without lock, but delete is not
266
securityManager.checkPermissions(
267             cms.getRequestContext(),
268             resource,
269             CmsPermissionSet.ACCESS_WRITE,
270             true,
271             CmsResourceFilter.IGNORE_EXPIRATION);
272
273         copyResource(cms, securityManager, resource, destination, CmsResource.COPY_AS_SIBLING);
274
275         deleteResource(cms, securityManager, resource, CmsResource.DELETE_PRESERVE_SIBLINGS);
276     }
277
278     /**
279      * @see org.opencms.file.types.I_CmsResourceType#replaceResource(org.opencms.file.CmsObject, CmsSecurityManager, CmsResource, int, byte[], List)
280      */

281     public void replaceResource(
282         CmsObject cms,
283         CmsSecurityManager securityManager,
284         CmsResource resource,
285         int type,
286         byte[] content,
287         List JavaDoc properties) throws CmsException, CmsDataNotImplementedException {
288
289         if (type != getTypeId()) {
290             // it is not possible to replace a folder with a different type
291
throw new CmsDataNotImplementedException(Messages.get().container(
292                 Messages.ERR_REPLACE_RESOURCE_FOLDER_1,
293                 cms.getSitePath(resource)));
294         }
295         // properties of a folder can be replaced, content is ignored
296
super.replaceResource(cms, securityManager, resource, getTypeId(), null, properties);
297     }
298
299     /**
300      * @see org.opencms.file.types.I_CmsResourceType#restoreResourceBackup(org.opencms.file.CmsObject, CmsSecurityManager, CmsResource, int)
301      */

302     public void restoreResourceBackup(
303         CmsObject cms,
304         CmsSecurityManager securityManager,
305         CmsResource resourename,
306         int tag) throws CmsException {
307
308         // it is not possible to restore a folder from the backup
309
throw new CmsDataNotImplementedException(Messages.get().container(Messages.ERR_RESTORE_FOLDERS_0));
310     }
311     
312     /**
313      * @see org.opencms.file.types.I_CmsResourceType#setDateExpired(org.opencms.file.CmsObject, CmsSecurityManager, CmsResource, long, boolean)
314      */

315     public void setDateExpired(
316         CmsObject cms,
317         CmsSecurityManager securityManager,
318         CmsResource resource,
319         long dateLastModified,
320         boolean recursive) throws CmsException {
321
322         // handle the folder itself
323
super.setDateExpired(cms, securityManager, resource, dateLastModified, recursive);
324
325         if (recursive) {
326             // collect all resources in the folder (but exclude deleted ones)
327
List JavaDoc resources = securityManager.readChildResources(
328                 cms.getRequestContext(),
329                 resource,
330                 CmsResourceFilter.IGNORE_EXPIRATION,
331                 true,
332                 true);
333
334             // now walk through all sub-resources in the folder
335
for (int i = 0; i < resources.size(); i++) {
336                 CmsResource childResource = (CmsResource)resources.get(i);
337                 if (childResource.isFolder()) {
338                     // recurse into this method for subfolders
339
setDateExpired(cms, securityManager, childResource, dateLastModified, recursive);
340                 } else {
341                     // handle child resources
342
getResourceType(childResource.getTypeId()).setDateExpired(
343                         cms,
344                         securityManager,
345                         childResource,
346                         dateLastModified,
347                         recursive);
348                 }
349             }
350         }
351     }
352
353     /**
354      * @see org.opencms.file.types.I_CmsResourceType#setDateLastModified(org.opencms.file.CmsObject, CmsSecurityManager, CmsResource, long, boolean)
355      */

356     public void setDateLastModified(
357         CmsObject cms,
358         CmsSecurityManager securityManager,
359         CmsResource resource,
360         long dateLastModified,
361         boolean recursive) throws CmsException {
362
363         // handle the folder itself
364
super.setDateLastModified(cms, securityManager, resource, dateLastModified, recursive);
365
366         if (recursive) {
367             // collect all resources in the folder (but exclude deleted ones)
368
List JavaDoc resources = securityManager.readChildResources(
369                 cms.getRequestContext(),
370                 resource,
371                 CmsResourceFilter.IGNORE_EXPIRATION,
372                 true,
373                 true);
374
375             // now walk through all sub-resources in the folder
376
for (int i = 0; i < resources.size(); i++) {
377                 CmsResource childResource = (CmsResource)resources.get(i);
378                 if (childResource.isFolder()) {
379                     // recurse into this method for subfolders
380
setDateLastModified(cms, securityManager, childResource, dateLastModified, recursive);
381                 } else {
382                     // handle child resources
383
getResourceType(childResource.getTypeId()).setDateLastModified(
384                         cms,
385                         securityManager,
386                         childResource,
387                         dateLastModified,
388                         recursive);
389                 }
390             }
391         }
392     }
393     
394     /**
395      * @see org.opencms.file.types.I_CmsResourceType#setDateReleased(org.opencms.file.CmsObject, CmsSecurityManager, CmsResource, long, boolean)
396      */

397     public void setDateReleased(
398         CmsObject cms,
399         CmsSecurityManager securityManager,
400         CmsResource resource,
401         long dateLastModified,
402         boolean recursive) throws CmsException {
403
404         // handle the folder itself
405
super.setDateReleased(cms, securityManager, resource, dateLastModified, recursive);
406
407         if (recursive) {
408             // collect all resources in the folder (but exclude deleted ones)
409
List JavaDoc resources = securityManager.readChildResources(
410                 cms.getRequestContext(),
411                 resource,
412                 CmsResourceFilter.IGNORE_EXPIRATION,
413                 true,
414                 true);
415
416             // now walk through all sub-resources in the folder
417
for (int i = 0; i < resources.size(); i++) {
418                 CmsResource childResource = (CmsResource)resources.get(i);
419                 if (childResource.isFolder()) {
420                     // recurse into this method for subfolders
421
setDateReleased(cms, securityManager, childResource, dateLastModified, recursive);
422                 } else {
423                     // handle child resources
424
getResourceType(childResource.getTypeId()).setDateReleased(
425                         cms,
426                         securityManager,
427                         childResource,
428                         dateLastModified,
429                         recursive);
430                 }
431             }
432         }
433     }
434
435     /**
436      * @see org.opencms.file.types.I_CmsResourceType#undoChanges(org.opencms.file.CmsObject, CmsSecurityManager, CmsResource, boolean)
437      */

438     public void undoChanges(CmsObject cms, CmsSecurityManager securityManager, CmsResource resource, boolean recursive)
439     throws CmsException {
440
441         // handle the folder itself
442
super.undoChanges(cms, securityManager, resource, recursive);
443
444         if (recursive) {
445             // collect all resources in the folder (but exclude deleted ones)
446
List JavaDoc resources = securityManager.readChildResources(
447                 cms.getRequestContext(),
448                 resource,
449                 CmsResourceFilter.ALL,
450                 true,
451                 true);
452
453             // now walk through all sub-resources in the folder
454
for (int i = 0; i < resources.size(); i++) {
455                 CmsResource childResource = (CmsResource)resources.get(i);
456                 if (childResource.isFolder()) {
457                     // recurse into this method for subfolders
458
undoChanges(cms, securityManager, childResource, recursive);
459                 } else {
460                     // handle child resources
461
getResourceType(childResource.getTypeId()).undoChanges(
462                         cms,
463                         securityManager,
464                         childResource,
465                         recursive);
466                 }
467             }
468         }
469     }
470
471     /**
472      * Checks if there are at least one character in the folder name,
473      * also ensures that it starts and ends with a '/'.<p>
474      *
475      * @param resourcename folder name to check (complete path)
476      * @return the validated folder name
477      * @throws CmsIllegalArgumentException if the folder name is empty or null
478      */

479     private String JavaDoc validateFoldername(String JavaDoc resourcename) throws CmsIllegalArgumentException {
480
481         if (CmsStringUtil.isEmpty(resourcename)) {
482             throw new CmsIllegalArgumentException(org.opencms.db.Messages.get().container(
483                 org.opencms.db.Messages.ERR_BAD_RESOURCENAME_1,
484                 resourcename));
485         }
486         if (!CmsResource.isFolder(resourcename)) {
487             resourcename = resourcename.concat("/");
488         }
489         if (resourcename.charAt(0) != '/') {
490             resourcename = "/".concat(resourcename);
491         }
492         return resourcename;
493     }
494 }
Popular Tags