001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $
018 */
019
020package org.nuxeo.ecm.webapp.contentbrowser;
021
022import static org.jboss.seam.ScopeType.CONVERSATION;
023
024import java.io.Serializable;
025import java.security.Principal;
026import java.util.ArrayList;
027import java.util.List;
028
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.jboss.seam.annotations.Begin;
032import org.jboss.seam.annotations.End;
033import org.jboss.seam.annotations.Factory;
034import org.jboss.seam.annotations.In;
035import org.jboss.seam.annotations.Name;
036import org.jboss.seam.annotations.Scope;
037import org.jboss.seam.core.Manager;
038import org.nuxeo.common.utils.IdUtils;
039import org.nuxeo.ecm.core.api.Blob;
040import org.nuxeo.ecm.core.api.CoreSession;
041import org.nuxeo.ecm.core.api.DocumentModel;
042import org.nuxeo.ecm.core.api.DocumentModelList;
043import org.nuxeo.ecm.core.api.DocumentRef;
044import org.nuxeo.ecm.core.api.IdRef;
045import org.nuxeo.ecm.core.api.PropertyException;
046import org.nuxeo.ecm.core.api.security.SecurityConstants;
047import org.nuxeo.ecm.platform.el.ContextStringWrapper;
048import org.nuxeo.ecm.platform.ui.web.util.BaseURL;
049import org.nuxeo.ecm.platform.usermanager.UserManager;
050import org.nuxeo.ecm.webapp.base.InputController;
051import org.nuxeo.ecm.webapp.documenttemplates.DocumentTemplatesActions;
052import org.nuxeo.ecm.webapp.security.PrincipalListManager;
053import org.nuxeo.ecm.webapp.security.SecurityActions;
054
055/**
056 * Action listener that deals with operations with the workspaces.
057 * <p>
058 * This action listener handles the workspace creation wizard.
059 *
060 * @author <a href="mailto:rcaraghin@nuxeo.com">Razvan Caraghin</a>
061 */
062@Name("workspaceActions")
063@Scope(CONVERSATION)
064public class WorkspaceActionsBean extends InputController implements WorkspaceActions, Serializable {
065
066    private static final long serialVersionUID = 1L;
067
068    private static final Log log = LogFactory.getLog(WorkspaceActionsBean.class);
069
070    @In(create = true, required = false)
071    private transient CoreSession documentManager;
072
073    @In(create = true)
074    private transient Principal currentUser;
075
076    @In(required = false)
077    private transient DocumentActions documentActions;
078
079    @In(create = true)
080    private transient PrincipalListManager principalListManager;
081
082    @In(create = true)
083    protected transient UserManager userManager;
084
085    @In(create = true)
086    private transient SecurityActions securityActions;
087
088    @In(value = "org.jboss.seam.core.manager")
089    private transient Manager conversationManager;
090
091    // Wizard implementation : driven by PageFlow addWorkspace
092
093    private Boolean useTemplateFlag;
094
095    @In(required = false)
096    private transient DocumentModel tmpWorkspace;
097
098    @In(create = true)
099    private transient DocumentModelList availableTemplates;
100
101    // inject wizard vars
102    @In(value = "#{selectedTemplateId.value}", required = false)
103    private transient String selectedTemplateId;
104
105    @In(value = "#{selectedSecurityModel.value}", required = false)
106    private transient String selectedSecurityModel;
107
108    @In(value = "#{selectedOwnerModel.value}", required = false)
109    private transient String selectedOwnerModel;
110
111    @In(create = true)
112    private transient DocumentTemplatesActions documentTemplatesActions;
113
114    public String cancel() {
115        return "view_workspaces";
116    }
117
118    // Flag for indicating if template will be used
119    @Override
120    public void setUseTemplate(Boolean value) {
121        useTemplateFlag = value;
122    }
123
124    @Override
125    public Boolean getUseTemplate() {
126        if (availableTemplates == null) {
127            return false;
128        }
129        if (useTemplateFlag == null) {
130            return false;
131        }
132        return useTemplateFlag;
133    }
134
135    // initialize pageflow context using Factories
136    @Override
137    @Factory(value = "selectedTemplateId")
138    public ContextStringWrapper FactoryForSelectedTemplateId() {
139        return new ContextStringWrapper("none");
140    }
141
142    @Override
143    @Factory(value = "selectedSecurityModel")
144    public ContextStringWrapper FactoryForSelectSecurityModel() {
145        return new ContextStringWrapper("inherit");
146    }
147
148    @Override
149    @Factory(value = "selectedOwnerModel")
150    public ContextStringWrapper FactoryForSelectSecurityOwner() {
151        return new ContextStringWrapper("me");
152    }
153
154    @Override
155    @Begin(pageflow = "createWorkspace", nested = true)
156    @Factory(value = "tmpWorkspace")
157    public DocumentModel getTmpWorkspace() {
158        if (tmpWorkspace == null) {
159            tmpWorkspace = documentManager.createDocumentModel("Workspace");
160        }
161        return tmpWorkspace;
162    }
163
164    @Override
165    @Factory(value = "availableWorkspaceTemplates")
166    public DocumentModelList getTemplates() {
167        availableTemplates = documentTemplatesActions.getTemplates("Workspace");
168        return availableTemplates;
169    }
170
171    @Override
172    public String finishPageFlow() {
173        return "done";
174    }
175
176    @Override
177    public String getSelectedTemplateDescription() {
178        if (selectedTemplateId == null) {
179            return "";
180        }
181        if (selectedTemplateId.equals("none")) {
182            return "no_template";
183        }
184        for (DocumentModel t : getTemplates()) {
185            if (selectedTemplateId.equals(t.getId())) {
186                return (String) t.getProperty("dublincore", "description");
187            }
188        }
189        return "";
190    }
191
192    @Override
193    public DocumentModel getSelectedTemplate() {
194        if (selectedTemplateId == null) {
195            return null;
196        }
197        if (selectedTemplateId.equals("none")) {
198            return null;
199        }
200        for (DocumentModel t : getTemplates()) {
201            if (selectedTemplateId.equals(t.getId())) {
202                return t;
203            }
204        }
205        return null;
206    }
207
208    // @End(beforeRedirect = true)
209    @Override
210    @End
211    public String createWorkspace() {
212        String navResult = null;
213
214        if (useTemplateFlag == null || !useTemplateFlag || selectedTemplateId == null
215                || selectedTemplateId.equals("none")) {
216            // create the new Workspace without Template
217            // and navigate to it
218            navResult = documentTemplatesActions.createDocumentFromTemplate(tmpWorkspace, null);
219        } else {
220            // create the workspace from template
221            navResult = documentTemplatesActions.createDocumentFromTemplate(tmpWorkspace, selectedTemplateId);
222        }
223
224        if (!selectedSecurityModel.equals("inherit")) {
225            List<String> principalsName;
226
227            // get principals list to apply rights to
228            if (selectedOwnerModel.equals("me")) {
229                principalsName = new ArrayList<String>();
230                principalsName.add(currentUser.getName());
231            } else {
232                principalsName = principalListManager.getSelectedUsers();
233            }
234
235            // Force addition of administrators groups
236            principalsName.addAll(userManager.getAdministratorsGroups());
237
238            // Grant to principalList
239            for (String principalName : principalsName) {
240                securityActions.addPermission(principalName, SecurityConstants.EVERYTHING, true);
241            }
242
243            // DENY at root
244            securityActions.addPermission(SecurityConstants.EVERYONE, SecurityConstants.EVERYTHING, false);
245            securityActions.updateSecurityOnDocument();
246        }
247
248        String res = navigationContext.navigateToDocument(navigationContext.getCurrentDocument());
249        navigationContext.setChangeableDocument(navigationContext.getCurrentDocument());
250        return res;
251    }
252
253    @End(beforeRedirect = true)
254    public String createWorkspaceOld() {
255        String navResult = null;
256
257        if (!useTemplateFlag || selectedTemplateId.equals("none")) {
258            // create the new Workspace without Template
259            // and navigate to it
260            navResult = documentActions.saveDocument(tmpWorkspace);
261        } else {
262            // create the workspace from template
263
264            DocumentRef currentDocRef = navigationContext.getCurrentDocument().getRef();
265
266            // duplicate the template
267            String title = (String) tmpWorkspace.getProperty("dublincore", "title");
268            String name = IdUtils.generatePathSegment(title);
269            documentManager.copy(new IdRef(selectedTemplateId), currentDocRef, name);
270            DocumentModel created = documentManager.getChild(currentDocRef, name);
271
272            // Update from user input.
273            created.setProperty("dublincore", "title", title);
274            String descr = (String) tmpWorkspace.getProperty("dublincore", "description");
275            if (!descr.equals("")) {
276                created.setProperty("dublincore", "description", descr);
277            }
278            Blob blob = (Blob) tmpWorkspace.getProperty("file", "content");
279            if (blob != null) {
280                created.setProperty("file", "content", blob);
281                String fname = (String) tmpWorkspace.getProperty("file", "filename");
282                created.setProperty("file", "filename", fname);
283            }
284
285            created = documentManager.saveDocument(created);
286            documentManager.save();
287
288            // navigate to the newly created doc
289            navResult = navigationContext.navigateToDocument(created, "after-create");
290        }
291
292        if (!selectedSecurityModel.equals("inherit")) {
293            List<String> principalsName;
294
295            // get principals list to apply rights to
296            if (selectedOwnerModel.equals("me")) {
297                principalsName = new ArrayList<String>();
298                principalsName.add(currentUser.getName());
299            } else {
300                principalsName = principalListManager.getSelectedUsers();
301            }
302
303            // Grant to principalList
304            for (String principalName : principalsName) {
305                securityActions.addPermission(principalName, SecurityConstants.EVERYTHING, true);
306            }
307
308            // Force addition of administrators groups
309            principalsName.addAll(userManager.getAdministratorsGroups());
310
311            // DENY at root
312            securityActions.addPermission(SecurityConstants.EVERYONE, SecurityConstants.EVERYTHING, false);
313            securityActions.updateSecurityOnDocument();
314        }
315
316        String res = navigationContext.navigateToDocument(navigationContext.getCurrentDocument());
317        navigationContext.setChangeableDocument(navigationContext.getCurrentDocument());
318        return res;
319    }
320
321    @Override
322    @End(beforeRedirect = true)
323    public String exitWizard() {
324        return navigationContext.navigateToDocument(navigationContext.getCurrentDocument());
325    }
326
327    @Override
328    public String getA4JHackingURL() {
329        String url = BaseURL.getBaseURL() + "wizards/createWorkspace/a4jUploadHack.faces?";
330        url = conversationManager.encodeConversationId(url);
331        return url;
332    }
333
334}