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   **************************************************************************/
18  package org.exoplatform.ecm.webui.component.explorer.rightclick.manager;
19  
20  import java.util.ArrayList;
21  import java.util.Arrays;
22  import java.util.Collection;
23  import java.util.GregorianCalendar;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.regex.Matcher;
28  
29  import javax.jcr.Node;
30  import javax.jcr.PathNotFoundException;
31  import javax.jcr.Session;
32  import javax.jcr.Value;
33  import javax.jcr.ValueFactory;
34  import javax.jcr.version.Version;
35  
36  import org.exoplatform.ecm.webui.component.explorer.UIJCRExplorer;
37  import org.exoplatform.ecm.webui.component.explorer.UIWorkingArea;
38  import org.exoplatform.ecm.webui.component.explorer.control.filter.CanSetPropertyFilter;
39  import org.exoplatform.ecm.webui.component.explorer.control.filter.IsNotCheckedOutFilter;
40  import org.exoplatform.ecm.webui.component.explorer.control.filter.IsNotContainBinaryFilter;
41  import org.exoplatform.ecm.webui.component.explorer.control.filter.IsNotInTrashFilter;
42  import org.exoplatform.ecm.webui.component.explorer.control.filter.IsNotLockedFilter;
43  import org.exoplatform.ecm.webui.component.explorer.control.filter.IsNotTrashHomeNodeFilter;
44  import org.exoplatform.ecm.webui.component.explorer.control.filter.IsVersionableFilter;
45  import org.exoplatform.ecm.webui.component.explorer.control.listener.UIWorkingAreaActionListener;
46  import org.exoplatform.ecm.webui.utils.JCRExceptionManager;
47  import org.exoplatform.portal.webui.util.Util;
48  import org.exoplatform.services.wcm.extensions.publication.lifecycle.authoring.AuthoringPublicationConstant;
49  import org.exoplatform.services.wcm.publication.lifecycle.stageversion.config.VersionData;
50  import org.exoplatform.services.wcm.publication.lifecycle.stageversion.config.VersionLog;
51  import org.exoplatform.web.application.ApplicationMessage;
52  import org.exoplatform.webui.config.annotation.ComponentConfig;
53  import org.exoplatform.webui.config.annotation.EventConfig;
54  import org.exoplatform.webui.core.UIApplication;
55  import org.exoplatform.webui.core.UIComponent;
56  import org.exoplatform.webui.event.Event;
57  import org.exoplatform.webui.ext.filter.UIExtensionFilter;
58  import org.exoplatform.webui.ext.filter.UIExtensionFilters;
59  import org.exoplatform.webui.ext.manager.UIAbstractManager;
60  import org.exoplatform.webui.ext.manager.UIAbstractManagerComponent;
61  
62  /**
63   * Created by The eXo Platform SARL
64   * Author : Hoang Van Hung
65   *          hunghvit@gmail.com
66   * Aug 6, 2009
67   */
68  
69  @ComponentConfig(
70      events = {
71        @EventConfig(listeners = CheckOutManageComponent.CheckOutActionListener.class)
72      }
73  )
74  
75  public class CheckOutManageComponent extends UIAbstractManagerComponent {
76  
77    private static final List<UIExtensionFilter> FILTERS
78        = Arrays.asList(new UIExtensionFilter[]{new IsNotInTrashFilter(),
79                                                new CanSetPropertyFilter(),
80                                                new IsNotLockedFilter(),
81                                                new IsNotCheckedOutFilter(),
82                                                new IsVersionableFilter(),
83                                                new IsNotTrashHomeNodeFilter(),
84                                                new IsNotContainBinaryFilter()});
85  
86    @UIExtensionFilters
87    public List<UIExtensionFilter> getFilters() {
88      return FILTERS;
89    }
90  
91    public static void checkOutManage(Event<? extends UIComponent> event, UIJCRExplorer uiExplorer,
92        UIApplication uiApp) throws Exception {
93      String nodePath = event.getRequestContext().getRequestParameter(OBJECTID);
94      if (nodePath == null) {
95        nodePath = uiExplorer.getCurrentWorkspace() + ':' + uiExplorer.getCurrentPath();
96      }
97      Matcher matcher = UIWorkingArea.FILE_EXPLORER_URL_SYNTAX.matcher(nodePath);
98      String wsName = null;
99      if (matcher.find()) {
100       wsName = matcher.group(1);
101       nodePath = matcher.group(2);
102     } else {
103       throw new IllegalArgumentException("The ObjectId is invalid '"+ nodePath + "'");
104     }
105     Session session = uiExplorer.getSessionByWorkspace(wsName);
106     // Use the method getNodeByPath because it is link aware
107     Node node = uiExplorer.getNodeByPath(nodePath, session);
108     // Reset the path to manage the links that potentially create virtual path
109     nodePath = node.getPath();
110     // Reset the session to manage the links that potentially change of workspace
111     session = node.getSession();
112     // Reset the workspace name to manage the links that potentially change of workspace
113     wsName = session.getWorkspace().getName();
114 
115     try {
116       String userId = "";
117       try {
118         userId = Util.getPortalRequestContext().getRemoteUser();
119       } catch (Exception e) {
120         userId = node.getSession().getUserID();
121       }
122       Version liveVersion = node.getBaseVersion();
123       node.checkout();
124       String currentState = node.getProperty("publication:currentState").getString();
125       VersionLog versionLog = new VersionLog(liveVersion.getName(),currentState, userId,new GregorianCalendar(),"UIPublicationHistory.create-version");
126       Map<String, VersionData> revisionsMap = getRevisionData(node);
127       VersionData liveRevisionData = new VersionData(liveVersion.getUUID(),currentState,userId);
128       revisionsMap.put(liveVersion.getUUID(), liveRevisionData);
129       addRevisionData(node, revisionsMap.values());
130       addLog(node,versionLog);
131       node.save();      
132     } catch(PathNotFoundException path) {
133       uiApp.addMessage(new ApplicationMessage("UIPopupMenu.msg.path-not-found-exception",
134           null,ApplicationMessage.WARNING));
135       
136       return;
137     } catch (Exception e) {
138       JCRExceptionManager.process(uiApp, e);
139       
140       uiExplorer.updateAjax(event);
141     }
142   }
143 
144   public static class CheckOutActionListener extends UIWorkingAreaActionListener<CheckOutManageComponent> {
145     public void processEvent(Event<CheckOutManageComponent> event) throws Exception {
146       UIJCRExplorer uiExplorer = event.getSource().getAncestorOfType(UIJCRExplorer.class);
147       UIApplication uiApp = event.getSource().getAncestorOfType(UIApplication.class);
148       checkOutManage(event, uiExplorer, uiApp);
149     }
150   }
151 
152   @Override
153   public Class<? extends UIAbstractManager> getUIAbstractManagerClass() {
154     return null;
155   }
156   
157   /**
158    * Adds the log.
159    *
160    * @param node the node
161    * @param versionLog the version log
162    * @throws Exception the exception
163    */
164   private static void addLog(Node node, VersionLog versionLog) throws Exception {
165     Value[] values = node.getProperty(AuthoringPublicationConstant.HISTORY).getValues();
166     ValueFactory valueFactory = node.getSession().getValueFactory();
167     List<Value> list = new ArrayList<Value>(Arrays.asList(values));
168     list.add(valueFactory.createValue(versionLog.toString()));
169     node.setProperty(AuthoringPublicationConstant.HISTORY, list.toArray(new Value[] {}));
170   }
171   
172   /**
173    * Gets the revision data.
174    *
175    * @param node the node
176    * @return the revision data
177    * @throws Exception the exception
178    */
179   private static Map<String, VersionData> getRevisionData(Node node) throws Exception {
180     Map<String, VersionData> map = new HashMap<String, VersionData>();
181     try {
182       for (Value v : node.getProperty(AuthoringPublicationConstant.REVISION_DATA_PROP).getValues()) {
183         VersionData versionData = VersionData.toVersionData(v.getString());
184         map.put(versionData.getUUID(), versionData);
185       }
186     } catch (Exception e) {
187       return map;
188     }
189     return map;
190   }
191   
192   /**
193    * Adds the revision data.
194    *
195    * @param node the node
196    * @param list the list
197    * @throws Exception the exception
198    */
199   private static void addRevisionData(Node node, Collection<VersionData> list) throws Exception {
200     List<Value> valueList = new ArrayList<Value>();
201     ValueFactory factory = node.getSession().getValueFactory();
202     for (VersionData versionData : list) {
203       valueList.add(factory.createValue(versionData.toStringValue()));
204     }
205     node.setProperty(AuthoringPublicationConstant.REVISION_DATA_PROP,
206                      valueList.toArray(new Value[] {}));
207   }
208 
209 }