1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
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
177
178
179
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 }