View Javadoc
1   /*
2    * Copyright (C) 2003-2010 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.wcm.webui.authoring;
18  
19  import java.util.ArrayList;
20  import java.util.HashMap;
21  import java.util.HashSet;
22  import java.util.List;
23  import java.util.Set;
24  
25  import javax.jcr.Node;
26  
27  import org.exoplatform.commons.utils.LazyPageList;
28  import org.exoplatform.commons.utils.ListAccess;
29  import org.exoplatform.commons.utils.ListAccessImpl;
30  import org.exoplatform.portal.application.PortalRequestContext;
31  import org.exoplatform.portal.webui.util.Util;
32  import org.exoplatform.services.cms.drives.ManageDriveService;
33  import org.exoplatform.services.wcm.core.NodeLocation;
34  import org.exoplatform.services.wcm.extensions.publication.PublicationManager;
35  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
36  import org.exoplatform.wcm.webui.Utils;
37  import org.exoplatform.webui.application.WebuiRequestContext;
38  import org.exoplatform.webui.application.portlet.PortletRequestContext;
39  import org.exoplatform.webui.config.annotation.ComponentConfig;
40  import org.exoplatform.webui.config.annotation.EventConfig;
41  import org.exoplatform.webui.core.UIComponent;
42  import org.exoplatform.webui.core.UIContainer;
43  import org.exoplatform.webui.core.lifecycle.Lifecycle;
44  import org.exoplatform.webui.event.Event;
45  import org.exoplatform.webui.event.EventListener;
46  
47  /**
48   * Created by The eXo Platform SAS
49   * Author : eXoPlatform
50   *          exo@exoplatform.com
51   * Feb 2, 2010
52   */
53  @ComponentConfig(lifecycle = Lifecycle.class,
54                   template = "app:/groovy/authoring/UIDashboardForm.gtmpl",
55                   events = {
56      @EventConfig(listeners = UIDashboardForm.ShowDocumentActionListener.class),
57      @EventConfig(listeners = UIDashboardForm.RefreshActionListener.class) })
58  public class UIDashboardForm extends UIContainer {
59  
60    private int pageSize_ = 10;
61  
62    public UIDashboardForm() throws Exception {
63      addChild(UIDashBoardColumn.class, null, "UIDashboardDraft").setLabel("UIDashboardForm.label.mydraft");
64      addChild(UIDashBoardColumn.class, null, "UIDashboardWaiting").setLabel("UIDashboardForm.label.waitingapproval");
65      addChild(UIDashBoardColumn.class, null, "UIDashboardPublish").setLabel("UIDashboardForm.label.publishedtomorrow");
66      refreshData();
67    }
68  
69    public List<Node> getContents(String fromstate) {
70      return getContents(fromstate, null, null);
71    }
72  
73    public List<Node> getContents(String fromstate, String tostate) {
74      return getContents(fromstate, tostate, null);
75    }
76  
77    public List<Node> getContents(String fromstate, String tostate, String date) {
78      PublicationManager manager = WCMCoreUtils.getService(PublicationManager.class);
79      String user = PortalRequestContext.getCurrentInstance().getRemoteUser();
80      String lang = Util.getPortalRequestContext().getLocale().getLanguage();
81      List<Node> nodes = new ArrayList<Node>();
82      List<Node> temp = new ArrayList<Node>();
83      try {
84        nodes = manager.getContents(fromstate, tostate, date, user, lang, 
85                WCMCoreUtils.getRepository().getConfiguration().getDefaultWorkspaceName());
86        Set<String> uuidList = new HashSet<String>();
87        for(Node node : nodes) {
88          String currentState = null;
89          if(node.hasProperty("publication:currentState"))
90            currentState = node.getProperty("publication:currentState").getString();
91          if(currentState == null || !currentState.equals("published")) {
92            if(!org.exoplatform.services.cms.impl.Utils.isInTrash(node) &&
93              !uuidList.contains(node.getSession().getWorkspace().getName() + node.getUUID())) {
94              uuidList.add(node.getSession().getWorkspace().getName() + node.getUUID());
95              temp.add(node);
96            }
97          }
98        }
99      } catch (Exception e) {
100       temp = new ArrayList<Node>();
101     }
102     return temp;
103   }
104 
105   private void refreshData() {
106     List<UIDashBoardColumn> children = new ArrayList<UIDashBoardColumn>();
107     for (UIComponent component : getChildren()) {
108       if (component instanceof UIDashBoardColumn) {
109         children.add((UIDashBoardColumn)component);
110       }
111     }
112     ListAccess<NodeLocation> draftNodes = new ListAccessImpl<NodeLocation>(NodeLocation.class,
113         NodeLocation.getLocationsByNodeList(getContents("draft")));
114     children.get(0).getUIPageIterator().setPageList(
115     new LazyPageList<NodeLocation>(draftNodes,  pageSize_));
116 
117     ListAccess<NodeLocation> waitingNodes = new ListAccessImpl<NodeLocation>(NodeLocation.class,
118     NodeLocation.getLocationsByNodeList(getContents("pending")));
119     children.get(1).getUIPageIterator().setPageList(
120     new LazyPageList<NodeLocation>(waitingNodes, pageSize_));
121 
122     ListAccess<NodeLocation> publishedNodes = new ListAccessImpl<NodeLocation>(NodeLocation.class,
123     NodeLocation.getLocationsByNodeList(getContents("staged", null, "2")));
124     children.get(2).getUIPageIterator().setPageList(
125     new LazyPageList<NodeLocation>(publishedNodes, pageSize_));
126 
127   }
128 
129   public void processRender(WebuiRequestContext context) throws Exception
130   {
131     refreshData();
132     super.processRender(context);
133   }
134 
135 
136   /**
137    * The listener interface for receiving ShowDocumentAction events.
138    * The class that is interested in processing a changeRepositoryAction
139    * event implements this interface, and the object created
140    * with that class is registered with a component using the
141    * component's <code>addShowDocumentActionListener</code> method. When
142    * the ShowDocumentAction event occurs, that object's appropriate
143    * method is invoked.
144    */
145   public static class ShowDocumentActionListener extends EventListener<UIDashboardForm> {
146 
147     /* (non-Javadoc)
148      * @see org.exoplatform.webui.event.EventListener#execute(org.exoplatform.webui.event.Event)
149      */
150     public void execute(Event<UIDashboardForm> event) throws Exception {
151       PortalRequestContext context = Util.getPortalRequestContext();
152       String path = event.getRequestContext().getRequestParameter(OBJECTID);
153       HashMap<String, String> map = new HashMap<String, String>();
154       ManageDriveService driveService = WCMCoreUtils.getService(ManageDriveService.class);
155       map.put("repository", "repository");
156       map.put("drive", driveService.getDriveOfDefaultWorkspace());
157       map.put("path", path);
158       context.setAttribute("jcrexplorer-show-document", map);
159       Utils.updatePortal((PortletRequestContext) event.getRequestContext());
160 
161     }
162   }
163 
164   /**
165    * The listener interface for receiving RefreshAction events.
166    * The class that is interested in processing a changeRepositoryAction
167    * event implements this interface, and the object created
168    * with that class is registered with a component using the
169    * component's <code>RefreshActionListener</code> method. When
170    * the RefreshAction event occurs, that object's appropriate
171    * method is invoked.
172    */
173   public static class RefreshActionListener extends EventListener<UIDashboardForm> {
174 
175       /* (non-Javadoc)
176        * @see org.exoplatform.webui.event.EventListener#execute(org.exoplatform.webui.event.Event)
177        */
178     public void execute(Event<UIDashboardForm> event) throws Exception {
179       UIDashboardForm src = event.getSource();
180       Utils.updatePortal((PortletRequestContext) event.getRequestContext());
181     }
182   }
183 
184 }