KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > taglib > DbBlobContentTag


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/DbBlobContentTag.java,v 1.24 2004/10/24 13:47:30 hkollmann Exp $
3  * $Revision: 1.24 $
4  * $Date: 2004/10/24 13:47:30 $
5  *
6  * DbForms - a Rapid Application Development Framework
7  * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */

23
24 package org.dbforms.taglib;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import org.dbforms.config.FieldTypes;
30 import org.dbforms.config.Table;
31
32 import org.dbforms.util.FileHolder;
33 import org.dbforms.util.SqlUtil;
34
35 import java.io.*;
36
37 import java.sql.Connection JavaDoc;
38 import java.sql.PreparedStatement JavaDoc;
39 import java.sql.ResultSet JavaDoc;
40 import java.sql.SQLException JavaDoc;
41
42 import javax.servlet.jsp.*;
43
44 /**
45  * #fixme docu to come
46  *
47  * @author Joe Peer
48  */

49 public class DbBlobContentTag extends DbBaseHandlerTag implements
50         javax.servlet.jsp.tagext.TryCatchFinally JavaDoc {
51     private static Log logCat = LogFactory.getLog(DbBlobContentTag.class);
52
53     private String JavaDoc dbConnectionName;
54
55     /**
56      * DOCUMENT ME!
57      *
58      * @param string
59      */

60     public void setDbConnectionName(String JavaDoc string) {
61         dbConnectionName = string;
62     }
63
64     /**
65      * DOCUMENT ME!
66      *
67      * @return
68      */

69     public String JavaDoc getDbConnectionName() {
70         return dbConnectionName;
71     }
72
73     /**
74      * @see javax.servlet.jsp.tagext.TryCatchFinally#doCatch(java.lang.Throwable)
75      */

76     public void doCatch(Throwable JavaDoc t) throws Throwable JavaDoc {
77         throw t;
78     }
79
80     /**
81      * DOCUMENT ME!
82      *
83      * @return DOCUMENT ME!
84      *
85      * @throws javax.servlet.jsp.JspException
86      * DOCUMENT ME!
87      * @throws IllegalArgumentException
88      * DOCUMENT ME!
89      * @throws JspException
90      * DOCUMENT ME!
91      */

92     public int doEndTag() throws javax.servlet.jsp.JspException JavaDoc {
93         try {
94             if (getParentForm().isFooterReached()) {
95                 return EVAL_PAGE; // nothing to do when no data available..
96
}
97
98             StringBuffer JavaDoc queryBuf = new StringBuffer JavaDoc();
99             queryBuf.append("SELECT ");
100             queryBuf.append(getField().getName());
101             queryBuf.append(" FROM ");
102             queryBuf.append(getParentForm().getTable().getName());
103             queryBuf.append(" WHERE ");
104             queryBuf.append(getParentForm().getTable()
105                     .getWhereClauseForKeyFields());
106             logCat.info("blobcontent query- " + queryBuf.toString());
107
108             StringBuffer JavaDoc contentBuf = new StringBuffer JavaDoc();
109
110             try {
111                 Connection JavaDoc con = getConfig().getConnection(dbConnectionName);
112                 PreparedStatement JavaDoc ps = con
113                         .prepareStatement(queryBuf.toString());
114                 getParentForm().getTable().populateWhereClauseWithKeyFields(
115                         getKeyVal(), ps, 1);
116
117                 ResultSet JavaDoc rs = ps.executeQuery();
118
119                 if (rs.next()) {
120                     InputStream is = null;
121                     String JavaDoc fileName = null;
122
123                     if (getField().getType() == FieldTypes.DISKBLOB) {
124                         fileName = rs.getString(1);
125                         is = SqlUtil.readDiskBlob(fileName, getField()
126                                 .getDirectory(), null);
127                     } else if (getField().getTable().getBlobHandlingStrategy() == Table.BLOB_CLASSIC) {
128                         FileHolder fh = SqlUtil.readFileHolderBlob(rs);
129                         is = fh.getInputStreamFromBuffer();
130                     } else {
131                         is = SqlUtil.readDbFieldBlob(rs);
132                     }
133                     if (is != null) {
134                         try {
135                             BufferedReader br = new BufferedReader(
136                                     new InputStreamReader(is));
137                             char[] c = new char[1024];
138                             int read;
139
140                             while ((read = br.read(c)) != -1) {
141                                 contentBuf.append(c, 0, read);
142                             }
143                         } finally {
144                             is.close();
145                         }
146                     }
147                 } else {
148                     logCat.info("fs- we have got no result" + queryBuf);
149                 }
150
151                 SqlUtil.closeConnection(con);
152             } catch (SQLException JavaDoc sqle) {
153                 sqle.printStackTrace();
154             }
155
156             pageContext.getOut().write(escapeHTML(contentBuf.toString()));
157         } catch (java.io.IOException JavaDoc ioe) {
158             throw new JspException("IO Error: " + ioe.getMessage());
159         }
160
161         return EVAL_PAGE;
162     }
163
164     /**
165      * DOCUMENT ME!
166      */

167     public void doFinally() {
168         dbConnectionName = null;
169         super.doFinally();
170     }
171
172     // ------------------------------------------------------ Protected Methods
173
// DbForms specific
174

175     /**
176      * DOCUMENT ME!
177      *
178      * @return DOCUMENT ME!
179      */

180     private String JavaDoc getKeyVal() {
181         return getParentForm().getTable().getKeyPositionString(
182                 getParentForm().getResultSetVector());
183     }
184 }
185
Popular Tags