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 boolean isEditorBinaryLink(){
147 return getNodeData() != null;
148 }
149
150 public String getNodeDataName() {
151 return this.nodeDataName;
152 }
153
154 public void setNodeDataName(String nodeDataName) {
155 this.nodeDataName = nodeDataName;
156 }
157
158 public String getHandle() {
159 if(StringUtils.isEmpty(this.handle)){
160 if(getNode() != null){
161 handle = getNode().getHandle();
162 } else {
163 handle = this.getFallbackHandle();
164 }
165 }
166 return this.handle;
167 }
168
169 public void setHandle(String path) {
170 this.handle = path;
171 }
172
173 public String getRepository() {
174 return this.repository;
175 }
176
177 public void setRepository(String repository) {
178 this.repository = repository;
179 }
180
181 public String getUUID() {
182 if(StringUtils.isEmpty(this.uuid) && this.getNode() != null){
183 this.uuid = this.getNode().getUUID();
184 }
185 return this.uuid;
186 }
187
188 public void setUUID(String uuid) {
189 this.uuid = uuid;
190 }
191
192 public String getFallbackHandle() {
193 return this.fallbackHandle;
194 }
195
196 public void setFallbackHandle(String fallbackPath) {
197 this.fallbackHandle = fallbackPath;
198 }
199
200 public String getAnchor() {
201 return this.anchor;
202 }
203
204 public void setAnchor(String anchor) {
205 this.anchor = anchor;
206 }
207
208 public String getParameters() {
209 return this.parameters;
210 }
211
212 public void setParameters(String parameters) {
213 this.parameters = parameters;
214 }
215 }