View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/DbBlobContentTag.java,v 1.28 2006/01/13 13:38:50 hkollmann Exp $
3    * $Revision: 1.28 $
4    * $Date: 2006/01/13 13:38:50 $
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;
38  import java.sql.PreparedStatement;
39  import java.sql.ResultSet;
40  import java.sql.SQLException;
41  
42  import javax.servlet.jsp.*;
43  
44  /***
45   * #fixme docu to come
46   * 
47   * @author Joe Peer
48   */
49  public class DbBlobContentTag extends AbstractDbBaseHandlerTag implements
50          javax.servlet.jsp.tagext.TryCatchFinally {
51      private static Log logCat = LogFactory.getLog(DbBlobContentTag.class);
52  
53      private String dbConnectionName;
54  
55      /***
56       * DOCUMENT ME!
57       * 
58       * @param string
59       */
60      public void setDbConnectionName(String string) {
61          dbConnectionName = string;
62      }
63  
64      /***
65       * DOCUMENT ME!
66       * 
67       * @return
68       */
69      public String getDbConnectionName() {
70          return dbConnectionName;
71      }
72  
73  
74      /***
75       * DOCUMENT ME!
76       * 
77       * @return DOCUMENT ME!
78       * 
79       * @throws javax.servlet.jsp.JspException
80       *             DOCUMENT ME!
81       * @throws IllegalArgumentException
82       *             DOCUMENT ME!
83       * @throws JspException
84       *             DOCUMENT ME!
85       */
86      public int doEndTag() throws javax.servlet.jsp.JspException {
87          try {
88              if (getParentForm().isFooterReached()) {
89                  return EVAL_PAGE; // nothing to do when no data available..
90              }
91  
92              String keyVal = getKeyVal();
93              StringBuffer queryBuf = new StringBuffer();
94              queryBuf.append("SELECT ");
95              queryBuf.append(getField().getName());
96              queryBuf.append(" FROM ");
97              queryBuf.append(getParentForm().getTable().getQueryFrom());
98              queryBuf.append(" WHERE ");
99              queryBuf.append(getParentForm().getTable()
100                     .getWhereClauseForKeyFields(keyVal));
101             logCat.info("blobcontent query- " + queryBuf.toString());
102 
103             StringBuffer contentBuf = new StringBuffer();
104 
105             try {
106                 Connection con = getConfig().getConnection(dbConnectionName);
107                 PreparedStatement ps = con
108                         .prepareStatement(queryBuf.toString());
109                 getParentForm().getTable().populateWhereClauseWithKeyFields(
110                         keyVal, ps, 1);
111 
112                 ResultSet rs = ps.executeQuery();
113 
114                 if (rs.next()) {
115                     InputStream is = null;
116                     String fileName = null;
117 
118                     if (getField().getType() == FieldTypes.DISKBLOB) {
119                         fileName = rs.getString(1);
120                         is = SqlUtil.readDiskBlob(fileName, getField()
121                                 .getDirectory(), null);
122                     } else if (getField().getTable().getBlobHandlingStrategy() == Table.BLOB_CLASSIC) {
123                         FileHolder fh = SqlUtil.readFileHolderBlob(rs);
124                         is = fh.getInputStreamFromBuffer();
125                     } else {
126                         is = SqlUtil.readDbFieldBlob(rs);
127                     }
128                     if (is != null) {
129                         try {
130                             BufferedReader br = new BufferedReader(
131                                     new InputStreamReader(is));
132                             char[] c = new char[1024];
133                             int read;
134 
135                             while ((read = br.read(c)) != -1) {
136                                 contentBuf.append(c, 0, read);
137                             }
138                         } finally {
139                             is.close();
140                         }
141                     }
142                 } else {
143                     logCat.info("fs- we have got no result" + queryBuf);
144                 }
145 
146                 SqlUtil.closeConnection(con);
147             } catch (SQLException sqle) {
148                 sqle.printStackTrace();
149             }
150 
151             pageContext.getOut().write(escapeHTML(contentBuf.toString()));
152         } catch (java.io.IOException ioe) {
153             throw new JspException("IO Error: " + ioe.getMessage());
154         }
155 
156         return EVAL_PAGE;
157     }
158 
159     /***
160      * DOCUMENT ME!
161      */
162     public void doFinally() {
163         dbConnectionName = null;
164         super.doFinally();
165     }
166 
167     // ------------------------------------------------------ Protected Methods
168     // DbForms specific
169 
170     /***
171      * DOCUMENT ME!
172      * 
173      * @return DOCUMENT ME!
174      */
175     private String getKeyVal() {
176         return getParentForm().getTable().getKeyPositionString(
177                 getParentForm().getResultSetVector());
178     }
179 }