View Javadoc
1   /*
2    * Copyright (C) 2003-2008 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.popup.admin;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.List;
22  
23  import javax.jcr.Node;
24  
25  import org.exoplatform.commons.utils.LazyPageList;
26  import org.exoplatform.commons.utils.ListAccess;
27  import org.exoplatform.commons.utils.ListAccessImpl;
28  import org.exoplatform.ecm.webui.component.explorer.UIJCRExplorer;
29  import org.exoplatform.ecm.webui.core.UIPagingGrid;
30  import org.exoplatform.portal.webui.util.Util;
31  import org.exoplatform.services.ecm.publication.AlreadyInPublicationLifecycleException;
32  import org.exoplatform.services.ecm.publication.PublicationPlugin;
33  import org.exoplatform.services.ecm.publication.PublicationPresentationService;
34  import org.exoplatform.services.ecm.publication.PublicationService;
35  import org.exoplatform.services.ecm.publication.plugins.webui.UIPublicationLogList;
36  import org.exoplatform.services.log.ExoLogger;
37  import org.exoplatform.services.log.Log;
38  import org.exoplatform.services.wcm.extensions.publication.lifecycle.authoring.AuthoringPublicationConstant;
39  import org.exoplatform.services.wcm.publication.WCMPublicationService;
40  import org.exoplatform.web.application.ApplicationMessage;
41  import org.exoplatform.webui.application.WebuiRequestContext;
42  import org.exoplatform.webui.config.annotation.ComponentConfig;
43  import org.exoplatform.webui.config.annotation.EventConfig;
44  import org.exoplatform.webui.core.UIApplication;
45  import org.exoplatform.webui.core.UIContainer;
46  import org.exoplatform.webui.core.UIPopupComponent;
47  import org.exoplatform.webui.core.UIPopupContainer;
48  import org.exoplatform.webui.event.Event;
49  import org.exoplatform.webui.event.EventListener;
50  import org.exoplatform.webui.form.UIForm;
51  
52  /*
53   * Created by The eXo Platform SAS Author : Anh Do Ngoc anh.do@exoplatform.com
54   * Sep 9, 2008
55   */
56  
57  /**
58   * The Class UIActivePublication.
59   */
60  @ComponentConfig(template = "system:/groovy/ecm/webui/UIGridWithButton.gtmpl", events = {
61      @EventConfig(listeners = UIActivePublication.EnrolActionListener.class),
62      @EventConfig(listeners = UIActivePublication.CancelActionListener.class) })
63      public class UIActivePublication extends UIPagingGrid implements UIPopupComponent {
64  
65    /** The Constant LIFECYCLE_NAME. */
66    public final static String LIFECYCLE_NAME     = "LifecycleName";
67  
68    /** The Constant LIFECYCLE_DESC. */
69    public final static String LIFECYCLE_DESC     = "LifecycleDesc";
70  
71    /** The LIFECYCL e_ fields. */
72    public static String[]     LIFECYCLE_FIELDS   = { LIFECYCLE_NAME, LIFECYCLE_DESC };
73  
74    /** The LIFECYCL e_ action. */
75    public static String[]     LIFECYCLE_ACTION   = { "Enrol" };
76  
77    /** The Constant LIFECYCLE_SELECTED. */
78    public final static String LIFECYCLE_SELECTED = "LifecycleSelected";
79    private static final Log LOG  = ExoLogger.getLogger(UIActivePublication.class.getName());
80    /**
81     * Instantiates a new uI active publication.
82     *
83     * @throws Exception the exception
84     */
85    public UIActivePublication() throws Exception {
86      configure(LIFECYCLE_NAME, LIFECYCLE_FIELDS, LIFECYCLE_ACTION);
87      getUIPageIterator().setId("LifecyclesIterator");
88    }
89  
90    /**
91     * Gets the actions.
92     *
93     * @return the actions
94     */
95    public String[] getActions() {
96      return new String[] { "Cancel" };
97    }
98  
99    /**
100    * Update lifecycles grid.
101    *
102    * @throws Exception the exception
103    */
104   public void refresh(int currentPage) throws Exception {
105     List<PublicationLifecycleBean> publicationLifecycleBeans = new ArrayList<PublicationLifecycleBean>();
106     PublicationService publicationService = getApplicationComponent(PublicationService.class);
107     Collection<PublicationPlugin> publicationPlugins = publicationService.getPublicationPlugins()
108                                                                          .values();
109     if (publicationPlugins.size() != 0) {
110       for (PublicationPlugin publicationPlugin : publicationPlugins) {
111         PublicationLifecycleBean lifecycleBean = new PublicationLifecycleBean();
112         lifecycleBean.setLifecycleName(publicationPlugin.getLifecycleName());
113         lifecycleBean.setLifecycleDesc(publicationPlugin.getDescription());
114         publicationLifecycleBeans.add(lifecycleBean);
115       }
116     }
117 
118     ListAccess<PublicationLifecycleBean> beanList = new ListAccessImpl<PublicationLifecycleBean>(PublicationLifecycleBean.class,
119                                                                                                  publicationLifecycleBeans);
120     LazyPageList<PublicationLifecycleBean> dataPageList =
121       new LazyPageList<PublicationLifecycleBean>(beanList, getUIPageIterator().getItemsPerPage());
122     getUIPageIterator().setPageList(dataPageList);
123     getUIPageIterator().setTotalItems(publicationLifecycleBeans.size());
124     if (currentPage > getUIPageIterator().getAvailablePage())
125       getUIPageIterator().setCurrentPage(getUIPageIterator().getAvailablePage());
126     else
127       getUIPageIterator().setCurrentPage(currentPage);
128   }
129 
130   public void enrolNodeInLifecycle(Node currentNode,
131                                    String lifecycleName,
132                                    WebuiRequestContext requestContext) throws Exception {
133     UIJCRExplorer uiJCRExplorer = getAncestorOfType(UIJCRExplorer.class);
134     UIApplication uiApp = getAncestorOfType(UIApplication.class);
135     UIPublicationManager uiPublicationManager = uiJCRExplorer.createUIComponent(
136         UIPublicationManager.class, null, null);
137     uiJCRExplorer.addLockToken(currentNode);
138     Node parentNode = currentNode.getParent();
139     uiJCRExplorer.addLockToken(parentNode);
140     WCMPublicationService wcmPublicationService = getApplicationComponent(WCMPublicationService.class);
141     PublicationPresentationService publicationPresentationService = getApplicationComponent(PublicationPresentationService.class);
142     try {
143       if(!currentNode.isCheckedOut()) {
144         uiApp.addMessage(new ApplicationMessage("UIActionBar.msg.node-checkedin", null,
145             ApplicationMessage.WARNING));
146         return;
147       }
148       String siteName = Util.getPortalRequestContext().getPortalOwner();
149       String remoteUser = Util.getPortalRequestContext().getRemoteUser();
150       if (AuthoringPublicationConstant.LIFECYCLE_NAME.equals(lifecycleName)) {
151         wcmPublicationService.enrollNodeInLifecycle(currentNode, siteName, remoteUser);
152       } else {
153         wcmPublicationService.enrollNodeInLifecycle(currentNode, lifecycleName);
154       }
155     } catch (AlreadyInPublicationLifecycleException e) {
156       uiApp.addMessage(new ApplicationMessage("UIActivePublication.msg.already-enroled", null,
157           ApplicationMessage.ERROR));
158       return;
159     } catch (Exception e) {
160       if (LOG.isErrorEnabled()) {
161         LOG.error("Unexpected error", e);
162       }
163       uiApp.addMessage(new ApplicationMessage("UIActivePublication.msg.unknow-error",
164           new String[] { e.getMessage() }, ApplicationMessage.ERROR));
165       return;
166     }
167     // refresh node prevent the situation node is changed in other session
168     currentNode.refresh(true);
169     UIContainer container = createUIComponent(UIContainer.class, null, null);
170     UIForm uiFormPublicationManager = publicationPresentationService.getStateUI(currentNode, container);
171     UIPopupContainer UIPopupContainer = uiJCRExplorer.getChild(UIPopupContainer.class);
172     if(uiFormPublicationManager instanceof UIPopupComponent) {
173       //TODO for future version, we need remove this code
174       //This is special case for wcm which wants to more than 2 tabs in PublicationManager
175       //The uiForm in this case should be a UITabPane or UIFormTabPane and need be an UIPopupComponent
176       UIPopupContainer.activate(uiFormPublicationManager,700,500);
177     }else {
178       uiPublicationManager.addChild(uiFormPublicationManager);
179       uiPublicationManager.addChild(UIPublicationLogList.class, null, null).setRendered(false);
180       UIPublicationLogList uiPublicationLogList = uiPublicationManager.getChild(UIPublicationLogList.class);
181       UIPopupContainer.activate(uiPublicationManager, 700, 500);
182       uiPublicationLogList.setNode(currentNode);
183       uiPublicationLogList.updateGrid();
184     }
185   }
186   /*
187    * (non-Javadoc)
188    *
189    * @see org.exoplatform.ecm.webui.popup.UIPopupComponent#activate()
190    */
191   public void activate() {
192   }
193 
194   /*
195    * (non-Javadoc)
196    *
197    * @see org.exoplatform.ecm.webui.popup.UIPopupComponent#deActivate()
198    */
199   public void deActivate() {
200   }
201 
202   /**
203    * The Class PublicationLifecycleBean.
204    */
205   public class PublicationLifecycleBean {
206     private String lifecycleName;
207     private String lifecycleDesc;
208 
209     /**
210      * Gets the lifecycle name.
211      *
212      * @return the lifecycle name
213      */
214     public String getLifecycleName() {
215       return lifecycleName;
216     }
217 
218     /**
219      * Sets the lifecycle name.
220      *
221      * @param lifecycleName the new lifecycle name
222      */
223     public void setLifecycleName(String lifecycleName) {
224       this.lifecycleName = lifecycleName;
225     }
226 
227     /**
228      * Gets the lifecycle desc.
229      *
230      * @return the lifecycle desc
231      */
232     public String getLifecycleDesc() {
233       return lifecycleDesc;
234     }
235 
236     /**
237      * Sets the lifecycle desc.
238      *
239      * @param lifecycleDesc the new lifecycle desc
240      */
241     public void setLifecycleDesc(String lifecycleDesc) {
242       this.lifecycleDesc = lifecycleDesc;
243     }
244   }
245 
246   /**
247    * The listener interface for receiving cancelAction events. The class that is
248    * interested in processing a cancelAction event implements this interface,
249    * and the object created with that class is registered with a component using
250    * the component's <code>addCancelActionListener</code> method. When
251    * the cancelAction event occurs, that object's appropriate
252    * method is invoked.
253    */
254   public static class CancelActionListener extends EventListener<UIActivePublication> {
255 
256     /*
257      * (non-Javadoc)
258      *
259      * @see org.exoplatform.webui.event.EventListener#execute(org.exoplatform.webui.event.Event)
260      */
261     public void execute(Event<UIActivePublication> event) throws Exception {
262       UIJCRExplorer uiExplorer = event.getSource().getAncestorOfType(UIJCRExplorer.class);
263       uiExplorer.cancelAction();
264     }
265   }
266 
267   /**
268    * The listener interface for receiving enrolAction events. The class that is
269    * interested in processing a enrolAction event implements this interface, and
270    * the object created with that class is registered with a component using the
271    * component's <code>addEnrolActionListener</code> method. When
272    * the enrolAction event occurs, that object's appropriate
273    * method is invoked.
274    */
275   public static class EnrolActionListener extends EventListener<UIActivePublication> {
276 
277     /*
278      * (non-Javadoc)
279      *
280      * @see org.exoplatform.webui.event.EventListener#execute(org.exoplatform.webui.event.Event)
281      */
282     public void execute(Event<UIActivePublication> event) throws Exception {
283       UIActivePublication uiActivePub = event.getSource();
284       UIJCRExplorer uiJCRExplorer = uiActivePub.getAncestorOfType(UIJCRExplorer.class);
285       String selectedLifecycle = event.getRequestContext().getRequestParameter(OBJECTID);
286       Node currentNode = uiJCRExplorer.getCurrentNode();
287       uiActivePub.enrolNodeInLifecycle(currentNode, selectedLifecycle,event.getRequestContext());
288     }
289   }
290 }