KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 * File : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/workplace/CmsWpMain.java,v $
3 * Date : $Date: 2005/06/27 23:22:07 $
4 * Version: $Revision: 1.4 $
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.db.CmsUserSettings;
32 import org.opencms.file.CmsGroup;
33 import org.opencms.file.CmsObject;
34 import org.opencms.file.CmsProject;
35 import org.opencms.file.CmsRequestContext;
36 import org.opencms.file.CmsUser;
37 import org.opencms.main.CmsException;
38 import org.opencms.main.CmsLog;
39
40 import com.opencms.core.I_CmsSession;
41 import com.opencms.legacy.CmsXmlTemplateLoader;
42 import com.opencms.template.A_CmsXmlContent;
43 import com.opencms.template.CmsXmlTemplateFile;
44
45 import java.util.Hashtable JavaDoc;
46 import java.util.List JavaDoc;
47 import java.util.Vector JavaDoc;
48
49 /**
50  * Template class for displaying OpenCms workplace main screen.
51  * <P>
52  * Reads template files of the content type <code>CmsXmlWpTemplateFile</code>.
53  *
54  * @author Alexander Lucas
55  * @author Michael Emmerich
56  * @version $Revision: 1.4 $ $Date: 2005/06/27 23:22:07 $
57  * @see com.opencms.workplace.CmsXmlWpTemplateFile
58  *
59  * @deprecated Will not be supported past the OpenCms 6 release.
60  */

61
62 public class CmsWpMain extends CmsWorkplaceDefault {
63         
64     /**
65      * The name of the tag for sync button.
66      */

67     static final String JavaDoc C_SYNC_BUTTON = "SYNC";
68
69     /**
70      * The name of the tag for disabled sync button.
71      */

72     static final String JavaDoc C_SYNC_BUTTON_DISABLED = "SYNC_DISABLED";
73
74     
75     public CmsWpMain() {
76         super();
77         
78         m_SelectedPrjIndex = 0;
79         m_ProjectIds = null;
80         m_ProjectNames = null;
81     }
82         
83     /**
84      * Gets the content of a defined section in a given template file and its subtemplates
85      * with the given parameters.
86      *
87      * @see #getContent(CmsObject, String, String, Hashtable, String)
88      * @param cms CmsObject Object for accessing system resources.
89      * @param templateFile Filename of the template file.
90      * @param elementName Element name of this template in our parent template.
91      * @param parameters Hashtable with all template class parameters.
92      * @param templateSelector template section that should be processed.
93      */

94
95     public byte[] getContent(CmsObject cms, String JavaDoc templateFile, String JavaDoc elementName, Hashtable JavaDoc parameters, String JavaDoc templateSelector) throws CmsException {
96         if (CmsLog.getLog(this).isDebugEnabled() && C_DEBUG) {
97             CmsLog.getLog(this).debug("[CmsXmlTemplate] getting content of element " + ((elementName == null) ? "<root>" : elementName));
98             CmsLog.getLog(this).debug("[CmsXmlTemplate] template file is: " + templateFile);
99             CmsLog.getLog(this).debug("[CmsXmlTemplate] selected template section is: " + ((templateSelector == null) ? "<default>" : templateSelector));
100         }
101
102         I_CmsSession session = CmsXmlTemplateLoader.getSession(cms.getRequestContext(), true);
103         CmsRequestContext reqCont = cms.getRequestContext();
104 // String newGroup = (String) parameters.get("group");
105
String JavaDoc newProject = (String JavaDoc) parameters.get("project");
106         String JavaDoc newView = (String JavaDoc) parameters.get(CmsWorkplaceDefault.C_PARA_VIEW);
107         CmsXmlTemplateFile xmlTemplateDocument = getOwnTemplateFile(cms, templateFile, elementName, parameters, templateSelector);
108
109         // Check if the user requested a group change
110
// if (newGroup != null && !("".equals(newGroup))) {
111
// if (!(newGroup.equals(reqCont.currentGroup().getName()))) {
112
// reqCont.setCurrentGroup(newGroup);
113
// }
114
// }
115

116         // Check if the user requested a project change
117
if (newProject != null && !("".equals(newProject))) {
118             if (!(Integer.parseInt(newProject) == reqCont.currentProject().getId())) {
119                 reqCont.setCurrentProject(cms.readProject(Integer.parseInt(newProject)));
120             }
121         }
122
123         // Check if the user requested a new view
124
if (newView != null && !("".equals(newView))) {
125             session = CmsXmlTemplateLoader.getSession(cms.getRequestContext(), true);
126             session.putValue(CmsWorkplaceDefault.C_PARA_VIEW, newView);
127         }
128
129         // set the publishProject Button to enable if user has the right to publish the project
130
if (templateFile.equalsIgnoreCase(CmsWorkplaceDefault.C_VFS_PATH_DEFAULT_INTERNAL + "head")) {
131             if ((cms.isAdmin() || cms.isManagerOfProject()) && (!reqCont.currentProject().isOnlineProject())) {
132                 xmlTemplateDocument.setData("publish", xmlTemplateDocument.getProcessedDataValue("PUBLISH_ENABLED", this));
133             }
134             else {
135                 xmlTemplateDocument.setData("publish", xmlTemplateDocument.getProcessedDataValue("PUBLISH_DISABLED", this));
136             }
137         }
138
139         // set the sync button to enabled if no entries for synchronisation in registry
140
// if (templateFile.equalsIgnoreCase(C_VFS_PATH_DEFAULT_INTERNAL + "head")) {
141
// String syncpath = null;
142
// syncpath = cms.getRegistry().getSystemValue(I_CmsConstants.C_SYNCHRONISATION_PROJECT);
143
// if (syncpath == null) {
144
xmlTemplateDocument.setData(C_SYNC_BUTTON, xmlTemplateDocument.getProcessedDataValue(C_SYNC_BUTTON_DISABLED, this));
145 // }
146
// else {
147
// xmlTemplateDocument.setData(CmsSyncFolder.C_SYNC_BUTTON, xmlTemplateDocument.getProcessedDataValue(CmsSyncFolder.C_SYNC_BUTTON_ENABLED, this));
148
// }
149
// }
150

151 // // send message, if this is the foot
152
// if (templateFile.equalsIgnoreCase(C_VFS_PATH_DEFAULT_INTERNAL + "foot")) {
153
// String message = (String) CmsXmlTemplateLoader.getSession(cms.getRequestContext(), true).getValue(I_CmsConstants.C_SESSION_BROADCASTMESSAGE);
154
// if (message != null) {
155
// CmsXmlTemplateLoader.getSession(cms.getRequestContext(), true).removeValue(I_CmsConstants.C_SESSION_BROADCASTMESSAGE);
156
// xmlTemplateDocument.setData("message", "alert(unescape('BROADCASTMESSAGE: " + CmsEncoder.escape(message, cms.getRequestContext().getEncoding()) + "'));");
157
// }
158
// }
159

160         // test if the "help"- button has to be displayed for the user's
161
// current language in case we process the head template
162
if (templateFile.equalsIgnoreCase(CmsWorkplaceDefault.C_VFS_PATH_DEFAULT_INTERNAL + "head")) {
163             String JavaDoc userLanguage = CmsXmlLanguageFile.getCurrentUserLanguage(cms);
164             xmlTemplateDocument.setData("LOCALE", "" + userLanguage);
165
166             try {
167                 cms.readFolder(CmsWorkplaceDefault.C_VFS_PATH_HELP + userLanguage);
168                 // the localized help- folder exists
169
xmlTemplateDocument.setData("HELP", xmlTemplateDocument.getProcessedDataValue("HELP_ENABLED", this));
170             }
171             catch (CmsException e) {
172                 // the localized help- folder does not exist
173
try {
174                     xmlTemplateDocument.setData("HELP", xmlTemplateDocument.getProcessedDataValue("HELP_DISABLED", this));
175                 } catch (Exception JavaDoc ex) {
176                     // probably the "head" template is old, ignore this error so the workplace can still be used
177
}
178             }
179         }
180         
181         this.fetchProjects(cms, xmlTemplateDocument);
182
183         // Now load the template file and start the processing
184
return startProcessing(cms, xmlTemplateDocument, elementName, parameters, templateSelector);
185     }
186
187     /**
188      * Gets all groups of the currently logged in user.
189      * <P>
190      * The given vectors <code>names</code> and <code>values</code> will
191      * be filled with the appropriate information to be used for building
192      * a select box.
193      * <P>
194      * Both <code>names</code> and <code>values</code> will contain
195      * the group names after returning from this method.
196      * <P>
197      *
198      * @param cms CmsObject Object for accessing system resources.
199      * @param lang reference to the currently valid language file
200      * @param names Vector to be filled with the appropriate values in this method.
201      * @param values Vector to be filled with the appropriate values in this method.
202      * @param parameters Hashtable containing all user parameters <em>(not used here)</em>.
203      * @return Index representing the user's current group in the vectors.
204      * @throws CmsException
205      */

206
207     public Integer JavaDoc getGroups(CmsObject cms, CmsXmlLanguageFile lang, Vector JavaDoc names, Vector JavaDoc values, Hashtable JavaDoc parameters) throws CmsException {
208
209         // Get a vector of all of the user's groups by asking the request context
210
CmsRequestContext reqCont = cms.getRequestContext();
211 // CmsGroup currentGroup = reqCont.currentGroup();
212
List JavaDoc allGroups = cms.getGroupsOfUser(reqCont.currentUser().getName());
213
214         // Now loop through all groups and fill the result vectors
215
int numGroups = allGroups.size();
216 // int currentGroupNum = 0;
217
for (int i = 0; i < numGroups; i++) {
218             CmsGroup loopGroup = (CmsGroup) allGroups.get(i);
219             String JavaDoc loopGroupName = loopGroup.getName();
220             values.addElement(loopGroupName);
221             names.addElement(loopGroupName);
222 // if (loopGroup.equals(currentGroup)) {
223
//
224
// // Fine. The group of this loop is the user's current group. Save it!
225
// currentGroupNum = i;
226
// }
227
}
228         return new Integer JavaDoc(0);
229     }
230
231     /**
232      * Gets all projects of the currently logged in user.
233      * <P>
234      * The given vectors <code>names</code> and <code>values</code> will
235      * be filled with the appropriate information to be used for building
236      * a select box.
237      * <P>
238      * Both <code>names</code> and <code>values</code> will contain
239      * the project names after returning from this method.
240      * <P>
241      *
242      * @param cms CmsObject Object for accessing system resources.
243      * @param lang reference to the currently valid language file
244      * @param names Vector to be filled with the appropriate values in this method.
245      * @param values Vector to be filled with the appropriate values in this method.
246      * @param parameters Hashtable containing all user parameters <em>(not used here)</em>.
247      * @return Index representing the user's current project in the vectors.
248      * @throws CmsException
249      */

250
251     public Integer JavaDoc getProjects(CmsObject cms, CmsXmlLanguageFile lang, Vector JavaDoc names, Vector JavaDoc values, Hashtable JavaDoc parameters) throws CmsException {
252         if (m_ProjectNames == null || m_ProjectIds == null) {
253             new Integer JavaDoc(0);
254         }
255
256         values.addAll(m_ProjectIds);
257         names.addAll(m_ProjectNames);
258
259         return new Integer JavaDoc(m_SelectedPrjIndex);
260     }
261     
262     private void fetchProjects(CmsObject cms, CmsXmlTemplateFile xmlTemplateDocument) throws CmsException {
263         // Get all project information
264
CmsRequestContext reqCont = cms.getRequestContext();
265         Vector JavaDoc allProjects = new Vector JavaDoc(cms.getAllAccessibleProjects());
266         int currentProjectId = reqCont.currentProject().getId();
267
268         // Now loop through all projects and fill the result vectors
269
int numProjects = allProjects.size();
270         int currentProjectNum = 0;
271         int currentLength = 0;
272         
273         int maxPrjNameLen = 0;
274         m_ProjectIds = new Vector JavaDoc();
275         m_ProjectNames = new Vector JavaDoc();
276         
277         for (int i = 0; i < numProjects; i++) {
278             CmsProject loopProject = (CmsProject) allProjects.elementAt(i);
279             String JavaDoc loopProjectName = loopProject.getName();
280             String JavaDoc loopProjectId = loopProject.getId() + "";
281             
282             m_ProjectIds.addElement(loopProjectId);
283             m_ProjectNames.addElement(loopProjectName);
284             
285             if (loopProject.getId() == currentProjectId) {
286                 // Fine. The project of this loop is the user's current project. Save it!
287
currentProjectNum = i;
288             }
289             
290             currentLength = loopProjectName.length();
291             if (currentLength>maxPrjNameLen) {
292                 maxPrjNameLen = currentLength;
293             }
294         }
295         
296         m_SelectedPrjIndex = currentProjectNum;
297         
298         try {
299             if (maxPrjNameLen <= 20) {
300                 xmlTemplateDocument.setData("PRJ_SELECT", xmlTemplateDocument.getProcessedDataValue("PRJ_SELECT_NORMAL", this));
301             } else {
302                 xmlTemplateDocument.setData("PRJ_SELECT", xmlTemplateDocument.getProcessedDataValue("PRJ_SELECT_LARGE", this));
303             }
304         } catch (CmsException e) {
305             // this exception is only caugth for backwards compatibility with older templates
306
// missing the data blocks above.
307
}
308     }
309     
310     private int m_SelectedPrjIndex;
311     private Vector JavaDoc m_ProjectIds;
312     private Vector JavaDoc m_ProjectNames;
313
314     /**
315      * Gets all views available for this user in the workplace screen from the Registry.
316      * <P>
317      * The given vectors <code>names</code> and <code>values</code> will
318      * be filled with the appropriate information to be used for building
319      * a select box.
320      * <P>
321      * <code>names</code> will contain language specific view descriptions
322      * and <code>values</code> will contain the correspondig URL for each
323      * of these views after returning from this method.
324      * <P>
325      *
326      * @param cms CmsObject Object for accessing system resources.
327      * @param lang reference to the currently valid language file
328      * @param names Vector to be filled with the appropriate values in this method.
329      * @param values Vector to be filled with the appropriate values in this method.
330      * @param parameters Hashtable containing all user parameters <em>(not used here)</em>.
331      * @return Index representing the user's current workplace view in the vectors.
332      * @throws CmsException
333      */

334
335     public Integer JavaDoc getRegViews(CmsObject cms, CmsXmlLanguageFile lang, Vector JavaDoc names, Vector JavaDoc values, Hashtable JavaDoc parameters) throws CmsException {
336
337         // Let's see if we have a session
338
CmsRequestContext reqCont = cms.getRequestContext();
339         I_CmsSession session = CmsXmlTemplateLoader.getSession(cms.getRequestContext(), true);
340
341         // try to get an existing view
342
String JavaDoc currentView = null;
343         Hashtable JavaDoc startSettings = null;
344
345         // check out the user infor1ation if a default view is stored there.
346
startSettings = (Hashtable JavaDoc) reqCont.currentUser().getAdditionalInfo(CmsUserSettings.ADDITIONAL_INFO_STARTSETTINGS);
347         if (startSettings != null) {
348             currentView = (String JavaDoc) startSettings.get(com.opencms.core.I_CmsConstants.C_START_VIEW);
349         }
350
351         // If there is a session, let's see if it has a view stored
352
if (session != null) {
353             if (session.getValue(CmsWorkplaceDefault.C_PARA_VIEW) != null) {
354                 currentView = (String JavaDoc) session.getValue(CmsWorkplaceDefault.C_PARA_VIEW);
355             }
356         }
357         if (currentView == null) {
358             currentView = "";
359         }
360         Vector JavaDoc viewNames = new Vector JavaDoc();
361         Vector JavaDoc viewLinks = new Vector JavaDoc();
362         int currentViewIndex = 0;
363
364 // // get the List of available views from the Registry
365
// int numViews = (cms.getRegistry()).getViews(viewNames, viewLinks);
366
//
367
// // Loop through the vectors and fill the resultvectors
368
// for (int i = 0; i < numViews; i++) {
369
// String loopName = (String) viewNames.elementAt(i);
370
// String loopLink = (String) viewLinks.elementAt(i);
371
//
372
// boolean visible = true;
373
// try {
374
// cms.readFileHeader(loopLink);
375
// }
376
// catch (CmsException e) {
377
// visible = false;
378
// }
379
// if (visible) {
380
// if (loopLink.equals(currentView)) {
381
// currentViewIndex = values.size();
382
// }
383
// names.addElement(lang.getLanguageValue(loopName));
384
// values.addElement(loopLink);
385
// }
386
// }
387
return new Integer JavaDoc(currentViewIndex);
388     }
389
390     /**
391      * Gets the currently logged in user.
392      * <P>
393      * Used for displaying information in the 'foot' frame of the workplace.
394      *
395      * @param cms CmsObject Object for accessing system resources.
396      * @param tagcontent Additional parameter passed to the method <em>(not used here)</em>.
397      * @param doc Reference to the A_CmsXmlContent object of the initiating XLM document <em>(not used here)</em>.
398      * @param userObj Hashtable with parameters <em>(not used here)</em>.
399      * @return String containing the current user.
400      * @throws CmsException
401      */

402
403     public Object JavaDoc getUser(CmsObject cms, String JavaDoc tagcontent, A_CmsXmlContent doc, Object JavaDoc userObj) throws CmsException {
404         CmsRequestContext reqContext = cms.getRequestContext();
405         CmsUser currentUser = reqContext.currentUser();
406         return currentUser.getName();
407     }
408
409     /**
410      * Indicates if the results of this class are cacheable.
411      *
412      * @param cms CmsObject Object for accessing system resources
413      * @param templateFile Filename of the template file
414      * @param elementName Element name of this template in our parent template.
415      * @param parameters Hashtable with all template class parameters.
416      * @param templateSelector template section that should be processed.
417      * @return <EM>true</EM> if cacheable, <EM>false</EM> otherwise.
418      */

419
420     public boolean isCacheable(CmsObject cms, String JavaDoc templateFile, String JavaDoc elementName, Hashtable JavaDoc parameters, String JavaDoc templateSelector) {
421         return false;
422     }
423 }
424
Popular Tags