View Javadoc
1   /*
2    * Copyright (C) 2003-2007 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.services.ecm.publication.plugins.webui;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import javax.jcr.Node;
23  
24  import org.exoplatform.commons.utils.LazyPageList;
25  import org.exoplatform.commons.utils.ListAccess;
26  import org.exoplatform.commons.utils.ListAccessImpl;
27  import org.exoplatform.portal.webui.util.Util;
28  import org.exoplatform.portal.webui.workspace.UIPortalApplication;
29  import org.exoplatform.services.ecm.publication.NotInPublicationLifecycleException;
30  import org.exoplatform.services.ecm.publication.PublicationService;
31  import org.exoplatform.services.wcm.core.NodeLocation;
32  import org.exoplatform.webui.config.annotation.ComponentConfig;
33  import org.exoplatform.webui.config.annotation.EventConfig;
34  import org.exoplatform.webui.core.UIComponentDecorator;
35  import org.exoplatform.webui.core.UIPageIterator;
36  import org.exoplatform.webui.core.UIPopupWindow;
37  import org.exoplatform.webui.event.Event;
38  import org.exoplatform.webui.event.EventListener;
39  
40  /**
41   * Created by The eXo Platform SARL
42   * Author : Dang Van Minh
43   *          minh.dang@exoplatform.com
44   * Jun 26, 2008 1:16:08 AM
45   */
46  @ComponentConfig(
47      template = "classpath:groovy/workflow/webui/UIPublicationLogList.gtmpl",
48      events = {
49          @EventConfig(listeners = UIPublicationLogList.CloseActionListener.class)
50      }
51  )
52  
53  public class UIPublicationLogList extends UIComponentDecorator {
54  
55    private UIPageIterator uiPageIterator_ ;
56    private NodeLocation currentNode_ ;
57  
58    public UIPublicationLogList() throws Exception {
59      uiPageIterator_ = createUIComponent(UIPageIterator.class, null, "PublicationLogListIterator");
60      setUIComponent(uiPageIterator_) ;
61    }
62  
63    public void setNode(Node node) throws Exception { currentNode_ = NodeLocation.getNodeLocationByNode(node); }
64    
65    private Node getCurrentNode() {
66      return NodeLocation.getNodeByLocation(currentNode_);
67    }
68  
69    public List<HistoryBean> getLog() throws NotInPublicationLifecycleException, Exception {
70      PublicationService publicationService = getApplicationComponent(PublicationService.class);
71      String[][] array = publicationService.getLog(getCurrentNode());
72      List<HistoryBean> list = new ArrayList<HistoryBean>();
73      for (int i = 0; i < array.length; i++) {
74        HistoryBean bean = new HistoryBean();
75        String[] currentLog = array[i];
76        bean.setDate(bean.formatStringByDateTime(currentLog[0]));
77        bean.setNewState(currentLog[1]);
78        bean.setUser(currentLog[2]);
79        String[] values = new String[currentLog.length - 4];
80        System.arraycopy(currentLog, 4, values, 0, currentLog.length-4);
81        String description = publicationService.getLocalizedAndSubstituteLog(getCurrentNode(),
82            Util.getUIPortal().getAncestorOfType(UIPortalApplication.class).getLocale(), currentLog[3], values);
83        bean.setDescription(description);
84        list.add(bean);
85      }
86      return list;
87    }
88  
89    @SuppressWarnings("unchecked")
90    public void updateGrid() throws Exception {
91      ListAccess<HistoryBean> historyList = new ListAccessImpl<HistoryBean>(HistoryBean.class,
92                                                                            getLog());
93      LazyPageList<HistoryBean> dataPageList = new LazyPageList<HistoryBean>(historyList, 10);
94      uiPageIterator_.setPageList(dataPageList);
95    }
96  
97    public UIPageIterator getUIPageIterator() { return uiPageIterator_ ; }
98  
99    public List getLogList() throws Exception { return uiPageIterator_.getCurrentPageData() ; }
100 
101   public String[] getActions() {return new String[]{"Close"} ;}
102 
103   static public class CloseActionListener extends EventListener<UIPublicationLogList> {
104     public void execute(Event<UIPublicationLogList> event) throws Exception {
105       UIPublicationLogList uiPublicationLogList = event.getSource() ;
106       UIPopupWindow uiPopup = uiPublicationLogList.getAncestorOfType(UIPopupWindow.class) ;
107       uiPopup.setRendered(false) ;
108       uiPopup.setShow(false) ;
109     }
110   }
111 
112   public class HistoryBean {
113     private String date;
114     private String newState;
115     private String user;
116     private String description;
117 
118     public String getDate() { return date; }
119     public void setDate(String date) { this.date = date; }
120     public String getDescription() { return description; }
121     public void setDescription(String description) { this.description = description; }
122     public String getNewState() { return newState; }
123     public void setNewState(String newState) { this.newState = newState; }
124     public String getUser() { return user; }
125     public void setUser(String user) { this.user = user; }
126 
127     /**
128      * Updated by Nguyen Van Chien
129      * @param stringInput
130      * @return
131      */
132     public String formatStringByDateTime(String stringInput) {
133       String dateYear = stringInput.substring(0, 4);
134       String dateMonth = stringInput.substring(4, 6);
135       String dateDay = stringInput.substring(6, 8);
136       String dateHour = stringInput.substring(9, 11);
137       String dateMinute = stringInput.substring(11, 13);
138       String dateSecond = stringInput.substring(13, 15);
139       StringBuilder builder = new StringBuilder();
140       builder.append(dateMonth).append("/")
141             .append(dateDay).append("/")
142             .append(dateYear).append(" ")
143             .append(dateHour).append(":")
144             .append(dateMinute).append(":")
145             .append(dateSecond);
146 
147       return builder.toString();
148     }
149   }
150 }