Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/opencms/file/CmsResourceTypeLink.java


1   /*
2   * File   : $Source: /usr/local/cvs/opencms/src/com/opencms/file/CmsResourceTypeLink.java,v $
3   * Date   : $Date: 2003/04/01 15:20:18 $
4   * Version: $Revision: 1.10 $
5   *
6   * This library is part of OpenCms -
7   * the Open Source Content Mananagement System
8   *
9   * Copyright (C) 2001  The OpenCms Group
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 OpenCms, please see the
22  * OpenCms Website: http://www.opencms.org 
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27  */
28  
29  package com.opencms.file;
30  
31  import com.opencms.core.CmsException;
32  
33  import java.util.Enumeration;
34  import java.util.HashMap;
35  import java.util.Iterator;
36  import java.util.Map;
37  import java.util.Vector;
38  
39  /**
40   * Implementation of a resource type for links between resources in the virtual 
41   * file system (VFS). A VFS link is nothing else but a text
42   * file of a certain resource type. The content of this file is the name/path
43   * of the target resource of the link, including the site root (which is at least
44   * "/default/vfs/").
45   * <p>
46   * VFS links and their target resources are tracked by the RESOURCE_FLAGS table
47   * attribute. Each VFS link saves there the ID of it's target resource. Each 
48   * resource that has VFS links saves the count of it's VFS links (to fix
49   * wheter it has VFS links at all or not).
50   * <p>
51   * All resource types are created by the factory getResourceType() in CmsObject.
52   *
53   * @author Thomas Weckert (t.weckert@alkacon.com)
54   * @version $Revision: 1.10 $
55   */
56  public class CmsResourceTypeLink extends CmsResourceTypePlain {
57  
58      /**
59       * The name of the resource type node in registry.xml to read the
60       * general configurations for VFS links.
61       */
62      public static final String C_TYPE_RESOURCE_NAME = "link";
63      private static final int DEBUG = 0;
64      
65      /**
66       * Create a new VFS link.
67       * 
68       * @param cms the current user's CmsObject instance
69       * @param folder the folder of the new VFS link resource
70       * @param name the name of the new VFS link resource
71       * @param properties a Hashtable with the properties of the new VFS link resource
72       * @param contents a byte array with the content of the new VFS link resource, which is it's target resource name, including the site root
73       * @return the new VFS link resource
74       * @throws CmsException
75       */
76      public CmsResource createResource(CmsObject cms, String newResourceName, Map properties, byte[] contents, Object parameter) throws CmsException{
77          HashMap targetProperties = null;
78          Vector linkPropertyDefs = null;
79                  
80          // create the new link
81          CmsResource res = cms.doCreateFile(newResourceName, contents, C_TYPE_RESOURCE_NAME, properties);
82          
83          // lock the new file
84          cms.lockResource(newResourceName);
85          
86          if (parameter!=null) {
87              targetProperties = (HashMap)parameter;
88              
89              // read all existing properties defined for links
90              Vector propertyDefs = cms.readAllPropertydefinitions( CmsResourceTypeLink.C_TYPE_RESOURCE_NAME );
91              Enumeration allPropertyDefs = propertyDefs.elements();
92              linkPropertyDefs = new Vector( propertyDefs.size() );
93                          
94              while (allPropertyDefs.hasMoreElements()) {
95                  CmsPropertydefinition currentPropertyDefinition = (CmsPropertydefinition)allPropertyDefs.nextElement();
96                  linkPropertyDefs.add( (String)currentPropertyDefinition.getName() );
97              }
98              
99              // copy all properties of the target to the link
100             Iterator i = targetProperties.keySet().iterator();
101             while (i.hasNext()) {
102                 String currentProperty = (String)i.next();
103                 
104                 if (!linkPropertyDefs.contains((String)currentProperty)) {
105                     // add the property definition if the property is not yet defined for links
106                     if (DEBUG>0) System.out.println( "adding property definition " + currentProperty + " for resource type " + CmsResourceTypeLink.C_TYPE_RESOURCE_NAME );
107                     cms.createPropertydefinition( currentProperty, CmsResourceTypeLink.C_TYPE_RESOURCE_NAME );
108                 }
109                 
110                 // write the target property on the link
111                 if (DEBUG>0) System.out.println( "writing property " + currentProperty + " with value " + (String)targetProperties.get(currentProperty) );
112                 cms.writeProperty( newResourceName, currentProperty, (String)targetProperties.get(currentProperty) );
113             }
114         }        
115         
116     // update the link management
117     String targetResourceName = new String(contents);
118     cms.linkResourceToTarget(newResourceName, targetResourceName);
119     cms.doIncrementLinkCountForResource(targetResourceName);
120 
121         return res;
122     }
123 
124     /**
125      * Delete a VFS link. The link counter of the target resource is decremented.
126      * 
127      * @param cms the current user's CmsObject instance
128      * @param theResourceName the resource name of the link
129      * @throws CmsException
130      */
131   public void deleteResource(CmsObject cms, String theResourceName) throws CmsException {
132         String targetResourceName = new String(cms.readFile(theResourceName).getContents());        
133     super.deleteResource(cms, theResourceName);
134 
135     // update the link management
136     cms.doDecrementLinkCountForResource(targetResourceName);
137 }    
138     /**
139      * Undelete a VFS link. The link counter of the target resource is incremented again.
140      * 
141      * @param cms the current user's CmsObject instance
142      * @param theResourceName the resource name of the link
143      * @throws CmsException
144      */    
145     public void undeleteResource(CmsObject cms, String theResourceName) throws CmsException{   
146         super.undeleteResource(cms, theResourceName);
147         String targetResourceName = new String(cms.readFile(theResourceName).getContents());
148         
149         // update the link management
150         cms.doIncrementLinkCountForResource(targetResourceName);
151         cms.linkResourceToTarget(theResourceName, targetResourceName);        
152     }    
153 
154     /**
155      * Copy a VFS link. The link counter of the target resource is incremented. 
156      * The ID of the target resource is saved in the new link resource.
157      * 
158      * @param cms the current user's CmsObject instance
159      * @param theSourceResourceName the source resource name of the link
160      * @param theDestinationResourceName the destination resource name of the link
161      * @param keepFlags boolean whether the copy should use the file flags of the source or the user's default file flags
162      * @throws CmsException
163      */
164   public void copyResource(CmsObject cms, String theSourceResourceName, String theDestinationResourceName, boolean keepFlags) throws CmsException {        
165     super.copyResource(cms, theSourceResourceName, theDestinationResourceName, keepFlags);
166 
167     // update the link management
168     String targetResourceName = new String(cms.readFile(theDestinationResourceName).getContents());
169     cms.doIncrementLinkCountForResource(targetResourceName);
170         cms.linkResourceToTarget(theDestinationResourceName, targetResourceName);
171   }
172 
173     /**
174      * Move a VFS link. The link counter of the target resource remains unchanged.
175      * The ID of the target resource is saved in the new link resource.
176      * 
177      * @param cms the current user's CmsObject instance
178      * @param theSourceResourceName the source resource name of the link
179      * @param theDestinationResourceName the destination resource name of the link
180      * @throws CmsException
181      */
182   public void moveResource(CmsObject cms, String theSourceResourceName, String theDestinationResourceName) throws CmsException {        
183     super.moveResource(cms, theSourceResourceName, theDestinationResourceName);
184 
185     // update the link management
186     String targetResourceName = new String(cms.readFile(theDestinationResourceName).getContents());
187     cms.linkResourceToTarget(theDestinationResourceName, targetResourceName);
188   }
189 
190     /**
191      * Rename a VFS link. The link counter of the target resource remains unchanged.
192      * The ID of the target resource is saved in the new link resource.
193      * 
194      * @param cms the current user's CmsObject instance
195      * @param theOldResourceName the old resource name of the link
196      * @param theNewResourceName the new resource name of the link
197      * @throws CmsException
198      */
199   public void renameResource(CmsObject cms, String theOldResourceName, String theNewResourceName) throws CmsException {        
200     super.renameResource(cms, theOldResourceName, theNewResourceName);
201 
202     // update the link management
203         String folder = theOldResourceName.substring(0, theOldResourceName.lastIndexOf("/") + 1);
204         theNewResourceName = folder + theNewResourceName;
205     String targetResourceName = new String(cms.readFile(theNewResourceName).getContents());
206     cms.linkResourceToTarget(theNewResourceName, targetResourceName);
207   }
208 
209     /**
210      * Undo the changes on a VFS link. In case the target of the link has to be changed
211      * back to another target resource, the link counter of the new target resource
212      * is decremented, and so the link counter of the previous target resource is
213      * incremented again. The ID of the target resource is saved in the new link resource.
214      * 
215      * @param cms the current user's CmsObject instance
216      * @param theResourceName the resource name of the link
217      * @throws CmsException
218      */
219   public void undoChanges(CmsObject cms, String theResourceName) throws CmsException {       
220     String oldTargetResourceName = new String(cms.readFile(theResourceName).getContents());
221     super.undoChanges(cms, theResourceName);
222     String newTargetResourceName = new String(cms.readFile(theResourceName).getContents());
223 
224     // update the link management
225     if (!oldTargetResourceName.equals(newTargetResourceName)) {
226       cms.doDecrementLinkCountForResource(oldTargetResourceName);
227       cms.doIncrementLinkCountForResource(newTargetResourceName);
228       cms.linkResourceToTarget(theResourceName, newTargetResourceName);
229     }
230   }
231         
232 }