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.link;
35
36 import info.magnolia.cms.beans.config.ServerConfiguration;
37 import info.magnolia.cms.beans.runtime.File;
38 import info.magnolia.cms.core.Content;
39 import info.magnolia.cms.core.NodeData;
40 import info.magnolia.cms.util.ContentUtil;
41 import info.magnolia.context.MgnlContext;
42 import info.magnolia.jcr.RuntimeRepositoryException;
43 import info.magnolia.jcr.util.NodeTypes;
44 import info.magnolia.objectfactory.Components;
45
46 import javax.jcr.Node;
47 import javax.jcr.Property;
48 import javax.jcr.PropertyType;
49 import javax.jcr.RepositoryException;
50
51 import org.apache.commons.lang.StringUtils;
52 import org.apache.jackrabbit.JcrConstants;
53
54
55
56
57
58
59 public class Link {
60
61 private String workspace;
62 private String path;
63 private String uuid;
64 private String extension;
65 private String fileName;
66 private String fallbackPath;
67 private String anchor;
68 private String parameters;
69
70 private Node jcrNode;
71 private Property property;
72 private String propertyName;
73
74
75
76
77 public Link() {
78 }
79
80
81
82
83
84 public Link(Content content) {
85 this(content.getJCRNode());
86 }
87
88
89
90
91 public Link(Node node) {
92 try {
93 setJCRNode(node);
94 setWorkspace(node.getSession().getWorkspace().getName());
95 if (node.isNodeType(JcrConstants.MIX_REFERENCEABLE)) {
96 setUUID(node.getIdentifier());
97 }
98 } catch (RepositoryException e) {
99 throw new RuntimeRepositoryException(e);
100 }
101 }
102
103
104
105
106 public Link(String workspaceName, Content parent, NodeData nodedata) {
107 initLink(workspaceName, parent, nodedata);
108 }
109
110 public Link(Property property) {
111 try{
112 setJCRNode(property.getParent());
113 setWorkspace(property.getSession().getWorkspace().toString());
114 setProperty(property);
115 setPropertyName(property.getName());
116 }catch(RepositoryException e){
117 throw new RuntimeRepositoryException(e);
118 }
119 }
120
121
122
123
124
125
126
127
128 public Link initLink(String workspaceName, Content parent, NodeData nodedata){
129 try {
130 if(nodedata.getType() != PropertyType.BINARY){
131 return new Link(nodedata.getJCRProperty());
132 }
133 return new Link(MgnlContext.getJCRSession(nodedata.getHierarchyManager().getWorkspace().getName()).getNode(nodedata.getHandle()));
134 } catch (RepositoryException e) {
135 throw new RuntimeRepositoryException(e);
136 }
137 }
138
139 public String getExtension() {
140 try{
141 if(StringUtils.isEmpty(this.extension) && this.getJCRNode() != null && this.getJCRNode().isNodeType(NodeTypes.Resource.NAME)){
142 File binary = new File(jcrNode);
143 extension = binary.getExtension();
144 }
145 }catch(RepositoryException e){
146
147 }
148 return StringUtils.defaultIfEmpty(this.extension, Components.getComponent(ServerConfiguration.class).getDefaultExtension());
149 }
150
151 public void setExtension(String extension) {
152 this.extension = extension;
153 }
154
155 public String getFileName() {
156 try{
157 if(StringUtils.isEmpty(this.fileName) && this.getJCRNode() != null && this.getJCRNode().isNodeType(NodeTypes.Resource.NAME)){
158 File binary = new File(jcrNode);
159 fileName = binary.getFileName();
160 }
161 }catch(RepositoryException e){
162
163 }
164 return fileName;
165 }
166
167 public void setFileName(String fileName) {
168 this.fileName = fileName;
169 }
170
171
172
173
174 public Content getNode() {
175 return ContentUtil.asContent(this.jcrNode);
176 }
177
178
179
180
181 public void setNode(Content node) {
182 this.jcrNode = node.getJCRNode();
183 }
184
185 public Node getJCRNode() {
186 return this.jcrNode;
187 }
188
189 public void setJCRNode(Node jcrNode) {
190 this.jcrNode = jcrNode;
191 }
192
193 public Property getProperty() throws LinkException{
194 try{
195 if(this.property == null && StringUtils.isNotEmpty(this.propertyName) && this.getJCRNode() != null && this.getJCRNode().hasProperty(propertyName)){
196 this.property = this.getJCRNode().getProperty(this.propertyName);
197 }
198 }catch(RepositoryException e){
199
200 }
201 return this.property;
202 }
203
204 public void setProperty(Property property){
205 this.property = property;
206 }
207
208
209
210
211 public NodeData getNodeData() {
212 try {
213 if(this.jcrNode != null && this.jcrNode.isNodeType(NodeTypes.Resource.NAME)){
214 return ContentUtil.asContent(jcrNode.getParent()).getNodeData(jcrNode.getName());
215 }
216 else if(this.property == null && StringUtils.isNotEmpty(this.propertyName) && this.getJCRNode() != null){
217 this.property = this.getJCRNode().getProperty(this.propertyName);
218 }
219 if(property == null){
220 return null;
221 }
222 return ContentUtil.asContent(this.property.getParent()).getNodeData(this.propertyName);
223 } catch (RepositoryException e) {
224 throw new RuntimeRepositoryException(e);
225 }
226 }
227
228
229
230
231 public void setNodeData(NodeData nodeData) {
232 if(nodeData != null){
233 try {
234 if(nodeData.getType() != PropertyType.BINARY){
235 this.property = nodeData.getJCRProperty();
236 }else{
237 this.jcrNode = nodeData.getParent().getJCRNode().getNode(nodeData.getName());
238 }
239 } catch (RepositoryException e) {
240 throw new RuntimeRepositoryException(e);
241 }
242 }else{
243 this.property = null;
244 }
245 }
246
247 public boolean isEditorBinaryLink(){
248 try {
249 return getJCRNode().isNodeType(NodeTypes.Resource.NAME);
250 } catch (RepositoryException e) {
251 throw new RuntimeRepositoryException(e);
252 }
253 }
254
255 public String getPropertyName() {
256 return this.propertyName;
257 }
258
259 public void setPropertyName(String propertyName) {
260 this.propertyName = propertyName;
261 }
262
263
264
265
266 public String getNodeDataName() {
267 return this.propertyName;
268 }
269
270
271
272
273 public void setNodeDataName(String nodeDataName) {
274 this.propertyName = nodeDataName;
275 }
276
277
278
279
280 public String getHandle() {
281 return getPath();
282 }
283
284
285
286
287 public void setHandle(String path) {
288 setPath(path);
289 }
290
291 public String getPath() {
292 if(StringUtils.isEmpty(this.path)){
293 if(getJCRNode() != null){
294 try {
295 path = getJCRNode().getPath();
296 } catch (RepositoryException e) {
297 throw new RuntimeRepositoryException(e);
298 }
299 } else {
300 path = this.getFallbackHandle();
301 }
302 }
303 return this.path;
304 }
305
306 public void setPath(String path) {
307 this.path = path;
308 }
309
310
311
312
313 public String getRepository() {
314 return getWorkspace();
315 }
316
317
318
319
320 public void setRepository(String repository) {
321 setWorkspace(repository);
322 }
323
324 public String getWorkspace() {
325 return this.workspace;
326 }
327
328 public void setWorkspace(String workspace) {
329 this.workspace = workspace;
330 }
331
332 public String getUUID() {
333 if(StringUtils.isEmpty(this.uuid) && this.getJCRNode() != null){
334 try {
335 this.uuid = this.getJCRNode().getIdentifier();
336 } catch (RepositoryException e) {
337 throw new RuntimeRepositoryException(e);
338 }
339 }
340 return this.uuid;
341 }
342
343 public void setUUID(String uuid) {
344 this.uuid = uuid;
345 }
346
347
348
349
350 public String getFallbackHandle() {
351 return getFallbackPath();
352 }
353
354
355
356
357 public void setFallbackHandle(String fallbackPath) {
358 setFallbackPath(fallbackPath);
359 }
360
361 public String getFallbackPath() {
362 return this.fallbackPath;
363 }
364
365 public void setFallbackPath(String fallbackPath) {
366 this.fallbackPath = fallbackPath;
367 }
368
369 public String getAnchor() {
370 return this.anchor;
371 }
372
373 public void setAnchor(String anchor) {
374 this.anchor = anchor;
375 }
376
377 public String getParameters() {
378 return this.parameters;
379 }
380
381 public void setParameters(String parameters) {
382 this.parameters = parameters;
383 }
384 }