Source code: com/RuntimeCollective/content/bean/ForwardLink.java
1 /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/content/bean/ForwardLink.java,v 1.11 2003/09/30 15:12:46 joe Exp $
2 * $Revision: 1.11 $
3 * $Date: 2003/09/30 15:12:46 $
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.content.bean;
31
32 import com.RuntimeCollective.webapps.RuntimeDataSource;
33 import com.RuntimeCollective.webapps.RuntimeParameters;
34
35 import com.RuntimeCollective.webapps.bean.EntityBean;
36 import com.RuntimeCollective.webapps.bean.User;
37 import com.RuntimeCollective.webapps.bean.DateBean;
38
39 import org.apache.struts.action.ActionMapping;
40
41 import java.net.MalformedURLException;
42 import java.net.URL;
43 import java.sql.SQLException;
44 import java.util.ArrayList;
45 import java.util.Iterator;
46 import java.util.Vector;
47
48 /**
49 * The ForwardLink class implements the Link abstract class, using a URL.
50 * <br><br>
51 * For now, we cannot directly access the forwards defined in struts-config.xml.
52 * So all forwards used for ForwardLinks have to be copied into web.xml,
53 * as a param.FORWARDforwardname parameter. Sorry for the inconvenience.<br>
54 *<br>
55 * HOW TO : add a new Static Link<br>
56 * ------------------------------<br>
57 *<br>
58 * Step 1 - creating in the database a new ForwardLink. See /se/datamodel/mssqlserver/Content-sedata-load.sql for examples. Just copy an example, and change the name of the struts Forward you will be using, and its display name.<br>
59 *<br>
60 * Step 2 - in struts-config.xml, define the struts Forward used by the ForwardLink. There you'll specify the path to go to (eg: /admin/myStaticPage.jsp).<br>
61 * <br>
62 * Step 3 - in web.xml (and web.xml.staging, web.xml.live), add a new parameter for this struts Forward, eg:<br>
63 * <br>
64 * <init-param><br>
65 * <param-name>param.FORWARDnavViewBoards</param-name><br>
66 * <param-value>/viewBoards.jsp</param-value><br>
67 * </init-param><br>
68 * <br>
69 * @version $Id: ForwardLink.java,v 1.11 2003/09/30 15:12:46 joe Exp $
70 */
71 public class ForwardLink extends Link {
72
73 // ---Inherited from EntityBean---------------------------
74
75 /** The name of the database table for this bean type. */
76 public static final String DATABASE_TABLE = "content_fwlink";
77
78 /** Save this ForwardLink to the database.
79 * @exception SQLException is thrown if the object couldn't be saved properly to the database.
80 */
81 public void save() throws SQLException {
82
83 String dbalias = RuntimeDataSource.getDefaultAlias();
84
85 // Save the ForwardLink's Link attributes
86 super.save();
87
88 // Save the ForwardLink's basic attributes
89 String fname = ForwardName;
90 if (fname == null)
91 fname = "";
92
93 RuntimeDataSource.save( id, "content_fwlink", new String[] { "forward_name" }, new Object[] { fname } );
94 }
95
96 /** Delete this ForwardLink from the database.
97 * @exception SQLException is thrown if the object couldn't be deleted properly from the database.
98 */
99 public void delete() throws SQLException {
100
101 String[] updates;
102
103 updates = new String[] { "delete from content_fwlink where id = "+id };
104 RuntimeDataSource.update( updates);
105
106 super.delete();
107 }
108
109
110
111 //---Inherited from Link---------------------
112
113 /** Is this link displayable, ie is it valid ?
114 * @return a boolean
115 */
116 public boolean isDisplayable() {
117 // has this forward been put in web.xml too ?
118 try {
119 return (RuntimeParameters.get("FORWARD"+ForwardName) != null);
120 } catch (RuntimeException e) {
121 return false;
122 }
123 }
124
125 /** Get the LinkURL
126 * @param The URL to link to
127 */
128 public URL getLinkURL() {
129 if (isDisplayable()) {
130 try {
131 return new URL((new StringBuffer(50)).append("http://").append(RuntimeParameters.get("pageRoot")).append(RuntimeParameters.get("FORWARD"+ForwardName)).toString());
132 } catch (Exception e) {
133 RuntimeParameters.logDebug(this, "Couldn't generate URL for ForwardLink "+getId()+" on ForwardName "+ForwardName+"\n"+e);
134 return null;
135 }
136
137 } else {
138 RuntimeParameters.logDebug(this, "Couldn't generate URL for ForwardLink "+getId()+", as it is not displayable. Has it been defined in web.xml ? (ForwardName : "+ForwardName+")");
139 return null;
140 }
141 }
142
143
144 //---Specific to ForwardLink---------------------
145
146 /** The ForwardName */
147 protected String ForwardName;
148
149 /** Set the ForwardName
150 * @param The ForwardName
151 */
152 public void setForwardName(String forwardName) {
153 ForwardName = forwardName;
154 }
155
156 /** Get the ForwardName
157 * @return The ForwardName
158 */
159 public String getForwardName() {
160 return ForwardName;
161 }
162
163 /** Construct a new blank ForwardLink, giving it a new unique ID.
164 * @exception SQLException is thrown if a new ID cannot be created.
165 */
166 public ForwardLink() throws SQLException {
167 super();
168
169 setForwardName(null);
170 }
171
172 /** Get a ForwardLink from the database, given an id.
173 * @param id ID of the ForwardLink.
174 * @exception SQLException is thrown if no such ForwardLink exists.
175 */
176 public ForwardLink(int id) throws SQLException {
177 super(id);
178
179 Object[] result = RuntimeDataSource.queryRow("select fl.forward_name from content_fwlink fl where fl.id = "+id);
180 if (result.length != 1) {
181 throw new SQLException("Cannot load ForwardLink "+id+": "+result.length+" fields found in content_textcomponent instead of 1.");
182 }
183
184 if (result[0] != null) {
185 setForwardName(result[0].toString());
186 }
187 }
188 }
189
190
191
192