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.module.templatingkit.dam.handlers;
35
36 import info.magnolia.cms.core.Content;
37 import info.magnolia.cms.core.HierarchyManager;
38 import info.magnolia.cms.core.MgnlNodeType;
39 import info.magnolia.cms.core.NodeData;
40 import info.magnolia.context.MgnlContext;
41 import info.magnolia.dam.asset.Asset;
42 import info.magnolia.dam.asset.AssetNotFoundException;
43 import info.magnolia.dam.asset.DamException;
44 import info.magnolia.module.templatingkit.dam.assets.AssetWithOverridingMetaData;
45 import info.magnolia.module.templatingkit.dam.assets.DMSAsset;
46 import info.magnolia.module.templatingkit.dam.assets.MetaDataOnlyAsset;
47 import info.magnolia.templating.functions.TemplatingFunctions;
48
49 import javax.inject.Inject;
50 import javax.jcr.ItemNotFoundException;
51 import javax.jcr.RepositoryException;
52
53
54
55
56
57
58 public class DMSDAMHandler extends AbstractInternalContentDAMHandler {
59
60
61 private final TemplatingFunctions templatingFunctions;
62
63 @Inject
64 public DMSDAMHandler(TemplatingFunctions templatingFunctions){
65 this.templatingFunctions = templatingFunctions;
66 }
67
68 @Override
69 public Asset getAsset(Content node, String nodeDataPrefix) throws DamException {
70 final Asset asset = getAssetByKey(getKey(node, nodeDataPrefix));
71 try {
72 return new AssetWithOverridingMetaData(asset, new MetaDataOnlyAsset(node, nodeDataPrefix, templatingFunctions));
73 }
74 catch (RepositoryException e) {
75 throw new DamException("Can't read meta data from content for [" + asset + "]", e);
76 }
77 }
78
79 @Override
80 public Asset getAssetByKey(String key) throws AssetNotFoundException, DamException{
81
82 final HierarchyManager hm = MgnlContext.getHierarchyManager("dam");
83 try {
84 try {
85 Content node = hm.getContentByUUID(key);
86 if(node.hasMixin(MgnlNodeType.MIX_DELETED)){
87 throw new AssetNotFoundException("Asset with key [" + key + "] is marked as deleted");
88 }
89 final NodeData binaryNodeData = node.getNodeData("jcr:content");
90 return new DMSAsset(this, node, binaryNodeData, templatingFunctions);
91 }
92 catch (ItemNotFoundException e) {
93 throw new AssetNotFoundException("No asset found for key [" + key + "]");
94 }
95 }
96 catch (RepositoryException e) {
97 throw new DamException("Can't create asset for key " + key, e);
98 }
99 }
100
101 protected String getKey(Content node, String nodeDataPrefix) throws AssetNotFoundException, DamException {
102 final String name = getNodeDataName(node, nodeDataPrefix);
103 final NodeData nodeData = node.getNodeData(name);
104 if(!nodeData.isExist()){
105 throw new AssetNotFoundException("Can't find asset for " + node.getHandle() + "/" + nodeDataPrefix);
106 }
107 return nodeData.getString();
108 }
109
110 }
111