View Javadoc
1   /**
2    * This file Copyright (c) 2013-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.asset.model;
35  
36  import java.util.Collection;
37  import java.util.Map;
38  import java.util.Set;
39  
40  import org.apache.commons.lang3.StringUtils;
41  import org.slf4j.Logger;
42  import org.slf4j.LoggerFactory;
43  
44  /**
45   * Map based <b>read only</b> representation of an Asset. This class is for
46   * instance used in template scripts to allow notations like <code>asset.propName</code>. <br>
47   * It read a property with name (key) and return his value. If not found an
48   * empty string is returned. <br>
49   * <b>Binary data will not be part of the AssetMap</b>.<br>
50   * 
51   * <pre>
52   * Try to access the property name from the Asset object.
53   *      key = fileName, returned value is Asset.getFileName().
54   * If not found try to access the value based on the Metadata map.
55   *      key = contributor, returned value is metadataMap.get('contributor').
56   * If not part of the MetadataMap, return an empty String.
57   * </pre>
58   * 
59   * By convenience <br>
60   * We also support asset.metadata.dc or asset.metadata.mgnl call for metadata
61   * property access.
62   */
63  public class AssetMap implements Map<String, Object> {
64  
65      private final static Logger log = LoggerFactory.getLogger(AssetMap.class);
66  
67      private final Map<String, Object> assetMap;
68  
69      public AssetMap(Map<String, Object> assetMap) {
70          this.assetMap = assetMap;
71      }
72  
73      @Override
74      public boolean containsKey(Object key) {
75          // Sanity check.
76          String strKey = convertKey(key);
77          if (!isValidKey(strKey)) {
78              return false;
79          }
80          return assetMap.containsKey(strKey);
81      }
82  
83      @Override
84      public Object get(Object key) {
85          Object value = StringUtils.EMPTY;
86          if (!containsKey(key)) {
87              return value;
88          }
89          String strKey = convertKey(key);
90  
91          return assetMap.get(strKey);
92      }
93  
94      @Override
95      public Set<String> keySet() {
96          return assetMap.keySet();
97      }
98  
99      @Override
100     public int size() {
101         return keySet().size();
102     }
103 
104     private boolean isValidKey(String strKey) {
105         return !StringUtils.isBlank(strKey);
106     }
107 
108     private String convertKey(Object key) {
109         if (key == null) {
110             return null;
111         }
112         try {
113             return (String) key;
114         } catch (ClassCastException e) {
115             log.debug("Invalid key. Expected String, but got {}.", key.getClass().getName());
116         }
117         return null;
118     }
119 
120 
121     @Override
122     public Collection<Object> values() {
123         throw new UnsupportedOperationException("Value collections are not supported");
124     }
125 
126     @Override
127     public Set<java.util.Map.Entry<String, Object>> entrySet() {
128         throw new UnsupportedOperationException("Entry collections are not supported");
129     }
130 
131     @Override
132     public boolean containsValue(Object arg0) {
133         throw new UnsupportedOperationException("Value checks are not supported");
134     }
135 
136     @Override
137     public boolean isEmpty() {
138         // can never be empty because an asset has at least a Name.
139         return false;
140     }
141 
142     @Override
143     public void clear() {
144         // ignore, read only.
145     }
146 
147     @Override
148     public Object put(String arg0, Object arg1) {
149         // ignore, read only.
150         return null;
151     }
152 
153     @Override
154     public void putAll(Map<? extends String, ? extends Object> arg0) {
155         // ignore, read only.
156     }
157 
158     @Override
159     public Object remove(Object arg0) {
160         // ignore, read only.
161         return null;
162     }
163 
164 }
165