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