1 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 ; 35 import javax.servlet.http.HttpServletResponse ; 36 37 import org.slf4j.Logger; 38 import org.slf4j.LoggerFactory; 39 40 41 45 public abstract class VersionsList extends AbstractList { 46 47 private static Logger log = LoggerFactory.getLogger(VersionsList.class); 48 49 52 private String repository; 53 54 57 protected String path; 58 59 62 private String versionLabel; 63 64 70 public VersionsList(String name, HttpServletRequest request, HttpServletResponse response) throws Exception { 71 super(name, request, response); 72 } 73 74 77 public ListModel getModel() { 78 try { 79 Content node = getNode(); 80 return new VersionListModel(node, this.getMaxShowedVersions()); 81 } 82 catch (Exception e) { 83 log.error("can't find node for version list {}", this.path); 84 } 85 return null; 86 } 87 88 91 protected int getMaxShowedVersions() { 92 return Math.min((int)VersionConfig.getInstance().getMaxVersionAllowed(), 50); 93 } 94 95 public void configureList(ListControl list) { 96 list.setRenderer(new AdminListControlRenderer() { 98 99 public String onSelect(ListControl list, Integer index) { 100 String js = super.onSelect(list, index); 101 js += "mgnlVersionsList.currentVersionLabel = '" + list.getIteratorValue("versionLabel") + "';"; 102 return js; 103 } 104 105 public String onDblClick(ListControl list, Integer 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 120 public abstract String 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 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 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 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 e) { 165 log.error("can't restore", e); 166 AlertUtil.setMessage(MessagesManager.get("versions.restore.exception", new String []{e.getMessage()})); 167 } 168 return show(); 169 } 170 171 174 public String getPath() { 175 return this.path; 176 } 177 178 181 public void setPath(String path) { 182 this.path = path; 183 } 184 185 188 public String getRepository() { 189 return this.repository; 190 } 191 192 195 public void setRepository(String repository) { 196 this.repository = repository; 197 } 198 199 202 public String onRender() { 203 return FreeMarkerUtil.process(VersionsList.class, this); 204 } 205 206 209 public String getVersionLabel() { 210 return this.versionLabel; 211 } 212 213 216 public void setVersionLabel(String versionLabel) { 217 this.versionLabel = versionLabel; 218 } 219 220 } 221 | Popular Tags |