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.ItemType;
40 import info.magnolia.cms.core.MgnlNodeType;
41 import info.magnolia.cms.core.NodeData;
42
43 import javax.jcr.Node;
44 import javax.jcr.Property;
45 import javax.jcr.PropertyType;
46 import javax.jcr.RepositoryException;
47
48 import org.apache.commons.lang.StringUtils;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52
53
54
55
56
57 public class Link {
58
59 private static final Logger log = LoggerFactory.getLogger(Link.class);
60
61 private String repository;
62 private String handle;
63 private String uuid;
64 private String nodeDataName;
65 private String extension;
66 private Content node;
67 private NodeData nodeData;
68 private String fileName;
69 private String fallbackHandle;
70 private String anchor;
71 private String parameters;
72
73 private Node jcrNode;
74 private Property property;
75 private String propertyName;
76 private String identifier;
77
78
79
80
81 public Link() {
82 }
83
84
85
86
87
88 public Link(Content content) {
89 setNode(content);
90 try {
91 setRepository(content.getWorkspace().getName());
92 } catch (RepositoryException e) {
93 throw new RuntimeException(e);
94 }
95
96 if (content.isNodeType(ItemType.MIX_REFERENCEABLE)) {
97 setUUID(content.getUUID());
98 }
99 }
100
101
102
103
104
105 public Link(Node node) throws RepositoryException {
106 setJCRNode(node);
107 try {
108 setJCRNode(node);
109 setRepository(node.getSession().getWorkspace().getName());
110 } catch (RepositoryException e) {
111 throw new RuntimeException(e);
112 }
113 if (node.isNodeType(MgnlNodeType.MIX_REFERENCEABLE)) {
114 setIdentifier(node.getIdentifier());
115 }
116 }
117
118 public Link(String repoName, Content parent, NodeData nodedata) {
119 setNode(parent);
120 setRepository(repoName);
121 setNodeData(nodedata);
122 setNodeDataName(nodedata.getName());
123 }
124
125 public String getExtension() {
126 if(StringUtils.isEmpty(this.extension) && this.getNodeData() != null && this.getNodeData().getType() == PropertyType.BINARY){
127 File binary = new File(nodeData);
128 extension = binary.getExtension();
129 }
130 return StringUtils.defaultIfEmpty(this.extension, ServerConfiguration.getInstance().getDefaultExtension());
131 }
132
133 public void setExtension(String extension) {
134 this.extension = extension;
135 }
136
137
138 public String getFileName() {
139 if(StringUtils.isEmpty(this.fileName) && this.getNodeData() != null && this.getNodeData().getType() == PropertyType.BINARY){
140 File binary = new File(nodeData);
141 fileName = binary.getFileName();
142 }
143 return fileName;
144 }
145
146
147 public void setFileName(String fileName) {
148 this.fileName = fileName;
149 }
150
151
152
153
154 public Content getNode() {
155 return this.node;
156 }
157
158
159
160
161 public void setNode(Content node) {
162 this.node = node;
163 }
164
165 public Node getJCRNode() {
166 return this.jcrNode;
167 }
168
169
170 public void setJCRNode(Node jcrNode) {
171 this.jcrNode = jcrNode;
172 }
173
174 public Property getProperty(){
175 return this.property;
176 }
177
178 public void setProperty(Property property){
179 this.property = property;
180 }
181
182
183
184
185 public NodeData getNodeData() {
186 if(this.nodeData == null && StringUtils.isNotEmpty(this.nodeDataName) && this.getNode() != null){
187 this.nodeData = this.getNode().getNodeData(this.nodeDataName);
188 }
189 return this.nodeData;
190 }
191
192
193
194
195 public void setNodeData(NodeData nodeData) {
196 this.nodeData = nodeData;
197 }
198
199 public boolean isEditorBinaryLink(){
200 return getNodeData() != null;
201 }
202
203 public String getPropertyName() {
204 return this.propertyName;
205 }
206
207 public void setPropertyName(String propertyName) {
208 this.propertyName = propertyName;
209 }
210
211 public String getNodeDataName() {
212 return this.nodeDataName;
213 }
214
215 public void setNodeDataName(String nodeDataName) {
216 this.nodeDataName = nodeDataName;
217 }
218
219 public String getHandle() {
220 if(StringUtils.isEmpty(this.handle)){
221 if(getNode() != null){
222 handle = getNode().getHandle();
223 } else {
224 handle = this.getFallbackHandle();
225 }
226 }
227 return this.handle;
228 }
229
230 public void setHandle(String path) {
231 this.handle = path;
232 }
233
234 public String getRepository() {
235 return this.repository;
236 }
237
238 public void setRepository(String repository) {
239 this.repository = repository;
240 }
241
242 public String getIdentifier() throws RepositoryException {
243 if(StringUtils.isEmpty(this.identifier) && this.getJCRNode() != null){
244 this.identifier = this.getJCRNode().getIdentifier();
245 }
246 return this.uuid;
247 }
248
249 public void setIdentifier(String identifier) {
250 this.identifier = identifier;
251 }
252
253 public String getUUID() {
254 if(StringUtils.isEmpty(this.uuid) && this.getNode() != null){
255 this.uuid = this.getNode().getUUID();
256 }
257 return this.uuid;
258 }
259
260 public void setUUID(String uuid) {
261 this.uuid = uuid;
262 }
263
264 public String getFallbackHandle() {
265 return this.fallbackHandle;
266 }
267
268 public void setFallbackHandle(String fallbackPath) {
269 this.fallbackHandle = fallbackPath;
270 }
271
272 public String getAnchor() {
273 return this.anchor;
274 }
275
276 public void setAnchor(String anchor) {
277 this.anchor = anchor;
278 }
279
280 public String getParameters() {
281 return this.parameters;
282 }
283
284 public void setParameters(String parameters) {
285 this.parameters = parameters;
286 }
287 }