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 com.fatwire.gst.foundation.facade.assetapi.asset;
18  
19  import java.util.AbstractMap;
20  import java.util.HashSet;
21  import java.util.Iterator;
22  import java.util.Map;
23  import java.util.Set;
24  
25  import com.fatwire.assetapi.def.AttributeDef;
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   * 
38   * @author Dolf.Dijkstra
39   * @since Nov 23,2009
40   * 
41   */
42  public class AssetMapAdapter extends AbstractMap<String, Object> implements Map<String, Object> {
43  
44      private final TemplateAsset delegate;
45      private final Set<String> metaAttributes = new HashSet<String>();
46  
47      public AssetMapAdapter(TemplateAsset delegate) {
48          super();
49          this.delegate = delegate;
50          // meta attributes are part of the assettype definition.
51          for (AttributeDef d : delegate.getAssetTypeDef().getAttributeDefs()) {
52              if (d.isMetaDataAttribute())
53                  metaAttributes.add(d.getName());
54          }
55  
56      }
57  
58      @Override
59      public Set<java.util.Map.Entry<String, Object>> entrySet() {
60          return entries;
61      }
62  
63      private Set<java.util.Map.Entry<String, Object>> entries = new java.util.AbstractSet<java.util.Map.Entry<String, Object>>() {
64  
65          @Override
66          public Iterator<java.util.Map.Entry<String, Object>> iterator() {
67              final Iterator<String> names = delegate.getAttributeNames().iterator();
68              return new Iterator<java.util.Map.Entry<String, Object>>() {
69  
70                  public boolean hasNext() {
71                      return names.hasNext();
72                  }
73  
74                  public java.util.Map.Entry<String, Object> next() {
75                      final String name = names.next();
76                      final Object value = delegate.getAttribute(name);
77                      return new Entry<String, Object>() {
78  
79                          public String getKey() {
80                              return name;
81                          }
82  
83                          public Object getValue() {
84                              return value;
85                          }
86  
87                          public Object setValue(Object value) {
88                              throw new UnsupportedOperationException(
89                                      "Not allowed to set an asset attribute value, asset is immutable");
90                          }
91  
92                      };
93                  }
94  
95                  public void remove() {
96                      throw new UnsupportedOperationException(
97                              "Not allowed to remove an asset attribute, asset is immutable");
98  
99                  }
100 
101             };
102         }
103 
104         @Override
105         public int size() {
106             return delegate.getAttributeNames().size();
107         }
108     };
109 }