1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.services.wcm.core;
18
19 import javax.jcr.Item;
20 import javax.jcr.Node;
21 import javax.jcr.PathNotFoundException;
22 import javax.jcr.RepositoryException;
23 import javax.jcr.Session;
24
25 import org.exoplatform.services.cms.link.NodeFinder;
26 import org.exoplatform.services.jcr.core.ManageableRepository;
27 import org.exoplatform.services.security.IdentityConstants;
28 import org.exoplatform.services.wcm.utils.WCMCoreUtils;
29
30
31
32
33
34
35
36 public class ItemLocation {
37
38
39 protected String repository;
40
41
42 protected String workspace;
43
44
45 protected String path;
46
47
48 protected String uuid;
49
50
51 protected boolean isSystemSession;
52
53
54
55
56 public ItemLocation() {}
57
58
59
60
61
62
63
64
65
66
67 public ItemLocation(final String repository, final String workspace, final String path, final String uuid,
68 final boolean isSystem) {
69 this.repository = repository;
70 this.workspace = workspace;
71 this.path = path;
72 this.uuid = uuid;
73 this.isSystemSession = isSystem;
74 }
75
76
77
78
79
80
81
82
83
84 public ItemLocation(final String repository, final String workspace, final String path, final String uuid ) {
85 this(repository, workspace, path, uuid, false);
86 }
87
88
89
90
91
92
93
94
95 public ItemLocation(final String repository, final String workspace, final String path) {
96 this(repository, workspace, path, null, false);
97 }
98
99
100
101
102 public ItemLocation(ItemLocation itemLocation) {
103 this(itemLocation.repository, itemLocation.workspace, itemLocation.path, itemLocation.uuid, itemLocation.isSystemSession);
104 }
105
106
107
108
109
110
111 public String getRepository() {
112 return repository;
113 }
114
115
116
117
118
119
120 public void setRepository(String repository) {
121 this.repository = repository;
122 }
123
124
125
126
127
128
129 public String getWorkspace() {
130 return workspace;
131 }
132
133
134
135
136
137
138 public void setWorkspace(String workspace) {
139 this.workspace = workspace;
140 }
141
142
143
144
145
146
147 public String getPath() {
148 return path;
149 }
150
151
152
153
154
155
156
157 public void setPath(String path) {
158 this.path = path;
159 }
160
161
162
163
164
165
166 public void setUUID(String uuid) {
167 this.uuid = uuid;
168 }
169
170
171
172
173
174
175 public String getUUID() {
176 return uuid;
177 }
178
179
180
181
182
183
184 public void setSystemSession(boolean value) {
185 this.isSystemSession = value;
186 }
187
188
189
190
191
192
193 public boolean isSystemSession() {
194 return this.isSystemSession;
195 }
196
197
198
199
200
201
202
203
204 public static final ItemLocation getItemLocationByItem(final Item item) {
205 Session session = null;
206 try {
207 session = item.getSession();
208 String repository = ((ManageableRepository)session.getRepository()).getConfiguration().getName();
209 String workspace = session.getWorkspace().getName();
210 String path = item.getPath();
211 String uuid = null;
212 try {
213 if (item instanceof Node)
214 uuid = ((Node)item).getUUID();
215 } catch (RepositoryException e) {
216 uuid = null;
217 }
218 boolean isSystemSession = IdentityConstants.SYSTEM.equals(session.getUserID());
219 return new ItemLocation(repository, workspace, path, uuid, isSystemSession);
220 } catch (RepositoryException e) {
221 return null;
222 }
223 }
224
225
226
227
228
229
230
231
232 public static final Item getItemByLocation(final ItemLocation itemLocation) {
233 Session session = null;
234 try {
235 ManageableRepository repository = WCMCoreUtils.getRepository();
236 session = (itemLocation.isSystemSession ?
237 WCMCoreUtils.getSystemSessionProvider() : WCMCoreUtils.getUserSessionProvider())
238 .getSession(itemLocation.getWorkspace(), repository);
239 if (itemLocation.getUUID() != null)
240 return session.getNodeByUUID(itemLocation.getUUID());
241 else {
242 return WCMCoreUtils.getService(NodeFinder.class).getItem(session, itemLocation.getPath());
243 }
244 } catch(PathNotFoundException pne) {
245 return null;
246 } catch (Exception e) {
247 return null;
248 }
249 }
250
251
252
253 }