View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.workflow.inbox;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.cms.core.HierarchyManager;
38  import info.magnolia.cms.gui.control.ContextMenu;
39  import info.magnolia.cms.gui.control.ContextMenuItem;
40  import info.magnolia.cms.gui.control.Tree;
41  import info.magnolia.cms.gui.controlx.list.AbstractListModel;
42  import info.magnolia.cms.gui.controlx.list.IconListColumnRenderer;
43  import info.magnolia.cms.gui.controlx.list.ListColumn;
44  import info.magnolia.cms.gui.controlx.list.ListControl;
45  import info.magnolia.cms.gui.controlx.list.ListModel;
46  import info.magnolia.cms.util.DateUtil;
47  import info.magnolia.cms.util.MetaDataUtil;
48  import info.magnolia.context.Context;
49  import info.magnolia.context.MgnlContext;
50  import info.magnolia.module.admininterface.lists.AbstractList;
51  import info.magnolia.module.admininterface.lists.AdminListControlRenderer;
52  import info.magnolia.module.workflow.WorkflowUtil;
53  
54  import java.util.ArrayList;
55  import java.util.Collection;
56  import java.util.Iterator;
57  import java.util.List;
58  import java.util.Map;
59  
60  import javax.jcr.RepositoryException;
61  import javax.servlet.http.HttpServletRequest;
62  import javax.servlet.http.HttpServletResponse;
63  
64  import openwfe.org.engine.workitem.InFlowWorkItem;
65  import openwfe.org.engine.workitem.ListAttribute;
66  
67  import org.apache.commons.lang.ObjectUtils;
68  import org.apache.commons.lang.StringUtils;
69  import org.slf4j.Logger;
70  import org.slf4j.LoggerFactory;
71  
72  
73  /**
74   * @author philipp
75   * @version $Id: SubPagesList.java 32667 2010-03-13 00:37:06Z gjoseph $
76   */
77  public class SubPagesList extends AbstractList {
78  
79      /**
80       * Logger.
81       */
82      private static Logger log = LoggerFactory.getLogger(SubPagesList.class);
83  
84      private String workItemId;
85  
86      private InFlowWorkItem item;
87  
88      private String rootPath;
89  
90      private String repository;
91  
92      private HierarchyManager hm;
93  
94      private List subPages = new ArrayList();
95  
96      public SubPagesList(String name, HttpServletRequest request, HttpServletResponse response) {
97          super(name, request, response);
98      }
99  
100     public void init() {
101         super.init();
102         item = WorkflowUtil.getWorkItem(getWorkItemId());
103 
104         if (item == null) {
105             throw new RuntimeException("can't read workitem");
106         }
107 
108         rootPath = item.getAttribute("path").toString();
109         repository = item.getAttribute("repository").toString();
110         hm = MgnlContext.getHierarchyManager(repository);
111 
112         ListAttribute versions = (ListAttribute) item.getAttribute(Context.ATTRIBUTE_VERSION_MAP);
113         for (Iterator iter = versions.iterator(); iter.hasNext();) {
114             Map versionMap = (Map) iter.next();
115             String version = versionMap.get("version").toString();
116             String uuid = versionMap.get("uuid").toString();
117             try {
118                 Content node = hm.getContentByUUID(uuid);
119                 if (!node.getHandle().equals(rootPath)) {
120                     this.subPages.add(new SubPage(node, version));
121                 }
122             }
123             catch (RepositoryException e) {
124                 log.error("can't read subpage for [" + rootPath + "]", e);
125             }
126         }
127     }
128 
129     public void configureList(ListControl list) {
130         ListColumn iconColumn = new ListColumn("icon", "", "30px", true);
131         iconColumn.setRenderer(new IconListColumnRenderer());
132         list.addColumn(iconColumn);
133 
134         list.addColumn(new ListColumn("label", "", "200", true));
135         list.addColumn(new ListColumn("version", this.getMsgs().get("subpages.version"), "70px", true));
136         list.addColumn(new ListColumn("modDate", this.getMsgs().get("subpages.date"), "150px", true));
137 
138         ListColumn statusColumn = new ListColumn("activationStatusIcon", this.getMsgs().get("subpages.status"), "50px", true);
139         statusColumn.setRenderer(new IconListColumnRenderer());
140         list.addColumn(statusColumn);
141 
142         list.setRenderer(new AdminListControlRenderer() {
143 
144             {
145                 setBorder(true);
146             }
147 
148             public String onDblClick(ListControl list, Integer index) {
149                 return "mgnl.workflow.Inbox.show();";
150             }
151 
152             public String onSelect(ListControl list, Integer index) {
153 
154                 String path = ObjectUtils.toString(list.getIteratorValue("path"));
155 
156                 StringBuffer js = new StringBuffer();
157                 js.append("mgnl.workflow.Inbox.current = ");
158                 js.append("{");
159                 js.append("id : '").append(list.getIteratorValue("id")).append("',");
160                 js.append("path : '").append(path).append("',");
161                 js.append("version : '").append(list.getIteratorValue("version")).append("',");
162                 js.append("repository : '").append(repository).append("'");
163                 js.append("};");
164                 js.append("mgnl.workflow.Inbox.show = ").append(InboxHelper.getShowJSFunction(repository, path)).append(";");
165                 js.append(super.onSelect(list, index));
166 
167                 return js.toString();
168             }
169         });
170     }
171 
172     protected void configureContextMenu(ContextMenu menu) {
173         ContextMenuItem show = new ContextMenuItem("show");
174         show.setLabel(this.getMsgs().get("subpages.show"));
175         show.setOnclick("mgnl.workflow.Inbox.show();");
176         show.setIcon(MgnlContext.getContextPath() + "/.resources/icons/16/note_view.gif");
177         show.addJavascriptCondition("{test: function(){return mgnl.workflow.Inbox.current.id!=null}}");
178         menu.addMenuItem(show);
179     }
180 
181     public ListModel getModel() {
182         return new AbstractListModel() {
183 
184             protected Collection getResult() throws Exception {
185                 return subPages;
186             }
187         };
188     }
189 
190     public String getWorkItemId() {
191         return this.workItemId;
192     }
193 
194     public void setWorkItemId(String workItemId) {
195         this.workItemId = workItemId;
196     }
197 
198     public class SubPage {
199 
200         private String version;
201 
202         private Content node;
203 
204         private Content versionedNode;
205 
206         protected SubPage(Content node, String version) throws RepositoryException {
207             this.version = version;
208             this.node = node;
209             versionedNode = node.getVersionedContent(version);
210         }
211 
212         public String getPath(){
213             return node.getHandle();
214         }
215 
216         public String getIcon() {
217             return "/" + InboxHelper.getIcon(repository, node.getHandle());
218         }
219 
220         public String getLabel() {
221             return StringUtils.removeStart(node.getHandle(), rootPath + "/");
222         }
223 
224         public String getActivationStatusIcon() {
225             return Tree.ICONDOCROOT + MetaDataUtil.getActivationStatusIcon(versionedNode);
226         }
227 
228         public String getModDate() {
229             return DateUtil.formatDateTime(versionedNode.getMetaData().getModificationDate());
230         }
231 
232         public String getVersion() {
233             return this.version;
234         }
235 
236     }
237 
238 }