View Javadoc

1   /**
2    * This file Copyright (c) 2008-2012 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.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   * DMS DAM Handler.
55   * @author tmiyar
56   * @version $Id$
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