1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
40
41
42
43
44
45
46
47 public class WraCoreFieldApiBypassDao extends AssetApiWraCoreFieldDao {
48
49
50
51
52
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
79
80
81
82
83
84
85
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
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
121 return true;
122 }
123
124
125
126
127
128
129
130
131
132
133 @Override
134 public WebReferenceableAsset getWra(final AssetId id) {
135 if (directSqlAccessTools.isFlex(id)) {
136
137
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
211
212
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
225
226 if (attr.contains(row.getString("COLNAME").toLowerCase())) {
227 count++;
228 }
229 }
230 wraTable = count == attr.size();
231
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 }