View Javadoc
1   /*
2    * Copyright (C) 2003-2009 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.ecm.webui.component.explorer.control.action;
18  
19  import java.util.Arrays;
20  import java.util.List;
21  import java.util.regex.Matcher;
22  
23  import javax.jcr.AccessDeniedException;
24  import javax.jcr.Node;
25  import javax.jcr.PathNotFoundException;
26  import javax.jcr.RepositoryException;
27  import javax.jcr.Session;
28  import javax.jcr.ValueFormatException;
29  import javax.jcr.lock.Lock;
30  
31  import org.exoplatform.ecm.webui.component.explorer.UIDocumentContainer;
32  import org.exoplatform.ecm.webui.component.explorer.UIDocumentWorkspace;
33  import org.exoplatform.ecm.webui.component.explorer.UIDrivesArea;
34  import org.exoplatform.ecm.webui.component.explorer.UIJCRExplorer;
35  import org.exoplatform.ecm.webui.component.explorer.UIWorkingArea;
36  import org.exoplatform.ecm.webui.component.explorer.control.UIControl;
37  import org.exoplatform.ecm.webui.component.explorer.control.filter.CanSetPropertyFilter;
38  import org.exoplatform.ecm.webui.component.explorer.control.filter.IsCheckedOutFilter;
39  import org.exoplatform.ecm.webui.component.explorer.control.filter.IsDocumentFilter;
40  import org.exoplatform.ecm.webui.component.explorer.control.filter.IsEditableFilter;
41  import org.exoplatform.ecm.webui.component.explorer.control.filter.IsNotContainBinaryFilter;
42  import org.exoplatform.ecm.webui.component.explorer.control.filter.IsNotEditingDocumentFilter;
43  import org.exoplatform.ecm.webui.component.explorer.control.filter.IsNotInTrashFilter;
44  import org.exoplatform.ecm.webui.component.explorer.control.filter.IsNotLockedFilter;
45  import org.exoplatform.ecm.webui.component.explorer.control.listener.UIActionBarActionListener;
46  import org.exoplatform.ecm.webui.component.explorer.popup.actions.UIDocumentForm;
47  import org.exoplatform.ecm.webui.component.explorer.popup.actions.UIDocumentFormController;
48  import org.exoplatform.ecm.webui.component.explorer.popup.admin.UIActionContainer;
49  import org.exoplatform.ecm.webui.component.explorer.popup.admin.UIActionForm;
50  import org.exoplatform.ecm.webui.component.explorer.popup.admin.UIActionTypeForm;
51  import org.exoplatform.ecm.webui.component.explorer.search.UISearchResult;
52  import org.exoplatform.ecm.webui.utils.JCRExceptionManager;
53  import org.exoplatform.ecm.utils.lock.LockUtil;
54  import org.exoplatform.ecm.webui.utils.Utils;
55  import org.exoplatform.services.cms.lock.LockService;
56  import org.exoplatform.services.cms.mimetype.DMSMimeTypeResolver;
57  import org.exoplatform.services.organization.MembershipType;
58  import org.exoplatform.services.organization.OrganizationService;
59  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
60  import org.exoplatform.web.application.ApplicationMessage;
61  import org.exoplatform.webui.application.WebuiRequestContext;
62  import org.exoplatform.webui.config.annotation.ComponentConfig;
63  import org.exoplatform.webui.config.annotation.EventConfig;
64  import org.exoplatform.webui.core.UIApplication;
65  import org.exoplatform.webui.core.UIComponent;
66  import org.exoplatform.webui.core.UIPopupContainer;
67  import org.exoplatform.webui.event.Event;
68  import org.exoplatform.webui.ext.filter.UIExtensionFilter;
69  import org.exoplatform.webui.ext.filter.UIExtensionFilters;
70  import org.exoplatform.webui.ext.manager.UIAbstractManager;
71  import org.exoplatform.webui.ext.manager.UIAbstractManagerComponent;
72  
73  /**
74   * Created by The eXo Platform SAS
75   * Author : eXoPlatform
76   *          nicolas.filotto@exoplatform.com
77   * 6 mai 2009
78   */
79  @ComponentConfig(
80                   events = {
81                       @EventConfig(listeners = EditDocumentActionComponent.EditDocumentActionListener.class)
82                   }
83      )
84  
85  public class EditDocumentActionComponent extends UIAbstractManagerComponent {
86  
87    private static final List<UIExtensionFilter> FILTERS = Arrays.asList(new UIExtensionFilter[] {
88        new IsDocumentFilter(), new IsEditableFilter(), new CanSetPropertyFilter(),
89        new IsNotLockedFilter(), new IsCheckedOutFilter(), new IsNotInTrashFilter(), new IsNotEditingDocumentFilter(),
90        new IsNotContainBinaryFilter()});
91  
92    @UIExtensionFilters
93    public static List<UIExtensionFilter> getFilters() {
94      return FILTERS;
95    }
96  
97    private static void refresh(Node node) throws Exception {
98      node.refresh(true);
99    }
100 
101   @SuppressWarnings("unchecked")
102   public static void editDocument(Event <? extends UIComponent> event,
103                                   WebuiRequestContext context,
104                                   UIComponent comp,
105                                   UIJCRExplorer uiExplorer,
106                                   Node selectedNode,
107                                   UIApplication uiApp) throws RepositoryException,
108                                   Exception,
109                                   ValueFormatException,
110                                   PathNotFoundException {
111     if (event != null)
112       context = event.getRequestContext();
113     if (selectedNode.isNodeType(Utils.EXO_ACTION)) {
114       UIActionContainer uiContainer = uiExplorer.createUIComponent(UIActionContainer.class, null, null);
115       uiExplorer.setIsHidePopup(true);
116       UIActionForm uiActionForm =  uiContainer.getChild(UIActionForm.class);
117       uiContainer.getChild(UIActionTypeForm.class).setRendered(false);
118       uiActionForm.createNewAction(selectedNode, selectedNode.getPrimaryNodeType().getName(), false);
119       uiActionForm.setIsUpdateSelect(false);
120       uiActionForm.setNodePath(selectedNode.getPath());
121       uiActionForm.setWorkspace(selectedNode.getSession().getWorkspace().getName());
122       uiActionForm.setStoredPath(selectedNode.getPath());
123       UIPopupContainer UIPopupContainer = uiExplorer.getChild(UIPopupContainer.class);
124       UIPopupContainer.activate(uiContainer, 700, 550);
125       context.addUIComponentToUpdateByAjax(UIPopupContainer);
126     } else {
127       String nodeType = null;
128       if(selectedNode.hasProperty("exo:presentationType")) {
129         nodeType = selectedNode.getProperty("exo:presentationType").getString();
130       }else {
131         nodeType = selectedNode.getPrimaryNodeType().getName();
132       }
133       UIDocumentFormController uiController =
134           event != null ?
135            event.getSource().createUIComponent(UIDocumentFormController.class, null, "EditFormController") :
136              comp.createUIComponent(UIDocumentFormController.class, null, "EditFormController");
137            UIDocumentForm uiDocumentForm = uiController.getChild(UIDocumentForm.class);
138            uiDocumentForm.setRepositoryName(uiExplorer.getRepositoryName());
139            uiDocumentForm.setContentType(nodeType);
140            uiDocumentForm.clearRemovedNode();
141            uiDocumentForm.clearDataRemovedList();
142            if(uiDocumentForm.getTemplate() == null) {
143              uiApp.addMessage(new ApplicationMessage("UIActionBar.msg.template-null", null));
144              return;
145            }
146            refresh(selectedNode);
147            // Check document is lock for editing
148            uiDocumentForm.setIsKeepinglock(false);
149            if (!selectedNode.isLocked()) {
150              OrganizationService service = WCMCoreUtils.getService(OrganizationService.class);
151              List<MembershipType> memberships = (List<MembershipType>) service.getMembershipTypeHandler().findMembershipTypes();
152              synchronized (EditDocumentActionComponent.class) {
153                refresh(selectedNode);
154                if (!selectedNode.isLocked()) {
155                  if(selectedNode.canAddMixin(Utils.MIX_LOCKABLE)){
156                    selectedNode.addMixin(Utils.MIX_LOCKABLE);
157                    selectedNode.save();
158                  }
159                  Lock lock = selectedNode.lock(false, false);
160                  LockUtil.keepLock(lock);
161                  LockService lockService = uiExplorer.getApplicationComponent(LockService.class);
162                  List<String> settingLockList = lockService.getAllGroupsOrUsersForLock();
163                  for (String settingLock : settingLockList) {
164                    LockUtil.keepLock(lock, settingLock);
165                    if (!settingLock.startsWith("*"))
166                      continue;
167                    String lockTokenString = settingLock;
168                    for (MembershipType membership : memberships) {
169                      lockTokenString = settingLock.replace("*", membership.getName());
170                      LockUtil.keepLock(lock, lockTokenString);
171                    }
172                  }
173                  selectedNode.save();
174                  uiDocumentForm.setIsKeepinglock(true);
175                }
176              }
177            }
178            // Add mixin type exo:documentSize if the current node is flash file
179            String mimeType = DMSMimeTypeResolver.getInstance().getMimeType(selectedNode.getName());
180            if(mimeType.indexOf(Utils.FLASH_MIMETYPE) >= 0 && selectedNode.canAddMixin(Utils.EXO_RISIZEABLE)) {
181              selectedNode.addMixin(Utils.EXO_RISIZEABLE);
182              selectedNode.save();          	
183            }
184            // Update data avoid concurrent modification by other session
185            refresh(selectedNode);
186            // Check again after node is locking by current user or another
187            if (LockUtil.getLockTokenOfUser(selectedNode) == null) {
188              Object[] arg = { selectedNode.getPath() };
189              uiApp.addMessage(new ApplicationMessage("UIPopupMenu.msg.node-locked-editing", arg,
190                                                      ApplicationMessage.WARNING));
191              return;
192            }
193            uiDocumentForm.setNodePath(selectedNode.getPath());
194            uiDocumentForm.addNew(false);
195            uiDocumentForm.setWorkspace(selectedNode.getSession().getWorkspace().getName());
196            uiDocumentForm.setStoredPath(selectedNode.getPath());
197            uiController.setRenderedChild(UIDocumentForm.class);
198 
199            UIWorkingArea uiWorkingArea = uiExplorer.getChild(UIWorkingArea.class);
200            UIDocumentWorkspace uiDocumentWorkspace = uiWorkingArea.getChild(UIDocumentWorkspace.class);
201            if(!uiDocumentWorkspace.isRendered()) {
202              uiWorkingArea.getChild(UIDrivesArea.class).setRendered(false);
203              uiWorkingArea.getChild(UIDocumentWorkspace.class).setRendered(true);
204            }
205            uiDocumentWorkspace.getChild(UIDocumentContainer.class).setRendered(false);
206            uiDocumentWorkspace.getChild(UISearchResult.class).setRendered(false);
207            UIDocumentFormController controller = uiDocumentWorkspace.removeChild(UIDocumentFormController.class);
208            if (controller != null) {
209              controller.getChild(UIDocumentForm.class).releaseLock();
210            }
211            uiDocumentWorkspace.addChild(uiController);
212            uiController.initOptionBlockPanel();
213            uiController.setRendered(true);
214            context.addUIComponentToUpdateByAjax(uiWorkingArea);
215            if (event != null) {
216              uiExplorer.updateAjax(event);
217            }
218            context.addUIComponentToUpdateByAjax(uiExplorer.getChild(UIControl.class));
219     }
220   }
221 
222   public static class EditDocumentActionListener extends UIActionBarActionListener<EditDocumentActionComponent> {
223     public void processEvent(Event<EditDocumentActionComponent> event) throws Exception {
224       EditDocumentActionComponent uicomp = event.getSource();
225       String nodePath = event.getRequestContext().getRequestParameter(OBJECTID);
226       UIJCRExplorer uiExplorer = uicomp.getAncestorOfType(UIJCRExplorer.class);
227       Node selectedNode = null;
228       if (nodePath != null && nodePath.length() != 0) {
229         Matcher matcher = UIWorkingArea.FILE_EXPLORER_URL_SYNTAX.matcher(nodePath);
230         String wsName = null;
231         if (matcher.find()) {
232           wsName = matcher.group(1);
233           nodePath = matcher.group(2);
234         } else {
235           throw new IllegalArgumentException("The ObjectId is invalid '" + nodePath + "'");
236         }
237         Session session = uiExplorer.getSessionByWorkspace(wsName);
238         UIApplication uiApp = uicomp.getAncestorOfType(UIApplication.class);
239         try {
240           // Use the method getNodeByPath because it is link aware
241           selectedNode = uiExplorer.getNodeByPath(nodePath, session);
242         } catch (PathNotFoundException path) {
243           uiApp.addMessage(new ApplicationMessage("UIPopupMenu.msg.path-not-found-exception", null,
244                                                   ApplicationMessage.WARNING));
245 
246           return;
247         } catch (AccessDeniedException ace) {
248           uiApp.addMessage(new ApplicationMessage("UIDocumentInfo.msg.null-exception", null,
249                                                   ApplicationMessage.WARNING));
250 
251           return;
252         } catch (Exception e) {
253           JCRExceptionManager.process(uiApp, e);
254           return;
255         }
256       }
257       if (selectedNode == null)  selectedNode = uiExplorer.getCurrentNode();
258       uiExplorer.setSelectNode(selectedNode.getPath());
259       UIApplication uiApp = uicomp.getAncestorOfType(UIApplication.class);
260       editDocument(event, null, uicomp, uiExplorer, selectedNode, uiApp);
261     }
262   }
263 
264   @Override
265   public Class<? extends UIAbstractManager> getUIAbstractManagerClass() {
266     return null;
267   }
268 }