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.gui.control.ContextMenu;
37  import info.magnolia.cms.gui.control.ContextMenuItem;
38  import info.magnolia.cms.gui.control.FunctionBar;
39  import info.magnolia.cms.gui.control.FunctionBarItem;
40  import info.magnolia.cms.gui.controlx.list.ListColumn;
41  import info.magnolia.cms.gui.controlx.list.ListControl;
42  import info.magnolia.cms.gui.controlx.list.ListModel;
43  import info.magnolia.cms.i18n.Messages;
44  import info.magnolia.cms.i18n.MessagesManager;
45  import info.magnolia.cms.util.AlertUtil;
46  import info.magnolia.cms.util.DateUtil;
47  import info.magnolia.freemarker.FreemarkerUtil;
48  import info.magnolia.context.MgnlContext;
49  import info.magnolia.module.admininterface.lists.AbstractList;
50  import info.magnolia.module.admininterface.lists.AdminListControlRenderer;
51  import info.magnolia.module.workflow.WorkflowConstants;
52  import info.magnolia.module.workflow.WorkflowUtil;
53  
54  import java.text.ParseException;
55  import java.text.SimpleDateFormat;
56  import java.util.Date;
57  
58  import javax.servlet.http.HttpServletRequest;
59  import javax.servlet.http.HttpServletResponse;
60  
61  import org.apache.commons.lang.ObjectUtils;
62  import org.apache.commons.lang.StringUtils;
63  
64  
65  /**
66   * @author Philipp Bracher
67   * @version $Revision:3416 $ ($Author:philipp $)
68   */
69  public class Inbox extends AbstractList {
70  
71      /**
72       * The id of the workitem on which we called the command
73       */
74      private String flowItemId;
75  
76      /**
77       * The comment the user entered by proceeding or rejecting
78       */
79      private String comment;
80  
81      /**
82       * Show all the values of the workitem if true
83       */
84      private boolean debug = false;
85  
86      protected Messages msgs = MessagesManager.getMessages("info.magnolia.module.workflow.messages");
87  
88      /**
89       * @param name
90       * @param request
91       * @param response
92       */
93      public Inbox(String name, HttpServletRequest request, HttpServletResponse response) {
94          super(name, request, response);
95      }
96  
97      /**
98       * @see info.magnolia.module.admininterface.lists.AbstractList#getModel()
99       */
100     public ListModel getModel() {
101         return new InboxListModel(MgnlContext.getUser().getName());
102     }
103 
104     public String getSortBy() {
105         if(StringUtils.isEmpty(super.getSortBy())){
106             setSortBy("lastModified");
107         }
108         return super.getSortBy();
109     }
110 
111     /**
112      * Sets the select js code and defines the columns
113      */
114     public void configureList(ListControl list) {
115 
116         // define the select action
117         list.setRenderer(new AdminListControlRenderer() {
118 
119             public String onSelect(ListControl list, Integer index) {
120 
121                 String customEditDialog = ObjectUtils.toString(list
122                     .getIteratorValue(WorkflowConstants.ATTRIBUTE_EDIT_DIALOG));
123 
124                 String editDialog = StringUtils.defaultIfEmpty(customEditDialog, WorkflowConstants.DEFAULT_EDIT_DIALOG);
125                 String repository = ObjectUtils.toString(list.getIteratorValue("repository"));
126                 String path = ObjectUtils.toString(list.getIteratorValue("path"));
127 
128                 StringBuffer js = new StringBuffer();
129                 js.append("mgnl.workflow.Inbox.current = ");
130                 js.append("{");
131                 js.append("id : '").append(list.getIteratorValue("id")).append("',");
132                 js.append("path : '").append(path).append("',");
133                 js.append("version : '").append(list.getIteratorValue("version")).append("',");
134                 js.append("repository : '").append(repository).append("',");
135                 js.append("workItemPath : '").append(list.getIteratorValue("workItemPath")).append("',");
136                 js.append("editDialog : '").append(editDialog).append("'");
137                 js.append("};");
138                 js.append("mgnl.workflow.Inbox.show = ").append(getShowJSFunction(repository, path)).append(";");
139                 js.append(super.onSelect(list, index));
140                 return js.toString();
141             }
142 
143             public String onDblClick(ListControl list, Integer index) {
144                 return "mgnl.workflow.Inbox.edit();";
145             }
146         });
147 
148         list.addSortableField("lastModified");
149         list.addGroupableField("repository");
150         list.addGroupableField("workflow");
151 
152         list.addColumn(new ListColumn() {
153 
154             {
155                 setName("icon");
156                 setLabel("");
157                 setWidth("30px");
158                 setSeparator(false);
159             }
160 
161             public Object getValue() {
162                 String path = "" + this.getListControl().getIteratorValue("path");
163                 String repository = "" + this.getListControl().getIteratorValue("repository");
164                 return "<img src=\""
165                     + MgnlContext.getContextPath()
166                     + "/"
167                     + getIcon(path, repository)
168                     + "\" alt=\"\" border=\"0\" />";
169             }
170         });
171         list.addColumn(new ListColumn("name", msgs.get("inbox.item"), "100", true));
172         list.addColumn(new ListColumn("repository", msgs.get("inbox.repository"), "100px", true));
173         list.addColumn(new ListColumn("workflow", msgs.get("inbox.workflow"), "100px", true));
174         list.addColumn(new ListColumn("comment", msgs.get("inbox.comment"), "200", true));
175         list.addColumn(new ListColumn() {
176 
177             {
178                 setName("lastModified");
179                 setLabel(msgs.get("inbox.date"));
180                 setWidth("150px");
181             }
182 
183             public Object getValue() {
184                 String str = (String) super.getValue();
185                 Date date = null;
186                 try {
187                     date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ").parse(str);
188                     return DateUtil.formatDateTime(date);
189 
190                 }
191                 catch (ParseException e) {
192                     return StringUtils.EMPTY;
193                 }
194             }
195         });
196 
197     }
198 
199     protected String getIcon(String path, String repository) {
200         return InboxHelper.getIcon(repository, path);
201     }
202 
203     protected String getShowJSFunction(String repository, String path) {
204         return InboxHelper.getShowJSFunction(repository, path);
205     }
206 
207     public void configureContextMenu(ContextMenu menu) {
208         ContextMenuItem edit = new ContextMenuItem("edit");
209         edit.setLabel(msgs.get("inbox.edit"));
210         edit.setOnclick("mgnl.workflow.Inbox.edit();");
211         edit.setIcon(MgnlContext.getContextPath() + "/.resources/icons/16/mail_write.gif");
212         edit.addJavascriptCondition("{test: function(){return mgnl.workflow.Inbox.current.id!=null}}");
213 
214         ContextMenuItem show = new ContextMenuItem("show");
215         show.setLabel(msgs.get("inbox.show"));
216         show.setOnclick("mgnl.workflow.Inbox.show();");
217         show.setIcon(MgnlContext.getContextPath() + "/.resources/icons/16/note_view.gif");
218         show.addJavascriptCondition("{test: function(){return mgnl.workflow.Inbox.current.id!=null}}");
219 
220         ContextMenuItem proceed = new ContextMenuItem("proceed");
221         proceed.setLabel(msgs.get("inbox.proceed"));
222         proceed.setOnclick("mgnl.workflow.Inbox.proceed();");
223         proceed.setIcon(MgnlContext.getContextPath() + "/.resources/icons/16/navigate_right2_green.gif");
224         proceed.addJavascriptCondition("{test: function(){return mgnl.workflow.Inbox.current.id!=null}}");
225 
226         ContextMenuItem reject = new ContextMenuItem("reject");
227         reject.setLabel(msgs.get("inbox.reject"));
228         reject.setOnclick("mgnl.workflow.Inbox.reject();");
229         reject.setIcon(MgnlContext.getContextPath() + "/.resources/icons/16/navigate_left2_red.gif");
230         reject.addJavascriptCondition("{test: function(){return mgnl.workflow.Inbox.current.id!=null}}");
231 
232         ContextMenuItem cancel = new ContextMenuItem("cancel");
233         cancel.setLabel(msgs.get("inbox.cancel"));
234         cancel.setOnclick("mgnl.workflow.Inbox.cancel();");
235         cancel.setIcon(MgnlContext.getContextPath() + "/.resources/icons/16/delete2.gif");
236         cancel.addJavascriptCondition("{test: function(){return mgnl.workflow.Inbox.current.id!=null}}");
237 
238         menu.addMenuItem(edit);
239         menu.addMenuItem(null);
240         menu.addMenuItem(show);
241         menu.addMenuItem(null);
242         menu.addMenuItem(proceed);
243         menu.addMenuItem(reject);
244         menu.addMenuItem(null);
245         menu.addMenuItem(cancel);
246     }
247 
248     /**
249      * Same as the context menu
250      */
251     public void configureFunctionBar(FunctionBar bar) {
252         ContextMenu menu = this.getContextMenu();
253         bar.addMenuItem(new FunctionBarItem(menu.getMenuItemByName("edit")));
254         bar.addMenuItem(null);
255         bar.addMenuItem(new FunctionBarItem(menu.getMenuItemByName("show")));
256         bar.addMenuItem(null);
257         bar.addMenuItem(new FunctionBarItem(menu.getMenuItemByName("reject")));
258         bar.addMenuItem(new FunctionBarItem(menu.getMenuItemByName("proceed")));
259         bar.addMenuItem(null);
260         bar.addMenuItem(new FunctionBarItem(menu.getMenuItemByName("cancel")));
261     }
262 
263     /**
264      * Add some inbox specific stuff: mainly hidden fields.
265      */
266     public String onRender() {
267         return FreemarkerUtil.process(this);
268     }
269 
270     /**
271      * Proceed the item
272      */
273     public String proceed() {
274         try {
275             WorkflowUtil.proceed(this.getFlowItemId(), WorkflowConstants.ACTION_PROCEED);
276         }
277         catch (Exception e) {
278             AlertUtil.setMessage("can't proceed:", e);
279         }
280         return this.show();
281     }
282 
283     /**
284      * Reject the item (adds a comment)
285      */
286     public String reject() {
287         try {
288             WorkflowUtil.proceed(this.getFlowItemId(), WorkflowConstants.ACTION_REJECT, this.getComment());
289         }
290         catch (Exception e) {
291             AlertUtil.setMessage("can't reject:", e);
292         }
293         return this.show();
294     }
295 
296     /**
297      * Stop the workflow.
298      */
299     public String cancel() {
300         try {
301             WorkflowUtil.proceed(this.getFlowItemId(), WorkflowConstants.ACTION_CANCEL);
302         }
303         catch (Exception e) {
304             AlertUtil.setMessage("can't cancel:", e);
305         }
306         return this.show();
307     }
308 
309     /**
310      * @return Returns the flowItemId.
311      */
312     public String getFlowItemId() {
313         return this.flowItemId;
314     }
315 
316     /**
317      * @param flowItemId The flowItemId to set.
318      */
319     public void setFlowItemId(String flowItemId) {
320         this.flowItemId = flowItemId;
321     }
322 
323     /**
324      * @return Returns the comment.
325      */
326     public String getComment() {
327         return this.comment;
328     }
329 
330     /**
331      * @param comment The comment to set.
332      */
333     public void setComment(String comment) {
334         this.comment = comment;
335     }
336 
337     /**
338      * @return Returns the debug.
339      */
340     public boolean isDebug() {
341         return this.debug;
342     }
343 
344     /**
345      * @param debug The debug to set.
346      */
347     public void setDebug(boolean debug) {
348         this.debug = debug;
349     }
350 
351 }