001: /*
002: * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/projects/CmsEditProjectDialog.java,v $
003: * Date : $Date: 2008-02-27 12:05:51 $
004: * Version: $Revision: 1.21 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.workplace.tools.projects;
033:
034: import org.opencms.file.CmsGroup;
035: import org.opencms.file.CmsProject;
036: import org.opencms.jsp.CmsJspActionElement;
037: import org.opencms.main.CmsException;
038: import org.opencms.main.CmsIllegalArgumentException;
039: import org.opencms.main.OpenCms;
040: import org.opencms.security.CmsOrganizationalUnit;
041: import org.opencms.security.CmsRole;
042: import org.opencms.security.I_CmsPrincipal;
043: import org.opencms.util.CmsFileUtil;
044: import org.opencms.util.CmsStringUtil;
045: import org.opencms.util.CmsUUID;
046: import org.opencms.widgets.CmsCheckboxWidget;
047: import org.opencms.widgets.CmsDisplayWidget;
048: import org.opencms.widgets.CmsGroupWidget;
049: import org.opencms.widgets.CmsInputWidget;
050: import org.opencms.widgets.CmsOrgUnitWidget;
051: import org.opencms.widgets.CmsTextareaWidget;
052: import org.opencms.widgets.CmsVfsFileWidget;
053: import org.opencms.workplace.CmsDialog;
054: import org.opencms.workplace.CmsWidgetDialog;
055: import org.opencms.workplace.CmsWidgetDialogParameter;
056: import org.opencms.workplace.CmsWorkplaceSettings;
057:
058: import java.util.ArrayList;
059: import java.util.HashMap;
060: import java.util.Iterator;
061: import java.util.List;
062: import java.util.Map;
063:
064: import javax.servlet.http.HttpServletRequest;
065: import javax.servlet.http.HttpServletResponse;
066: import javax.servlet.jsp.PageContext;
067:
068: /**
069: * Dialog to edit new and existing project in the administration view.<p>
070: *
071: * @author Michael Moossen
072: *
073: * @version $Revision: 1.21 $
074: *
075: * @since 6.0.0
076: */
077: public class CmsEditProjectDialog extends CmsWidgetDialog {
078:
079: /** localized messages Keys prefix. */
080: public static final String KEY_PREFIX = "project";
081:
082: /** Defines which pages are valid for this dialog. */
083: public static final String[] PAGES = { "page1" };
084:
085: /** Request parameter name for the project id. */
086: public static final String PARAM_PROJECTID = "projectid";
087:
088: /** Request parameter name for the project name. */
089: public static final String PARAM_PROJECTNAME = "projectname";
090:
091: /** The project object that is edited on this dialog. */
092: protected CmsProject m_project;
093:
094: /** Stores the value of the request parameter for the project id. */
095: private String m_paramProjectid;
096:
097: /** Auxiliary Property for better representation of the bean VFS resources. */
098: private List m_resources;
099:
100: /**
101: * Public constructor with JSP action element.<p>
102: *
103: * @param jsp an initialized JSP action element
104: */
105: public CmsEditProjectDialog(CmsJspActionElement jsp) {
106:
107: super (jsp);
108: }
109:
110: /**
111: * Public constructor with JSP variables.<p>
112: *
113: * @param context the JSP page context
114: * @param req the JSP request
115: * @param res the JSP response
116: */
117: public CmsEditProjectDialog(PageContext context,
118: HttpServletRequest req, HttpServletResponse res) {
119:
120: this (new CmsJspActionElement(context, req, res));
121: }
122:
123: /**
124: * Commits the edited project to the database.<p>
125: */
126: public void actionCommit() {
127:
128: List errors = new ArrayList();
129:
130: try {
131: // if new create it first
132: if (m_project.getUuid() == null) {
133: CmsProject newProject = getCms().createProject(
134: m_project.getName(),
135: m_project.getDescription(), getUserGroup(),
136: getManagerGroup(), m_project.getType());
137: m_project = newProject;
138: } else {
139: getCms().writeProject(m_project);
140: }
141: // write the edited project resources
142: CmsProject currentProject = getCms().getRequestContext()
143: .currentProject();
144: // change the current project
145: getCms().getRequestContext().setCurrentProject(m_project);
146: // store the current site root
147: String currentSite = getCms().getRequestContext()
148: .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 itDel = getCms().readProjectResources(
156: m_project).iterator();
157: while (itDel.hasNext()) {
158: String resName = itDel.next().toString();
159: if (!getResources().contains(resName)) {
160: getCms().removeResourceFromProject(resName);
161: }
162: }
163: // read project resources again!
164: List currentResNames = getCms().readProjectResources(
165: m_project);
166: // copy missing resources
167: Iterator itAdd = getResources().iterator();
168: while (itAdd.hasNext()) {
169: String resName = itAdd.next().toString();
170: if (!currentResNames.contains(resName)) {
171: getCms().copyResourceToProject(resName);
172: }
173: }
174: } finally {
175: // switch back to current site and project
176: getCms().getRequestContext().setSiteRoot(currentSite);
177: getCms().getRequestContext().setCurrentProject(
178: currentProject);
179: }
180: // refresh the list
181: Map objects = (Map) getSettings().getListObject();
182: if (objects != null) {
183: objects.remove(CmsProjectsList.class.getName());
184: }
185: } catch (Throwable t) {
186: errors.add(t);
187: }
188: // set the list of errors to display when saving failed
189: setCommitErrors(errors);
190: }
191:
192: /**
193: * Returns the description of the parent ou.<p>
194: *
195: * @return the description of the parent ou
196: */
197: public String getAssignedOu() {
198:
199: try {
200: CmsOrganizationalUnit ou;
201: if (!isNewProject()) {
202: ou = OpenCms.getOrgUnitManager()
203: .readOrganizationalUnit(getCms(),
204: m_project.getOuFqn());
205: } else {
206: ou = (CmsOrganizationalUnit) OpenCms.getRoleManager()
207: .getOrgUnitsForRole(getCms(),
208: CmsRole.PROJECT_MANAGER, true).get(0);
209: }
210: return ou.getDisplayName(getLocale());
211: } catch (CmsException e) {
212: return null;
213: }
214: }
215:
216: /**
217: * Returns the manager Group name.<p>
218: *
219: * @return the manager Group name
220: */
221: public String getManagerGroup() {
222:
223: try {
224: return getCms().readGroup(m_project.getManagerGroupId())
225: .getName();
226: } catch (Throwable e) {
227: return "";
228: }
229: }
230:
231: /**
232: * Returns the simple name of the project.<p>
233: *
234: * @return the simple name of the project
235: */
236: public String getName() {
237:
238: String name = m_project.getSimpleName();
239: if (CmsStringUtil.isEmptyOrWhitespaceOnly(name)
240: || name.equals("/")) {
241: name = "";
242: }
243: return name;
244: }
245:
246: /**
247: * Returns the fully qualified name of the organizational unit.<p>
248: *
249: * @return the fully qualified name of the organizational unit
250: */
251: public String getOufqn() {
252:
253: return m_project.getOuFqn();
254: }
255:
256: /**
257: * Returns the project id parameter value.<p>
258: *
259: * @return the project id parameter value
260: */
261: public String getParamProjectid() {
262:
263: return m_paramProjectid;
264: }
265:
266: /**
267: * Returns the list of VFS resources that belong to this project.<p>
268: *
269: * @return the list of VFS resources that belong to this project
270: */
271: public List getResources() {
272:
273: if (m_resources == null) {
274: return new ArrayList();
275: }
276: return m_resources;
277: }
278:
279: /**
280: * Returns the user Group name.<p>
281: *
282: * @return the user Group name
283: */
284: public String getUserGroup() {
285:
286: try {
287: return getCms().readGroup(m_project.getGroupId()).getName();
288: } catch (Throwable e) {
289: return "";
290: }
291: }
292:
293: /**
294: * Just a setter method needed for the widget dialog.<p>
295: *
296: * @param ou ignored
297: */
298: public void setAssignedOu(String ou) {
299:
300: ou.length(); // prevent warning
301: }
302:
303: /**
304: * Sets the manager Group name.<p>
305: *
306: * @param managerGroup the manager Group name to set
307: */
308: public void setManagerGroup(String managerGroup) {
309:
310: CmsGroup group = checkGroup(managerGroup);
311: if (group != null) {
312: m_project.setManagerGroupId(group.getId());
313: }
314: }
315:
316: /**
317: * Sets the name of the project.<p>
318: *
319: * @param name the name to set
320: */
321: public void setName(String name) {
322:
323: m_project.setName(getOufqn() + "/" + name);
324: }
325:
326: /**
327: * Sets the fully qualified name of the organizational unit.<p>
328: *
329: * @param oufqn the fully qualified name of the organizational unit to set
330: */
331: public void setOufqn(String oufqn) {
332:
333: if (oufqn != null) {
334: if (!oufqn.endsWith("/")) {
335: oufqn += '/';
336: }
337: } else {
338: oufqn = "/";
339: }
340: String name = m_project.getSimpleName();
341: if (name == null) {
342: name = "";
343: }
344: name = oufqn + name;
345: if (CmsStringUtil.isEmptyOrWhitespaceOnly(name)) {
346: name = "/";
347: }
348: m_project.setName(name);
349: }
350:
351: /**
352: * Sets the project id parameter value.<p>
353: *
354: * @param projectId the project id parameter value
355: */
356: public void setParamProjectid(String projectId) {
357:
358: m_paramProjectid = projectId;
359: }
360:
361: /**
362: * Sets the resources of this project.<p>
363: *
364: * @param value the project resources to set
365: */
366: public void setResources(List value) {
367:
368: if (value == null) {
369: m_resources = new ArrayList();
370: return;
371: }
372: m_resources = CmsFileUtil.removeRedundancies(value);
373: }
374:
375: /**
376: * Sets the user Group name.<p>
377: *
378: * @param userGroup the user Group name to set
379: */
380: public void setUserGroup(String userGroup) {
381:
382: CmsGroup group = checkGroup(userGroup);
383: if (group != null) {
384: m_project.setGroupId(group.getId());
385: }
386: }
387:
388: /**
389: * @see org.opencms.workplace.CmsWidgetDialog#createDialogHtml(java.lang.String)
390: */
391: protected String createDialogHtml(String dialog) {
392:
393: StringBuffer result = new StringBuffer(1024);
394:
395: result.append(createWidgetTableStart());
396: // show error header once if there were validation errors
397: result.append(createWidgetErrorHeader());
398:
399: if (dialog.equals(PAGES[0])) {
400: // create the widgets for the first dialog page
401: result
402: .append(dialogBlockStart(key(Messages.GUI_PROJECT_EDITOR_LABEL_IDENTIFICATION_BLOCK_0)));
403: result.append(createWidgetTableStart());
404: result.append(createDialogRowsHtml(0, 5));
405: result.append(createWidgetTableEnd());
406: result.append(dialogBlockEnd());
407: result
408: .append(dialogBlockStart(key(Messages.GUI_PROJECT_EDITOR_LABEL_CONTENT_BLOCK_0)));
409: result.append(createWidgetTableStart());
410: result.append(createDialogRowsHtml(6, 6));
411: result.append(createWidgetTableEnd());
412: result.append(dialogBlockEnd());
413: }
414:
415: result.append(createWidgetTableEnd());
416: return result.toString();
417: }
418:
419: /**
420: * Creates the list of widgets for this dialog.<p>
421: */
422: protected void defineWidgets() {
423:
424: // initialize the project object to use for the dialog
425: initProjectObject();
426:
427: setKeyPrefix(KEY_PREFIX);
428:
429: // widgets to display
430: if (isNewProject()) {
431: addWidget(new CmsWidgetDialogParameter(this , "name",
432: PAGES[0], new CmsInputWidget()));
433: } else {
434: addWidget(new CmsWidgetDialogParameter(this , "name",
435: PAGES[0], new CmsDisplayWidget()));
436: }
437: addWidget(new CmsWidgetDialogParameter(m_project,
438: "description", "", PAGES[0], new CmsTextareaWidget(),
439: 0, 1));
440: addWidget(new CmsWidgetDialogParameter(this , "managerGroup",
441: PAGES[0], new CmsGroupWidget(new Integer(
442: I_CmsPrincipal.FLAG_GROUP_PROJECT_MANAGER),
443: null)));
444: addWidget(new CmsWidgetDialogParameter(this , "userGroup",
445: PAGES[0], new CmsGroupWidget(null, null)));
446: if (isNewProject()) {
447: int ous = 1;
448: try {
449: ous = OpenCms.getRoleManager().getOrgUnitsForRole(
450: getCms(), CmsRole.PROJECT_MANAGER, true).size();
451: } catch (CmsException e) {
452: // should never happen
453: }
454: if (ous < 2) {
455: addWidget(new CmsWidgetDialogParameter(this ,
456: "assignedOu", PAGES[0], new CmsDisplayWidget()));
457: } else {
458: addWidget(new CmsWidgetDialogParameter(this , "oufqn",
459: PAGES[0], new CmsOrgUnitWidget(
460: CmsRole.PROJECT_MANAGER)));
461: }
462: } else {
463: addWidget(new CmsWidgetDialogParameter(this , "assignedOu",
464: PAGES[0], new CmsDisplayWidget()));
465: }
466: addWidget(new CmsWidgetDialogParameter(m_project,
467: "deleteAfterPublishing", PAGES[0],
468: new CmsCheckboxWidget()));
469: addWidget(new CmsWidgetDialogParameter(this , "resources",
470: PAGES[0], new CmsVfsFileWidget(false, "", false, false)));
471: }
472:
473: /**
474: * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
475: */
476: protected String[] getPageArray() {
477:
478: return PAGES;
479: }
480:
481: /**
482: * @see org.opencms.workplace.CmsWorkplace#initMessages()
483: */
484: protected void initMessages() {
485:
486: // add specific dialog resource bundle
487: addMessages(Messages.get().getBundleName());
488: // add default resource bundles
489: super .initMessages();
490: }
491:
492: /**
493: * Initializes the project object to work with depending on the dialog state and request parameters.<p>
494: *
495: * Two initializations of the project object on first dialog call are possible:
496: * <ul>
497: * <li>edit an existing project</li>
498: * <li>create a new project</li>
499: * </ul>
500: */
501: protected void initProjectObject() {
502:
503: Object o = null;
504:
505: try {
506: if (CmsStringUtil.isEmpty(getParamAction())
507: || CmsDialog.DIALOG_INITIAL
508: .equals(getParamAction())) {
509: // edit an existing project, get the project object from database
510: m_project = getCms().readProject(
511: new CmsUUID(getParamProjectid()));
512: m_resources = getCms().readProjectResources(m_project);
513: } else {
514: // this is not the initial call, get the project object from session
515: o = getDialogObject();
516: Map map = (Map) o;
517: m_project = (CmsProject) map.get("prj");
518: // test
519: m_project.getUuid();
520: m_resources = (List) map.get("res");
521: m_resources.isEmpty();
522: }
523: } catch (Exception e) {
524: // create a new project object
525: m_project = new CmsProject();
526: m_resources = new ArrayList();
527: setOufqn(getCms().getRequestContext().getOuFqn());
528: setUserGroup(getCms().getRequestContext().getOuFqn()
529: + OpenCms.getDefaultUsers().getGroupUsers());
530: }
531: }
532:
533: /**
534: * Overridden to set a custom online help mapping.<p>
535: *
536: * @see org.opencms.workplace.CmsWorkplace#initWorkplaceMembers(org.opencms.jsp.CmsJspActionElement)
537: */
538: protected void initWorkplaceMembers(CmsJspActionElement jsp) {
539:
540: super .initWorkplaceMembers(jsp);
541: setOnlineHelpUriCustom("/projects/project_edit.jsp");
542: }
543:
544: /**
545: * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
546: */
547: protected void initWorkplaceRequestValues(
548: CmsWorkplaceSettings settings, HttpServletRequest request) {
549:
550: // initialize parameters and dialog actions in super implementation
551: super .initWorkplaceRequestValues(settings, request);
552:
553: Map map = new HashMap();
554: map.put("prj", m_project);
555: map.put("res", m_resources);
556: // save the current state of the project (may be changed because of the widget values)
557: setDialogObject(map);
558: }
559:
560: /**
561: * Checks if the Project overview has to be displayed.<p>
562: *
563: * @return <code>true</code> if the project overview has to be displayed
564: */
565: protected boolean isNewProject() {
566:
567: return getCurrentToolPath().equals("/projects/new");
568: }
569:
570: /**
571: * @see org.opencms.workplace.CmsWidgetDialog#validateParamaters()
572: */
573: protected void validateParamaters() throws Exception {
574:
575: if (!isNewProject()) {
576: // test the needed parameters
577: getCms().readProject(new CmsUUID(getParamProjectid()))
578: .getName();
579: }
580:
581: }
582:
583: /**
584: * Checks if the given group name is a valid opencms user group.<p>
585: *
586: * @param groupName the group name to check
587: *
588: * @return the read group
589: */
590: private CmsGroup checkGroup(String groupName) {
591:
592: try {
593: return getCms().readGroup(groupName);
594: } catch (CmsException e) {
595: throw new CmsIllegalArgumentException(e
596: .getMessageContainer());
597: }
598: }
599: }
|