KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > tools > projects > CmsEditProjectDialog


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/projects/CmsEditProjectDialog.java,v $
3  * Date : $Date: 2006/03/28 12:22:36 $
4  * Version: $Revision: 1.16 $
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.workplace.tools.projects;
33
34 import org.opencms.db.CmsUserSettings;
35 import org.opencms.file.CmsProject;
36 import org.opencms.jsp.CmsJspActionElement;
37 import org.opencms.security.I_CmsPrincipal;
38 import org.opencms.util.CmsFileUtil;
39 import org.opencms.util.CmsStringUtil;
40 import org.opencms.widgets.CmsCheckboxWidget;
41 import org.opencms.widgets.CmsDisplayWidget;
42 import org.opencms.widgets.CmsGroupWidget;
43 import org.opencms.widgets.CmsInputWidget;
44 import org.opencms.widgets.CmsTextareaWidget;
45 import org.opencms.widgets.CmsVfsFileWidget;
46 import org.opencms.workplace.CmsDialog;
47 import org.opencms.workplace.CmsWidgetDialogParameter;
48 import org.opencms.workplace.CmsWorkplaceSettings;
49
50 import java.util.ArrayList JavaDoc;
51 import java.util.Iterator JavaDoc;
52 import java.util.List JavaDoc;
53 import java.util.Map JavaDoc;
54
55 import javax.servlet.http.HttpServletRequest JavaDoc;
56 import javax.servlet.http.HttpServletResponse JavaDoc;
57 import javax.servlet.jsp.PageContext JavaDoc;
58
59 /**
60  * Dialog to edit new and existing project in the administration view.<p>
61  *
62  * @author Michael Moossen
63  *
64  * @version $Revision: 1.16 $
65  *
66  * @since 6.0.0
67  */

68 public class CmsEditProjectDialog extends A_CmsProjectDialog {
69
70     /** localized messages Keys prefix. */
71     public static final String JavaDoc KEY_PREFIX = "project";
72
73     /** Request parameter name for the project id. */
74     public static final String JavaDoc PARAM_PROJECTID = "projectid";
75
76     /** Request parameter name for the project name. */
77     public static final String JavaDoc PARAM_PROJECTNAME = "projectname";
78
79     /** The project object that is edited on this dialog. */
80     protected CmsProject m_project;
81
82     /** Stores the value of the request parameter for the project id. */
83     private String JavaDoc m_paramProjectid;
84
85     /** Auxiliary Property for better representation of the bean VFS resources. */
86     private List JavaDoc m_resources;
87
88     /**
89      * Public constructor with JSP action element.<p>
90      *
91      * @param jsp an initialized JSP action element
92      */

93     public CmsEditProjectDialog(CmsJspActionElement jsp) {
94
95         super(jsp);
96     }
97
98     /**
99      * Public constructor with JSP variables.<p>
100      *
101      * @param context the JSP page context
102      * @param req the JSP request
103      * @param res the JSP response
104      */

105     public CmsEditProjectDialog(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
106
107         this(new CmsJspActionElement(context, req, res));
108     }
109
110     /**
111      * Overridden to set a custom online help mapping.<p>
112      *
113      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceMembers(org.opencms.jsp.CmsJspActionElement)
114      */

115     protected void initWorkplaceMembers(CmsJspActionElement jsp) {
116
117         super.initWorkplaceMembers(jsp);
118         setOnlineHelpUriCustom("/projects/project_edit.jsp");
119     }
120
121     /**
122      * Commits the edited project to the db.<p>
123      */

124     public void actionCommit() {
125
126         List JavaDoc errors = new ArrayList JavaDoc();
127
128         try {
129             // if new create it first
130
if (m_project.getId() == 0) {
131                 CmsProject newProject = getCms().createProject(
132                     m_project.getName(),
133                     m_project.getDescription(),
134                     this.getUserGroup(),
135                     this.getManagerGroup(),
136                     m_project.getType());
137                 m_project = newProject;
138             } else {
139                 m_project.setGroupId(getCms().readGroup(getUserGroup()).getId());
140                 m_project.setManagerGroupId(getCms().readGroup(getManagerGroup()).getId());
141                 getCms().writeProject(m_project);
142             }
143             // write the edited project
144
CmsProject currentProject = getCms().getRequestContext().currentProject();
145             // change the current project
146
getCms().getRequestContext().setCurrentProject(m_project);
147             // store the current site root
148
String JavaDoc currentSite = getCms().getRequestContext().getSiteRoot();
149             // copy the resources to the current project
150
try {
151                 // switch to the root site
152
getCms().getRequestContext().setSiteRoot("");
153                 
154                 // remove deleted resources
155
Iterator JavaDoc itDel = getCms().readProjectResources(m_project).iterator();
156                 while (itDel.hasNext()) {
157                     String JavaDoc resName = itDel.next().toString();
158                     if (!getResources().contains(resName)) {
159                         getCms().removeResourceFromProject(resName);
160                     }
161                 }
162                 // read project resources again!
163
List JavaDoc currentResNames = getCms().readProjectResources(m_project);
164                 // copy missing resources
165
Iterator JavaDoc itAdd = getResources().iterator();
166                 while (itAdd.hasNext()) {
167                     String JavaDoc resName = itAdd.next().toString();
168                     if (!currentResNames.contains(resName)) {
169                         getCms().copyResourceToProject(resName);
170                     }
171                 }
172             } finally {
173                 // switch back to current site and project
174
getCms().getRequestContext().setSiteRoot(currentSite);
175                 getCms().getRequestContext().setCurrentProject(currentProject);
176             }
177             // refresh the list
178
Map JavaDoc objects = (Map JavaDoc)getSettings().getListObject();
179             if (objects != null) {
180                 objects.remove(CmsProjectsList.class.getName());
181             }
182         } catch (Throwable JavaDoc t) {
183             errors.add(t);
184         }
185         // set the list of errors to display when saving failed
186
setCommitErrors(errors);
187     }
188
189     /**
190      * Returns the project id parameter value.<p>
191      *
192      * @return the project id parameter value
193      */

194     public String JavaDoc getParamProjectid() {
195
196         return m_paramProjectid;
197     }
198
199     /**
200      * Returns the list of VFS resources that belong to this project.<p>
201      *
202      * @return the list of VFS resources that belong to this project
203      */

204     public List JavaDoc getResources() {
205
206         return m_resources;
207     }
208
209     /**
210      * Sets the project id parameter value.<p>
211      *
212      * @param projectId the project id parameter value
213      */

214     public void setParamProjectid(String JavaDoc projectId) {
215
216         m_paramProjectid = projectId;
217     }
218
219     /**
220      * Sets the resources of this project.<p>
221      *
222      * @param value the project resources to set
223      */

224     public void setResources(List JavaDoc value) {
225
226         if (value == null) {
227             m_resources = new ArrayList JavaDoc();
228             return;
229         }
230         m_resources = CmsFileUtil.removeRedundancies(value);
231     }
232
233     /**
234      * @see org.opencms.workplace.CmsWidgetDialog#createDialogHtml(java.lang.String)
235      */

236     protected String JavaDoc createDialogHtml(String JavaDoc dialog) {
237
238         StringBuffer JavaDoc result = new StringBuffer JavaDoc(1024);
239
240         result.append(createWidgetTableStart());
241         // show error header once if there were validation errors
242
result.append(createWidgetErrorHeader());
243
244         if (dialog.equals(PAGES[0])) {
245             // create the widgets for the first dialog page
246
result.append(dialogBlockStart(key(Messages.GUI_PROJECT_EDITOR_LABEL_IDENTIFICATION_BLOCK_0)));
247             result.append(createWidgetTableStart());
248             result.append(createDialogRowsHtml(0, 4));
249             result.append(createWidgetTableEnd());
250             result.append(dialogBlockEnd());
251             result.append(dialogBlockStart(key(Messages.GUI_PROJECT_EDITOR_LABEL_CONTENT_BLOCK_0)));
252             result.append(createWidgetTableStart());
253             result.append(createDialogRowsHtml(5, 5));
254             result.append(createWidgetTableEnd());
255             result.append(dialogBlockEnd());
256         }
257
258         result.append(createWidgetTableEnd());
259         return result.toString();
260     }
261
262     /**
263      * Creates the list of widgets for this dialog.<p>
264      */

265     protected void defineWidgets() {
266
267         // initialize the project object to use for the dialog
268
initProjectObject();
269
270         setKeyPrefix(KEY_PREFIX);
271
272         // widgets to display
273
if (isNewProject()) {
274             addWidget(new CmsWidgetDialogParameter(m_project, "name", PAGES[0], new CmsInputWidget()));
275         } else {
276             addWidget(new CmsWidgetDialogParameter(m_project, "name", PAGES[0], new CmsDisplayWidget()));
277         }
278         addWidget(new CmsWidgetDialogParameter(m_project, "description", "", PAGES[0], new CmsTextareaWidget(), 0, 1));
279         addWidget(new CmsWidgetDialogParameter(this, "managerGroup", PAGES[0], new CmsGroupWidget(new Integer JavaDoc(I_CmsPrincipal.FLAG_GROUP_PROJECT_MANAGER), null)));
280         addWidget(new CmsWidgetDialogParameter(this, "userGroup", PAGES[0], new CmsGroupWidget(null, null)));
281         addWidget(new CmsWidgetDialogParameter(m_project, "deleteAfterPublishing", PAGES[0], new CmsCheckboxWidget()));
282         addWidget(new CmsWidgetDialogParameter(this, "resources", PAGES[0], new CmsVfsFileWidget(false, "")));
283     }
284
285     /**
286      * Initializes the project object to work with depending on the dialog state and request parameters.<p>
287      *
288      * Two initializations of the project object on first dialog call are possible:
289      * <ul>
290      * <li>edit an existing project</li>
291      * <li>create a new project</li>
292      * </ul>
293      */

294     protected void initProjectObject() {
295
296         Object JavaDoc o = null;
297
298         try {
299             if (CmsStringUtil.isEmpty(getParamAction()) || CmsDialog.DIALOG_INITIAL.equals(getParamAction())) {
300                 // edit an existing project, get the project object from db
301
m_project = getCms().readProject(new Integer JavaDoc(getParamProjectid()).intValue());
302             } else {
303                 // this is not the initial call, get the project object from session
304
o = getDialogObject();
305                 m_project = (CmsProject)o;
306                 // test
307
m_project.getId();
308             }
309         } catch (Exception JavaDoc e) {
310             // create a new project object
311
m_project = new CmsProject();
312         }
313         try {
314             CmsUserSettings settings = new CmsUserSettings(getCms());
315             m_project.setDeleteAfterPublishing(settings.getProjectSettings().isDeleteAfterPublishing());
316         } catch (Exception JavaDoc e) {
317             // ignore
318
}
319         try {
320             setManagerGroup(getCms().readManagerGroup(m_project).getName());
321         } catch (Exception JavaDoc e) {
322             // ignore
323
}
324         try {
325             setUserGroup(getCms().readGroup(m_project).getName());
326         } catch (Exception JavaDoc e) {
327             // ignore
328
}
329         try {
330             setResources(getCms().readProjectResources(m_project));
331         } catch (Exception JavaDoc e) {
332             // ignore
333
}
334     }
335
336     /**
337      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
338      */

339     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
340
341         // initialize parameters and dialog actions in super implementation
342
super.initWorkplaceRequestValues(settings, request);
343
344         // save the current state of the project (may be changed because of the widget values)
345
setDialogObject(m_project);
346     }
347
348     /**
349      * @see org.opencms.workplace.CmsWidgetDialog#validateParamaters()
350      */

351     protected void validateParamaters() throws Exception JavaDoc {
352
353         if (!isNewProject()) {
354             // test the needed parameters
355
getCms().readProject(new Integer JavaDoc(getParamProjectid()).intValue()).getName();
356         }
357
358     }
359
360     /**
361      * Checks if the Project overview has to be displayed.<p>
362      *
363      * @return <code>true</code> if the project overview has to be displayed
364      */

365     private boolean isNewProject() {
366
367         return getCurrentToolPath().equals("/projects/new");
368     }
369 }
Popular Tags