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.module.samples.model;
35
36 import info.magnolia.cms.beans.config.ContentRepository;
37 import info.magnolia.cms.core.DefaultContent;
38 import info.magnolia.context.MgnlContext;
39 import info.magnolia.jcr.util.ContentMap;
40 import info.magnolia.jcr.util.SessionUtil;
41 import info.magnolia.link.LinkUtil;
42 import info.magnolia.rendering.model.RenderingModel;
43 import info.magnolia.rendering.model.RenderingModelImpl;
44 import info.magnolia.rendering.template.RenderableDefinition;
45
46 import javax.jcr.Node;
47 import javax.jcr.PathNotFoundException;
48 import javax.jcr.RepositoryException;
49 import javax.jcr.Session;
50 import javax.jcr.ValueFormatException;
51
52
53
54
55
56
57 public class InternalLinkModel extends RenderingModelImpl<RenderableDefinition> {
58
59
60 public InternalLinkModel(Node content, RenderableDefinition definition, RenderingModel<?> parent) {
61 super(content, definition, parent);
62 }
63
64 public ContentMap getTarget() throws ValueFormatException, PathNotFoundException, RepositoryException{
65 return new LinkItem(content, "target").getNode();
66 }
67
68 public String getTargetLink() throws ValueFormatException, PathNotFoundException, RepositoryException{
69 return new LinkItem(content, "target").getLink();
70 }
71
72
73
74
75 public class LinkItem {
76
77 private ContentMap targetContentMap = null;
78 private Node targetNode = null;
79 private String targetLink = null;
80
81
82 public LinkItem(Node componentNode, String propertyName) throws ValueFormatException, PathNotFoundException, RepositoryException{
83 if(componentNode.hasProperty(propertyName)){
84 String targetValue = componentNode.getProperty(propertyName).getString();
85 Session session = MgnlContext.getJCRSession(ContentRepository.WEBSITE);
86
87
88 if(targetValue.startsWith("/")){
89 targetNode = SessionUtil.getNode(ContentRepository.WEBSITE, targetValue);
90 }
91 else {
92
93 targetNode = session.getNodeByIdentifier(targetValue);
94 }
95 if(targetNode != null) {
96 targetContentMap = new ContentMap(targetNode);
97 targetLink = LinkUtil.createLink(new DefaultContent(targetNode));
98 }
99 }
100
101 }
102
103 public String getLink() {
104 return targetLink;
105 }
106
107 public ContentMap getNode() {
108 return targetContentMap;
109 }
110 }
111
112 }