KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/file/types/A_CmsResourceType.java,v $
3  * Date : $Date: 2006/10/20 10:36:51 $
4  * Version: $Revision: 1.44 $
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.configuration.CmsConfigurationCopyResource;
35 import org.opencms.configuration.CmsConfigurationException;
36 import org.opencms.db.CmsSecurityManager;
37 import org.opencms.file.CmsFile;
38 import org.opencms.file.CmsObject;
39 import org.opencms.file.CmsProperty;
40 import org.opencms.file.CmsResource;
41 import org.opencms.file.CmsResourceFilter;
42 import org.opencms.file.CmsVfsException;
43 import org.opencms.file.CmsVfsResourceAlreadyExistsException;
44 import org.opencms.lock.CmsLock;
45 import org.opencms.main.CmsException;
46 import org.opencms.main.CmsLog;
47 import org.opencms.main.OpenCms;
48 import org.opencms.security.CmsPermissionSet;
49 import org.opencms.security.CmsSecurityException;
50 import org.opencms.staticexport.CmsLinkManager;
51 import org.opencms.util.CmsFileUtil;
52 import org.opencms.util.CmsMacroResolver;
53
54 import java.util.ArrayList JavaDoc;
55 import java.util.Collections JavaDoc;
56 import java.util.Iterator JavaDoc;
57 import java.util.List JavaDoc;
58 import java.util.Map JavaDoc;
59
60 import org.apache.commons.logging.Log;
61
62 /**
63  * Base implementation for resource type classes.<p>
64  *
65  * @author Alexander Kandzior
66  * @author Thomas Weckert
67  *
68  * @version $Revision: 1.44 $
69  *
70  * @since 6.0.0
71  */

72 public abstract class A_CmsResourceType implements I_CmsResourceType {
73
74     /** Macro for the folder path of the current resouce. */
75     public static final String JavaDoc MACRO_RESOURCE_FOLDER_PATH = "resource.folder.path";
76
77     /** Macro for the name of the current resouce. */
78     public static final String JavaDoc MACRO_RESOURCE_NAME = "resource.name";
79
80     /** Macro for the parent folder path of the current resouce. */
81     public static final String JavaDoc MACRO_RESOURCE_PARENT_PATH = "resource.parent.path";
82
83     /** Macro for the root path of the current resouce. */
84     public static final String JavaDoc MACRO_RESOURCE_ROOT_PATH = "resource.root.path";
85
86     /** Macro for the site path of the current resouce. */
87     public static final String JavaDoc MACRO_RESOURCE_SITE_PATH = "resource.site.path";
88
89     /** The log object for this class. */
90     private static final Log LOG = CmsLog.getLog(A_CmsResourceType.class);
91
92     /** Flag for showing that this is an additional resource type which defined in a module. */
93     protected boolean m_addititionalModuleResourceType;
94
95     /** The configured class name of this resource type. */
96     protected String JavaDoc m_className;
97
98     /** The list of resources to copy. */
99     protected List JavaDoc m_copyResources;
100
101     /** The list of configured default properties. */
102     protected List JavaDoc m_defaultProperties;
103
104     /** Indicates that the configuration of the resource type has been frozen. */
105     protected boolean m_frozen;
106
107     /** Contains the file extensions mapped to this resource type. */
108     protected List JavaDoc m_mappings;
109
110     /** The configured id of this resource type. */
111     protected int m_typeId;
112
113     /** The configured name of this resource type. */
114     protected String JavaDoc m_typeName;
115
116     /**
117      * Default constructor, used to initialize some member variables.<p>
118      */

119     public A_CmsResourceType() {
120
121         m_typeId = -1;
122         m_mappings = new ArrayList JavaDoc();
123         m_defaultProperties = new ArrayList JavaDoc();
124         m_copyResources = new ArrayList JavaDoc();
125     }
126
127     /**
128      * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#addConfigurationParameter(java.lang.String, java.lang.String)
129      */

130     public void addConfigurationParameter(String JavaDoc paramName, String JavaDoc paramValue) {
131
132         // noop
133
}
134
135     /**
136      * Adds a new "copy resource" to this resource type,
137      * allowed only during the configuration phase.<p>
138      *
139      * The "copy resources" are copied to the specified location after
140      * a new resource of this type is created. Usually this feature is used to
141      * populate a newly created folder with some default resources.<p>
142      *
143      * If target is <code>null</code>, the macro {@link #MACRO_RESOURCE_FOLDER_PATH} is used as default.
144      * If type is <code>null</code>, the copy type {@link CmsResource#COPY_AS_NEW} is used as default.<p>
145      *
146      * @param source the source resource
147      * @param target the target resource (may contain macros)
148      * @param type the type of the copy, for example "as new", "as sibling" etc
149      *
150      * @throws CmsConfigurationException if the configuration is already frozen
151      */

152     public void addCopyResource(String JavaDoc source, String JavaDoc target, String JavaDoc type) throws CmsConfigurationException {
153
154         if (LOG.isDebugEnabled()) {
155             LOG.debug(Messages.get().getBundle().key(
156                 Messages.LOG_ADD_COPY_RESOURCE_4,
157                 new Object JavaDoc[] {this, source, target, type}));
158         }
159
160         if (m_frozen) {
161             // configuration already frozen
162
throw new CmsConfigurationException(Messages.get().container(
163                 Messages.ERR_CONFIG_FROZEN_3,
164                 this.getClass().getName(),
165                 getTypeName(),
166                 new Integer JavaDoc(getTypeId())));
167         }
168
169         // create the copy resource object an add it to the list
170
CmsConfigurationCopyResource copyResource = new CmsConfigurationCopyResource(source, target, type);
171         m_copyResources.add(copyResource);
172     }
173
174     /**
175      * Adds a default property to this resource type,
176      * allowed only during the configuration phase.<p>
177      *
178      * @param property the default property to add
179      *
180      * @throws CmsConfigurationException if the configuration is already frozen
181      */

182     public void addDefaultProperty(CmsProperty property) throws CmsConfigurationException {
183
184         if (LOG.isDebugEnabled()) {
185             LOG.debug(Messages.get().getBundle().key(Messages.LOG_ADD_DFLT_PROP_2, this, property));
186         }
187
188         if (m_frozen) {
189             // configuration already frozen
190
throw new CmsConfigurationException(Messages.get().container(
191                 Messages.ERR_CONFIG_FROZEN_3,
192                 this.getClass().getName(),
193                 getTypeName(),
194                 new Integer JavaDoc(getTypeId())));
195         }
196
197         m_defaultProperties.add(property);
198     }
199
200     /**
201      * @see org.opencms.file.types.I_CmsResourceType#addMappingType(java.lang.String)
202      */

203     public void addMappingType(String JavaDoc mapping) {
204
205         // this configuration does not support parameters
206
if (LOG.isDebugEnabled()) {
207             LOG.debug(Messages.get().getBundle().key(Messages.LOG_ADD_MAPPING_TYPE_2, mapping, this));
208         }
209         if (m_mappings == null) {
210             m_mappings = new ArrayList JavaDoc();
211         }
212         m_mappings.add(mapping);
213     }
214
215     /**
216      * @see org.opencms.file.types.I_CmsResourceType#changeLastModifiedProjectId(org.opencms.file.CmsObject, CmsSecurityManager, CmsResource)
217      */

218     public void changeLastModifiedProjectId(CmsObject cms, CmsSecurityManager securityManager, CmsResource resource)
219     throws CmsException {
220
221         securityManager.changeLastModifiedProjectId(cms.getRequestContext(), resource);
222     }
223
224     /**
225      * @see org.opencms.file.types.I_CmsResourceType#changeLock(org.opencms.file.CmsObject, CmsSecurityManager, CmsResource)
226      */

227     public void changeLock(CmsObject cms, CmsSecurityManager securityManager, CmsResource resource) throws CmsException {
228
229         securityManager.changeLock(cms.getRequestContext(), resource);
230     }
231
232     /**
233      * @see org.opencms.file.types.I_CmsResourceType#chflags(org.opencms.file.CmsObject, CmsSecurityManager, CmsResource, int)
234      */

235     public void chflags(CmsObject cms, CmsSecurityManager securityManager, CmsResource resource, int flags)
236     throws CmsException {
237
238         securityManager.chflags(cms.getRequestContext(), resource, flags);
239     }
240
241     /**
242      * @see org.opencms.file.types.I_CmsResourceType#chtype(org.opencms.file.CmsObject, CmsSecurityManager, CmsResource, int)
243      */

244     public void chtype(CmsObject cms, CmsSecurityManager securityManager, CmsResource resource, int type)
245     throws CmsException {
246
247         securityManager.chtype(cms.getRequestContext(), resource, type);
248     }
249
250     /**
251      * @see org.opencms.file.types.I_CmsResourceType#copyResource(org.opencms.file.CmsObject, CmsSecurityManager, CmsResource, java.lang.String, int)
252      */

253     public void copyResource(
254         CmsObject cms,
255         CmsSecurityManager securityManager,
256         CmsResource source,
257         String JavaDoc destination,
258         int siblingMode) throws CmsException {
259
260         securityManager.copyResource(
261             cms.getRequestContext(),
262             source,
263             cms.getRequestContext().addSiteRoot(destination),
264             siblingMode);
265     }
266
267     /**
268      * @see org.opencms.file.types.I_CmsResourceType#copyResourceToProject(org.opencms.file.CmsObject, CmsSecurityManager, CmsResource)
269      */

270     public void copyResourceToProject(CmsObject cms, CmsSecurityManager securityManager, CmsResource resource)
271     throws CmsException {
272
273         securityManager.copyResourceToProject(cms.getRequestContext(), resource);
274     }
275
276     /**
277      * @see org.opencms.file.types.I_CmsResourceType#createResource(org.opencms.file.CmsObject, CmsSecurityManager, java.lang.String, byte[], List)
278      */

279     public CmsResource createResource(
280         CmsObject cms,
281         CmsSecurityManager securityManager,
282         String JavaDoc resourcename,
283         byte[] content,
284         List JavaDoc properties) throws CmsException {
285
286         // check if the resource already exists by name
287
if (cms.existsResource(resourcename, CmsResourceFilter.IGNORE_EXPIRATION)) {
288             
289             int todo_v7;
290             // TODO: This should really be done in the securityManager#createResource() method!
291

292             throw new CmsVfsResourceAlreadyExistsException(org.opencms.db.generic.Messages.get().container(
293                 org.opencms.db.generic.Messages.ERR_RESOURCE_WITH_NAME_ALREADY_EXISTS_1,
294                 resourcename));
295         }
296  
297         // initialize a macroresolver with the current user OpenCms context
298
CmsMacroResolver resolver = getMacroResolver(cms, resourcename);
299
300         // add the predefined property values from the XML configuration to the resource
301
List JavaDoc newProperties = processDefaultProperties(properties, resolver);
302
303         CmsResource result = securityManager.createResource(
304             cms.getRequestContext(),
305             cms.getRequestContext().addSiteRoot(resourcename),
306             getTypeId(),
307             content,
308             newProperties);
309
310         // process the (optional) copy resources from the configuration
311
processCopyResources(cms, resourcename, resolver);
312
313         // return the created resource
314
return result;
315     }
316
317     /**
318      * @see org.opencms.file.types.I_CmsResourceType#createSibling(org.opencms.file.CmsObject, org.opencms.db.CmsSecurityManager, CmsResource, java.lang.String, java.util.List)
319      */

320     public void createSibling(
321         CmsObject cms,
322         CmsSecurityManager securityManager,
323         CmsResource source,
324         String JavaDoc destination,
325         List JavaDoc properties) throws CmsException {
326
327         securityManager.createSibling(
328             cms.getRequestContext(),
329             source,
330             cms.getRequestContext().addSiteRoot(destination),
331             properties);
332     }
333
334     /**
335      * @see org.opencms.file.types.I_CmsResourceType#deleteResource(org.opencms.file.CmsObject, CmsSecurityManager, CmsResource, int)
336      */

337     public void deleteResource(CmsObject cms, CmsSecurityManager securityManager, CmsResource resource, int siblingMode)
338     throws CmsException {
339
340         securityManager.deleteResource(cms.getRequestContext(), resource, siblingMode);
341     }
342
343     /**
344      * @see org.opencms.file.types.I_CmsResourceType#getCachePropertyDefault()
345      */

346     public String JavaDoc getCachePropertyDefault() {
347
348         return null;
349     }
350
351     /**
352      * Returns the configured class name of this resource type.<p>
353      *
354      * @see org.opencms.file.types.I_CmsResourceType#getClassName()
355      */

356     public String JavaDoc getClassName() {
357
358         return m_className;
359     }
360
361     /**
362      * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#getConfiguration()
363      */

364     public Map JavaDoc getConfiguration() {
365
366         if (LOG.isDebugEnabled()) {
367             LOG.debug(Messages.get().getBundle().key(Messages.LOG_GET_CONFIGURATION_1, this));
368         }
369         return null;
370     }
371
372     /**
373      * Returns the (unmodifiable) list of copy resources.<p>
374      *
375      * @return the (unmodifiable) list of copy resources
376      */

377     public List JavaDoc getConfiguredCopyResources() {
378
379         return m_copyResources;
380     }
381
382     /**
383      * Returns the default properties for this resource type in an unmodifiable List.<p>
384      *
385      * @return the default properties for this resource type in an unmodifiable List
386      */

387     public List JavaDoc getConfiguredDefaultProperties() {
388
389         return m_defaultProperties;
390     }
391
392     /**
393      * @see org.opencms.file.types.I_CmsResourceType#getConfiguredMappings()
394      */

395     public List JavaDoc getConfiguredMappings() {
396
397         return m_mappings;
398     }
399
400     /**
401      * @see org.opencms.file.types.I_CmsResourceType#getLoaderId()
402      */

403     public abstract int getLoaderId();
404
405     /**
406      * @see org.opencms.file.types.I_CmsResourceType#getTypeId()
407      */

408     public int getTypeId() {
409
410         return m_typeId;
411     }
412
413     /**
414      * @see org.opencms.file.types.I_CmsResourceType#getTypeName()
415      */

416     public String JavaDoc getTypeName() {
417
418         return m_typeName;
419     }
420
421     /**
422      * @see org.opencms.file.types.I_CmsResourceType#importResource(org.opencms.file.CmsObject, CmsSecurityManager, java.lang.String, org.opencms.file.CmsResource, byte[], List)
423      */

424     public CmsResource importResource(
425         CmsObject cms,
426         CmsSecurityManager securityManager,
427         String JavaDoc resourcename,
428         CmsResource resource,
429         byte[] content,
430         List JavaDoc properties) throws CmsException {
431
432         // this triggers the interal "is touched" state
433
// and prevents the security manager from inserting the current time
434
resource.setDateLastModified(resource.getDateLastModified());
435         // ensure resource record is updated
436
resource.setState(CmsResource.STATE_NEW);
437         return securityManager.importResource(
438             cms.getRequestContext(),
439             cms.getRequestContext().addSiteRoot(resourcename),
440             resource,
441             content,
442             properties,
443             true);
444     }
445
446     /**
447      * @see org.opencms.configuration.I_CmsConfigurationParameterHandler#initConfiguration()
448      */

449     public final void initConfiguration() {
450
451         // final since subclassed should NOT implement this, but rather the version with 3 String parameters (see below)
452
if (LOG.isDebugEnabled()) {
453             LOG.debug(Messages.get().getBundle().key(Messages.LOG_INIT_CONFIGURATION_1, this));
454         }
455     }
456
457     /**
458      * Special version of the configuration initialization used with resource types
459      * to set resource type and id, unsing the name of this class instance.<p>
460      *
461      * @param name the resource type name
462      * @param id the resource type id
463      *
464      * @throws CmsConfigurationException if the configuration is invalid
465      *
466      * @deprecated use <code>{@link #initConfiguration(String, String, String)}</code> instead
467      *
468      * @see I_CmsResourceType#initConfiguration(String, String, String)
469      */

470     public void initConfiguration(String JavaDoc name, String JavaDoc id) throws CmsConfigurationException {
471
472         // use this class instance name for the class name
473
initConfiguration(name, id, this.getClass().getName());
474     }
475
476     /**
477      * @see org.opencms.file.types.I_CmsResourceType#initConfiguration(java.lang.String, java.lang.String, java.lang.String)
478      */

479     public void initConfiguration(String JavaDoc name, String JavaDoc id, String JavaDoc className) throws CmsConfigurationException {
480
481         if (LOG.isDebugEnabled()) {
482             LOG.debug(Messages.get().getBundle().key(Messages.LOG_INIT_CONFIGURATION_3, this, name, id));
483
484         }
485
486         if (m_frozen) {
487             // configuration already frozen
488
throw new CmsConfigurationException(org.opencms.configuration.Messages.get().container(
489                 org.opencms.file.types.Messages.ERR_CONFIG_FROZEN_3,
490                 className,
491                 getTypeName(),
492                 new Integer JavaDoc(getTypeId())));
493         }
494
495         // freeze the configuration
496
m_frozen = true;
497
498         // set type name and id (please note that some resource types have a fixed type / id)
499
if (name != null) {
500             m_typeName = name;
501         }
502         if (id != null) {
503             m_typeId = Integer.valueOf(id).intValue();
504         }
505         if (className != null) {
506             m_className = className;
507         }
508
509         // check type id, type name and classs name
510
if ((getTypeId() < 0) || (getTypeName() == null) || (getClassName() == null)) {
511             throw new CmsConfigurationException(Messages.get().container(
512                 Messages.ERR_INVALID_RESTYPE_CONFIG_3,
513                 className,
514                 m_typeName,
515                 new Integer JavaDoc(m_typeId)));
516         }
517
518         m_defaultProperties = Collections.unmodifiableList(m_defaultProperties);
519         m_copyResources = Collections.unmodifiableList(m_copyResources);
520         m_mappings = Collections.unmodifiableList(m_mappings);
521     }
522
523     /**
524      * @see org.opencms.file.types.I_CmsResourceType#initialize(org.opencms.file.CmsObject)
525      */

526     public void initialize(CmsObject cms) {
527
528         // most resource type do not require any runtime information
529
if (LOG.isDebugEnabled()) {
530             LOG.debug(Messages.get().getBundle().key(Messages.LOG_INITIALIZE_1, this));
531         }
532     }
533
534     /**
535      * @see org.opencms.file.types.I_CmsResourceType#isAdditionalModuleResourceType()
536      */

537     public boolean isAdditionalModuleResourceType() {
538
539         return m_addititionalModuleResourceType;
540     }
541
542     /**
543      * @see org.opencms.file.types.I_CmsResourceType#isDirectEditable()
544      */

545     public boolean isDirectEditable() {
546
547         return false;
548     }
549
550     /**
551      * @see org.opencms.file.types.I_CmsResourceType#isFolder()
552      */

553     public boolean isFolder() {
554
555         return false;
556     }
557
558     /**
559      * @see org.opencms.file.types.I_CmsResourceType#lockResource(org.opencms.file.CmsObject, CmsSecurityManager, CmsResource, int)
560      */

561     public void lockResource(CmsObject cms, CmsSecurityManager securityManager, CmsResource resource, int mode)
562     throws CmsException {
563
564         securityManager.lockResource(cms.getRequestContext(), resource, mode);
565     }
566
567     /**
568      * @see org.opencms.file.types.I_CmsResourceType#moveResource(org.opencms.file.CmsObject, CmsSecurityManager, CmsResource, java.lang.String)
569      */

570     public void moveResource(CmsObject cms, CmsSecurityManager securityManager, CmsResource resource, String JavaDoc destination)
571     throws CmsException {
572
573         String JavaDoc dest = cms.getRequestContext().addSiteRoot(destination);
574
575         if (resource.getRootPath().equals(dest)) {
576             // move to target with same name is not allowed
577
throw new CmsVfsException(org.opencms.file.Messages.get().container(
578                 org.opencms.file.Messages.ERR_MOVE_SAME_NAME_1,
579                 destination));
580         }
581
582         // check if the user has write access and if resource is locked
583
// done here since copy is ok without lock, but delete is not
584
securityManager.checkPermissions(
585             cms.getRequestContext(),
586             resource,
587             CmsPermissionSet.ACCESS_WRITE,
588             true,
589             CmsResourceFilter.IGNORE_EXPIRATION);
590
591         // check if the resource to move is new or existing
592
boolean isNew = resource.getState() == CmsResource.STATE_NEW;
593
594         copyResource(cms, securityManager, resource, destination, CmsResource.COPY_AS_SIBLING);
595
596         deleteResource(cms, securityManager, resource, CmsResource.DELETE_PRESERVE_SIBLINGS);
597
598         // make sure lock is switched
599
CmsResource destinationResource = securityManager.readResource(
600             cms.getRequestContext(),
601             dest,
602             CmsResourceFilter.ALL);
603
604         if (isNew) {
605             // if the source was new, destination must get a new lock
606
securityManager.lockResource(cms.getRequestContext(), destinationResource, CmsLock.COMMON);
607         } else {
608             // if source existed, destination must "steal" the lock
609
securityManager.changeLock(cms.getRequestContext(), destinationResource);
610         }
611     }
612
613     public void removeResourceFromProject(CmsObject cms, CmsSecurityManager securityManager, CmsResource resource)
614     throws CmsException {
615
616         securityManager.removeResourceFromProject(cms.getRequestContext(), resource);
617     }
618
619     /**
620      * @see org.opencms.file.types.I_CmsResourceType#replaceResource(org.opencms.file.CmsObject, CmsSecurityManager, CmsResource, int, byte[], List)
621      */

622     public void replaceResource(
623         CmsObject cms,
624         CmsSecurityManager securityManager,
625         CmsResource resource,
626         int type,
627         byte[] content,
628         List JavaDoc properties) throws CmsException {
629
630         securityManager.replaceResource(cms.getRequestContext(), resource, type, content, properties);
631     }
632
633     /**
634      * @see org.opencms.file.types.I_CmsResourceType#restoreResourceBackup(org.opencms.file.CmsObject, CmsSecurityManager, CmsResource, int)
635      */

636     public void restoreResourceBackup(CmsObject cms, CmsSecurityManager securityManager, CmsResource resource, int tag)
637     throws CmsException {
638
639         securityManager.restoreResource(cms.getRequestContext(), resource, tag);
640     }
641
642     /**
643      * @see org.opencms.file.types.I_CmsResourceType#setAdditionalModuleResourceType(boolean)
644      */

645     public void setAdditionalModuleResourceType(boolean additionalType) {
646
647         m_addititionalModuleResourceType = additionalType;
648     }
649
650     /**
651      * @see org.opencms.file.types.I_CmsResourceType#setDateExpired(org.opencms.file.CmsObject, CmsSecurityManager, CmsResource, long, boolean)
652      */

653     public void setDateExpired(
654         CmsObject cms,
655         CmsSecurityManager securityManager,
656         CmsResource resource,
657         long dateExpired,
658         boolean recursive) throws CmsException {
659
660         securityManager.setDateExpired(cms.getRequestContext(), resource, dateExpired);
661     }
662
663     /**
664      * @see org.opencms.file.types.I_CmsResourceType#setDateLastModified(org.opencms.file.CmsObject, CmsSecurityManager, CmsResource, long, boolean)
665      */

666     public void setDateLastModified(
667         CmsObject cms,
668         CmsSecurityManager securityManager,
669         CmsResource resource,
670         long dateLastModified,
671         boolean recursive) throws CmsException {
672
673         securityManager.setDateLastModified(cms.getRequestContext(), resource, dateLastModified);
674     }
675
676     /**
677      * @see org.opencms.file.types.I_CmsResourceType#setDateReleased(org.opencms.file.CmsObject, CmsSecurityManager, CmsResource, long, boolean)
678      */

679     public void setDateReleased(
680         CmsObject cms,
681         CmsSecurityManager securityManager,
682         CmsResource resource,
683         long dateReleased,
684         boolean recursive) throws CmsException {
685
686         securityManager.setDateReleased(cms.getRequestContext(), resource, dateReleased);
687     }
688
689     /**
690      * @see java.lang.Object#toString()
691      */

692     public String JavaDoc toString() {
693
694         StringBuffer JavaDoc output = new StringBuffer JavaDoc();
695         output.append("[ResourceType] class=");
696         output.append(getClass().getName());
697         output.append(" name=");
698         output.append(getTypeName());
699         output.append(" id=");
700         output.append(getTypeId());
701         output.append(" loaderId=");
702         output.append(getLoaderId());
703         return output.toString();
704     }
705
706     /**
707      * @see org.opencms.file.types.I_CmsResourceType#undoChanges(org.opencms.file.CmsObject, CmsSecurityManager, CmsResource, boolean)
708      */

709     public void undoChanges(CmsObject cms, CmsSecurityManager securityManager, CmsResource resource, boolean recursive)
710     throws CmsException {
711
712         securityManager.undoChanges(cms.getRequestContext(), resource);
713     }
714
715     /**
716      * @see org.opencms.file.types.I_CmsResourceType#unlockResource(org.opencms.file.CmsObject, CmsSecurityManager, CmsResource)
717      */

718     public void unlockResource(CmsObject cms, CmsSecurityManager securityManager, CmsResource resource)
719     throws CmsException {
720
721         securityManager.unlockResource(cms.getRequestContext(), resource);
722     }
723
724     /**
725      * @see org.opencms.file.types.I_CmsResourceType#writeFile(org.opencms.file.CmsObject, CmsSecurityManager, CmsFile)
726      */

727     public CmsFile writeFile(CmsObject cms, CmsSecurityManager securityManager, CmsFile resource)
728     throws CmsException, CmsVfsException, CmsSecurityException {
729
730         if (resource.isFile()) {
731             return securityManager.writeFile(cms.getRequestContext(), resource);
732         }
733         // folders can never be written like a file
734
throw new CmsVfsException(Messages.get().container(
735             Messages.ERR_WRITE_FILE_IS_FOLDER_1,
736             cms.getSitePath(resource)));
737     }
738
739     /**
740      * @see org.opencms.file.types.I_CmsResourceType#writePropertyObject(org.opencms.file.CmsObject, org.opencms.db.CmsSecurityManager, CmsResource, org.opencms.file.CmsProperty)
741      */

742     public void writePropertyObject(
743         CmsObject cms,
744         CmsSecurityManager securityManager,
745         CmsResource resource,
746         CmsProperty property) throws CmsException {
747
748         securityManager.writePropertyObject(cms.getRequestContext(), resource, property);
749     }
750
751     /**
752      * @see org.opencms.file.types.I_CmsResourceType#writePropertyObjects(org.opencms.file.CmsObject, org.opencms.db.CmsSecurityManager, CmsResource, java.util.List)
753      */

754     public void writePropertyObjects(
755         CmsObject cms,
756         CmsSecurityManager securityManager,
757         CmsResource resource,
758         List JavaDoc properties) throws CmsException {
759
760         securityManager.writePropertyObjects(cms.getRequestContext(), resource, properties);
761     }
762
763     /**
764      * Creates a macro resolver based on the current users OpenCms context and the provided resource name.<p>
765      *
766      * @param cms the current OpenCms user context
767      * @param resourcename the resource name for macros like {@link A_CmsResourceType#MACRO_RESOURCE_FOLDER_PATH}
768      *
769      * @return a macro resolver based on the current users OpenCms context and the provided resource name
770      */

771     protected CmsMacroResolver getMacroResolver(CmsObject cms, String JavaDoc resourcename) {
772
773         CmsMacroResolver result = CmsMacroResolver.newInstance().setCmsObject(cms);
774         if (isFolder() && (!CmsResource.isFolder(resourcename))) {
775             // ensure folder ends with "/" so
776
resourcename = resourcename.concat("/");
777         }
778         // add special mappings for macros in default properties
779
result.addMacro(MACRO_RESOURCE_ROOT_PATH, cms.getRequestContext().addSiteRoot(resourcename));
780         result.addMacro(MACRO_RESOURCE_SITE_PATH, resourcename);
781         result.addMacro(MACRO_RESOURCE_FOLDER_PATH, CmsResource.getFolderPath(resourcename));
782         result.addMacro(MACRO_RESOURCE_PARENT_PATH, CmsResource.getParentFolder(resourcename));
783         result.addMacro(MACRO_RESOURCE_NAME, CmsResource.getName(resourcename));
784
785         return result;
786     }
787
788     /**
789      * Convenience method to return the initialized resource type
790      * instance for the given id.<p>
791      *
792      * @param resourceType the id of the resource type to get
793      * @return the initialized resource type instance for the given id
794      * @throws CmsException if something goes wrong
795      *
796      * @see org.opencms.loader.CmsResourceManager#getResourceType(int)
797      */

798     protected I_CmsResourceType getResourceType(int resourceType) throws CmsException {
799
800         return OpenCms.getResourceManager().getResourceType(resourceType);
801     }
802
803     /**
804      * Processes the copy resources of this resource type.<p>
805      *
806      * @param cms the current OpenCms user context
807      * @param resourcename the name of the base resource
808      * @param resolver the resolver used for resolving target macro names
809      */

810     protected void processCopyResources(CmsObject cms, String JavaDoc resourcename, CmsMacroResolver resolver) {
811
812         Iterator JavaDoc i = m_copyResources.iterator();
813         while (i.hasNext()) {
814             CmsConfigurationCopyResource copyResource = (CmsConfigurationCopyResource)i.next();
815
816             String JavaDoc target = copyResource.getTarget();
817             if (copyResource.isTargetWasNull() || CmsMacroResolver.isMacro(target, MACRO_RESOURCE_FOLDER_PATH)) {
818                 // target is just the resource folder, must add source file name to target
819
target = target.concat(CmsResource.getName(copyResource.getSource()));
820             }
821             // now resolve the macros in the target name
822
target = resolver.resolveMacros(target);
823             // now resolve possible releative paths in the target
824
target = CmsFileUtil.normalizePath(CmsLinkManager.getAbsoluteUri(target, resourcename), '/');
825
826             try {
827                 cms.copyResource(copyResource.getSource(), target, copyResource.getType());
828             } catch (Exception JavaDoc e) {
829                 // CmsIllegalArgumentException as well as CmsException
830
// log the error and continue with the other copy resources
831
if (LOG.isDebugEnabled()) {
832                     // log stack trace in debug level only
833
LOG.debug(Messages.get().getBundle().key(
834                         Messages.LOG_PROCESS_COPY_RESOURCES_3,
835                         resourcename,
836                         copyResource,
837                         target), e);
838                 } else {
839                     LOG.error(Messages.get().getBundle().key(
840                         Messages.LOG_PROCESS_COPY_RESOURCES_3,
841                         resourcename,
842                         copyResource,
843                         target));
844                 }
845             }
846         }
847     }
848
849     /**
850      * Returns a list of property objects that are attached to the resource on creation.<p>
851      *
852      * It's possible to use OpenCms macros for the property values.
853      * Please see {@link CmsMacroResolver} for allowed macro values.<p>
854      *
855      * @param properties the (optional) properties provided by the user
856      * @param resolver the resolver used to resolve the macro values
857      *
858      * @return a list of property objects that are attached to the resource on creation
859      */

860     protected List JavaDoc processDefaultProperties(List JavaDoc properties, CmsMacroResolver resolver) {
861
862         if ((m_defaultProperties == null) || (m_defaultProperties.size() == 0)) {
863             // no default properties are defined
864
return properties;
865         }
866
867         // the properties must be copied since the macros could contain macros that are
868
// resolved differently for every user / context
869
ArrayList JavaDoc result = new ArrayList JavaDoc();
870         Iterator JavaDoc i = m_defaultProperties.iterator();
871
872         while (i.hasNext()) {
873             // create a clone of the next property
874
CmsProperty property = (CmsProperty)((CmsProperty)i.next()).clone();
875
876             // resolve possible macros in the property values
877
if (property.getResourceValue() != null) {
878                 property.setResourceValue(resolver.resolveMacros(property.getResourceValue()));
879             }
880             if (property.getStructureValue() != null) {
881                 property.setStructureValue(resolver.resolveMacros(property.getStructureValue()));
882             }
883
884             // save the new property in the result list
885
result.add(property);
886         }
887
888         // add the original properties
889
if (properties != null) {
890             result.addAll(properties);
891         }
892
893         // return the result
894
return result;
895     }
896 }
Popular Tags