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.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   * 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).
50   * @author had
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       * A constructor for undefined links. (i.e linking to a nonexistent page, for instance)
71       */
72      public Link() {
73      }
74  
75      /**
76       * @param content
77       */
78      public Link(Content content) {
79          setNode(content);
80          setRepository(content.getHierarchyManager().getName());
81          // 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
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 }