Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/RuntimeCollective/sitemap/bean/SimpleSiteLocation.java


1   /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/sitemap/bean/SimpleSiteLocation.java,v 1.19 2003/09/30 15:12:59 joe Exp $
2    * $Revision: 1.19 $
3    * $Date: 2003/09/30 15:12:59 $
4    *
5    * ====================================================================
6    *
7    * Josephine : http://www.runtime-collective.com/josephine/index.html
8    *
9    * Copyright (C) 2003 Runtime Collective
10   * 
11   * This product includes software developed by the
12   * Apache Software Foundation (http://www.apache.org/).
13   *
14   * This library is free software; you can redistribute it and/or
15   * modify it under the terms of the GNU Lesser General Public
16   * License as published by the Free Software Foundation; either
17   * version 2.1 of the License, or (at your option) any later version.
18   *
19   * This library is distributed in the hope that it will be useful,
20   * but WITHOUT ANY WARRANTY; without even the implied warranty of
21   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22   * Lesser General Public License for more details.
23   *
24   * You should have received a copy of the GNU Lesser General Public
25   * License along with this library; if not, write to the Free Software
26   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27   *
28   */
29  
30  package com.RuntimeCollective.sitemap.bean;
31  
32  import com.RuntimeCollective.sitemap.SitemapException;
33  import com.RuntimeCollective.sitemap.bean.SimplePublishable;
34  import com.RuntimeCollective.sitemap.bean.SiteLocation;
35  import com.RuntimeCollective.sitemap.bean.SiteLocationLink;
36  import com.RuntimeCollective.webapps.EntityBeanStore;
37  import com.RuntimeCollective.webapps.RuntimeParameters;
38  import com.RuntimeCollective.webapps.RuntimeDataSource;
39  import com.RuntimeCollective.webapps.RuntimeParameters;
40  import com.RuntimeCollective.webapps.bean.DateBean;
41  import com.RuntimeCollective.webapps.IndexedEntityBeanStore;
42  
43  import java.net.MalformedURLException;
44  import java.net.URL;
45  import java.util.Date;
46  import java.util.Iterator;
47  import java.sql.SQLException;
48  
49  /**
50   * A commodity class which implements all the basic functionality of
51   * SiteLocation, so we can spare duplicating lots of simple code.
52   * <p>
53   * It reuses SimplePublishable.
54   * <p>
55   * This class should be subclassed to add more functionality.
56   *
57   * @version $Id: SimpleSiteLocation.java,v 1.19 2003/09/30 15:12:59 joe Exp $
58   */
59  public class SimpleSiteLocation extends SimplePublishable implements SiteLocation {
60  
61      /** The name of the database table for this bean type. */
62      public static final String DATABASE_TABLE = "sitemap_sitelocation";
63  
64      // EntityBean specific
65  
66      /** Save this bean to the database. */
67      public void save() {
68  
69    super.save();
70  
71    try {
72  
73        // save the template : NOT ANYMORE, template should be already saved
74        //if (this.TheTemplateId != -1) {
75        //RuntimeParameters.getStore().save("com.RuntimeCollective.sitemap.bean.Template", TheTemplateId);
76        //}
77        
78        // write SiteLocation data
79        String tid = null;
80        if (this.TheTemplateId != -1)
81      tid = ""+this.TheTemplateId;
82        
83        RuntimeDataSource.save(id, DATABASE_TABLE, new String[] { "name", "template_id", "weburlstub" }, new Object[] { Name, tid, WebURLStub } );
84  
85    } catch (SQLException e) {
86        throw new SitemapException("SimpleSiteLocation could not save() : "+e);
87    }
88      }
89  
90      /** Delete this bean from the database. */
91      public void delete() {
92  
93    try {
94        // delete the links to this SiteLocation
95        deleteLinks();
96  
97        RuntimeDataSource.update((new StringBuffer(55)).append("delete from ").append(DATABASE_TABLE).append(" where id = "+this.id).toString());
98        
99    } catch (SQLException e) {
100       throw new SitemapException("SimpleSiteLocation could not delete() : "+e);
101   }
102 
103   super.delete();
104     }
105 
106     /** Delete the Links to this SiteLocation */
107     public void deleteLinks() throws SQLException {
108 
109   // delete the links to this SiteLocation
110   EntityBeanStore ebs = RuntimeParameters.getStore();
111   Iterator allSiteLocationLinks = RuntimeParameters.getStore().getAll("com.RuntimeCollective.sitemap.bean.SiteLocationLink");
112   SiteLocationLink sllink;
113   while (allSiteLocationLinks.hasNext()) {
114       sllink = (SiteLocationLink) allSiteLocationLinks.next();
115       if (sllink.getSiteLocationId() == this.getId()) {
116     ebs.delete("com.RuntimeCollective.sitemap.bean.SiteLocationLink", sllink.getId());
117       }
118   }
119     }
120 
121 
122 
123     // SiteLocation specific
124 
125     /** The name */
126     protected String Name;
127 
128     /** Set the name */
129     public void setName(String name) {
130   this.Name = name;
131     }
132 
133     /** Get the name */
134     public String getName() {
135   return this.Name;
136     }
137 
138     /** The URL stub (e.g. "ecommerce" for the ecommerce node) */
139     protected String WebURLStub;
140 
141     /** Set the URL stub */
142     public void setWebURLStub(String stub) {
143   this.WebURLStub = stub;
144     }
145 
146     /** Get the URL stub */
147     public String getWebURLStub() {
148   return this.WebURLStub;
149     }
150 
151     /** The Template used by this SiteLocation */
152     protected int TheTemplateId;
153 
154     /** Set the template */
155     public void setTheTemplate(Template template) {
156   if (template != null)
157       this.TheTemplateId = template.getId();
158   else
159       this.TheTemplateId = -1;
160     }
161 
162     /** Get the template */
163     public Template getTheTemplate() {
164   if (this.TheTemplateId != -1)
165       return (Template) RuntimeParameters.getStore().get("com.RuntimeCollective.sitemap.bean.Template", this.TheTemplateId);
166   else
167       return null;
168     }
169 
170     /** Get the whole URL to get to this SiteLocation
171      * This is very basic and while be overriden by subclasses.
172      *
173      * @return A URL made of the pageRoot and the WebURLStub
174      */
175     public URL getURL() {
176   try {
177       return new URL((new StringBuffer(80)).append("http://").append(RuntimeParameters.get("pageRoot")).append("/").append(this.getWebURLStub()).toString());
178   } catch (MalformedURLException e) {
179       RuntimeParameters.logDebug(this, "Problem while getting the URL of SimpleSiteLocation "+this.getId()+" : "+e);
180       return null;
181   }
182     }
183 
184     /** Get the opening <a> Tag to this SiteLocation, including href and target attributes */
185     public String getLinkOpenTag() {
186   StringBuffer sb = new StringBuffer(100);
187 
188   sb.append("<a href=\"http://");
189   sb.append(RuntimeParameters.get("pageRoot"));
190   sb.append("/viewPage.jsp?id=");
191   sb.append(""+this.getId());
192   sb.append("\"");
193   if (this.getTheTemplate().getLinkToNewWindow())
194       sb.append(" target=\"_new\"");
195   sb.append(">");
196 
197   return sb.toString();
198     }
199 
200     /** Get the whole breadcrumb to get to this SiteLocation (in plain text)
201      * This is very basic and while be overriden by subclasses.
202      *
203      * @return A String
204      */
205     public String getBreadcrumb() {
206   return this.getName();
207     }
208 
209 
210     /** Get the title
211      *
212      * @return A String which can be used as the title of this object.
213      */
214     public String getTitle() {
215   return "SimpleSiteLocation";
216     }
217 
218     /** Get the description
219      *
220      * @return A String which can be used as the description of this object.
221      */
222     public String getDescription() {
223   return "None";
224     }
225 
226     /** Is this SiteLocation is of interest, ie should we record hits on it ? */
227     protected boolean OfInterest;
228 
229     /** Set whether this SiteLocation is of interest
230      * @param a boolean
231      */
232     public void setOfInterest(boolean bool) {
233   this.OfInterest = bool;
234     }
235 
236     /** Get whether this SiteLocation is of interest
237      * @return a boolean
238      */
239     public boolean getOfInterest() {
240   return this.OfInterest;
241     }
242 
243     /** Get whether this SiteLocation is of interest.
244      * Redirects to getOfInterest(boolean).
245      * @return a boolean
246      */
247     public boolean isOfInterest() {
248   return this.getOfInterest();
249     }
250 
251 
252 
253 
254     // SimplePublishable methods
255 
256     /**
257      * Mark this SiteLocation as archived.
258      * This is normal SimplePublishable behaviour, except we also delete the SiteLocationLink
259      * to this SiteLocation.
260      */
261     public void markAsArchived() {
262 
263   super.markAsArchived();
264 
265   // delete the links to this SiteLocation
266   try {
267       Iterator allSiteLocationLinks = RuntimeParameters.getStore().getAll("com.RuntimeCollective.sitemap.bean.SiteLocationLink");
268 
269       SiteLocationLink sllink;
270       while (allSiteLocationLinks.hasNext()) {
271     sllink = (SiteLocationLink) allSiteLocationLinks.next();
272     //RuntimeParameters.logDebug(this, " Deleting Link "+link.getId()+" ?");
273     if ((sllink.getSiteLocation() != null) && (sllink.getSiteLocation().getId() == this.getId()))
274         RuntimeParameters.getStore().delete("com.RuntimeCollective.sitemap.bean.SiteLocationLink", sllink.getId());
275       }
276   } catch (SQLException e) {
277       RuntimeParameters.logDebug(this, " *** SimpleSiteLocation.markAsArchived could not RuntimeParameters.getStore().getAll(\"com.RuntimeCollective.sitemap.bean.SiteLocationLink\") : "+e);
278   }
279     }
280 
281 
282 
283     // SimpleSiteLocation specific
284 
285     /** Construct a new blank SimpleSiteLocation, giving it a new unique ID. */
286     public SimpleSiteLocation() throws SQLException {
287 
288   super();
289 
290   this.setName("");
291   this.TheTemplateId = -1;
292     }
293   
294     /** Get a current SimpleSiteLocation from the RuntimeDataSource, given an id.
295      * @param id ID of the SimpleSiteLocation.
296      * @exception SQLException is thrown if no such SimpleSiteLocation exists.
297      */
298     public SimpleSiteLocation(int id) throws SQLException {
299    
300   super(id);
301 
302   // it's a shame this has to be here, but there is no alternative apparently
303   // do we use the flat table?
304   try {
305       if ((this instanceof SiteNode) && ("true".equals(RuntimeParameters.get(SiteNode.USE_FLAT_PARAM))))
306     return;
307   } catch (RuntimeException e) {
308       // param not set, constinue as normal
309   }
310 
311   Object[] result = RuntimeDataSource.queryRow("select sl.name, sl.template_id, sl.weburlstub from "+DATABASE_TABLE+" sl where sl.id = "+id);
312   if (result.length != 3) {
313       throw new SQLException("Cannot load SimpleSiteLocation "+id+": "+result.length+" fields found in "+DATABASE_TABLE+" instead of 3.");
314   }
315 
316   if (result[0] != null) {
317       this.Name = result[0].toString();
318   }
319   if (result[1] != null) {
320       this.TheTemplateId = Integer.parseInt(result[1].toString());
321   } else {
322       this.TheTemplateId = -1;
323   }
324   if (result[2] != null) {
325       this.WebURLStub = result[2].toString();
326   } else {
327       this.WebURLStub = "";
328   }
329     }
330 }
331 
332 
333 
334