View Javadoc
1   /*
2    * Copyright 2008 FatWire Corporation. All Rights Reserved.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package tools.gsf.facade.assetapi.asset;
18  
19  import com.fatwire.assetapi.def.AttributeDef;
20  
21  import java.util.AbstractMap;
22  import java.util.HashSet;
23  import java.util.Iterator;
24  import java.util.Map;
25  import java.util.Set;
26  
27  /**
28   * This class adapts an <tt>TemplateAsset</tt> into a <tt>Map</tt>. The Map is
29   * convenient in expression languages like JSP EL.
30   * <p>
31   * This class uses lazy-loading, so if used in a View layer, it might open
32   * connections to the database.
33   * <p>
34   * In case of naming conflicts with flex attribute names and meta attribute
35   * names the meta attributes take precedent.
36   *
37   * @author Dolf.Dijkstra
38   * @since Nov 23,2009
39   */
40  public class AssetMapAdapter extends AbstractMap<String, Object> implements Map<String, Object> {
41  
42      private final TemplateAsset delegate;
43      private final Set<String> metaAttributes = new HashSet<String>();
44  
45      public AssetMapAdapter(TemplateAsset delegate) {
46          super();
47          this.delegate = delegate;
48          // meta attributes are part of the assettype definition.
49          for (AttributeDef d : delegate.getAssetTypeDef().getAttributeDefs()) {
50              if (d.isMetaDataAttribute()) {
51                  metaAttributes.add(d.getName());
52              }
53          }
54  
55      }
56  
57      @Override
58      public Set<java.util.Map.Entry<String, Object>> entrySet() {
59          return entries;
60      }
61  
62      private Set<java.util.Map.Entry<String, Object>> entries = new java.util.AbstractSet<java.util.Map.Entry<String, Object>>() {
63  
64          @Override
65          public Iterator<java.util.Map.Entry<String, Object>> iterator() {
66              final Iterator<String> names = delegate.getAttributeNames().iterator();
67              return new Iterator<java.util.Map.Entry<String, Object>>() {
68  
69                  public boolean hasNext() {
70                      return names.hasNext();
71                  }
72  
73                  public java.util.Map.Entry<String, Object> next() {
74                      final String name = names.next();
75                      final Object value = delegate.getAttribute(name);
76                      return new Entry<String, Object>() {
77  
78                          public String getKey() {
79                              return name;
80                          }
81  
82                          public Object getValue() {
83                              return value;
84                          }
85  
86                          public Object setValue(Object value) {
87                              throw new UnsupportedOperationException(
88                                      "Not allowed to set an asset attribute value, asset is immutable");
89                          }
90  
91                      };
92                  }
93  
94                  public void remove() {
95                      throw new UnsupportedOperationException(
96                              "Not allowed to remove an asset attribute, asset is immutable");
97  
98                  }
99  
100             };
101         }
102 
103         @Override
104         public int size() {
105             return delegate.getAttributeNames().size();
106         }
107     };
108 }