View Javadoc

1   /*
2    * Copyright (C) 2003-2012 David E. Berry
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17   *
18   * A copy of the GNU Lesser General Public License may also be found at
19   * http://www.gnu.org/licenses/lgpl.txt
20   */
21  package org.synchronoss.cpo.jdbc;
22  
23  import org.slf4j.*;
24  import org.synchronoss.cpo.*;
25  import org.synchronoss.cpo.helper.ExceptionHelper;
26  import org.synchronoss.cpo.meta.domain.*;
27  
28  import java.sql.*;
29  import java.util.*;
30  
31  /**
32   * JdbcCallableStatementFactory is the object that encapsulates the creation of the actual CallableStatement for the
33   * JDBC driver.
34   *
35   * @author david berry
36   */
37  public class JdbcCallableStatementFactory implements CpoReleasible {
38  
39    /**
40     * Version Id for this class.
41     */
42    private static final long serialVersionUID = 1L;
43    /**
44     * DOCUMENT ME!
45     */
46    private static final Logger logger = LoggerFactory.getLogger(JdbcCallableStatementFactory.class);
47    private CallableStatement cs_ = null;
48  
49    @SuppressWarnings("unused")
50    private JdbcCallableStatementFactory() {
51    }
52    private List<CpoReleasible> releasibles = new ArrayList<CpoReleasible>();
53    private List<CpoArgument> outArguments = new ArrayList<CpoArgument>();
54  
55    /**
56     * Used to build the CallableStatement that is used by CPO to create the actual JDBC CallableStatement.
57     *
58     * The constructor is called by the internal CPO framework. This is not to be used by users of CPO. Programmers that
59     * build Transforms may need to use this object to get access to the actual connection.
60     *
61     * @param conn The actual jdbc connection that will be used to create the callable statement.
62     * @param jca The JdbcCpoAdapter that is controlling this transaction
63     * @param function The CpoFunction that is being executed
64     * @param obj The pojo that is being acted upon
65     *
66     * @throws CpoException if a CPO error occurs
67     * @throws SQLException if a JDBC error occurs
68     */
69    public JdbcCallableStatementFactory(Connection conn, JdbcCpoAdapter jca, CpoFunction function, Object obj) throws CpoException {
70      CallableStatement cstmt;
71      JdbcCpoAttribute attribute;
72      Logger localLogger = obj == null ? logger : LoggerFactory.getLogger(obj.getClass());
73  
74      try {
75        outArguments = function.getArguments();
76  
77        localLogger.debug("SQL = <" + function.getExpression() + ">");
78  
79        // prepare the Callable Statement
80        cstmt = conn.prepareCall(function.getExpression());
81        setCallableStatement(cstmt);
82  
83        int j = 1;
84        for (CpoArgument argument : outArguments) {
85          JdbcCpoArgument jdbcArgument = (JdbcCpoArgument) argument;
86          attribute = (JdbcCpoAttribute) argument.getAttribute();
87  
88          if (jdbcArgument.isInParameter()) {
89            CpoData cpoData = new CallableStatementCpoData(this, attribute, j);
90            cpoData.invokeSetter(obj);
91          }
92  
93          if (jdbcArgument.isOutParameter()) {
94            localLogger.debug("Setting OUT parameter " + j + " as Type " + attribute.getJavaSqlType());
95            if (jdbcArgument.getTypeInfo()!=null)
96              cstmt.registerOutParameter(j, attribute.getJavaSqlType(), jdbcArgument.getTypeInfo());
97            else 
98              cstmt.registerOutParameter(j, attribute.getJavaSqlType());
99          }
100         j++;
101       }
102 
103     } catch (Exception e) {
104       localLogger.error("Error Instantiating JdbcCallableStatementFactory" + ExceptionHelper.getLocalizedMessage(e));
105       throw new CpoException(e);
106     }
107 
108   }
109 
110   /**
111    * returns the jdbc callable statment associated with this object
112    */
113   public CallableStatement getCallableStatement() {
114     return cs_;
115   }
116 
117   protected void setCallableStatement(CallableStatement cs) {
118     cs_ = cs;
119   }
120 
121   /**
122    * returns the Out parameters from the callable statement
123    *
124    */
125   public List<CpoArgument> getOutArguments() {
126     return outArguments;
127   }
128 
129   /**
130    * Adds a releasible object to this object. The release method on the releasible will be called when the
131    * callableStatement is executed.
132    *
133    */
134   public void AddReleasible(CpoReleasible releasible) {
135     if (releasible != null) {
136       releasibles.add(releasible);
137     }
138 
139   }
140 
141   /**
142    * Called by the CPO framework. This method calls the
143    * <code>release</code> on all the CpoReleasible associated with this object
144    */
145   @Override
146   public void release() throws CpoException {
147     for (CpoReleasible releasible : releasibles) {
148       try {
149         releasible.release();
150       } catch (CpoException ce) {
151         logger.error("Error Releasing Callable Statement Transform Object", ce);
152         throw ce;
153       }
154     }
155   }
156 }