View Javadoc

1   /*
2    * Copyright 2010 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.wra;
17  
18  import java.util.Arrays;
19  import java.util.Collections;
20  import java.util.List;
21  
22  import COM.FutureTense.Interfaces.ICS;
23  import COM.FutureTense.Interfaces.IList;
24  
25  import com.fatwire.assetapi.data.AssetId;
26  import com.fatwire.cs.core.db.PreparedStmt;
27  import com.fatwire.cs.core.db.StatementParam;
28  import com.fatwire.gst.foundation.IListUtils;
29  import com.fatwire.gst.foundation.facade.assetapi.DirectSqlAccessTools;
30  import com.fatwire.gst.foundation.facade.ics.ICSFactory;
31  import com.fatwire.gst.foundation.facade.sql.IListIterable;
32  import com.fatwire.gst.foundation.facade.sql.Row;
33  import com.fatwire.gst.foundation.facade.sql.SqlHelper;
34  
35  import org.apache.commons.lang.StringUtils;
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  
39  /**
40   * Backdoor implementation of WraCoreFieldDao that does not utilize any Asset
41   * APIs. This class should be used sparingly and may result in some
42   * dependencies, that would ordinarily be recorded, being skipped.
43   * <p/>
44   * User: Tony Field Date: 2011-05-06
45   * 
46   * @author Dolf Dijkstra
47   */
48  public class WraCoreFieldApiBypassDao extends AssetApiWraCoreFieldDao {
49      public static WraCoreFieldApiBypassDao getBackdoorInstance(ICS ics) {
50          if (ics == null) {
51              ics = ICSFactory.getOrCreateICS();
52          }
53  
54          Object o = ics.GetObj(WraCoreFieldApiBypassDao.class.getName());
55          if (o == null) {
56              o = new WraCoreFieldApiBypassDao(ics);
57              ics.SetObj(WraCoreFieldApiBypassDao.class.getName(), o);
58          }
59          return (WraCoreFieldApiBypassDao) o;
60      }
61  
62      private final ICS ics;
63      private final DirectSqlAccessTools directSqlAccessTools;
64  
65      private WraCoreFieldApiBypassDao(final ICS ics) {
66          super(ics);
67          this.ics = ics;
68          directSqlAccessTools = new DirectSqlAccessTools(ics);
69      }
70  
71      private static final Log LOG = LogFactory.getLog(WraCoreFieldApiBypassDao.class);
72  
73      /**
74       * Method to test whether or not an asset is web-referenceable. todo: low
75       * priority: optimize as this will be called at runtime (assest api incache
76       * will mitigate the performance issue)
77       * 
78       * @param id asset ID to check
79       * @return true if the asset is a valid web-referenceable asset, false if it
80       *         is not
81       */
82      @Override
83      public boolean isWebReferenceable(final AssetId id) {
84  
85          if (directSqlAccessTools.isFlex(id)) {
86              if (!isWraEnabledFlexAssetType(id)) {
87                  return false;
88              }
89          } else {
90              if (!isWraEnabledBasicAssetType(id)) {
91                  return false;
92              }
93  
94          }
95          // type is wra, now lookup the asset data
96          try {
97              final WebReferenceableAsset wra = getWra(id);
98              final boolean b = StringUtils.isNotBlank(wra.getPath());
99              if (LOG.isTraceEnabled()) {
100                 LOG.trace("Asset " + id + (b ? " is " : " is not ")
101                         + "web-referenceable, as determinted by the presence of a path attribute.");
102             }
103             return b;
104         } catch (final RuntimeException e) {
105             if (LOG.isTraceEnabled()) {
106                 LOG.trace("Asset " + id + " is not web-referenceable: " + e, e);
107             }
108             return false;
109         }
110     }
111 
112     private boolean isWraEnabledFlexAssetType(final AssetId id) {
113         // TODO:medium implements proper check
114         return true;
115     }
116 
117     /**
118      * Return a web referenceable asset bean given an input id. Required fields
119      * must be set or an exception is thrown.
120      * 
121      * @param id asset id
122      * @return WebReferenceableAsset, never null
123      * @see #isWebReferenceable(AssetId)
124      */
125     @Override
126     public WebReferenceableAsset getWra(final AssetId id) {
127         if (directSqlAccessTools.isFlex(id)) {
128             // todo: medium: optimize as this is very inefficient for flex
129             // assets
130             final PreparedStmt basicFields = new PreparedStmt(
131                     "SELECT id,name,description,subtype,status,path,template,startdate,enddate" + " FROM "
132                             + id.getType() + " WHERE id = ?", Collections.singletonList(id.getType()));
133             basicFields.setElement(0, id.getType(), "id");
134 
135             final StatementParam param = basicFields.newParam();
136             param.setLong(0, id.getId());
137             final Row row = SqlHelper.selectSingle(ics, basicFields, param);
138 
139             final WraBeanImpl wra = new WraBeanImpl();
140             wra.setId(id);
141             wra.setName(row.getString("name"));
142             wra.setDescription(row.getString("description"));
143             wra.setSubtype(row.getString("subtype"));
144             wra.setPath(row.getString("path"));
145             wra.setTemplate(row.getString("template"));
146             if (StringUtils.isNotBlank(row.getString("startdate"))) {
147                 wra.setStartDate(row.getDate("startdate"));
148             }
149             if (StringUtils.isNotBlank(row.getString("enddate"))) {
150                 wra.setEndDate(row.getDate("enddate"));
151             }
152 
153             wra.setMetaTitle(directSqlAccessTools.getFlexAttributeValue(id, "metatitle"));
154             wra.setMetaDescription(directSqlAccessTools.getFlexAttributeValue(id, "metadescription"));
155             wra.setMetaKeyword(directSqlAccessTools.getFlexAttributeValue(id, "metakeywords"));
156             wra.setH1Title(directSqlAccessTools.getFlexAttributeValue(id, "h1title"));
157             wra.setLinkText(directSqlAccessTools.getFlexAttributeValue(id, "linktext"));
158 
159             return wra;
160         } else {
161 
162             final PreparedStmt basicFields = new PreparedStmt(
163                     "SELECT id,name,description,subtype,status,path,template,startdate,enddate,"
164                             + "metatitle,metadescription,metakeyword,h1title,linktext FROM " + id.getType()
165                             + " WHERE id = ?", Collections.singletonList(id.getType()));
166             basicFields.setElement(0, id.getType(), "id");
167 
168             final StatementParam param = basicFields.newParam();
169             param.setLong(0, id.getId());
170             final Row row = SqlHelper.selectSingle(ics, basicFields, param);
171 
172             final WraBeanImpl wra = new WraBeanImpl();
173             wra.setId(id);
174             wra.setName(row.getString("name"));
175             wra.setDescription(row.getString("description"));
176             wra.setSubtype(row.getString("subtype"));
177             wra.setMetaTitle(row.getString("metatitle"));
178             wra.setMetaDescription(row.getString("metadescription"));
179             wra.setMetaKeyword(row.getString("metakeywords"));
180             wra.setH1Title(row.getString("h1title"));
181             wra.setLinkText(row.getString("linktext"));
182             wra.setPath(row.getString("path"));
183             wra.setTemplate(row.getString("template"));
184             if (StringUtils.isNotBlank(row.getString("startdate"))) {
185                 wra.setStartDate(row.getDate("startdate"));
186             }
187             if (StringUtils.isNotBlank(row.getString("enddate"))) {
188                 wra.setEndDate(row.getDate("enddate"));
189             }
190             return wra;
191         }
192     }
193 
194     /**
195      * Checks if the table definition for a basic asset has all the wra fields.
196      * 
197      * @param id
198      */
199     private boolean isWraEnabledBasicAssetType(final AssetId id) {
200         boolean wraTable;
201         final String listname = IListUtils.generateRandomListName();
202         final IList list = ics.CatalogDef(id.getType(), listname, new StringBuffer());
203         ics.RegisterList(listname, null);
204         final List<String> attr = Arrays.asList(WRA_ATTRIBUTE_NAMES);
205         int count = 0;
206         for (final Row row : new IListIterable(list)) {
207             /*
208              * "COLNAME" "COLTYPE" "COLSIZE" "KEY"
209              */
210             if (attr.contains(row.getString("COLNAME").toLowerCase())) {
211                 count++;
212             }
213         }
214         wraTable = count == attr.size();// all wra attributes are found in the
215         // table def.
216         if (LOG.isTraceEnabled()) {
217             LOG.trace("Asset " + id + (wraTable ? " is " : " is not ")
218                     + "web-referenceable, as determinted by the table definition.");
219         }
220 
221         return wraTable;
222     }
223 
224     @Override
225     public boolean isVanityAsset(AssetId id) {
226         try {
227             final PreparedStmt basicFields = new PreparedStmt("SELECT path FROM " + id.getType() + " WHERE id = ?",
228                     Collections.singletonList(id.getType()));
229             basicFields.setElement(0, id.getType(), "id");
230 
231             final StatementParam param = basicFields.newParam();
232             param.setLong(0, id.getId());
233             final Row row = SqlHelper.selectSingle(ics, basicFields, param);
234             if (row == null)
235                 return false;
236             return StringUtils.isNotBlank(row.getString("path"));
237         } catch (RuntimeException e) {
238             return false;
239         }
240     }
241 
242 }