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   * @author had
50   *
51   */
52  public class Link {
53  
54      private static final Logger log = LoggerFactory.getLogger(Link.class);
55  
56      private String repository;
57      private String handle;
58      private String uuid;
59      private String nodeDataName;
60      private String extension;
61      private Content node;
62      private NodeData nodeData;
63      private String fileName;
64      private String fallbackHandle;
65      private String anchor;
66      private String parameters;
67  
68      /**
69       * A constructor for undefined links. (i.e linking to a nonexistent page, for instance)
70       */
71      public Link() {
72      }
73  
74      /**
75       * @param content
76       */
77      public Link(Content content) {
78          setNode(content);
79          setRepository(content.getHierarchyManager().getName());
80          // 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
81          if (content.isNodeType(ItemType.MIX_REFERENCEABLE)) {
82              setUUID(content.getUUID());
83          }
84      }
85  
86      public Link(String repoName, Content parent, NodeData nodedata) {
87          setNode(parent);
88          setRepository(repoName);
89          setNodeData(nodedata);
90          setNodeDataName(nodedata.getName());
91      }
92  
93      public String getExtension() {
94          if(StringUtils.isEmpty(this.extension) && this.getNodeData() != null && this.getNodeData().getType() == PropertyType.BINARY){
95              File binary = new File(nodeData);
96              extension = binary.getExtension();
97          }
98          return StringUtils.defaultIfEmpty(this.extension, ServerConfiguration.getInstance().getDefaultExtension());
99      }
100 
101 
102     public void setExtension(String extension) {
103         this.extension = extension;
104     }
105 
106 
107     public String getFileName() {
108         if(StringUtils.isEmpty(this.fileName) && this.getNodeData() != null && this.getNodeData().getType() == PropertyType.BINARY){
109             File binary = new File(nodeData);
110             fileName = binary.getFileName();
111         }
112         return fileName;
113     }
114 
115 
116     public void setFileName(String fileName) {
117         this.fileName = fileName;
118     }
119 
120     public Content getNode() {
121         return this.node;
122     }
123 
124 
125     public void setNode(Content node) {
126         this.node = node;
127     }
128 
129     public NodeData getNodeData() {
130         if(this.nodeData == null && StringUtils.isNotEmpty(this.nodeDataName) && this.getNode() != null){
131             this.nodeData = this.getNode().getNodeData(this.nodeDataName);
132         }
133         return this.nodeData;
134     }
135 
136     public void setNodeData(NodeData nodeData) {
137         this.nodeData = nodeData;
138     }
139 
140     public String getNodeDataName() {
141         return this.nodeDataName;
142     }
143 
144     public void setNodeDataName(String nodeDataName) {
145         this.nodeDataName = nodeDataName;
146     }
147 
148     public String getHandle() {
149         if(StringUtils.isEmpty(this.handle)){
150             if(getNode() != null){
151                 handle = getNode().getHandle();
152             } else {
153                 handle = this.getFallbackHandle();
154             }
155         }
156         return this.handle;
157     }
158 
159     public void setHandle(String path) {
160         this.handle = path;
161     }
162 
163     public String getRepository() {
164         return this.repository;
165     }
166 
167     public void setRepository(String repository) {
168         this.repository = repository;
169     }
170 
171     public String getUUID() {
172         if(StringUtils.isEmpty(this.uuid) && this.getNode() != null){
173             this.uuid = this.getNode().getUUID();
174         }
175         return this.uuid;
176     }
177 
178     public void setUUID(String uuid) {
179         this.uuid = uuid;
180     }
181 
182     public String getFallbackHandle() {
183         return this.fallbackHandle;
184     }
185 
186     public void setFallbackHandle(String fallbackPath) {
187         this.fallbackHandle = fallbackPath;
188     }
189 
190     public String getAnchor() {
191         return this.anchor;
192     }
193 
194     public void setAnchor(String anchor) {
195         this.anchor = anchor;
196     }
197 
198     public String getParameters() {
199         return this.parameters;
200     }
201 
202     public void setParameters(String parameters) {
203         this.parameters = parameters;
204     }
205 }