KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > util > CmsResourceUtil


1 /*
2  * File : $Source$
3  * Date : $Date$
4  * Version: $Revision$
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.util;
33
34 import org.opencms.db.CmsDbUtil;
35 import org.opencms.file.CmsObject;
36 import org.opencms.file.CmsProject;
37 import org.opencms.file.CmsPropertyDefinition;
38 import org.opencms.file.CmsRequestContext;
39 import org.opencms.file.CmsResource;
40 import org.opencms.file.types.CmsResourceTypePlain;
41 import org.opencms.file.types.I_CmsResourceType;
42 import org.opencms.lock.CmsLock;
43 import org.opencms.main.OpenCms;
44 import org.opencms.workplace.CmsWorkplace;
45 import org.opencms.workplace.explorer.CmsExplorerTypeSettings;
46
47 import java.util.List JavaDoc;
48
49 /**
50  * Provides {@link CmsResource} utility functions.<p>
51  *
52  * This class provides in java all resource information used by the explorer view,
53  * mostly generated in javascript (see explorer.js)<p>
54  *
55  * @author Michael Moossen
56  *
57  * @version $Revision$
58  *
59  * @since 6.0.0
60  */

61 public final class CmsResourceUtil {
62
63     /** Resource states abbreviations table. */
64     private static final char[] RESOURCE_STATE = new char[] {'U', 'C', 'N', 'D', '_'};
65
66     /** The folder size display string constant. */
67     private static final String JavaDoc SIZE_DIR = "-";
68
69     /** The current cms context. */
70     private CmsObject m_cms;
71
72     /** The current resource lock. */
73     private CmsLock m_lock;
74
75     /** Reference project resources cache. */
76     private List JavaDoc m_projectResources;
77
78     /** The project to use to check project state, if <code>null</code> the current project will be used. */
79     private CmsProject m_referenceProject;
80
81     /** The current request context. */
82     private CmsRequestContext m_request;
83
84     /** The current resource. */
85     private CmsResource m_resource;
86
87     /** The current resource type. */
88     private I_CmsResourceType m_resourceType;
89
90     // TODO: Remove this class, maybe refactor to org.opencms.workplace.list package
91
// TODO: Check if CmsResource should be extended by this class
92
private int todo = 0;
93
94     /**
95      * Creates a new {@link CmsRequestUtil} object.<p>
96      *
97      * @param cms the cms context
98      */

99     public CmsResourceUtil(CmsObject cms) {
100
101         setCms(cms);
102     }
103
104     /**
105      * Creates a new {@link CmsRequestUtil} object.<p>
106      *
107      * @param cms the cms context
108      * @param resource the resource
109      */

110     public CmsResourceUtil(CmsObject cms, CmsResource resource) {
111
112         setCms(cms);
113         setResource(resource);
114     }
115
116     /**
117      * Creates a new {@link CmsRequestUtil} object.<p>
118      *
119      * @param resource the resource
120      */

121     public CmsResourceUtil(CmsResource resource) {
122
123         setResource(resource);
124     }
125
126     /**
127      * Returns resource state abbreviation.<p>
128      *
129      * @param state the resource state
130      *
131      * @return resource state abbreviation
132      */

133     public static char getStateAbbreviation(int state) {
134
135         if (state >= 0 && state <= 3) {
136             return RESOURCE_STATE[state];
137         } else {
138             return RESOURCE_STATE[4];
139         }
140     }
141
142     /**
143      * Returns the cms context.<p>
144      *
145      * @return the cms context
146      */

147     public CmsObject getCms() {
148
149         return m_cms;
150     }
151
152     /**
153      * Returns the resource icon path displayed in the explorer view for the given resource.<p>
154      *
155      * Relative to <code>/system/workplace/resources/</code>.<p>
156      *
157      * If the resource has no sibling it is the same as {@link #getIconPathResourceType()}.<p>
158      *
159      * @return the resource icon path displayed in the explorer view for the given resource
160      *
161      * @see #getStyleSiblings()
162      */

163     public String JavaDoc getIconPathExplorer() {
164
165         if (m_resource.getSiblingCount() > 1) {
166             // links are present
167
if (m_resource.isLabeled()) {
168                 // there is at least one link in a marked site
169
return "explorer/link_labeled.gif";
170             } else {
171                 // common links are present
172
return "explorer/link.gif";
173             }
174         } else {
175             return getIconPathResourceType();
176         }
177     }
178
179     /**
180      * Returns the lock icon path for the given resource.<p>
181      *
182      * Relative to <code>/system/workplace/resources/</code>.<p>
183      *
184      * Returns <code>explorer/project_none.gif</code> if request context is <code>null</code>.<p>
185      *
186      * @return the lock icon path for the given resource
187      */

188     public String JavaDoc getIconPathLock() {
189
190         int lockId = getLock().getType();
191         String JavaDoc iconPath = null;
192         if (lockId != CmsLock.TYPE_UNLOCKED && m_request != null && isInsideProject()) {
193             if (getLock().getUserId().equals(m_request.currentUser().getId())
194                 && getLockedInProjectId() == getReferenceProject().getId()) {
195                 if (lockId == CmsLock.TYPE_SHARED_EXCLUSIVE || lockId == CmsLock.TYPE_SHARED_INHERITED) {
196                     iconPath = "shared";
197                 } else {
198                     iconPath = "user";
199                 }
200             } else {
201                 iconPath = "other";
202             }
203         }
204         if (iconPath == null) {
205             iconPath = "project_none";
206         } else {
207             iconPath = "lock_" + iconPath;
208         }
209         return "explorer/" + iconPath + ".gif";
210     }
211
212     /**
213      * Returns the project state icon path for the given resource.<p>
214      *
215      * Relative to <code>/system/workplace/resources/</code>.<p>
216      *
217      * @return the project state icon path for the given resource
218      */

219     public String JavaDoc getIconPathProjectState() {
220
221         String JavaDoc iconPath;
222         if (getProjectState() == null) {
223             iconPath = "none";
224         } else if (getProjectState().booleanValue()) {
225             iconPath = "this";
226         } else {
227             iconPath = "other";
228         }
229         return "explorer/project_" + iconPath + ".gif";
230     }
231
232     /**
233      * Returns the resource type icon path for the given resource.<p>
234      *
235      * Relative to <code>/system/workplace/resources/</code>.<p>
236      *
237      * @return the resource type icon path for the given resource
238      */

239     public String JavaDoc getIconPathResourceType() {
240
241         if (!isEditable()) {
242             return "filetypes/"
243                 + OpenCms.getWorkplaceManager().getExplorerTypeSetting(CmsResourceTypePlain.getStaticTypeName()).getIcon();
244         }
245         return "filetypes/" + OpenCms.getWorkplaceManager().getExplorerTypeSetting(getResourceTypeName()).getIcon();
246     }
247
248     /**
249      * Returns the the lock for the given resource.<p>
250      *
251      * @return the lock the given resource
252      */

253     public CmsLock getLock() {
254
255         if (m_lock == null) {
256             try {
257                 m_lock = getCms().getLock(m_resource);
258             } catch (Throwable JavaDoc e) {
259                 m_lock = CmsLock.getNullLock();
260             }
261         }
262         return m_lock;
263     }
264
265     /**
266      * Returns the user name who owns the lock for the given resource.<p>
267      *
268      * @return the user name who owns the lock for the given resource
269      */

270     public String JavaDoc getLockedByName() {
271
272         String JavaDoc lockedBy = "";
273         if (!getLock().isNullLock()) {
274             try {
275                 lockedBy = getCms().readUser(getLock().getUserId()).getName();
276             } catch (Throwable JavaDoc e) {
277                 lockedBy = e.getMessage();
278             }
279         }
280         return lockedBy;
281     }
282
283     /**
284      * Returns the id of the project in which the given resource is locked.<p>
285      *
286      * @return the id of the project in which the given resource is locked
287      */

288     public int getLockedInProjectId() {
289
290         int lockedInProject = CmsDbUtil.UNKNOWN_ID;
291         if (getLock().isNullLock() && getResource().getState() != CmsResource.STATE_UNCHANGED) {
292             // resource is unlocked and modified
293
lockedInProject = getResource().getProjectLastModified();
294         } else {
295             if (getResource().getState() != CmsResource.STATE_UNCHANGED) {
296                 // resource is locked and modified
297
lockedInProject = getProjectId();
298             } else {
299                 // resource is locked and unchanged
300
lockedInProject = getLock().getProjectId();
301             }
302         }
303         return lockedInProject;
304     }
305
306     /**
307      * Returns the project name that locked the current resource's.<p>
308      *
309      * @return the the project name that locked the current resource's
310      */

311     public String JavaDoc getLockedInProjectName() {
312
313         try {
314             int pId = getLockedInProjectId();
315             if (pId == CmsDbUtil.UNKNOWN_ID) {
316                 // the resource is unlocked and unchanged
317
return "";
318             }
319             return getCms().readProject(pId).getName();
320         } catch (Throwable JavaDoc e) {
321             return e.getMessage();
322         }
323     }
324
325     /**
326      * Returns the permissions string for the given resource.<p>
327      *
328      * @return the permissions string for the given resource
329      */

330     public String JavaDoc getPermissions() {
331
332         String JavaDoc permissions;
333         try {
334             permissions = getCms().getPermissions(getCms().getSitePath(getResource())).getPermissionString();
335         } catch (Throwable JavaDoc e) {
336             try {
337                 permissions = getCms().getPermissions(getResource().getRootPath()).getPermissionString();
338             } catch (Throwable JavaDoc e1) {
339                 permissions = e1.getMessage();
340             }
341         }
342         return permissions;
343     }
344
345     /**
346      * Returns the id of the project which the resource belongs to.<p>
347      *
348      * @return the id of the project which the resource belongs to
349      */

350     public int getProjectId() {
351
352         int projectId = m_resource.getProjectLastModified();
353         if (!getLock().isNullLock()
354             && getLock().getType() != CmsLock.TYPE_INHERITED
355             && getLock().getType() != CmsLock.TYPE_SHARED_INHERITED) {
356             // use lock project ID only if lock is not inherited
357
projectId = getLock().getProjectId();
358         }
359         return projectId;
360     }
361
362     /**
363      * Returns the project state of the given resource.<p>
364      *
365      * <ul>
366      * <li>null: unchanged.</li>
367      * <li>true: locked in current project.</li>
368      * <li>false: not locked in current project.</li>
369      * </ul>
370      *
371      * @return the project state of the given resource
372      */

373     public Boolean JavaDoc getProjectState() {
374
375         if (m_resource.getState() == CmsResource.STATE_UNCHANGED || !isInsideProject()) {
376             return null;
377         } else {
378             return new Boolean JavaDoc(getLockedInProjectId() == getReferenceProject().getId());
379         }
380     }
381
382     /**
383      * Returns the project to use to check project state.<p>
384      *
385      * @return the project to use to check project state
386      */

387     public CmsProject getReferenceProject() {
388
389         if (m_referenceProject == null) {
390             if (m_request != null) {
391                 m_referenceProject = m_request.currentProject();
392             }
393         }
394         return m_referenceProject;
395     }
396
397     /**
398      * Returns the resource.<p>
399      *
400      * @return the resource
401      */

402     public CmsResource getResource() {
403
404         return m_resource;
405     }
406
407     /**
408      * Returns the resource type for the given resource.<p>
409      *
410      * @return the resource type for the given resource
411      */

412     public I_CmsResourceType getResourceType() {
413
414         if (m_resourceType == null) {
415             try {
416                 m_resourceType = OpenCms.getResourceManager().getResourceType(m_resource.getTypeId());
417             } catch (Throwable JavaDoc e) {
418                 try {
419                     m_resourceType = OpenCms.getResourceManager().getResourceType(
420                         CmsResourceTypePlain.getStaticTypeId());
421                 } catch (Throwable JavaDoc e1) {
422                     // should never happen
423
m_resourceType = null;
424                 }
425             }
426         }
427         return m_resourceType;
428     }
429
430     /**
431      * Returns the resource type id for the given resource.<p>
432      *
433      * @return the resource type id for the given resource
434      */

435     public int getResourceTypeId() {
436
437         return getResourceType().getTypeId();
438     }
439
440     /**
441      * Returns the resource type name for the given resource.<p>
442      *
443      * @return the resource type name for the given resource
444      */

445     public String JavaDoc getResourceTypeName() {
446
447         return getResourceType().getTypeName();
448     }
449
450     /**
451      * Returns the size of the given resource as a String.<p>
452      *
453      * For directories it returns {@link #SIZE_DIR}.<p>
454      *
455      * @return the size of the given resource as a String
456      */

457     public String JavaDoc getSizeString() {
458
459         return m_resource.getLength() == -1 ? SIZE_DIR : "" + m_resource.getLength();
460     }
461
462     /**
463      * Returns resource state abbreviation.<p>
464      *
465      * @return resource state abbreviation
466      */

467     public char getStateAbbreviation() {
468
469         int state = getResource().getState();
470         if (state >= 0 && state <= 3) {
471             return RESOURCE_STATE[state];
472         } else {
473             return RESOURCE_STATE[4];
474         }
475     }
476
477     /**
478      * Returns the state name for a resource.<p>
479      *
480      * Uses default locale if request context is <code>null</code>.<p>
481      *
482      * @return the state name for that resource
483      */

484     public String JavaDoc getStateName() {
485
486         int state = m_resource.getState();
487         String JavaDoc name;
488         if (m_request == null) {
489             name = org.opencms.workplace.explorer.Messages.get().getBundle().key(
490                 org.opencms.workplace.explorer.Messages.getStateKey(state));
491         } else {
492             name = org.opencms.workplace.explorer.Messages.get().getBundle(m_request.getLocale()).key(
493                 org.opencms.workplace.explorer.Messages.getStateKey(state));
494         }
495         return name;
496     }
497
498     /**
499      * Returns the style class to use for the given resource.<p>
500      *
501      * @return style class name
502      *
503      * @see org.opencms.workplace.list.CmsListExplorerColumn#getExplorerStyleDef()
504      */

505     public String JavaDoc getStyleClassName() {
506
507         if (isInsideProject() && isEditable()) {
508             switch (m_resource.getState()) {
509                 case CmsResource.STATE_CHANGED:
510                     return "fc";
511                 case CmsResource.STATE_NEW:
512                     return "fn";
513                 case CmsResource.STATE_DELETED:
514                     return "fd";
515                 case CmsResource.STATE_UNCHANGED:
516                 default:
517                     return "nf";
518             }
519         }
520         return "fp";
521     }
522
523     /**
524      * Returns additional style sheets depending on publication constraints.<p>
525      *
526      * That is, depending on {@link CmsResource#getDateReleased()} and
527      * {@link CmsResource#getDateExpired()}.<p>
528      *
529      * @return additional style sheets depending on publication constraints
530      */

531     public String JavaDoc getStyleRange() {
532
533         return isInRange() ? "" : "font-style:italic;";
534     }
535
536     /**
537      * Returns additional style sheets for the resource type icon depending on siblings.<p>
538      *
539      * That is, depending on {@link CmsResource#getSiblingCount()}
540      *
541      * Use it with the {@link #getIconPathExplorer} method.<p>
542      *
543      * @return additional style sheets depending on siblings
544      */

545     public String JavaDoc getStyleSiblings() {
546
547         StringBuffer JavaDoc style = new StringBuffer JavaDoc(128);
548         if (m_resource.getSiblingCount() > 1) {
549             style.append("background-image:url(");
550             style.append(CmsWorkplace.getSkinUri());
551             style.append(getIconPathResourceType());
552             style.append("); background-position: 0px 0px; background-repeat: no-repeat; ");
553         }
554         return style.toString();
555     }
556
557     /**
558      * Returns the title of a resource.<p>
559      *
560      * @return the title for that resource
561      */

562     public String JavaDoc getTitle() {
563
564         String JavaDoc title;
565         try {
566             title = getCms().readPropertyObject(
567                 getCms().getSitePath(m_resource),
568                 CmsPropertyDefinition.PROPERTY_TITLE,
569                 false).getValue();
570         } catch (Throwable JavaDoc e) {
571             title = e.getMessage();
572         }
573         if (title == null) {
574             title = "";
575         }
576         return title;
577     }
578
579     /**
580      * Returns the name of the user who created the given resource.<p>
581      *
582      * @return the name of the user who created the given resource
583      */

584     public String JavaDoc getUserCreated() {
585
586         String JavaDoc user = m_resource.getUserCreated().toString();
587         try {
588             user = getCms().readUser(m_resource.getUserCreated()).getName();
589         } catch (Throwable JavaDoc e) {
590             // ignore
591
}
592         return user;
593     }
594
595     /**
596      * Returns the name of the user who last modified the given resource.<p>
597      *
598      * @return the name of the user who last modified the given resource
599      */

600     public String JavaDoc getUserLastModified() {
601
602         String JavaDoc user = m_resource.getUserLastModified().toString();
603         try {
604             user = getCms().readUser(m_resource.getUserLastModified()).getName();
605         } catch (Throwable JavaDoc e) {
606             // ignore
607
}
608         return user;
609     }
610
611     /**
612      * Returns <code>true</code> if the given resource is editable by the current user.<p>
613      *
614      * Retuns <code>false</code> if no request context is set.<p>
615      *
616      * @return <code>true</code> if the given resource is editable by the current user
617      */

618     public boolean isEditable() {
619
620         if (m_request == null) {
621             return false;
622         }
623         CmsExplorerTypeSettings settings = OpenCms.getWorkplaceManager().getExplorerTypeSetting(getResourceTypeName());
624         if (settings != null) {
625             return settings.getAccess().getPermissions(getCms()).requiresWritePermission();
626         }
627         return false;
628     }
629
630     /**
631      * Returns <code>true</code> if the given resource has expired.<p>
632      *
633      * Retuns <code>true</code> if no request context is set.<p>
634      *
635      * @return <code>true</code> if the given resource has expired
636      */

637     public boolean isExpired() {
638
639         if (m_request == null) {
640             return m_resource.getDateExpired() < System.currentTimeMillis();
641         }
642         return m_resource.getDateExpired() < m_request.getRequestTime();
643     }
644
645     /**
646      * Returns <code>true</code> if the given resource has been released and has not expired.<p>
647      *
648      * Retuns <code>false</code> if no request context is set.<p>
649      *
650      * @return <code>true</code> if the given resource has been released and has not expired
651      */

652     public boolean isInRange() {
653
654         return isReleased() && !isExpired();
655     }
656
657     /**
658      * Returns <code>true</code> if the given resource is in the reference project.<p>
659      *
660      * Returns <code>false</code> if the request context is <code>null</code>.<p>
661      *
662      * @return <code>true</code> if the given resource is in the reference project
663      *
664      * @see #getReferenceProject()
665      */

666     public boolean isInsideProject() {
667
668         if (m_projectResources == null) {
669             try {
670                 m_projectResources = getCms().readProjectResources(getReferenceProject());
671             } catch (Throwable JavaDoc e) {
672                 return false;
673             }
674         }
675         return CmsProject.isInsideProject(m_projectResources, m_resource);
676     }
677
678     /**
679      * Returns <code>true</code> if the given resource has been released.<p>
680      *
681      * @return <code>true</code> if the given resource has been released
682      */

683     public boolean isReleased() {
684
685         if (m_request == null) {
686             return m_resource.getDateReleased() < System.currentTimeMillis();
687         }
688         return m_resource.getDateReleased() < m_request.getRequestTime();
689     }
690
691     /**
692      * Sets the cms context.<p>
693      *
694      * @param cms the cms context to set
695      */

696     public void setCms(CmsObject cms) {
697
698         m_cms = cms;
699         m_request = cms.getRequestContext();
700         m_referenceProject = null;
701         m_projectResources = null;
702     }
703
704     /**
705      * Sets the project to use to check project state.<p>
706      *
707      * @param project the project to set
708      */

709     public void setReferenceProject(CmsProject project) {
710
711         m_referenceProject = project;
712         m_projectResources = null;
713     }
714
715     /**
716      * Sets the resource.<p>
717      *
718      * @param resource the resource to set
719      */

720     public void setResource(CmsResource resource) {
721
722         m_resource = resource;
723         m_lock = null;
724         m_resourceType = null;
725     }
726 }
Popular Tags