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