View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 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   * The {@link info.magnolia.module.admininterface.lists.AbstractList} responsible for displaying sub pages included in an activation workitem.
75   *
76   * @author philipp
77   * @version $Id: SubPagesList.java 45581 2011-05-26 15:37:55Z dlipp $
78   */
79  public class SubPagesList extends AbstractList {
80      private static final Logger log = LoggerFactory.getLogger(SubPagesList.class);
81  
82      private String workItemId;
83  
84      private InFlowWorkItem item;
85  
86      private String rootPath;
87  
88      private String repository;
89  
90      private HierarchyManager hm;
91  
92      private List subPages = new ArrayList();
93  
94      public SubPagesList(String name, HttpServletRequest request, HttpServletResponse response) {
95          super(name, request, response);
96      }
97  
98      @Override
99      public void init() {
100         super.init();
101         item = WorkflowUtil.getWorkItem(getWorkItemId());
102 
103         if (item == null) {
104             throw new RuntimeException("can't read workitem");
105         }
106 
107         rootPath = item.getAttribute("path").toString();
108         repository = item.getAttribute("repository").toString();
109         hm = MgnlContext.getHierarchyManager(repository);
110 
111         ListAttribute versions = (ListAttribute) item.getAttribute(Context.ATTRIBUTE_VERSION_MAP);
112         for (Iterator iter = versions.iterator(); iter.hasNext();) {
113             Map versionMap = (Map) iter.next();
114             String version = versionMap.get("version").toString();
115             String uuid = versionMap.get("uuid").toString();
116             try {
117                 Content node = hm.getContentByUUID(uuid);
118                 if (!node.getHandle().equals(rootPath)) {
119                     this.subPages.add(new SubPage(node, version));
120                 }
121             }
122             catch (RepositoryException e) {
123                 log.error("can't read subpage for [" + rootPath + "]", e);
124             }
125         }
126     }
127 
128     @Override
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             @Override
149             public String onDblClick(ListControl list, Integer index) {
150                 return "mgnl.workflow.Inbox.show();";
151             }
152 
153             @Override
154             public String onSelect(ListControl list, Integer index) {
155 
156                 String path = ObjectUtils.toString(list.getIteratorValue("path"));
157 
158                 StringBuffer js = new StringBuffer();
159                 js.append("mgnl.workflow.Inbox.current = ");
160                 js.append("{");
161                 js.append("id : '").append(list.getIteratorValue("id")).append("',");
162                 js.append("path : '").append(path).append("',");
163                 js.append("version : '").append(list.getIteratorValue("version")).append("',");
164                 js.append("repository : '").append(repository).append("'");
165                 js.append("};");
166                 js.append("mgnl.workflow.Inbox.show = ").append(InboxHelper.getShowJSFunction(repository, path)).append(";");
167                 js.append(super.onSelect(list, index));
168 
169                 return js.toString();
170             }
171         });
172     }
173 
174     @Override
175     protected void configureContextMenu(ContextMenu menu) {
176         ContextMenuItem show = new ContextMenuItem("show");
177         show.setLabel(this.getMsgs().get("subpages.show"));
178         show.setOnclick("mgnl.workflow.Inbox.show();");
179         show.setIcon(MgnlContext.getContextPath() + "/.resources/icons/16/note_view.gif");
180         show.addJavascriptCondition("{test: function(){return mgnl.workflow.Inbox.current.id!=null}}");
181         menu.addMenuItem(show);
182     }
183 
184     @Override
185     public ListModel getModel() {
186         return new AbstractListModel() {
187 
188             @Override
189             protected Collection getResult() throws Exception {
190                 return subPages;
191             }
192         };
193     }
194 
195     public String getWorkItemId() {
196         return this.workItemId;
197     }
198 
199     public void setWorkItemId(String workItemId) {
200         this.workItemId = workItemId;
201     }
202 
203     /**
204      * Simple model bean for displayed subpages included in an activation workitem.
205      */
206     public class SubPage {
207 
208         private String version;
209 
210         private Content node;
211 
212         private Content versionedNode;
213 
214         protected SubPage(Content node, String version) throws RepositoryException {
215             this.version = version;
216             this.node = node;
217             versionedNode = node.getVersionedContent(version);
218         }
219 
220         public String getPath(){
221             return node.getHandle();
222         }
223 
224         public String getIcon() {
225             return "/" + InboxHelper.getIcon(repository, node.getHandle());
226         }
227 
228         public String getLabel() {
229             return StringUtils.removeStart(node.getHandle(), rootPath + "/");
230         }
231 
232         public String getActivationStatusIcon() {
233             return Tree.ICONDOCROOT + MetaDataUtil.getActivationStatusIcon(versionedNode.getJCRNode());
234         }
235 
236         public String getModDate() {
237             return DateUtil.formatDateTime(versionedNode.getMetaData().getModificationDate());
238         }
239 
240         public String getVersion() {
241             return this.version;
242         }
243 
244     }
245 
246 }