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.cms.core.version;
35
36 import info.magnolia.cms.core.HierarchyManager;
37 import info.magnolia.cms.core.Path;
38 import info.magnolia.cms.security.PermissionUtil;
39 import info.magnolia.context.MgnlContext;
40 import info.magnolia.context.MgnlContext.Op;
41 import info.magnolia.jcr.wrapper.ChildWrappingNodeWrapper;
42 import info.magnolia.logging.AuditLoggingUtil;
43
44 import javax.jcr.InvalidItemStateException;
45 import javax.jcr.ItemExistsException;
46 import javax.jcr.ItemNotFoundException;
47 import javax.jcr.Node;
48 import javax.jcr.PathNotFoundException;
49 import javax.jcr.RepositoryException;
50 import javax.jcr.Session;
51 import javax.jcr.UnsupportedRepositoryOperationException;
52 import javax.jcr.lock.LockException;
53 import javax.jcr.nodetype.ConstraintViolationException;
54 import javax.jcr.nodetype.NodeType;
55 import javax.jcr.version.Version;
56 import javax.jcr.version.VersionException;
57 import javax.jcr.version.VersionHistory;
58 import javax.jcr.version.VersionIterator;
59
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
62
63
64
65
66
67
68 public class MgnlVersioningNodeWrapper extends ChildWrappingNodeWrapper {
69
70 private static final Logger log = LoggerFactory.getLogger(MgnlVersioningNodeWrapper.class);
71
72 public MgnlVersioningNodeWrapper(Node wrapped) {
73 super(wrapped);
74 }
75
76
77 @Override
78 public void restore(String versionName, boolean removeExisting) throws VersionException, ItemExistsException, UnsupportedRepositoryOperationException,
79 LockException, InvalidItemStateException, RepositoryException {
80 VersionManager versionMan = VersionManager.getInstance();
81 Node raw = deepUnwrap(getClass());
82 Version version = versionMan.getVersion(raw, versionName);
83 versionMan.restore(raw, version, removeExisting);
84 }
85
86 @Override
87 public void restore(Version version, boolean removeExisting) throws VersionException, ItemExistsException, InvalidItemStateException,
88 UnsupportedRepositoryOperationException, LockException, RepositoryException {
89 VersionManager versionMan = VersionManager.getInstance();
90 versionMan.restore(deepUnwrap(getClass()), version, removeExisting);
91 }
92
93 @Override
94 public void restore(Version version, String relPath, boolean removeExisting) throws PathNotFoundException, ItemExistsException, VersionException,
95 ConstraintViolationException, UnsupportedRepositoryOperationException, LockException, InvalidItemStateException, RepositoryException {
96 throw new UnsupportedOperationException("Magnolia doesn't support restore into specified path at the moment");
97 }
98
99 @Override
100 public void restoreByLabel(String versionLabel, boolean removeExisting) throws VersionException, ItemExistsException,
101 UnsupportedRepositoryOperationException, LockException, InvalidItemStateException, RepositoryException {
102 throw new UnsupportedOperationException("Magnolia doesn't support locating versions by label at the moment");
103 }
104
105
106
107
108 @Override
109 public void remove() throws RepositoryException {
110 wrapped = getWrappedNode();
111 final String nodePath = Path.getAbsolutePath(wrapped.getPath());
112 log.debug("removing {} from {}", wrapped.getPath(), getSession().getWorkspace().getName());
113 PermissionUtil.verifyIsGrantedOrThrowException(getSession(), Path.getAbsolutePath(getPath()), Session.ACTION_REMOVE);
114 NodeType nodeType = this.getPrimaryNodeType();
115 String workspaceName = getSession().getWorkspace().getName();
116 if (!workspaceName.equals("mgnlVersion")) {
117 MgnlContext.doInSystemContext(new Op<Void, RepositoryException>() {
118 @Override
119 public Void exec() throws RepositoryException {
120 try {
121 final String uuid = wrapped.getUUID();
122 HierarchyManager hm = MgnlContext.getHierarchyManager("mgnlVersion");
123 Node versionedNode = hm.getContentByUUID(uuid).getJCRNode();
124 log.debug("Located versioned node {}({})", uuid, nodePath);
125
126 VersionHistory history = versionedNode.getVersionHistory();
127
128 log.debug("Removing versioned node {}({})", uuid, nodePath);
129 versionedNode.remove();
130 hm.save();
131
132 VersionIterator iter = history.getAllVersions();
133
134 iter.nextVersion();
135 while (iter.hasNext()) {
136 Version version = iter.nextVersion();
137 log.debug("removing version {}", version.getName());
138 history.removeVersion(version.getName());
139 }
140
141 } catch (ItemNotFoundException e) {
142
143 } catch (UnsupportedRepositoryOperationException e) {
144
145 }
146 return null;
147 }
148 });
149 }
150 wrapped.remove();
151 AuditLoggingUtil.log(AuditLoggingUtil.ACTION_DELETE, workspaceName, nodeType, nodePath);
152 }
153 }