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.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   * Dao for dealing with core fields in an alias. Aliases may override fields in
40   * their target WRA if it points to another asset. Aliases may also refer to
41   * external URLs.
42   * 
43   * @author Tony Field
44   * @since Jul 21, 2010
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       * Return an AssetData object containing the core fields found in an alias
61       * asset.
62       * <p/>
63       * Also includes selected metadata fields:
64       * <ul>
65       * <li>id</li>
66       * <li>name</li>
67       * <li>subtype</li>
68       * <li>startdate</li>
69       * <li>enddate</li>
70       * <li>status</li>
71       * </ul>
72       * 
73       * @param id id of alias asset
74       * @return AssetData containing core fields for Alias asset
75       */
76      public AssetData getAsAssetData(AssetId id) {
77          return AssetDataUtils.getAssetData(ics, id); // load all instead of
78                                                       // specific
79          // fields, because we do not
80          // know which fields are set.
81      }
82  
83      /**
84       * Method to test whether or not an asset is an Alias. todo: low priority:
85       * optimize as this will be called at runtime
86       * 
87       * @param id asset ID to check
88       * @return true if the asset is a valid Alias asset, false if it is not
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      * Return an alias asset bean given an input id. Required fields must be set
103      * or an exception is thrown.
104      * 
105      * @param id asset id
106      * @return Alias
107      * @see #isAlias
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             // Wra fields
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             // these fields really don't make much sense in an external link...
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             // Alias fields
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             // Wra fields
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             // Alias fields
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")); // todo: low
243                                                             // priority:
244                                                             // determine
245     // why publication
246     // cannot fit there.
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 }