1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.services.cms.link;
18
19 import javax.jcr.AccessDeniedException;
20 import javax.jcr.InvalidItemStateException;
21 import javax.jcr.Item;
22 import javax.jcr.ItemExistsException;
23 import javax.jcr.ItemNotFoundException;
24 import javax.jcr.ItemVisitor;
25 import javax.jcr.Node;
26 import javax.jcr.Property;
27 import javax.jcr.ReferentialIntegrityException;
28 import javax.jcr.RepositoryException;
29 import javax.jcr.Session;
30 import javax.jcr.lock.LockException;
31 import javax.jcr.nodetype.ConstraintViolationException;
32 import javax.jcr.nodetype.NoSuchNodeTypeException;
33 import javax.jcr.version.VersionException;
34
35 import org.exoplatform.services.jcr.ext.common.SessionProvider;
36 import org.exoplatform.services.wcm.core.ItemLocation;
37 import org.exoplatform.services.wcm.utils.WCMCoreUtils;
38
39
40
41
42
43
44
45 public abstract class ItemLinkAware implements Item {
46
47 protected final ItemLocation itemLocation;
48 protected final String originalWorkspaceName;
49 protected final String virtualPath;
50
51 protected ItemLinkAware(String originalWorkspaceName, String virtualPath, Item item) {
52 this.originalWorkspaceName = originalWorkspaceName;
53 this.itemLocation = ItemLocation.getItemLocationByItem(item);
54 if (!virtualPath.startsWith("/")) {
55 throw new IllegalArgumentException("The path '" + virtualPath + "' must be an absolute path");
56 }
57 this.virtualPath = virtualPath;
58 }
59
60 public static ItemLinkAware newInstance(String originalWorkspaceName, String originalAbsPath, Item item) {
61 if (item instanceof Node) {
62 return new NodeLinkAware(originalWorkspaceName, originalAbsPath, (Node) item);
63 }
64 return new PropertyLinkAware(originalWorkspaceName, originalAbsPath, (Property) item);
65 }
66
67 public Item getItem() {
68 return ItemLocation.getItemByLocation(itemLocation);
69 }
70
71 public Session getItemSession() throws RepositoryException {
72 return getItem().getSession();
73 }
74
75 public Session getOriginalSession() throws RepositoryException {
76 SessionProvider sessionProvider;
77 if (itemLocation.isSystemSession()) {
78 sessionProvider = WCMCoreUtils.getSystemSessionProvider();
79 } else {
80 sessionProvider = WCMCoreUtils.getUserSessionProvider();
81 }
82 return sessionProvider.getSession(originalWorkspaceName, WCMCoreUtils.getRepository());
83 }
84
85
86
87
88 public void accept(ItemVisitor visitor) throws RepositoryException {
89 getItem().accept(visitor);
90 }
91
92
93
94
95 public Item getAncestor(int depth) throws ItemNotFoundException,
96 AccessDeniedException,
97 RepositoryException {
98 return LinkUtils.getNodeFinder().getItem(getOriginalSession(), LinkUtils.getAncestorPath(virtualPath, depth));
99 }
100
101
102
103
104 public int getDepth() throws RepositoryException {
105 return LinkUtils.getDepth(virtualPath);
106 }
107
108
109
110
111 public String getName() throws RepositoryException {
112 return getItem().getName();
113 }
114
115
116
117
118 public Node getParent() throws ItemNotFoundException, AccessDeniedException, RepositoryException {
119 return (Node) LinkUtils.getNodeFinder().getItem(getOriginalSession(), LinkUtils.getParentPath(virtualPath));
120 }
121
122
123
124
125 public String getPath() throws RepositoryException {
126 return virtualPath;
127 }
128
129
130
131
132 public Session getSession() throws RepositoryException {
133 return new SessionLinkAware(this);
134 }
135
136
137
138
139 public boolean isModified() {
140 return getItem().isModified();
141 }
142
143
144
145
146 public boolean isNew() {
147 return getItem().isNew();
148 }
149
150
151
152
153 public boolean isNode() {
154 return getItem().isNode();
155 }
156
157
158
159
160 public boolean isSame(Item otherItem) throws RepositoryException {
161 return getItem().isSame(otherItem);
162 }
163
164
165
166
167 public void refresh(boolean keepChanges) throws InvalidItemStateException, RepositoryException {
168 getItem().refresh(keepChanges);
169 }
170
171
172
173
174 public void remove() throws VersionException,
175 LockException,
176 ConstraintViolationException,
177 RepositoryException {
178 getItem().remove();
179 }
180
181
182
183
184 public void save() throws AccessDeniedException,
185 ItemExistsException,
186 ConstraintViolationException,
187 InvalidItemStateException,
188 ReferentialIntegrityException,
189 VersionException,
190 LockException,
191 NoSuchNodeTypeException,
192 RepositoryException {
193 getItem().save();
194 }
195 }