KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > ext > taglib > DBMaint


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.ext.taglib;
66
67 import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
68 import com.jcorporate.expresso.core.i18n.Messages;
69 import com.jcorporate.expresso.core.misc.ConfigManager;
70 import com.jcorporate.expresso.core.misc.ConfigurationException;
71 import com.jcorporate.expresso.core.misc.CurrentLogin;
72 import com.jcorporate.expresso.core.misc.URLUTF8Encoder;
73 import com.jcorporate.expresso.ext.struts.taglib.bean.ExMessageTag;
74 import com.jcorporate.expresso.kernel.util.FastStringBuffer;
75 import org.apache.struts.config.ActionConfig;
76
77 import javax.servlet.jsp.JspTagException JavaDoc;
78 import javax.servlet.jsp.JspWriter JavaDoc;
79 import javax.servlet.jsp.PageContext JavaDoc;
80 import javax.servlet.jsp.tagext.Tag JavaDoc;
81 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
82 import java.io.IOException JavaDoc;
83 import java.util.Locale JavaDoc;
84
85
86 /**
87  * A simple tag to provide a row for a table that contains links to the DBMaint controller
88  * providing add/update/delete functionality for a specific DB object.
89  *
90  * @author Michael Nash
91  */

92 public class DBMaint
93         extends TagSupport JavaDoc {
94     private String JavaDoc dbobj;
95     private String JavaDoc label;
96     private String JavaDoc help;
97     private String JavaDoc definition;
98
99     /**
100      * By default, the mapping is DBMaint.do. However, this can be changed
101      * with the "mapping" attribute.
102      */

103     private String JavaDoc mapping = "DBMaint.do";
104     private String JavaDoc schemaClass = null;
105
106     public DBMaint() {
107         super();
108     }
109
110
111     public int doStartTag()
112             throws javax.servlet.jsp.JspTagException JavaDoc {
113
114         schemaClass = null;
115         String JavaDoc dbName = "default";
116         CurrentLogin cl = (CurrentLogin) pageContext.getAttribute(CurrentLogin.LOGIN_KEY,
117                 PageContext.PAGE_SCOPE);
118
119         if (cl != null) {
120             dbName = cl.getDBName();
121         }
122
123         // if a substring match of 'DBMaint' on all action mappings is found, set that
124
// as the default mapping
125
ActionConfig dbMaintMapping = ConfigManager.getActionConfig(com.jcorporate.expresso
126
                .services.controller.DBMaint.class.getName(), "");
127         if (dbMaintMapping != null) {
128             mapping = dbMaintMapping.getPath().substring(1) + ".do";
129         }
130
131         String JavaDoc contextPath = ConfigManager.getContextPath();
132         String JavaDoc images = "";
133
134         try {
135             images = ConfigManager.getContext(dbName).getImages();
136         } catch (ConfigurationException ce) {
137             throw new JspTagException JavaDoc("Configuration error:" +
138                     ce.getMessage());
139         }
140         try {
141             if (dbobj != null) {
142                 JspWriter JavaDoc writer = pageContext.getOut();
143                 Locale JavaDoc l = (Locale JavaDoc) pageContext.getAttribute(Messages.LOCALE_KEY,
144                         PageContext.SESSION_SCOPE);
145
146                 if (l == null) {
147                     l = Locale.getDefault();
148                 }
149
150                 String JavaDoc useLabel = null;
151
152                 String JavaDoc definitionName = null;
153                 if (getDefinition() != null) {
154                     definitionName = URLUTF8Encoder.encode(this.
155                             getDefinition());
156                 }
157
158                 if (label == null) {
159                     useLabel = getDBObjDescrip(l);
160                 } else {
161                     useLabel = label;
162                 }
163                 FastStringBuffer fsb = FastStringBuffer.getInstance();
164                 FastStringBuffer imageStart = FastStringBuffer.getInstance();
165                 try {
166                     String JavaDoc listString = getString(l, "List");
167                     String JavaDoc addString = getString(l, "Add");
168                     String JavaDoc searchForString = getString(l, "SearchFor");
169                     fsb.append("<tr class=\"jc-default\">");
170                     fsb.append("<td scope=\"row\"><a HREF=\"");
171                     fsb.append(contextPath);
172                     fsb.append("/");
173                     fsb.append(mapping);
174                     fsb.append("?dbobj=");
175                     fsb.append(dbobj);
176                     if (definitionName != null) {
177                         fsb.append("&definition=");
178                         fsb.append(definitionName);
179                     }
180                     fsb.append("&state=List\">");
181                     fsb.append(useLabel);
182                     fsb.append("</a></td>");
183
184                     imageStart.append("<td align=\"center\" width=\"60\"><a HREF=\"");
185                     imageStart.append(contextPath);
186                     imageStart.append("/");
187                     imageStart.append(mapping);
188                     imageStart.append("?dbobj=");
189                     imageStart.append(dbobj);
190                     if (definitionName != null) {
191                         imageStart.append("&definition=");
192                         imageStart.append(definitionName);
193                     }
194
195                     FastStringBuffer imageEnd = new FastStringBuffer(56);
196                     imageEnd.append(" ");
197                     imageEnd.append(useLabel);
198                     imageEnd.append("\" border=\"0\" align=\"middle\"></a></td>");
199
200                     //List
201
fsb.append(imageStart);
202                     fsb.append("&state=List\"><img SRC=\"");
203                     fsb.append(images);
204                     fsb.append("/documentmag.png\" alt=\"");
205                     fsb.append(listString);
206                     fsb.append(imageEnd);
207
208
209                     //
210
//Add
211
//
212
fsb.append(imageStart);
213                     fsb.append("&state=Add\"><img SRC=\"");
214                     fsb.append(images);
215                     fsb.append("/add.png\" alt=\"");
216                     fsb.append(addString);
217                     fsb.append(imageEnd);
218
219
220                     //
221
//Search
222
//
223
fsb.append(imageStart);
224                     fsb.append("&state=Search\"><img SRC=\"");
225                     fsb.append(images);
226                     fsb.append("/search.png\" alt=\"");
227                     fsb.append(searchForString);
228                     fsb.append(imageEnd);
229
230
231                     if (help != null) {
232                         fsb.append("<td align=\"center\" width=\"60\"> <a HREF=\"");
233                         fsb.append(help);
234                         fsb.append("\" target=\"new\"><img SRC=\"");
235                         fsb.append(images);
236                         fsb.append("/help.png\" alt=\"");
237                         fsb.append(getString(l, "generalHelpAbout"));
238                         fsb.append(" ");
239                         fsb.append(useLabel);
240                         fsb.append("\" border=\"0\" align=\"middle\"></a></td>");
241
242
243                     } else {
244                         fsb.append("<td>&nbsp;</td>");
245                     }
246                     fsb.append("</tr>\n");
247
248                     writer.print(fsb.toString());
249                 } finally {
250                     fsb.release();
251                     imageStart.release();
252                 }
253             } else {
254                 throw new JspTagException JavaDoc("No dbobj supplied");
255             }
256         } catch (IOException JavaDoc ie) {
257             throw new JspTagException JavaDoc("IO Exception:" + ie.getMessage());
258         }
259
260         return Tag.SKIP_BODY;
261     }
262
263     public java.lang.String JavaDoc getDbobj() {
264         return dbobj;
265     }
266
267     public void setDbobj(java.lang.String JavaDoc newDbobj) {
268         dbobj = newDbobj;
269     }
270
271     public java.lang.String JavaDoc getLabel() {
272         return label;
273     }
274
275     public void setLabel(java.lang.String JavaDoc newLabel) {
276         label = newLabel;
277     }
278
279     public void setHelp(java.lang.String JavaDoc newHelp) {
280         help = newHelp;
281     }
282
283     public void setMapping(java.lang.String JavaDoc newMapping) {
284         if (mapping == null) {
285             mapping = "DBMaint.do";
286         } else {
287             mapping = newMapping;
288         }
289     }
290
291     public String JavaDoc getHelp() {
292         return help;
293     }
294
295     public String JavaDoc getMapping() {
296         return mapping;
297     }
298
299     protected String JavaDoc getDBObjDescrip(Locale JavaDoc l) {
300         try {
301             SecuredDBObject sd = (SecuredDBObject) Class.forName(getDbobj()).newInstance();
302             sd.setLocale(l);
303
304             return getString(l, sd.getMetaData().getDescription());
305         } catch (Exception JavaDoc ee) {
306             return getDbobj();
307         }
308     }
309
310     protected String JavaDoc getString(Locale JavaDoc l, String JavaDoc key) {
311         if (schemaClass == null) {
312             schemaClass = (String JavaDoc) pageContext.getAttribute(ExMessageTag.SCHEMA_KEY,
313                     PageContext.PAGE_SCOPE);
314         }
315
316         if (schemaClass != null) {
317             Object JavaDoc[] args = {};
318
319             return Messages.getString(schemaClass, l, key, args);
320         } else {
321             return Messages.getString(l, key);
322         }
323     }
324
325     public String JavaDoc getDefinition() {
326         return definition;
327     }
328
329     public void setDefinition(String JavaDoc definition) {
330         this.definition = definition;
331     }
332 } /* DBMaint */
333
334
Popular Tags