View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * Representation of the link to a content in Magnolia CMS. The target for the link might be a content (page, paragraph) or the node data (binary file).
54   * @author had
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       * A constructor for undefined links. (i.e linking to a nonexistent page, for instance)
80       */
81      public Link() {
82      }
83  
84      /**
85       * @param content
86       * @deprecated since 5.0
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          // should we have Content.hasUUID()? ... DefaultContent hides the fact that some nodes might not have UUIDs (we can link to other content then just the one in website
96          if (content.isNodeType(ItemType.MIX_REFERENCEABLE)) {
97              setUUID(content.getUUID());
98          }
99      }
100     
101     /**
102      * @param node
103      * @throws RepositoryException 
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      * @deprecated since 5.0
153      */
154     public Content getNode() {
155         return this.node;
156     }
157 
158     /**
159      * @deprecated since 5.0
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      * @deprecated since 5.0
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      * @deprecated since 5.0
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 }