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

Quick Search    Search Deep

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


1   /*
2   * File   : $Source: /usr/local/cvs/opencms/src/com/opencms/file/CmsResource.java,v $
3   * Date   : $Date: 2003/04/01 15:20:18 $
4   * Version: $Revision: 1.44 $
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.I_CmsConstants;
32  
33  import java.io.Serializable;
34  
35  /**
36   * Describes a resource in the Cms.
37   * This resource can be a A_CmsFile or a A_CmsFolder.
38   *
39   * @author Michael Emmerich
40   * @version $Revision: 1.44 $ $Date: 2003/04/01 15:20:18 $
41   */
42  public class CmsResource implements I_CmsConstants, Cloneable, Serializable {
43      
44       /**
45        * The database ID
46        */
47       private int m_resourceId;
48  
49       /**
50        * The database Parent ID
51        */
52       private int m_parentId;
53  
54       /**
55        * The database Parent ID
56        */
57       private int m_fileId;
58  
59       /**
60        * The name of this resource.
61        */
62       private String m_resourceName;
63  
64       /**
65        * The type of this resource.
66        */
67       private int m_resourceType;
68  
69       /**
70        * The flags of this resource ( not used yet; the Accessflags are stored in m_accessFlags).
71        */
72       private int m_resourceFlags;
73  
74       /**
75        * The project id this recouce belongs to.
76        */
77       private int m_projectId;
78  
79        /**
80        * The owner  of this resource.
81        */
82       private int m_user;
83  
84       /**
85        * The group  of this resource.
86        */
87       private int m_group;
88  
89       /**
90        * The access flags of this resource.
91        */
92       private int m_accessFlags;
93  
94       /**
95        * The creation date of this resource.
96        */
97       private long m_dateCreated;
98  
99       /**
100       * The date of the last modification of this resource.
101       */
102      private long m_dateLastModified;
103      
104       /** Boolean flag whether the timestamp of this resource was modified by a touch command. */
105       private boolean m_isTouched;
106 
107       /**
108       * The size of the file content.
109       */
110       protected int m_size;
111 
112       /**
113       * The state of this resource. <br>
114       * A resource can have the following states:
115       * <ul>
116       * <li> unchanged </li>
117       * <li> changed </li>
118       * <li> new </li>
119       * <li> deleted </li>
120       * </ul>
121       */
122      private int m_state;
123 
124      /**
125       * The user id of the usrer who locked this resource.
126       */
127      private int m_lockedBy;
128 
129      /**
130       * The type of the launcher which is used to process this resource.
131       */
132      private int m_launcherType;
133 
134      /**
135       * The Java class thas is invoked by the launcher to process this resource.
136       */
137      private String m_launcherClassname;
138 
139      /**
140       * The UserId of the user who modified this resource last.
141       */
142      private int m_resourceLastModifiedBy;
143 
144      /**
145       * The projectId of the project where the resource was locked or modified in
146       */
147     private int m_lockedInProject;
148      /**
149       * Constructor, creates a new CmsRecource object.
150       *
151       * @param resourceId The database Id.
152       * @param parentId The database Id of the parent folder.
153       * @param fileId The id of the content.
154       * @param resourceName The name (including complete path) of the resouce.
155       * @param resourceType The type of this resource.
156       * @param rescourceFlags The flags of thei resource.
157       * @param userId The id of the user of this resource.
158       * @param groupId The id of the group of this resource.
159       * @param projectId The project id this resource belongs to.
160       * @param accessFlags The access flags of this resource.
161       * @param state The state of this resource.
162       * @param lockedBy The user id of the user who has locked this resource.
163       * @param launcherType The launcher that is require to process this recource.
164       * @param launcherClassname The name of the Java class invoked by the launcher.
165       * @param dateCreated The creation date of this resource.
166       * @param dateLastModified The date of the last modification of the resource.
167       * @param resourceLastModifiedBy The user who changed the file.
168       */
169      public CmsResource(int resourceId, int parentId,
170                         int fileId,String resourceName,
171                         int resourceType, int resourceFlags,
172                         int user, int group, int projectId,
173                         int accessFlags, int state, int lockedBy,
174                         int launcherType, String launcherClassname,
175                         long dateCreated, long dateLastModified,
176                         int resourceLastModifiedBy,int size, int lockedInProject){
177 
178         m_resourceId = resourceId;
179         m_parentId = parentId;
180         m_fileId = fileId;
181         m_resourceName=resourceName;
182         m_resourceType=resourceType;
183         m_resourceFlags=resourceFlags;
184         m_user=user;
185         m_group=group;
186         m_projectId=projectId;
187         m_accessFlags=accessFlags;
188         m_launcherType=launcherType;
189         m_launcherClassname=launcherClassname;
190         m_state=state;
191         m_lockedBy=lockedBy;
192         m_dateCreated=dateCreated;
193         m_dateLastModified=dateLastModified;
194         m_resourceLastModifiedBy = resourceLastModifiedBy;
195         m_size=size;
196         m_lockedInProject=lockedInProject;
197         m_isTouched = false;
198      }
199     /**
200      * Clones the CmsResource by creating a new CmsObject.
201      * @return Cloned CmsObject.
202      */
203     public Object clone() {
204         return new CmsResource(m_resourceId, m_parentId,m_fileId,
205                                m_resourceName, m_resourceType, m_resourceFlags,
206                                m_user, m_group, m_projectId,
207                                m_accessFlags, m_state, m_lockedBy,
208                                m_launcherType, m_launcherClassname,
209                                m_dateCreated, m_dateLastModified,
210                                m_resourceLastModifiedBy,m_size, m_lockedInProject);
211     }
212     /**
213      * Compares the overgiven object with this object.
214      *
215      * @return true, if the object is identically else it returns false.
216      */
217       public boolean equals(Object obj) {
218         boolean equal=false;
219         // check if the object is a CmsResource object
220         if (obj instanceof CmsResource) {
221             // same ID than the current user?
222             if (((CmsResource)obj).getResourceName().equals(m_resourceName)){
223                 equal = true;
224             }
225         }
226         return equal;
227       }
228       
229     /**
230      * Returns the absolute path of this resource,
231      * e.g. <code>/system/workplace/action/index.html</code><p>
232      *
233      * @return the absolute path for this resource
234      */
235     public String getAbsolutePath() {
236         return getAbsolutePath(m_resourceName);
237     }
238 
239     /**
240      * Returns the absolute path of the provided resource,
241      * e.g. <code>/system/workplace/action/index.html</code><p>
242      *
243      * @return the absolute path of the provided resource
244      */
245     public static String getAbsolutePath(String resourceName) {
246         if (resourceName == null) return null;
247         return resourceName.substring(resourceName.indexOf("/", resourceName.indexOf("/", 1) + 1));
248     }
249 
250     /**
251      * Returns the root name of this resource,
252      * e.g. <code>/default/vfs</code><p>
253      *
254      * @return the root name for this resource
255      */
256     public String getRootName() {
257         int rootIndex = m_resourceName.indexOf("/", m_resourceName.indexOf("/", 1) + 1);
258         return m_resourceName.substring(0, rootIndex);
259     }
260     
261     /**
262      * Returns the resource name of this resource,
263      * e.g. <code>/default/vfs/system/workplace/action/index.html</code><p>
264      *
265      * @return the resource name for this resource.
266      */
267     public String getResourceName() {
268         return m_resourceName;
269     }
270      
271     /**
272      * Returns the accessflags of this resource.
273      *
274      * @return the accessflags of this resource.
275      */
276       public int getAccessFlags() {
277          return m_accessFlags;
278       }
279     /**
280      * Returns the date of the creation for this resource.
281      *
282      * @return the date of the creation for this resource.
283      */
284      public long getDateCreated() {
285          return m_dateCreated;
286      }
287     /**
288      * Returns the date of the last modification for this resource.
289      *
290      * @return the date of the last modification for this resource.
291      */
292      public long getDateLastModified() {
293          return m_dateLastModified;
294      }
295      
296      /**
297       * Sets the date of the last modification for this resource.
298       */
299      public void setDateLastModified( long time ) {
300         m_isTouched = true;
301         m_dateLastModified = time;
302      }
303           
304     /**
305      * Gets the File id for this resource.
306      *
307      * @return the File id of this resource.
308      */
309      public int getFileId(){
310         return m_fileId;
311      }
312     /**
313      * Returns the flags of this resource ( not used yet; the Accessflags are served in getAccessFlags).
314      *
315      * @return the flags of this resource (this are not the AccessFlags!!).
316      */
317       public int getFlags() {
318          return m_resourceFlags;
319       }
320     /**
321      * Creates a Unix-Style string of access rights from the access right flag of a
322      * CmsResource
323      *
324      * @return String of access rights
325      */
326     public String getFlagString()
327     {
328         String str = "";
329         str += ((m_accessFlags & C_ACCESS_OWNER_READ)>0?"r":"-");
330         str += ((m_accessFlags & C_ACCESS_OWNER_WRITE)>0?"w":"-");
331         str += ((m_accessFlags & C_ACCESS_OWNER_VISIBLE)>0?"v":"-");
332         str += ((m_accessFlags & C_ACCESS_GROUP_READ)>0?"r":"-");
333         str += ((m_accessFlags & C_ACCESS_GROUP_WRITE)>0?"w":"-");
334         str += ((m_accessFlags & C_ACCESS_GROUP_VISIBLE)>0?"v":"-");
335         str += ((m_accessFlags & C_ACCESS_PUBLIC_READ)>0?"r":"-");
336         str += ((m_accessFlags & C_ACCESS_PUBLIC_WRITE)>0?"w":"-");
337         str += ((m_accessFlags & C_ACCESS_PUBLIC_VISIBLE)>0?"v":"-");
338         str += ((m_accessFlags & C_ACCESS_INTERNAL_READ)>0?"i":"-");
339         return str;
340     }
341     /**
342      * Returns the groupid of this resource.
343      *
344      * @return the groupid of this resource.
345      */
346      public int getGroupId() {
347          return  m_group;
348       }
349     /**
350      * Gets the launcher classname for this resource.
351      *
352      * @return the launcher classname for this resource.
353      */
354      public String getLauncherClassname() {
355          return m_launcherClassname;
356      }
357      /**
358      * Gets the launcher type id for this resource.
359      *
360      * @return the launcher type id of this resource.
361      */
362      public int getLauncherType() {
363          return m_launcherType;
364      }
365      /**
366      * Gets the length of the content (filesize).
367      *
368      * @return the length of the content.
369      */
370      public int getLength() {
371         return m_size;
372      }
373     /**
374      * Returns the name of this resource.<BR/>
375      * Example: retuns language.cms for the
376      * resource /system/def/language.cms
377      *
378      * @return the name of this resource.
379      */
380      public String getName() {
381          String name= null;
382          String absoluteName = getAbsolutePath();
383          // check if this is a file
384          if (!absoluteName.endsWith("/")) {
385              name=absoluteName.substring(absoluteName.lastIndexOf("/")+1,
386                                            absoluteName.length());
387          }else{
388               name=absoluteName.substring(0,absoluteName.length()-1);
389               name=name.substring(name.lastIndexOf("/")+1,
390                                   name.length());
391          }
392 
393          return name;
394      }
395     /**
396      * Returns the userid of the resource owner.
397      *
398      * @return the userid of the resource owner.
399      */
400     public int getOwnerId() {
401          return m_user;
402       }
403       
404     /**
405      * Returns the absolute parent folder name of this resource.<p>
406      * 
407      * The parent resource of a file is the folder of the file.
408      * The parent resource of a folder is the parent folder.
409      * The parent resource of the root folder is <code>null</code>.<p>
410      * 
411      * Example: <code>/system/workplace/</code> has the parent <code>/system/</code>.
412      * 
413      * @return the calculated parent absolute folder path, or <code>null</code> for the root folder 
414      */
415      public String getParent() {
416         return getParent(getAbsolutePath());
417      }
418      
419     /**
420      * Returns the absolute parent folder name of a resource.<p>
421      * 
422      * The parent resource of a file is the folder of the file.
423      * The parent resource of a folder is the parent folder.
424      * The parent resource of the root folder is <code>null</code>.<p>
425      * 
426      * Example: <code>/system/workplace/</code> has the parent <code>/system/</code>.
427      * 
428      * @param resource the resource to find the parent folder for
429      * @return the calculated parent absolute folder path, or <code>null</code> for the root folder 
430      */
431     public static String getParent(String resource) {
432         if (C_ROOT.equals(resource)) return null;
433         // remove the last char, for a folder this will be "/", for a file it does not matter
434         String parent = (resource.substring(0, resource.length() - 1));
435         // now as the name does not end with "/", check for the last "/" which is the parent folder name
436         return parent.substring(0, parent.lastIndexOf("/") + 1);
437     }
438     
439     /**
440      * Returns the folder path of this resource,
441      * if the resource is a folder, the complete path of the folder is returned 
442      * (not the parent folder path).<p>
443      * 
444      * Example: Returns <code>/system/def/</code> for the
445      * resource <code>/system/def/file.html</code> and 
446      * <code>/system/def/</code> for the (folder) resource <code>/system/def/</code>.
447      * 
448      * Does not append the repository information to the result, 
449      * i.e. <code>/system/def/</code> will be returned, not <code>/default/vfs/system/def/</code>.
450      *
451      * @return the folder of this resource
452      */
453     public String getPath() {
454         return getPath(getAbsolutePath());
455     }
456     
457     /**
458      * Returns the folder path of the resource with the given name,
459      * if the resource is a folder (i.e. ends with a "/"), the complete path of the folder 
460      * is returned (not the parent folder path).<p>
461      * 
462      * This is achived by just cutting of everthing behind the last occurence of a "/" character
463      * in the String, no check if performed if the resource exists or not in the VFS, 
464      * only resources that end with a "/" are considered to be folders.
465      * 
466      * Example: Returns <code>/system/def/</code> for the
467      * resource <code>/system/def/file.html</code> and 
468      * <code>/system/def/</code> for the (folder) resource <code>/system/def/</code>..
469      *
470      * @param resource the name of a resource
471      * @return the folder of the given resource
472      */
473     public static String getPath(String resource) {
474         return resource.substring(0, resource.lastIndexOf("/") + 1);
475     }
476         
477     /**
478      * Returns the name of a parent folder of the given resource, 
479      * that is either minus levels up 
480      * from the current folder, or that is plus levels down from the 
481      * root folder.<p>
482      * 
483      * @param resource the name of a resource
484      * @param number of levels to walk up or down
485      * @return the name of a parent folder of the given resource, 
486      * that is either minus levels up 
487      * from the current folder, or that is plus levels down from the 
488      * root folder
489      */
490     public static String getPathPart(String resource, int level) {
491         resource = getPath(resource); 
492         String result = null;
493         int pos = 0, count = 0;
494         if (level >= 0) {
495             // Walk down from the root folder /
496             while ((count < level) && (pos > -1)) {
497                 count ++;
498                 pos = resource.indexOf('/', pos+1);
499             }
500         } else {
501             // Walk up from the current folder
502             pos = resource.length();
503             while ((count > level) && (pos > -1)) {
504                 count--;
505                 pos = resource.lastIndexOf('/', pos-1);
506             }      
507         }
508         if (pos > -1) {
509             // To many levels walked
510             result = resource.substring(0, pos+1);
511         } else {
512             // Add trailing slash
513             result = (level < 0)?"/":resource;
514         }        
515         return result;
516     }
517     
518     /**
519      * Returns the directory level of a resource.<p>
520      * 
521      * The root folder "/" has level 0,
522      * a folder "/foo/" would have level 1,
523      * a folfer "/foo/bar/" level 2 etc.<p> 
524      * 
525      * @return the directory level of a resource
526      */
527     public static int getPathLevel(String resource) {
528         int level = -1;
529         int pos = 0;
530         while (resource.indexOf('/', pos) >= 0) {
531             pos = resource.indexOf('/', pos) + 1;
532             level++;
533         }
534         return level;
535     }    
536         
537     /**
538      * Gets the Parent database id for this resource.
539      *
540      * @return the Parent database id of this resource.
541      */
542     public int getParentId() {
543         return m_parentId;
544     }
545           
546     /**
547      * Returns the project id for this resource.
548      *
549      * @return the project id for this resource.
550      */
551     public int getProjectId() {
552         return m_projectId;
553     }
554       
555     /**
556      * Gets the database id for this resource.
557      *
558      * @return the database id of this resource.
559      */
560      public int getResourceId(){
561         return m_resourceId;
562      }
563     /**
564      * Gets the userId from the user who made the last change.
565      *
566      * @return the userId from the user who made the last change.
567      */
568      public int getResourceLastModifiedBy(){
569         return m_resourceLastModifiedBy;
570      }
571     /**
572      * Returns the state of this resource.<BR/>
573      * This may be C_STATE_UNCHANGED, C_STATE_CHANGED, C_STATE_NEW or C_STATE_DELETED.
574      *
575      * @return the state of this resource.
576      */
577       public int getState() {
578       return m_state;
579       }
580     /**
581      * Gets the type id for this resource.
582      *
583      * @return the type id of this resource.
584      */
585      public int getType() {
586        return m_resourceType;
587      }
588     /**
589      * Gets the project id of the project that has locked this resource.
590      *
591      * @return the project id.
592      */
593      public int getLockedInProject() {
594        return m_lockedInProject;
595      }
596      /**
597       * Checks if a resource belongs to a project.
598       * @param project The project which the resources is checked about.
599       * @return true if the resource is in the project, false otherwise.
600       */
601      public boolean inProject(CmsProject project){
602          boolean inProject=false;
603          if (project.getId() == m_projectId) {
604              inProject=true;
605          }
606          return inProject;
607      }
608     /**
609      * Determines, if this resource is a file.
610      *
611      * @return true, if this resource is a file, else it returns false.
612      */
613       public boolean isFile() {
614          boolean isFile=true;
615          if (m_resourceName.endsWith("/")){
616              isFile=false;
617          }
618          return isFile;
619       }
620     /**
621      * Determines, if this resource is a folder.
622      *
623      * @return true, if this resource is a folder, else it returns false.
624      */
625       public boolean isFolder(){
626          boolean isFolder=false;
627          if (m_resourceName.endsWith("/")){
628              isFolder=true;
629          }
630          return isFolder;
631 
632       }
633     /**
634      * Determines, if this resource is locked by a user.
635      *
636      * @return true, if this resource is locked by a user, else it returns false.
637      */
638       public boolean isLocked() {
639           boolean isLocked=true;
640           //check if the user id in the locked by field is the unknown user id.
641           if (m_lockedBy == C_UNKNOWN_ID) {
642               isLocked=false;
643           }
644           return isLocked;
645       }
646     /**
647      * Returns the user idthat locked this resource.
648      *
649      * @return the user id that locked this resource.
650      * If this resource is free it returns the unknown user id.
651      */
652       public int isLockedBy() {
653         return m_lockedBy;
654       }
655      /**
656      * Sets the accessflags of this resource.
657      *
658      * @param The new accessflags of this resource.
659      */
660       public void setAccessFlags(int flags){
661           m_accessFlags=flags;
662       }
663     /**
664      * Sets the File id for this resource.
665      *
666      * @param The File id of this resource.
667      */
668     public void setFileId(int fileId){
669         m_fileId = fileId;
670     }
671      /**
672      * Sets the flags of this resource.
673      *
674      * @param The new flags of this resource.
675      */
676       void setFlags(int flags){
677           m_resourceFlags=flags;
678       }
679     /**
680      * Sets the groupId of this resource.
681      *
682      * @param The new groupId of this resource.
683      */
684       public void setGroupId(int group) {
685           m_group= group;
686       }
687      /**
688      * Sets launcher classname for this resource.
689      *
690      * @param The new launcher classname for this resource.
691      */
692      void setLauncherClassname(String name) {
693       m_launcherClassname=name;
694          }
695      /**
696      * Sets launcher the type id for this resource.
697      *
698      * @param The new launcher type id of this resource.
699      */
700      public void setLauncherType(int type){
701          m_launcherType=type;
702      }
703      /**
704      * Sets the the user id that locked this resource.
705      *
706      * @param The new the user id that locked this resource.
707      */
708      public void setLocked(int id) {
709           m_lockedBy=id;
710       }
711      /**
712      * Sets the parent database id for this resource.
713      *
714      * @param The new database id of this resource.
715      */
716     public void setParentId(int parentId){
717         m_parentId = parentId;
718     }
719     /**
720      * Sets the user id from the user who changes the resource.
721      *
722      * @param The userId from the user who changes the resource.
723      */
724     void setResourceLastModifiedBy(int resourceLastModifiedBy){
725         m_resourceLastModifiedBy = resourceLastModifiedBy;
726     }
727       /**
728      * Sets the state of this resource.
729      *
730      * @param The new state of this resource.
731      */
732       public void setState(int state) {
733           m_state=state;
734       }
735      /**
736      * Sets the type id for this resource.
737      *
738      * @param The new type id of this resource.
739      */
740      public void setType(int type) {
741          m_resourceType=type;
742      }
743     /**
744      * Sets the userId of this resource.
745      *
746      * @param The new userId of this resource.
747      */
748     public  void setUserId(int user) {
749           m_user = user;
750       }
751 
752     /**
753      * Sets the projectId of this resource.
754      *
755      * @param The new projectId of this resource.
756      */
757     public  void setProjectId(int project) {
758           m_projectId = project;
759       }
760 
761 
762     /**
763      * Sets the projectId in which this resource is locked.
764      *
765      * @param The new projectId of this resource.
766      */
767     public  void setLockedInProject(int project) {
768           m_lockedInProject = project;
769       }
770 
771     /**
772      * Returns a string-representation for this object.
773      * This can be used for debugging.
774      *
775      * @return string-representation for this object.
776      */
777       public String toString() {
778         StringBuffer output=new StringBuffer();
779         output.append("[Resource]:");
780         output.append(m_resourceName);
781         output.append(" ID: ");
782         output.append(m_resourceId);
783         output.append(" ParentID: ");
784         output.append(m_parentId);
785         output.append(" , Project=");
786         output.append(m_projectId);
787         output.append(" , User=");
788         output.append(m_user);
789         output.append(" , Group=");
790         output.append(m_group);
791         output.append(" : Access=");
792         output.append(getFlagString());
793         output.append(" : Resource-type=");
794         output.append(getType());
795         output.append(" : Locked=");
796         output.append(isLockedBy());
797         output.append(" : length=");
798         output.append(getLength());
799         output.append(" : state=");
800         output.append(getState());
801         return output.toString();
802       }
803   /**
804    * Returns the isTouched.
805    * @return boolean
806    */
807   public boolean isTouched() {
808     return m_isTouched;
809   }
810 
811 }