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.Collections;
19 import java.util.Date;
20
21 import COM.FutureTense.Interfaces.ICS;
22
23 import com.fatwire.assetapi.data.AssetData;
24 import com.fatwire.assetapi.data.AssetId;
25 import com.fatwire.cs.core.db.PreparedStmt;
26 import com.fatwire.cs.core.db.StatementParam;
27 import com.fatwire.gst.foundation.facade.assetapi.AssetDataUtils;
28 import com.fatwire.gst.foundation.facade.assetapi.asset.TemplateAsset;
29 import com.fatwire.gst.foundation.facade.runtag.asset.Children;
30 import com.fatwire.gst.foundation.facade.sql.Row;
31 import com.fatwire.gst.foundation.facade.sql.SqlHelper;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36 import static COM.FutureTense.Interfaces.Utilities.goodString;
37
38
39
40
41
42
43
44
45
46 public class AssetApiAliasCoreFieldDao implements AliasCoreFieldDao {
47
48 public static final String TARGET_ASSOCIATION_NAME = "target";
49 private final ICS ics;
50 private final WraCoreFieldDao wraCoreFieldDao;
51
52 public AssetApiAliasCoreFieldDao(ICS ics, WraCoreFieldDao wraCoreFieldDao) {
53 this.ics = ics;
54 this.wraCoreFieldDao = wraCoreFieldDao;
55 }
56
57 private static final Log LOG = LogFactory.getLog(AssetApiAliasCoreFieldDao.class);
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 public AssetData getAsAssetData(AssetId id) {
77 return AssetDataUtils.getAssetData(ics, id);
78
79
80
81 }
82
83
84
85
86
87
88
89
90 public boolean isAlias(AssetId id) {
91 if (Alias.ALIAS_ASSET_TYPE_NAME.equals(id.getType()) == false)
92 return false;
93 try {
94 getAlias(id);
95 return true;
96 } catch (RuntimeException e) {
97 return false;
98 }
99 }
100
101
102
103
104
105
106
107
108
109 public Alias getAlias(AssetId id) {
110 TemplateAsset alias = new TemplateAsset(getAsAssetData(id));
111 AssetId target = Children.getOptionalSingleAssociation(ics, id.getType(), Long.toString(id.getId()),
112 TARGET_ASSOCIATION_NAME);
113 if (target == null) {
114 String targetUrl = alias.asString("target_url");
115 if (targetUrl != null && targetUrl.length() == 0)
116 throw new IllegalStateException(
117 "Asset is not an alias because it has neither a target_url attribute nor a target named association: "
118 + id);
119 LOG.trace("Alias " + id + " refers to an external URL");
120
121 AliasBeanImpl o = new AliasBeanImpl();
122
123 o.setId(id);
124 o.setName(alias.asString("name"));
125 o.setDescription(alias.asString("description"));
126 o.setSubtype("GSTAlias");
127 o.setStatus(alias.asString("status"));
128 o.setStartDate(alias.asDate("startdate"));
129 o.setEndDate(alias.asDate("enddate"));
130 String linktext = alias.asString("linktext");
131 o.setLinkText(goodString(linktext) ? linktext : o.getH1Title());
132
133
134 if (alias.isAttribute("h1title"))
135 o.setH1Title(alias.asString("h1title"));
136 if (alias.isAttribute("metatitle"))
137 o.setMetaTitle(alias.asString("metatitle"));
138 if (alias.isAttribute("metadescription"))
139 o.setMetaDescription(alias.asString("metadescription"));
140 if (alias.isAttribute("metakeyword"))
141 o.setMetaKeyword(alias.asString("metakeyword"));
142 o.setPath(alias.asString("path"));
143 o.setTemplate(alias.asString("template"));
144
145
146 o.setTargetUrl(targetUrl);
147 if (alias.isAttribute("popup"))
148 o.setPopup(alias.asString("popup"));
149 if (alias.isAttribute("linkimage"))
150 o.setLinkImage(alias.asAssetId("linkimage"));
151 return o;
152
153 } else {
154 if (!wraCoreFieldDao.isWebReferenceable(target)) {
155 throw new IllegalStateException(
156 "Asset is not a valid alias because it refers to a target asset that is not web-referenceable. Alias:"
157 + id + ", target:" + target);
158 }
159 WebReferenceableAsset wra = wraCoreFieldDao.getWra(target);
160 LOG.trace("Alias " + id + " refers to another wra asset: " + target);
161
162 AliasBeanImpl o = new AliasBeanImpl();
163
164 o.setId(id);
165 if (LOG.isTraceEnabled())
166 LOG.trace("alias name: " + alias.asString("name") + ", wra name:" + wra.getName());
167 o.setName(alias.asString("name"));
168 if (LOG.isTraceEnabled())
169 LOG.trace("alias description: " + alias.asString("description") + ", wra description:"
170 + wra.getDescription());
171 o.setDescription(alias.asString("description"));
172 o.setSubtype("GSTAlias");
173 if (LOG.isTraceEnabled())
174 LOG.trace("alias status: " + alias.asString("status") + ", wra status:" + wra.getStatus());
175 o.setStatus(alias.asString("status"));
176
177 Date d = alias.asDate("startdate");
178 o.setStartDate(d == null ? wra.getStartDate() : d);
179 d = alias.asDate("enddate");
180 o.setEndDate(d == null ? wra.getEndDate() : d);
181
182 if (alias.isAttribute("h1title")) {
183 String s = alias.asString("h1title");
184 o.setH1Title(s == null ? wra.getH1Title() : s);
185 }
186
187 if (alias.isAttribute("linktext")) {
188 String s = alias.asString("linktext");
189 o.setLinkText(s == null ? wra.getLinkText() : s);
190 }
191
192 if (alias.isAttribute("metatitle")) {
193 String s = alias.asString("metatitle");
194 if (!goodString(s))
195 s = wra.getMetaTitle();
196 o.setMetaTitle(s);
197 }
198
199 if (alias.isAttribute("metadescription")) {
200 String s = alias.asString("metadescription");
201 if (!goodString(s))
202 s = wra.getMetaDescription();
203 o.setMetaDescription(s);
204 }
205
206 if (alias.isAttribute("metakeyword")) {
207 String s = alias.asString("metakeyword");
208 if (!goodString(s))
209 s = wra.getMetaKeyword();
210 o.setMetaKeyword(s);
211 }
212
213 String s = alias.asString("path");
214 if (LOG.isTraceEnabled())
215 LOG.trace("alias path: " + s + ", wra path:" + wra.getPath());
216 if (!goodString(s))
217 s = wra.getPath();
218 o.setPath(s);
219
220 s = alias.asString("template");
221 if (LOG.isTraceEnabled())
222 LOG.trace("alias template: " + s + ", wra template:" + wra.getTemplate());
223 if (!goodString(s))
224 s = wra.getTemplate();
225 o.setTemplate(s);
226
227
228 o.setTarget(target);
229 if (alias.isAttribute("popup"))
230 o.setPopup(alias.asString("popup"));
231 if (alias.isAttribute("linkimage"))
232 o.setLinkImage(alias.asAssetId("linkimage"));
233
234 return o;
235 }
236
237 }
238
239 private static final String ASSETPUBLICATION_QRY = "SELECT p.name from Publication p, AssetPublication ap "
240 + "WHERE ap.assettype = ? " + "AND ap.assetid = ? " + "AND ap.pubid=p.id";
241 static final PreparedStmt AP_STMT = new PreparedStmt(ASSETPUBLICATION_QRY,
242 Collections.singletonList("AssetPublication"));
243
244
245
246
247
248 static {
249 AP_STMT.setElement(0, "AssetPublication", "assettype");
250 AP_STMT.setElement(1, "AssetPublication", "assetid");
251 }
252
253 public String resolveSite(String c, String cid) {
254 final StatementParam param = AP_STMT.newParam();
255 param.setString(0, c);
256 param.setLong(1, Long.parseLong(cid));
257 String result = null;
258 for (Row pubid : SqlHelper.select(ics, AP_STMT, param)) {
259 if (result != null) {
260 LOG.warn("Found asset "
261 + c
262 + ":"
263 + cid
264 + " in more than one publication. It should not be shared; aliases are to be used for cross-site sharing. Controller will use first site found: "
265 + result);
266 } else {
267 result = pubid.getString("name");
268 }
269 }
270 return result;
271 }
272
273 }