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.url.db;
17  
18  import java.util.Collections;
19  
20  import COM.FutureTense.Interfaces.ICS;
21  
22  import com.fatwire.assetapi.data.AssetId;
23  import com.fatwire.cs.core.db.PreparedStmt;
24  import com.fatwire.cs.core.db.StatementParam;
25  import com.fatwire.gst.foundation.facade.logging.LogUtil;
26  import com.fatwire.gst.foundation.facade.sql.Row;
27  import com.fatwire.gst.foundation.facade.sql.SqlHelper;
28  import com.fatwire.gst.foundation.wra.SimpleWRADao;
29  import com.fatwire.gst.foundation.wra.SimpleWra;
30  
31  import org.apache.commons.logging.Log;
32  
33  /**
34   * @author Dolf Dijkstra
35   * @since November 1, 2011
36   * 
37   */
38  public class DbSimpleWRADao implements SimpleWRADao {
39  
40      private static final Log LOG = LogUtil.getLog(DbSimpleWRADao.class);
41  
42      private final ICS ics;
43  
44      public DbSimpleWRADao(final ICS ics) {
45          super();
46          this.ics = ics;
47      }
48  
49      @Override
50      public SimpleWra getWra(final AssetId id) {
51  
52          final PreparedStmt ASSET_SQL = new PreparedStmt("SELECT id, path, template, startdate, enddate FROM "
53                  + id.getType() + " WHERE id=? AND status !='VO'", Collections.singletonList(id.getType()));
54  
55          ASSET_SQL.setElement(0, id.getType(), "id");
56          final StatementParam param = ASSET_SQL.newParam();
57          param.setLong(0, id.getId());
58          final Row row = SqlHelper.selectSingle(ics, ASSET_SQL, param);
59          if (row == null) {
60              return null;
61          }
62          return new SimpleWra(row, id);
63  
64      }
65  
66      private static final String ASSETPUBLICATION_QRY = "SELECT p.name from Publication p, AssetPublication ap "
67              + "WHERE ap.assettype = ? " + "AND ap.assetid = ? " + "AND ap.pubid=p.id";
68      static final PreparedStmt AP_STMT = new PreparedStmt(ASSETPUBLICATION_QRY,
69              Collections.singletonList("AssetPublication")); // todo: low
70                                                              // priority:
71      // determine
72      // why publication
73      // cannot fit there.
74  
75      static {
76          AP_STMT.setElement(0, "AssetPublication", "assettype");
77          AP_STMT.setElement(1, "AssetPublication", "assetid");
78      }
79  
80      @Override
81      public String resolveSite(final AssetId id) {
82          final StatementParam param = AP_STMT.newParam();
83          param.setString(0, id.getType());
84          param.setLong(1, id.getId());
85          String result = null;
86          for (final Row pubid : SqlHelper.select(ics, AP_STMT, param)) {
87              if (result != null) {
88                  LOG.warn("Found asset "
89                          + id
90                          + " 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: "
91                          + result);
92              } else {
93                  result = pubid.getString("name");
94              }
95          }
96          return result;
97      }
98  
99  }