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