View Javadoc

1   /*
2    * Copyright 2011 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  package com.fatwire.gst.foundation.mapping;
17  
18  import java.util.Arrays;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import COM.FutureTense.Interfaces.ICS;
23  
24  import com.fatwire.cs.core.db.PreparedStmt;
25  import com.fatwire.cs.core.db.StatementParam;
26  import com.fatwire.gst.foundation.controller.AssetIdWithSite;
27  import com.fatwire.gst.foundation.facade.assetapi.AssetAccessTemplate;
28  import com.fatwire.gst.foundation.facade.sql.Row;
29  import com.fatwire.gst.foundation.facade.sql.SqlHelper;
30  
31  /**
32   * MappingService implementation making use to sql queries to perform fast lookup of mapping values. 
33   * 
34   * @author Dolf Dijkstra
35   * @since Apr 13, 2011
36   */
37  public final class IcsMappingService implements MappingService {
38  
39      private final static PreparedStmt template = new PreparedStmt("SELECT * FROM Template_Map WHERE cs_ownerid=? AND cs_siteid=?",
40              Arrays.asList("Template", "Template_Map"));
41      private final static PreparedStmt element = new PreparedStmt("SELECT * FROM CSElement_Map WHERE cs_ownerid=? AND cs_siteid=?",
42              Arrays.asList("CSElement", "CSElement_Map"));
43  
44      static {
45          template.setElement(0, "Template_Map", "cs_ownerid");
46          template.setElement(1, "Template_Map", "cs_siteid");
47          element.setElement(0, "CSElement_Map", "cs_ownerid");
48          element.setElement(1, "CSElement_Map", "cs_siteid");
49  
50      }
51  
52      private final ICS ics;
53      private final AssetAccessTemplate aat;
54  
55      /**
56       * @param ics
57       */
58      public IcsMappingService(final ICS ics) {
59          super();
60          this.ics = ics;
61          aat = new AssetAccessTemplate(ics);
62      }
63  
64      /*
65       * (non-Javadoc)
66       * 
67       * @see
68       * com.fatwire.gst.foundation.mapping.MappingService#readMapping(com.fatwire
69       * .gst.foundation.controller.AssetIdWithSite)
70       */
71      public Map<String, MappingValue> readMapping(final AssetIdWithSite id) {
72          if ("Template".equals(id.getType())) {
73              return readIt(id, template);
74          } else if ("CSElement".equals(id.getType())) {
75              return readIt(id, element);
76          } else {
77              throw new IllegalArgumentException("Cannot handle " + id.getType());
78          }
79  
80      }
81  
82      private Map<String, MappingValue> readIt(final AssetIdWithSite id, final PreparedStmt stmt) {
83          final StatementParam param = stmt.newParam();
84          param.setLong(0, id.getId());
85          param.setLong(1, aat.readSiteInfo(id.getSite()).getId());
86          final Map<String, MappingValue> map = new HashMap<String, MappingValue>();
87          for (final Row row : SqlHelper.select(ics, stmt, param)) {
88              final String key = row.getString("cs_key");
89  
90              final MappingValue k = new MappingValue(MappingValue.Type.valueOf(row.getString("cs_type").toLowerCase()),
91                      row.getString("cs_value"));
92              map.put(key, k);
93          }
94          return map;
95      }
96  }