1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.admininterface.templates;
35
36 import java.util.Collection;
37 import java.util.Iterator;
38
39 import javax.jcr.Node;
40 import javax.jcr.RepositoryException;
41 import javax.jcr.UnsupportedRepositoryOperationException;
42 import javax.jcr.version.Version;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 import info.magnolia.cms.core.Content;
47 import info.magnolia.cms.core.ItemType;
48 import info.magnolia.cms.core.version.ContentVersion;
49 import info.magnolia.cms.security.AccessDeniedException;
50 import info.magnolia.cms.util.ContentUtil;
51 import info.magnolia.cms.util.ContentWrapper;
52 import info.magnolia.module.admininterface.VersionUtil;
53 import info.magnolia.rendering.model.RenderingModel;
54 import info.magnolia.rendering.model.RenderingModelImpl;
55 import info.magnolia.rendering.template.RenderableDefinition;
56
57 public class MgnlDeletedTemplateModel extends RenderingModelImpl<RenderableDefinition> {
58
59 private static final Logger log = LoggerFactory.getLogger(MgnlDeletedTemplateModel.class);
60
61 public MgnlDeletedTemplateModel(Node content, RenderableDefinition definition, RenderingModel<RenderableDefinition> parent) {
62 super(content, definition, parent);
63 }
64
65 public String getLastVersion() {
66 Collection<Version> allVersions = null;
67
68 Iterator<Version> iterator = null;
69 Content maybeVersion = ContentUtil.asContent(getNode());
70 Content content = ContentUtil.asContent(getNode());
71 try {
72 try {
73 allVersions = VersionUtil.getSortedNotDeletedVersions(content);
74 } catch (AccessDeniedException e) {
75
76 while (maybeVersion != null && maybeVersion instanceof ContentWrapper) {
77 maybeVersion = ((ContentWrapper) maybeVersion).getWrappedContent();
78 }
79 if (maybeVersion instanceof ContentVersion) {
80 String versionName = ((ContentVersion) maybeVersion).getVersionLabel();
81 content = maybeVersion.getHierarchyManager().getContent(maybeVersion.getHandle());
82 allVersions = VersionUtil.getSortedNotDeletedVersionsBefore(content, versionName);
83 }
84 }
85
86 if (allVersions == null || allVersions.isEmpty()) {
87 return null;
88 }
89 return allVersions.iterator().next().getName();
90 } catch (UnsupportedRepositoryOperationException e) {
91
92 log.error("Failed to retrieve version history for " + content.getHandle() + ". This node doesn't support versioning", e);
93 } catch (RepositoryException e) {
94 log.error("Failed to retrieve version history for " + content.getHandle() + "", e);
95 }
96 return null;
97 }
98
99 public boolean hasChildren() throws RepositoryException {
100 return getNode().hasNode(ItemType.CONTENT.getSystemName());
101 }
102
103 public String getDeletionAuthor() throws RepositoryException {
104 return getNode().getProperty("mgnl:deletedBy").getString();
105 }
106
107 public String getDeletionDate() throws RepositoryException {
108 return getNode().getProperty("mgnl:deletedOn").getString();
109 }
110 }