KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 * File : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/workplace/CmsAdministration.java,v $
3 * Date : $Date: 2005/06/27 23:22:07 $
4 * Version: $Revision: 1.7 $
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
30 package com.opencms.workplace;
31
32 import org.opencms.file.CmsFile;
33 import org.opencms.file.CmsFolder;
34 import org.opencms.file.CmsObject;
35 import org.opencms.file.CmsPropertyDefinition;
36 import org.opencms.file.CmsResource;
37 import org.opencms.file.CmsVfsResourceNotFoundException;
38 import org.opencms.main.CmsException;
39 import org.opencms.main.CmsLog;
40 import org.opencms.security.CmsPermissionSet;
41 import org.opencms.security.CmsSecurityException;
42 import org.opencms.workplace.CmsWorkplace;
43
44 import com.opencms.core.I_CmsSession;
45 import com.opencms.legacy.CmsLegacyException;
46 import com.opencms.legacy.CmsXmlTemplateLoader;
47 import com.opencms.template.CmsTemplateClassManager;
48 import com.opencms.template.CmsXmlTemplateFile;
49
50 import java.lang.reflect.InvocationTargetException JavaDoc;
51 import java.lang.reflect.Method JavaDoc;
52 import java.util.ArrayList JavaDoc;
53 import java.util.Hashtable JavaDoc;
54 import java.util.List JavaDoc;
55 import java.util.Map JavaDoc;
56
57 /**
58  * This class is used to display the administration view.
59  *
60  * Creation date: (09.08.00 14:01:21)
61  * @author Hanjo Riege
62  * @version $Name: $ $Revision: 1.7 $ $Date: 2005/06/27 23:22:07 $
63  *
64  * @deprecated Will not be supported past the OpenCms 6 release.
65  */

66
67 public class CmsAdministration extends CmsWorkplaceDefault {
68
69     /**
70      * Returns the complete Icon.
71      *
72      * @param cms CmsObject Object for accessing system resources
73      * @param templateDocument contains the icondefinition.
74      * @param parameters Hashtable containing all user parameters.
75      * @param lang CmsXmlLanguageFile conataining the currently valid language file.
76      * @param picName the basic name of the picture.
77      * @param sender the name of the icon incl. path.
78      * @param languageKey the the key for the languagefile.
79      * @param iconActiveMethod the method for decision if icon is active, or null.
80      * @param iconVisibleMethod the method for decision if icon is visible, or null.
81      */

82
83     private String JavaDoc generateIcon(CmsObject cms, CmsXmlTemplateFile templateDocument, Hashtable JavaDoc parameters,
84             CmsXmlLanguageFile lang, String JavaDoc picName, String JavaDoc sender, String JavaDoc languageKey,
85                     String JavaDoc iconActiveMethod, String JavaDoc iconVisibleMethod, String JavaDoc accessVisible) throws CmsException {
86
87         boolean hasAccessVisible = (new Boolean JavaDoc(accessVisible)).booleanValue();
88         String JavaDoc iconPicPath = (String JavaDoc)resourcesUri(cms, "", null, null);
89         // change the iconPicPath if the point is from a module
90
if(sender.startsWith(CmsWorkplace.VFS_PATH_SYSTEM + "modules")) {
91             if (picName.startsWith("/")) {
92                 iconPicPath = CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getServletUrl() + sender.substring(0, sender.indexOf("/administration/"));
93             }
94             else {
95                 iconPicPath = CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getServletUrl() + sender.substring(0, sender.indexOf("administration/")) + "pics/";
96             }
97         }
98
99         // call the method for activation decision
100
boolean activate = true;
101         if(iconActiveMethod != null && !"".equals(iconActiveMethod)) {
102             String JavaDoc className = iconActiveMethod.substring(0, iconActiveMethod.lastIndexOf("."));
103             iconActiveMethod = iconActiveMethod.substring(iconActiveMethod.lastIndexOf(".") + 1);
104             Method JavaDoc groupsMethod = null;
105             try {
106                 Object JavaDoc o = CmsTemplateClassManager.getClassInstance(className);
107                 groupsMethod = o.getClass().getMethod(iconActiveMethod, new Class JavaDoc[] {
108                     CmsObject.class, CmsXmlLanguageFile.class, Hashtable JavaDoc.class
109                 });
110                 activate = ((Boolean JavaDoc)groupsMethod.invoke(o, new Object JavaDoc[] {
111                     cms, lang, parameters
112                 })).booleanValue();
113
114             }
115             catch(NoSuchMethodException JavaDoc exc) {
116
117                 // The requested method was not found.
118
throwException("Could not find icon activation method " + iconActiveMethod
119                         + " in calling class " + className + " for generating icon.", CmsLegacyException.C_NOT_FOUND);
120             }
121             catch(InvocationTargetException JavaDoc targetEx) {
122
123                 // the method could be invoked, but throwed a exception
124
// itself. Get this exception and throw it again.
125
Throwable JavaDoc e = targetEx.getTargetException();
126                 if(!(e instanceof CmsException)) {
127
128                     throwException("Icon activation method " + iconActiveMethod + " in calling class "
129                             + className + " throwed an exception. " + e, CmsLegacyException.C_UNKNOWN_EXCEPTION);
130                 }
131                 else {
132
133                     // This is a CmsException
134
// Error printing should be done previously.
135
throw (CmsException)e;
136                 }
137             }
138             catch(Exception JavaDoc exc2) {
139                 throwException("Icon activation method " + iconActiveMethod + " in calling class "
140                         + className + " was found but could not be invoked. " + exc2, CmsLegacyException.C_UNKNOWN_EXCEPTION);
141             }
142         }
143
144         // call the method for the visibility decision
145
boolean visible = true;
146         if(iconVisibleMethod != null && !"".equals(iconVisibleMethod)) {
147             String JavaDoc className = iconVisibleMethod.substring(0, iconVisibleMethod.lastIndexOf("."));
148             iconVisibleMethod = iconVisibleMethod.substring(iconVisibleMethod.lastIndexOf(".") + 1);
149             Method JavaDoc groupsMethod = null;
150             try {
151                 Object JavaDoc o = CmsTemplateClassManager.getClassInstance(className);
152                 groupsMethod = o.getClass().getMethod(iconVisibleMethod, new Class JavaDoc[] {
153                     CmsObject.class, CmsXmlLanguageFile.class, Hashtable JavaDoc.class
154                 });
155                 visible = ((Boolean JavaDoc)groupsMethod.invoke(o, new Object JavaDoc[] {
156                     cms, lang, parameters
157                 })).booleanValue();
158             }
159             catch(NoSuchMethodException JavaDoc exc) {
160
161                 // The requested method was not found.
162
throwException("Could not find icon activation method " + iconVisibleMethod
163                         + " in calling class " + className + " for generating icon.", CmsLegacyException.C_NOT_FOUND);
164             }
165             catch(InvocationTargetException JavaDoc targetEx) {
166
167                 // the method could be invoked, but throwed a exception
168

169                 // itself. Get this exception and throw it again.
170
Throwable JavaDoc e = targetEx.getTargetException();
171                 if(!(e instanceof CmsException)) {
172
173                     throwException("Icon activation method " + iconVisibleMethod + " in calling class "
174                             + className + " throwed an exception. " + e);
175                 }
176                 else {
177
178                     // This is a CmsException
179

180                     // Error printing should be done previously.
181
throw (CmsException)e;
182                 }
183             }
184             catch(Exception JavaDoc exc2) {
185                 throwException("Icon activation method " + iconVisibleMethod + " in calling class "
186                         + className + " was found but could not be invoked. " + exc2);
187             }
188         }
189         templateDocument.setData("linkTo", CmsXmlTemplateLoader.getRequest(cms.getRequestContext()).getServletUrl() + CmsWorkplace.VFS_PATH_WORKPLACE
190                 + "action/administration_content_top.html?sender=" + sender);
191         StringBuffer JavaDoc iconLabelBuffer = new StringBuffer JavaDoc(lang.getLanguageValue(languageKey));
192
193         // Insert a html-break, if needed
194
if(iconLabelBuffer.toString().indexOf("- ") != -1) {
195             iconLabelBuffer.insert(iconLabelBuffer.toString().indexOf("- ") + 1, "<BR>");
196         }
197         templateDocument.setData("linkName", iconLabelBuffer.toString());
198         if(visible && hasAccessVisible) {
199             if(activate) {
200                 templateDocument.setData("picture", iconPicPath + picName + ".gif");
201                 return templateDocument.getProcessedDataValue("defaulticon");
202             }
203             else {
204                 templateDocument.setData("picture", iconPicPath + picName + "_in.gif");
205                 return templateDocument.getProcessedDataValue("deactivatedicon");
206             }
207         }
208         else {
209             return templateDocument.getProcessedDataValue("noicon");
210         }
211     } // of generateIcon
212

213     /**
214      * Gets the content of a defined section in a given template file and its subtemplates
215      * with the given parameters.
216      *
217      * @see #getContent(CmsObject, String, String, Hashtable, String)
218      * @param cms CmsObject Object for accessing system resources.
219      * @param templateFile Filename of the template file.
220      * @param elementName Element name of this template in our parent template.
221      * @param parameters Hashtable with all template class parameters.
222      * @param templateSelector template section that should be processed.
223      */

224
225     public byte[] getContent(CmsObject cms, String JavaDoc templateFile, String JavaDoc elementName, Hashtable JavaDoc parameters, String JavaDoc templateSelector) throws CmsException {
226         I_CmsSession session = CmsXmlTemplateLoader.getSession(cms.getRequestContext(), true);
227         CmsXmlWpTemplateFile templateDocument = new CmsXmlWpTemplateFile(cms, templateFile);
228         CmsXmlLanguageFile lang = templateDocument.getLanguageFile();
229         String JavaDoc navPos = (String JavaDoc)session.getValue(com.opencms.core.I_CmsConstants.C_SESSION_ADMIN_POS);
230         templateDocument.setData("emptyPic", (String JavaDoc)resourcesUri(cms, "empty.gif", null, null));
231         CmsXmlWpConfigFile confFile = new CmsXmlWpConfigFile(cms);
232         String JavaDoc sentBy = (String JavaDoc)parameters.get("sender");
233         
234         if(sentBy == null) {
235             if(navPos == null) {
236                 sentBy = confFile.getWorkplaceAdministrationPath();
237             }
238             else {
239                 if(!navPos.endsWith("/")) {
240                     navPos = navPos.substring(0, navPos.indexOf("/") + 1);
241                 }
242                 sentBy = navPos;
243             }
244         }
245         
246         if(CmsLog.getLog(this).isDebugEnabled() && C_DEBUG) {
247             CmsLog.getLog(this).debug("Getting content of element "
248                     + ((elementName == null) ? "<root>" : elementName));
249             CmsLog.getLog(this).debug("Template file is: " + templateFile);
250             CmsLog.getLog(this).debug("Selected template section is: "
251                     + ((templateSelector == null) ? "<default>" : templateSelector));
252             CmsLog.getLog(this).debug("SentBy: " + sentBy );
253         }
254         
255         List JavaDoc iconVector = (List JavaDoc) new ArrayList JavaDoc();
256         if(sentBy.endsWith("/administration/")) {
257
258             // we must serch for administrationPoints in AdminPath and in system/modules/..
259
sentBy = confFile.getWorkplaceAdministrationPath();
260             iconVector = cms.getSubFolders(sentBy);
261             List JavaDoc modules = (List JavaDoc) new ArrayList JavaDoc();
262
263             modules = cms.getSubFolders(CmsWorkplace.VFS_PATH_MODULES);
264
265             for(int i = 0;i < modules.size();i++) {
266                 List JavaDoc moduleAdminPoints = (List JavaDoc) new ArrayList JavaDoc();
267                 String JavaDoc dummy = cms.getSitePath((CmsFolder)modules.get(i));
268                 dummy += "administration/";
269                 try {
270                     moduleAdminPoints = cms.getSubFolders(dummy);
271                 } catch (CmsVfsResourceNotFoundException e) {
272                     // folder does not exists
273
continue;
274                 } catch (CmsSecurityException e1) {
275                     // no access to this adminstration point, skip it
276
}
277                 for(int j = 0;j < moduleAdminPoints.size();j++) {
278                     CmsFolder currentModuleAdminFolder = (CmsFolder) moduleAdminPoints.get(j);
279                     iconVector.add(currentModuleAdminFolder);
280                     //System.err.println( currentModuleAdminFolder.getResourceName() );
281
}
282             }
283         }
284         else {
285             iconVector = cms.getSubFolders(sentBy);
286         }
287         session.putValue(com.opencms.core.I_CmsConstants.C_SESSION_ADMIN_POS, sentBy);
288         List JavaDoc iconVector2 = cms.getFilesInFolder(sentBy);
289         int numFolders = iconVector.size();
290         if(numFolders > 0) {
291             String JavaDoc iconNames[] = new String JavaDoc[numFolders];
292             int index[] = new int[numFolders];
293             String JavaDoc folderTitles[] = new String JavaDoc[numFolders];
294             String JavaDoc folderLangKeys[] = new String JavaDoc[numFolders];
295             String JavaDoc folderPos[] = new String JavaDoc[numFolders];
296             String JavaDoc folderVisible[] = new String JavaDoc[numFolders];
297             String JavaDoc folderActiv[] = new String JavaDoc[numFolders];
298             String JavaDoc accessVisible[] = new String JavaDoc[numFolders];
299             for(int i = 0;i < numFolders;i++) {
300                 CmsResource aktIcon = (CmsResource)iconVector.get(i);
301                 try {
302                     Map JavaDoc propertyinfos = cms.readProperties(cms.getSitePath(aktIcon));
303                     iconNames[i] = cms.getSitePath(aktIcon);
304                     index[i] = i;
305                     folderLangKeys[i] = getStringValue((String JavaDoc)propertyinfos.get(CmsPropertyDefinition.PROPERTY_NAVTEXT));
306                     folderTitles[i] = getStringValue((String JavaDoc)propertyinfos.get(CmsPropertyDefinition.PROPERTY_TITLE));
307                     folderPos[i] = getStringValue((String JavaDoc)propertyinfos.get(CmsPropertyDefinition.PROPERTY_NAVPOS));
308                     if(folderPos[i].equals("")) {
309                         folderPos[i] = "101";
310                     }
311                     folderVisible[i] = getStringValue((String JavaDoc)propertyinfos.get(org.opencms.file.CmsPropertyDefinition.PROPERTY_VISIBLE));
312                     folderActiv[i] = getStringValue((String JavaDoc)propertyinfos.get(org.opencms.file.CmsPropertyDefinition.PROPERTY_ACTIV));
313                     accessVisible[i] = new Boolean JavaDoc(checkVisible(cms, aktIcon)).toString();
314                 } catch(CmsSecurityException e) {
315                     // ignore all "access denied" type exceptions
316
} catch(CmsException e) {
317                     throw e;
318                 } catch(Throwable JavaDoc t) {
319                     throw new CmsLegacyException("[" + this.getClass().getName() + "] "
320                             + t.getMessage(), CmsLegacyException.C_SQL_ERROR, t);
321                 }
322             } // end of for
323
sort(iconNames, index, folderPos, numFolders);
324             String JavaDoc completeTable = "";
325             int element = 0;
326             int zeile = 0;
327             while(element < numFolders) {
328                 String JavaDoc completeRow = "";
329                 //while((element < numFolders) && (element < (zeile + 1) * C_ELEMENT_PER_ROW)) {
330
while((element < numFolders)) {
331                     int pos = index[element];
332                     if(iconNames[element] != null){
333                         completeRow += generateIcon(cms, templateDocument, parameters, lang, folderTitles[pos],
334                             iconNames[element], folderLangKeys[pos], folderActiv[pos], folderVisible[pos],
335                             accessVisible[pos]);
336                     }
337                     element++;
338                 }
339                 templateDocument.setData("entrys", completeRow);
340                 completeTable += templateDocument.getProcessedDataValue("list_row");
341                 zeile++;
342             } // of while
343
templateDocument.setData("iconTable", completeTable);
344         }
345         else {
346
347             // no Folders, just a real page
348
try {
349                 CmsXmlTemplateLoader.getResponse(cms.getRequestContext()).sendCmsRedirect(sentBy
350                         + "index.html?initial=true");
351             }
352             catch(Exception JavaDoc e) {
353                 throw new CmsLegacyException("Redirect fails :" + cms.getSitePath((CmsFile)iconVector2.get(0)),
354                     CmsLegacyException.C_UNKNOWN_EXCEPTION, e);
355             }
356             return null;
357         }
358         return startProcessing(cms, templateDocument, elementName, parameters, templateSelector);
359     }
360
361     /**
362      * returns the String or "" if it is null.
363      * Creation date: (29.10.00 16:05:38)
364      * @return java.lang.String
365      * @param param java.lang.String
366      */

367
368     private String JavaDoc getStringValue(String JavaDoc param) {
369         if(param == null) {
370             return "";
371         }
372         return param;
373     }
374
375     /**
376      * Indicates if the results of this class are cacheable.
377      *
378      * @param cms CmsObject Object for accessing system resources
379      * @param templateFile Filename of the template file
380      * @param elementName Element name of this template in our parent template.
381      * @param parameters Hashtable with all template class parameters.
382      * @param templateSelector template section that should be processed.
383      * @return <EM>true</EM> if cacheable, <EM>false</EM> otherwise.
384      */

385
386     public boolean isCacheable(CmsObject cms, String JavaDoc templateFile, String JavaDoc elementName,
387             Hashtable JavaDoc parameters, String JavaDoc templateSelector) {
388         return false;
389     }
390
391     /**
392      * Sorts a set of arrays containing navigation information depending on
393      * their navigation positions.
394      * @param filenames Array of filenames
395      * @param index Array of associate Strings
396      * @param positions Array of navpostions
397      */

398
399     private void sort(String JavaDoc[] filenames, int[] index, String JavaDoc[] positions, int max) {
400
401         // Sorting algorithm
402
// This method uses an bubble sort, so replace this with something more
403
// efficient
404
try {
405             for(int i = max - 1;i > 0;i--) {
406                 for(int j = 0;j < i;j++) {
407                     float a = new Float JavaDoc(positions[j]).floatValue();
408                     float b = new Float JavaDoc(positions[j + 1]).floatValue();
409                     if(a > b) {
410                         String JavaDoc tempfilename = filenames[j];
411                         int tempindex = index[j];
412                         String JavaDoc tempposition = positions[j];
413                         filenames[j] = filenames[j + 1];
414                         index[j] = index[j + 1];
415                         positions[j] = positions[j + 1];
416                         filenames[j + 1] = tempfilename;
417                         index[j + 1] = tempindex;
418                         positions[j + 1] = tempposition;
419                     }
420                 }
421             }
422         }
423         catch(Exception JavaDoc e) {
424              if(CmsLog.getLog(this).isWarnEnabled()){
425                  CmsLog.getLog(this).warn("Adminpoints unsorted cause I cant get a valid float value", e);
426              }
427         }
428     } // of sort
429

430     /**
431      * Check if this resource should be displayed in the administrationview.
432      * @param cms The CmsObject
433      * @param resource The resource to be checked.
434      * @return True or false.
435      * @throws CmsException if something goes wrong.
436      */

437
438     private boolean checkVisible(CmsObject cms, CmsResource resource) throws CmsException {
439         return cms.hasPermissions(resource, CmsPermissionSet.ACCESS_VIEW);
440     }
441 }
442
Popular Tags