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.NodeData;
41
42 import javax.jcr.PropertyType;
43
44 import org.apache.commons.lang.StringUtils;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48
49
50
51
52
53 public class Link {
54
55 private static final Logger log = LoggerFactory.getLogger(Link.class);
56
57 private String repository;
58 private String handle;
59 private String uuid;
60 private String nodeDataName;
61 private String extension;
62 private Content node;
63 private NodeData nodeData;
64 private String fileName;
65 private String fallbackHandle;
66 private String anchor;
67 private String parameters;
68
69
70
71
72 public Link() {
73 }
74
75
76
77
78 public Link(Content content) {
79 setNode(content);
80 setRepository(content.getHierarchyManager().getName());
81
82 if (content.isNodeType(ItemType.MIX_REFERENCEABLE)) {
83 setUUID(content.getUUID());
84 }
85 }
86
87 public Link(String repoName, Content parent, NodeData nodedata) {
88 setNode(parent);
89 setRepository(repoName);
90 setNodeData(nodedata);
91 setNodeDataName(nodedata.getName());
92 }
93
94 public boolean isEditorBinaryLink(){
95 return getNodeData() != null;
96 }
97
98 public String getExtension() {
99 if(StringUtils.isEmpty(this.extension) && this.getNodeData() != null && this.getNodeData().getType() == PropertyType.BINARY){
100 File binary = new File(nodeData);
101 extension = binary.getExtension();
102 }
103 return StringUtils.defaultIfEmpty(this.extension, ServerConfiguration.getInstance().getDefaultExtension());
104 }
105
106
107 public void setExtension(String extension) {
108 this.extension = extension;
109 }
110
111
112 public String getFileName() {
113 if(StringUtils.isEmpty(this.fileName) && this.getNodeData() != null && this.getNodeData().getType() == PropertyType.BINARY){
114 File binary = new File(nodeData);
115 fileName = binary.getFileName();
116 }
117 return fileName;
118 }
119
120
121 public void setFileName(String fileName) {
122 this.fileName = fileName;
123 }
124
125 public Content getNode() {
126 return this.node;
127 }
128
129
130 public void setNode(Content node) {
131 this.node = node;
132 }
133
134 public NodeData getNodeData() {
135 if(this.nodeData == null && StringUtils.isNotEmpty(this.nodeDataName) && this.getNode() != null){
136 this.nodeData = this.getNode().getNodeData(this.nodeDataName);
137 }
138 return this.nodeData;
139 }
140
141 public void setNodeData(NodeData nodeData) {
142 this.nodeData = nodeData;
143 }
144
145 public String getNodeDataName() {
146 return this.nodeDataName;
147 }
148
149 public void setNodeDataName(String nodeDataName) {
150 this.nodeDataName = nodeDataName;
151 }
152
153 public String getHandle() {
154 if(StringUtils.isEmpty(this.handle)){
155 if(getNode() != null){
156 handle = getNode().getHandle();
157 } else {
158 handle = this.getFallbackHandle();
159 }
160 }
161 return this.handle;
162 }
163
164 public void setHandle(String path) {
165 this.handle = path;
166 }
167
168 public String getRepository() {
169 return this.repository;
170 }
171
172 public void setRepository(String repository) {
173 this.repository = repository;
174 }
175
176 public String getUUID() {
177 if(StringUtils.isEmpty(this.uuid) && this.getNode() != null){
178 this.uuid = this.getNode().getUUID();
179 }
180 return this.uuid;
181 }
182
183 public void setUUID(String uuid) {
184 this.uuid = uuid;
185 }
186
187 public String getFallbackHandle() {
188 return this.fallbackHandle;
189 }
190
191 public void setFallbackHandle(String fallbackPath) {
192 this.fallbackHandle = fallbackPath;
193 }
194
195 public String getAnchor() {
196 return this.anchor;
197 }
198
199 public void setAnchor(String anchor) {
200 this.anchor = anchor;
201 }
202
203 public String getParameters() {
204 return this.parameters;
205 }
206
207 public void setParameters(String parameters) {
208 this.parameters = parameters;
209 }
210 }