View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 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 String getExtension() {
95          if(StringUtils.isEmpty(this.extension) && this.getNodeData() != null && this.getNodeData().getType() == PropertyType.BINARY){
96              File binary = new File(nodeData);
97              extension = binary.getExtension();
98          }
99          return StringUtils.defaultIfEmpty(this.extension, ServerConfiguration.getInstance().getDefaultExtension());
100     }
101 
102 
103     public void setExtension(String extension) {
104         this.extension = extension;
105     }
106 
107 
108     public String getFileName() {
109         if(StringUtils.isEmpty(this.fileName) && this.getNodeData() != null && this.getNodeData().getType() == PropertyType.BINARY){
110             File binary = new File(nodeData);
111             fileName = binary.getFileName();
112         }
113         return fileName;
114     }
115 
116 
117     public void setFileName(String fileName) {
118         this.fileName = fileName;
119     }
120 
121     public Content getNode() {
122         return this.node;
123     }
124 
125 
126     public void setNode(Content node) {
127         this.node = node;
128     }
129 
130     public NodeData getNodeData() {
131         if(this.nodeData == null && StringUtils.isNotEmpty(this.nodeDataName) && this.getNode() != null){
132             this.nodeData = this.getNode().getNodeData(this.nodeDataName);
133         }
134         return this.nodeData;
135     }
136 
137     public void setNodeData(NodeData nodeData) {
138         this.nodeData = nodeData;
139     }
140 
141     public String getNodeDataName() {
142         return this.nodeDataName;
143     }
144 
145     public void setNodeDataName(String nodeDataName) {
146         this.nodeDataName = nodeDataName;
147     }
148 
149     public String getHandle() {
150         if(StringUtils.isEmpty(this.handle)){
151             if(getNode() != null){
152                 handle = getNode().getHandle();
153             } else {
154                 handle = this.getFallbackHandle();
155             }
156         }
157         return this.handle;
158     }
159 
160     public void setHandle(String path) {
161         this.handle = path;
162     }
163 
164     public String getRepository() {
165         return this.repository;
166     }
167 
168     public void setRepository(String repository) {
169         this.repository = repository;
170     }
171 
172     public String getUUID() {
173         if(StringUtils.isEmpty(this.uuid) && this.getNode() != null){
174             this.uuid = this.getNode().getUUID();
175         }
176         return this.uuid;
177     }
178 
179     public void setUUID(String uuid) {
180         this.uuid = uuid;
181     }
182 
183     public String getFallbackHandle() {
184         return this.fallbackHandle;
185     }
186 
187     public void setFallbackHandle(String fallbackPath) {
188         this.fallbackHandle = fallbackPath;
189     }
190 
191     public String getAnchor() {
192         return this.anchor;
193     }
194 
195     public void setAnchor(String anchor) {
196         this.anchor = anchor;
197     }
198 
199     public String getParameters() {
200         return this.parameters;
201     }
202 
203     public void setParameters(String parameters) {
204         this.parameters = parameters;
205     }
206 }