KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opencms > workplace > CmsHelperMastertemplates


1 /*
2 * File : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/workplace/CmsHelperMastertemplates.java,v $
3 * Date : $Date: 2005/06/27 23:22:07 $
4 * Version: $Revision: 1.3 $
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.workplace;
30
31 import org.opencms.file.CmsFile;
32 import org.opencms.file.CmsFolder;
33 import org.opencms.file.CmsObject;
34 import org.opencms.file.CmsPropertyDefinition;
35 import org.opencms.file.CmsResource;
36 import org.opencms.main.CmsException;
37 import org.opencms.security.CmsPermissionSet;
38 import org.opencms.workplace.CmsWorkplace;
39
40 import java.util.ArrayList JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.List JavaDoc;
43 import java.util.Vector JavaDoc;
44
45 /**
46  * Helper class to receive all mastertemplates that are currently in the system.
47  * @version $Revision: 1.3 $ $Date: 2005/06/27 23:22:07 $
48  *
49  * @deprecated Will not be supported past the OpenCms 6 release.
50  */

51
52 public class CmsHelperMastertemplates {
53
54     /**
55      * Gets the templates displayed in the template select box.
56      * @param cms The CmsObject.
57      * @param names Will be filled with the display names of found the templates.
58      * @param values Will be filled with the file names of the found templates.
59      * @param currentTemplate The file name of the currently selected template.
60      * @return The index of the currently selected template and the vectors names and values, filled with the information
61      * about the available templates. The index refers to the vectors.
62      * @throws Throws CmsException if something goes wrong.
63      */

64     public static Integer JavaDoc getTemplates(CmsObject cms, Vector JavaDoc names, Vector JavaDoc values, String JavaDoc currentTemplate) throws CmsException {
65         return getTemplates(cms, names, values, currentTemplate, 0);
66     }
67
68     /**
69      * Gets the templates displayed in the template select box.
70      * @param cms The CmsObject.
71      * @param names Will be filled with the display names of found the templates.
72      * @param values Will be filled with the file names of the found templates.
73      * @param currentTemplate The file name of the currently selected template.
74      * @param defaultReturnValue The index used if no currentTemplate was found.
75      * @return The index of the currently selected template and the vectors names and values, filled with the information
76      * about the available templates. The index refers to the vectors.
77      * @throws Throws CmsException if something goes wrong.
78      */

79     public static Integer JavaDoc getTemplates(CmsObject cms, Vector JavaDoc names, Vector JavaDoc values, String JavaDoc currentTemplate, int defaultReturnValue) throws CmsException {
80         // first read the available templates from the VFS
81
getTemplateElements(cms, CmsWorkplace.VFS_DIR_TEMPLATES, names, values);
82          // find the correct index for the current template
83
if(currentTemplate != null) {
84            // it's required to do directory translation if comparing directory names
85
currentTemplate = cms.getRequestContext().getDirectoryTranslator().translateResource(currentTemplate);
86            for(int i = 0; i < values.size(); i++) {
87                 String JavaDoc template = cms.getRequestContext().getDirectoryTranslator().translateResource(((String JavaDoc)values.get(i)));
88                 if(currentTemplate.equals(template)) {
89                     // found the correct index - return it
90
return new Integer JavaDoc(i);
91                 }
92             }
93         }
94         return new Integer JavaDoc(defaultReturnValue);
95     }
96
97     /**
98      * Gets the templates displayed in the template select box.
99      * @param cms The CmsObject.
100      * @param subFolder The sub folder name in the modules to look for information.
101      * @param names Will be filled with the display names of found the templates.
102      * @param values Will be filled with the file names of the found templates.
103      * @throws Throws CmsException if something goes wrong.
104      */

105     public static void getTemplateElements(CmsObject cms, String JavaDoc subFolder, Vector JavaDoc names, Vector JavaDoc values) throws CmsException {
106         List JavaDoc files = (List JavaDoc) new ArrayList JavaDoc();
107
108         // get all selected template elements in the module folders
109
List JavaDoc modules = (List JavaDoc) new ArrayList JavaDoc();
110         modules = cms.getSubFolders(CmsWorkplace.VFS_PATH_MODULES);
111         for(int i = 0;i < modules.size();i++) {
112             List JavaDoc moduleTemplateFiles = (List JavaDoc) new ArrayList JavaDoc();
113             String JavaDoc folder = cms.getSitePath((CmsFolder)modules.get(i));
114             moduleTemplateFiles = cms.getFilesInFolder(folder + subFolder);
115             for(int j = 0;j < moduleTemplateFiles.size();j++) {
116                 files.add(moduleTemplateFiles.get(j));
117             }
118         }
119         
120         // now read the "nice name" (ie. title property) for the found elements
121
Iterator JavaDoc en = files.iterator();
122         while(en.hasNext()) {
123             CmsFile file = (CmsFile)en.next();
124             if(file.getState() != CmsResource.STATE_DELETED && checkVisible(cms, file)) {
125                 String JavaDoc nicename = cms.readProperty(cms.getSitePath(file), CmsPropertyDefinition.PROPERTY_TITLE);
126                 if(nicename == null) {
127                     nicename = file.getName();
128                 }
129                 names.addElement(nicename);
130                 values.addElement(cms.getSitePath(file));
131             }
132         }
133         
134         // finally sort the found elemets
135
CmsHelperMastertemplates.bubblesort(names, values);
136         
137         // no explicit return value, but the parameter vectors are filled with the found values
138
}
139
140     /**
141      * Check if this template should be displayed in the selectbox (this is only
142      * true if the visible flag is set for the current user or if he is admin).
143      * @param cms The CmsObject
144      * @param res The resource to be checked.
145      * @return True or false.
146      * @throws CmsException if something goes wrong.
147      */

148     public static boolean checkVisible(CmsObject cms, CmsResource res) throws CmsException {
149         return cms.hasPermissions(res, CmsPermissionSet.ACCESS_VIEW);
150     }
151
152     /**
153      * Sorts two vectors using bubblesort.<p>
154      *
155      * This is a quick hack to display templates sorted by title instead of
156      * by name in the template dropdown, because it is the title that is shown in the dropdown.
157      *
158      * @param names the vector to sort
159      * @param data vector with data that accompanies names
160      */

161     public static void bubblesort(Vector JavaDoc names, Vector JavaDoc data) {
162         for (int i = 0; i < names.size() - 1; i++) {
163             int len = names.size() - i - 1;
164             for (int j = 0; j < len; j++) {
165                 String JavaDoc a = (String JavaDoc)names.elementAt(j);
166                 String JavaDoc b = (String JavaDoc)names.elementAt(j + 1);
167                 if (a.toLowerCase().compareTo(b.toLowerCase()) > 0) {
168                     names.setElementAt(a, j + 1);
169                     names.setElementAt(b, j);
170                     a = (String JavaDoc)data.elementAt(j);
171                     data.setElementAt(data.elementAt(j + 1), j);
172                     data.setElementAt(a, j + 1);
173                 }
174             }
175         }
176     }
177 }
178
Popular Tags