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  import java.util.LinkedList;
20  import java.util.List;
21  
22  import COM.FutureTense.Interfaces.ICS;
23  import COM.FutureTense.Util.ftMessage;
24  
25  import com.fatwire.assetapi.data.AssetId;
26  import com.fatwire.cs.core.db.PreparedStmt;
27  import com.fatwire.cs.core.db.StatementParam;
28  import com.fatwire.gst.foundation.facade.cm.AddRow;
29  import com.fatwire.gst.foundation.facade.cm.ReplaceRow;
30  import com.fatwire.gst.foundation.facade.logging.LogUtil;
31  import com.fatwire.gst.foundation.facade.sql.Row;
32  import com.fatwire.gst.foundation.facade.sql.SqlHelper;
33  import com.fatwire.gst.foundation.facade.sql.table.TableColumn.Type;
34  import com.fatwire.gst.foundation.facade.sql.table.TableCreator;
35  import com.fatwire.gst.foundation.facade.sql.table.TableDef;
36  import com.fatwire.gst.foundation.vwebroot.VirtualWebroot;
37  import com.fatwire.gst.foundation.wra.SimpleWra;
38  
39  import org.apache.commons.lang.StringUtils;
40  import org.apache.commons.logging.Log;
41  
42  /**
43   * WraPathTranslationService that is backed by the GSTUrlRegistry table.
44   * 
45   * @author Dolf Dijkstra
46   * @since November 1, 2011
47   */
48  public class UrlRegistryDaoImpl implements UrlRegistryDao {
49  
50      private static final String OPT_SITE = "opt_site";
51  
52      private static final String OPT_URL_PATH = "opt_url_path";
53  
54      private static final String OPT_VWEBROOT = "opt_vwebroot";
55  
56      private static final String OPT_DEPTH = "opt_depth";
57  
58      private static final String ENDDATE = "enddate";
59  
60      private static final String STARTDATE = "startdate";
61  
62      private static final String PATH = "path";
63  
64      private static final String ASSETTYPE = "assettype";
65  
66      private static final String ASSETID = "assetid";
67  
68      private static final String ID = "id";
69  
70      private static final Log LOG = LogUtil.getLog(UrlRegistryDaoImpl.class);
71  
72      private final ICS ics;
73      private static final String URLREG_TABLE = "GSTUrlRegistry";
74      public static String TABLE_ACL_LIST = ""; // no ACLs because events are
75                                                // anonymous
76  
77      public UrlRegistryDaoImpl(final ICS ics) {
78          this.ics = ics;
79      }
80  
81      public void install() {
82          final TableDef def = new TableDef(URLREG_TABLE, TABLE_ACL_LIST, ftMessage.objecttbl);
83  
84          def.addColumn(ID, Type.ccbigint, true).setNullable(false);
85          def.addColumn(PATH, Type.ccvarchar).setLength(4000).setNullable(false);
86          def.addColumn(ASSETTYPE, Type.ccvarchar).setLength(255).setNullable(false);
87          def.addColumn(ASSETID, Type.ccbigint).setNullable(false);
88          def.addColumn(STARTDATE, Type.ccdatetime).setNullable(true);
89          def.addColumn(ENDDATE, Type.ccdatetime).setNullable(true);
90          def.addColumn(OPT_VWEBROOT, Type.ccvarchar).setLength(255).setNullable(true);
91          def.addColumn(OPT_URL_PATH, Type.ccvarchar).setLength(4000).setNullable(true);
92          def.addColumn(OPT_DEPTH, Type.ccinteger).setNullable(true);
93          def.addColumn(OPT_SITE, Type.ccvarchar).setLength(255).setNullable(true);
94  
95          new TableCreator(ics).createTable(def);
96      }
97  
98      public boolean isInstalled() {
99          return SqlHelper.tableExists(ics, URLREG_TABLE);
100     }
101 
102     private static final PreparedStmt REGISTRY_SELECT = new PreparedStmt("SELECT * FROM " + URLREG_TABLE
103             + " WHERE opt_vwebroot=? AND opt_url_path=? ORDER BY startdate,enddate",
104             Collections.singletonList(URLREG_TABLE));
105     private static final PreparedStmt REGISTRY_SELECT_ID = new PreparedStmt("SELECT * FROM " + URLREG_TABLE
106             + " WHERE assettype=? AND assetid=?", Collections.singletonList(URLREG_TABLE));
107 
108     static {
109         REGISTRY_SELECT.setElement(0, URLREG_TABLE, OPT_VWEBROOT);
110         REGISTRY_SELECT.setElement(1, URLREG_TABLE, OPT_URL_PATH);
111         REGISTRY_SELECT_ID.setElement(0, URLREG_TABLE, ASSETTYPE);
112         REGISTRY_SELECT_ID.setElement(1, URLREG_TABLE, ASSETID);
113 
114     }
115 
116     @Override
117     public List<VanityUrl> resolveAsset(final String virtual_webroot, final String url_path) {
118         final StatementParam param = REGISTRY_SELECT.newParam();
119         param.setString(0, virtual_webroot);
120         param.setString(1, url_path);
121         final List<VanityUrl> l = new LinkedList<VanityUrl>();
122         for (final Row row : SqlHelper.select(ics, REGISTRY_SELECT, param)) {
123             final VanityUrl url = new VanityUrl();
124             url.setId(row.getLong(ID));
125             url.setAssetid(row.getLong(ASSETID));
126             url.setAssettype(row.getString(ASSETTYPE));
127             url.setPath(row.getString(PATH));
128             url.setStartdate(row.getDate(STARTDATE));
129             url.setEnddate(row.getDate(ENDDATE));
130             url.setOpt_depth(Integer.parseInt(row.getString(OPT_DEPTH)));
131             url.setOpt_vwebroot(row.getString(OPT_VWEBROOT));
132             url.setOpt_url_path(row.getString(OPT_URL_PATH));
133             url.setOpt_site(row.getString(OPT_SITE));
134             l.add(url);
135         }
136 
137         return l;
138     }
139 
140     @Override
141     public void add(final SimpleWra wra, final VirtualWebroot vw, final String site) {
142         if (LOG.isTraceEnabled()) {
143             LOG.trace("addAsset(AssetId) called for asset " + wra);
144         }
145         final AssetId asset = wra.getId();
146 
147         if (vw != null) {
148             final String vwebroot = vw.getEnvironmentVirtualWebroot();
149 
150             final String urlpath = wra.getPath().substring(vw.getMasterVirtualWebroot().length());
151             final int depth = StringUtils.countMatches(urlpath, "/");
152 
153             AddRow addRow = new AddRow(URLREG_TABLE);
154             addRow.set(ID, ics.genID(true));
155             addRow.set(PATH, wra.getPath());
156             addRow.set(ASSETTYPE, asset.getType());
157             addRow.set(ASSETID, Long.toString(asset.getId()));
158             addRow.set(STARTDATE, wra.getStartDate());
159             addRow.set(ENDDATE, wra.getEndDate());
160             addRow.set(OPT_VWEBROOT, vwebroot);
161             addRow.set(OPT_URL_PATH, urlpath);
162             addRow.set(OPT_DEPTH, Integer.toString(depth));
163             addRow.set(OPT_SITE, site);
164             addRow.execute(ics);
165 
166             if (LOG.isDebugEnabled()) {
167                 LOG.debug("Added WRA " + asset + " to url registry");
168             }
169 
170         } else {
171             if (LOG.isTraceEnabled()) {
172                 LOG.trace("Did not add WRA " + asset + " to url registry because no valid virtual webroot was found");
173             }
174         }
175 
176     }
177 
178     @Override
179     public void update(final VanityUrl url) {
180         final ReplaceRow u = new ReplaceRow(URLREG_TABLE);
181         u.set(ID, url.getId());
182         u.set(PATH, url.getPath());
183         u.set(ASSETTYPE, url.getAssettype());
184         u.set(ASSETID, url.getAssetid());
185         u.set(STARTDATE, url.getStartdate());
186         u.set(ENDDATE, url.getEnddate());
187         u.set(OPT_VWEBROOT, url.getOpt_vwebroot());
188         u.set(OPT_URL_PATH, url.getOpt_url_path());
189         u.set(OPT_DEPTH, url.getOpt_depth());
190         u.set(OPT_SITE, url.getOpt_site());
191         u.execute(ics);
192     }
193 
194     @Override
195     public VanityUrl read(final AssetId id) {
196         final StatementParam param = REGISTRY_SELECT_ID.newParam();
197         param.setString(0, id.getType());
198         param.setLong(1, id.getId());
199         final Row row = SqlHelper.selectSingle(ics, REGISTRY_SELECT_ID, param);
200         if (row == null) {
201             return null;
202         }
203         final VanityUrl url = new VanityUrl();
204         url.setId(row.getLong(ID));
205         url.setAssetid(row.getLong(ASSETID));
206         url.setAssettype(row.getString(ASSETTYPE));
207         url.setPath(row.getString(PATH));
208         url.setStartdate(row.getDate(STARTDATE));
209         url.setEnddate(row.getDate(ENDDATE));
210         url.setOpt_depth(Integer.parseInt(row.getString(OPT_DEPTH)));
211         url.setOpt_vwebroot(row.getString(OPT_VWEBROOT));
212         url.setOpt_url_path(row.getString(OPT_URL_PATH));
213         url.setOpt_site(row.getString(OPT_SITE));
214         return url;
215 
216     }
217 
218     @Override
219     public void delete(final AssetId id) {
220         final StatementParam param = REGISTRY_SELECT_ID.newParam();
221         param.setString(0, id.getType());
222         param.setLong(1, id.getId());
223         final Row row = SqlHelper.selectSingle(ics, REGISTRY_SELECT_ID, param);
224         if (row != null) {
225             //only delete if a record is found to prevent trashing.
226             deleteAsset_(id);
227         }
228     }
229 
230     private void deleteAsset_(final AssetId id) {
231         if (LOG.isTraceEnabled()) {
232             LOG.trace("Attempting to delete asset " + id + " from " + URLREG_TABLE);
233         }
234         SqlHelper.execute(ics, URLREG_TABLE, "DELETE FROM " + URLREG_TABLE + " WHERE assettype = '" + id.getType()
235                 + "' AND assetid = " + id.getId());
236         if (LOG.isDebugEnabled()) {
237             LOG.debug("Asset " + id + " is now removed from " + URLREG_TABLE);
238         }
239     }
240 
241     public void clear() {
242         LOG.debug("Attempting to purge all rows from "+URLREG_TABLE);
243         SqlHelper.execute(ics, URLREG_TABLE, "DELETE FROM " + URLREG_TABLE);
244         LOG.info("Purged all rows from "+URLREG_TABLE);
245     }
246 }