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