View Javadoc
1   /**
2    * This file Copyright (c) 2014-2017 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.dam.jcr;
35  
36  import info.magnolia.dam.api.Asset;
37  import info.magnolia.dam.api.metadata.AssetMetadata;
38  import info.magnolia.dam.api.metadata.DublinCore;
39  import info.magnolia.dam.api.metadata.MagnoliaAssetMetadata;
40  import info.magnolia.jcr.RuntimeRepositoryException;
41  import info.magnolia.jcr.util.NodeTypes;
42  import info.magnolia.jcr.util.PropertyUtil;
43  import info.magnolia.objectfactory.Components;
44  
45  import java.io.InputStream;
46  import java.util.Calendar;
47  
48  import javax.jcr.Node;
49  import javax.jcr.Property;
50  import javax.jcr.RepositoryException;
51  
52  /**
53   * JCR implementation of a {@link Asset} definition.
54   */
55  public class JcrAsset extends AbstractJcrItem implements Asset {
56  
57      public JcrAsset(JcrAssetProvider assetProvider, Node itemNode) {
58          super(assetProvider, itemNode);
59      }
60  
61      @Override
62      public String getLink() {
63          return getAssetProvider().getLink(this);
64      }
65  
66      @Override
67      public boolean isAsset() {
68          return true;
69      }
70  
71      @Override
72      public String getTitle() {
73          return PropertyUtil.getString(getNode(), AssetNodeTypes.Asset.TITLE, "");
74      }
75  
76      @Override
77      public String getSubject() {
78          return getPropertyValue(getNode(), AssetNodeTypes.Asset.SUBJECT, "");
79      }
80  
81      private String getPropertyValue(Node node, String propertyName, String defaultValue) {
82          try {
83              if (node.hasProperty(propertyName)) {
84                  Property property = node.getProperty(propertyName);
85                  if (property.isMultiple()) {
86                      return property.getValues()[0].getString();
87                  }
88                  return property.getValue().getString();
89              }
90              return defaultValue;
91          } catch (RepositoryException e) {
92              throw new RuntimeRepositoryException(e);
93          }
94      }
95  
96      @Override
97      public String getDescription() {
98          return PropertyUtil.getString(getNode(), AssetNodeTypes.Asset.DESCRIPTION, "");
99      }
100 
101     @Override
102     public String getCaption() {
103         return PropertyUtil.getString(getNode(), AssetNodeTypes.Asset.CAPTION, "");
104     }
105 
106     @Override
107     public String getLanguage() {
108         return PropertyUtil.getString(getNode(), AssetNodeTypes.Asset.LANGUAGE, "");
109     }
110 
111     @Override
112     public String getCopyright() {
113         return PropertyUtil.getString(getNode(), AssetNodeTypes.Asset.COPYRIGHT, "");
114     }
115 
116     @Override
117     public String getComment() {
118         return PropertyUtil.getString(getNode(), AssetNodeTypes.Asset.COMMENT, "");
119     }
120 
121     @Override
122     public Calendar getCreated() {
123         try {
124             return NodeTypes.Created.getCreated(getNode());
125         } catch (RepositoryException e) {
126             throw new RuntimeRepositoryException(e);
127         }
128     }
129 
130     @Override
131     public Calendar getLastModified() {
132         try {
133             return NodeTypes.LastModified.getLastModified(getNode());
134         } catch (RepositoryException e) {
135             throw new RuntimeRepositoryException(e);
136         }
137     }
138 
139     @Override
140     public <M extends AssetMetadata> M getMetadata(Class<M> metaDataType) {
141         if (!getAssetProvider().supports(metaDataType)) {
142             throw new IllegalArgumentException("Unsupported metaData Type requested " + metaDataType != null ? metaDataType.getName() : "null");
143         }
144         return Components.newInstance(metaDataType, getNode(), getItemKey());
145     }
146 
147     @Override
148     public String getMimeType() {
149         return PropertyUtil.getString(AssetNodeTypes.AssetResource.getResourceNodeFromAsset(getNode()), AssetNodeTypes.AssetResource.MIMETYPE, "");
150     }
151 
152     @Override
153     public long getFileSize() {
154         return PropertyUtil.getLong(AssetNodeTypes.AssetResource.getResourceNodeFromAsset(getNode()), AssetNodeTypes.AssetResource.SIZE, 0l);
155     }
156 
157     @Override
158     public InputStream getContentStream() {
159         try {
160             Node node = AssetNodeTypes.AssetResource.getResourceNodeFromAsset(getNode());
161             if (node != null) {
162                 return node.getProperty(AssetNodeTypes.AssetResource.DATA).getBinary().getStream();
163             }
164         } catch (RepositoryException e) {
165             throw new RuntimeRepositoryException(e);
166         }
167         return null;
168     }
169 
170     @Override
171     public String getFileName() {
172         return PropertyUtil.getString(AssetNodeTypes.AssetResource.getResourceNodeFromAsset(getNode()), AssetNodeTypes.AssetResource.FILENAME, "");
173     }
174 
175     /**
176      * JcrAsset specific implementation that let you access a property value linked to an asset node.
177      * This required an open session.
178      * 
179      * @return property value Object if the property exist or null otherwise.
180      */
181     public Object getProperty(String propertyName) {
182         return PropertyUtil.getPropertyValueObject(getNode(), propertyName);
183     }
184 
185     @Override
186     public <M extends AssetMetadata> boolean supports(Class<M> metaDataType) {
187         return (MagnoliaAssetMetadata.class.isAssignableFrom(metaDataType) || DublinCore.class.isAssignableFrom(metaDataType));
188     }
189 }