View Javadoc

1   /**
2    * This file Copyright (c) 2003-2012 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.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   * 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).
51   * @author had
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       * A constructor for undefined links. (i.e linking to a nonexistent page, for instance)
72       */
73      public Link() {
74      }
75  
76      /**
77       * @param content
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          // 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
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 }