KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > module > admininterface > lists > VersionsList


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.module.admininterface.lists;
14
15 import info.magnolia.cms.beans.config.VersionConfig;
16 import info.magnolia.cms.core.Content;
17 import info.magnolia.cms.core.HierarchyManager;
18 import info.magnolia.cms.gui.control.ContextMenu;
19 import info.magnolia.cms.gui.control.ContextMenuItem;
20 import info.magnolia.cms.gui.control.FunctionBar;
21 import info.magnolia.cms.gui.control.FunctionBarItem;
22 import info.magnolia.cms.gui.controlx.list.ListColumn;
23 import info.magnolia.cms.gui.controlx.list.ListControl;
24 import info.magnolia.cms.gui.controlx.list.ListModel;
25 import info.magnolia.cms.gui.controlx.version.VersionListModel;
26 import info.magnolia.cms.i18n.MessagesManager;
27 import info.magnolia.cms.security.AccessDeniedException;
28 import info.magnolia.cms.util.AlertUtil;
29 import info.magnolia.cms.util.FreeMarkerUtil;
30 import info.magnolia.context.MgnlContext;
31
32 import javax.jcr.PathNotFoundException;
33 import javax.jcr.RepositoryException;
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
36
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40
41 /**
42  * @author Philipp Bracher
43  * @version $Revision: 6948 $ ($Author: philipp $)
44  */

45 public abstract class VersionsList extends AbstractList {
46
47     private static Logger log = LoggerFactory.getLogger(VersionsList.class);
48
49     /**
50      * The repository
51      */

52     private String JavaDoc repository;
53
54     /**
55      * The path of the node
56      */

57     protected String JavaDoc path;
58
59     /**
60      * If the command is restore, this defines the label of the version to restore.
61      */

62     private String JavaDoc versionLabel;
63
64     /**
65      * @param name
66      * @param request
67      * @param response
68      * @throws Exception
69      */

70     public VersionsList(String JavaDoc name, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
71         super(name, request, response);
72     }
73
74     /**
75      * @see info.magnolia.module.admininterface.lists.AbstractList#getModel()
76      */

77     public ListModel getModel() {
78         try {
79             Content node = getNode();
80             return new VersionListModel(node, this.getMaxShowedVersions());
81         }
82         catch (Exception JavaDoc e) {
83             log.error("can't find node for version list {}", this.path);
84         }
85         return null;
86     }
87
88     /**
89      * Check the version rollover. The latest can not get restored therfore we don't show it.
90      */

91     protected int getMaxShowedVersions() {
92         return Math.min((int)VersionConfig.getInstance().getMaxVersionAllowed(), 50);
93     }
94
95     public void configureList(ListControl list) {
96         // set onselect
97
list.setRenderer(new AdminListControlRenderer() {
98
99             public String JavaDoc onSelect(ListControl list, Integer JavaDoc index) {
100                 String JavaDoc js = super.onSelect(list, index);
101                 js += "mgnlVersionsList.currentVersionLabel = '" + list.getIteratorValue("versionLabel") + "';";
102                 return js;
103             }
104
105             public String JavaDoc onDblClick(ListControl list, Integer JavaDoc index) {
106                 return "mgnlVersionsList.showItem()";
107             }
108         });
109
110         list.addGroupableField("userName");
111         list.addSortableField("created");
112         list.addColumn(new ListColumn("name", "Name", "150", true));
113         list.addColumn(new ListColumn("created", "Date", "100", true));
114         list.addColumn(new ListColumn("userName", "User", "100", true));
115     }
116
117     /**
118      * The script executed on a show link
119      */

120     public abstract String JavaDoc getOnShowFunction();
121
122     protected void configureContextMenu(ContextMenu menu) {
123         ContextMenuItem show = new ContextMenuItem("show");
124         show.setLabel(MessagesManager.get("versions.show"));
125         show.setOnclick("mgnlVersionsList.showItem()");
126         show.setIcon(MgnlContext.getContextPath() + "/.resources/icons/16/note_view.gif");
127
128         ContextMenuItem restore = new ContextMenuItem("restore");
129         restore.setLabel(MessagesManager.get("versions.restore"));
130         restore.setOnclick("mgnlVersionsList.restore()");
131         restore.setIcon(MgnlContext.getContextPath() + "/.resources/icons/16/undo.gif");
132
133         menu.addMenuItem(show);
134         menu.addMenuItem(restore);
135     }
136
137     /**
138      * @see info.magnolia.module.admininterface.lists.AbstractList#configureFunctionBar(info.magnolia.cms.gui.control.FunctionBar)
139      */

140     protected void configureFunctionBar(FunctionBar bar) {
141         bar.addMenuItem(new FunctionBarItem(this.getContextMenu().getMenuItemByName("show")));
142         bar.addMenuItem(new FunctionBarItem(this.getContextMenu().getMenuItemByName("restore")));
143     }
144
145     /**
146      * @return
147      * @throws PathNotFoundException
148      * @throws RepositoryException
149      * @throws AccessDeniedException
150      */

151     protected Content getNode() throws PathNotFoundException, RepositoryException, AccessDeniedException {
152         HierarchyManager hm = MgnlContext.getHierarchyManager(this.getRepository());
153         Content node = hm.getContent(this.getPath());
154         return node;
155     }
156
157     public String JavaDoc restore() {
158         try {
159             Content node = this.getNode();
160             node.addVersion();
161             node.restore(this.getVersionLabel(), true);
162             AlertUtil.setMessage(MessagesManager.get("versions.restore.latest.success"));
163         }
164         catch (Exception JavaDoc e) {
165             log.error("can't restore", e);
166             AlertUtil.setMessage(MessagesManager.get("versions.restore.exception", new String JavaDoc[]{e.getMessage()}));
167         }
168         return show();
169     }
170
171     /**
172      * @return Returns the path.
173      */

174     public String JavaDoc getPath() {
175         return this.path;
176     }
177
178     /**
179      * @param path The path to set.
180      */

181     public void setPath(String JavaDoc path) {
182         this.path = path;
183     }
184
185     /**
186      * @return Returns the repository.
187      */

188     public String JavaDoc getRepository() {
189         return this.repository;
190     }
191
192     /**
193      * @param repository The repository to set.
194      */

195     public void setRepository(String JavaDoc repository) {
196         this.repository = repository;
197     }
198
199     /**
200      * @see com.obinary.magnolia.professional.lists.AbstractAdvancedSearchList#onRender()
201      */

202     public String JavaDoc onRender() {
203         return FreeMarkerUtil.process(VersionsList.class, this);
204     }
205
206     /**
207      * @return Returns the versionLabel.
208      */

209     public String JavaDoc getVersionLabel() {
210         return this.versionLabel;
211     }
212
213     /**
214      * @param versionLabel The versionLabel to set.
215      */

216     public void setVersionLabel(String JavaDoc versionLabel) {
217         this.versionLabel = versionLabel;
218     }
219
220 }
221
Popular Tags