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.CpoAttribute;
27  import org.synchronoss.cpo.transform.CpoTransform;
28  import org.synchronoss.cpo.transform.jdbc.JdbcCpoTransform;
29  
30  import java.io.*;
31  import java.lang.reflect.InvocationTargetException;
32  import java.sql.CallableStatement;
33  
34  /**
35   *
36   * @author dberry
37   */
38  public class CallableStatementCpoData extends AbstractJdbcCpoData {
39    
40    private static final Logger logger = LoggerFactory.getLogger(CallableStatementCpoData.class);
41    private CallableStatement cs = null;
42    JdbcCallableStatementFactory jcsf = null;
43    
44    public CallableStatementCpoData(CallableStatement cs, CpoAttribute cpoAttribute, int index) {
45      super(cpoAttribute, index);
46      this.cs = cs;
47    }
48  
49    public CallableStatementCpoData(JdbcCallableStatementFactory jcsf, CpoAttribute cpoAttribute, int index) {
50      super(cpoAttribute, index);
51      this.jcsf = jcsf;
52    }
53  
54    @Override
55    public Object invokeGetter() throws CpoException {
56      Object javaObject;
57      JavaSqlMethod<?> javaSqlMethod = JavaSqlMethods.getJavaSqlMethod(getDataGetterReturnType());
58      if (javaSqlMethod == null) {
59        throw new CpoException("Error Retrieveing Jdbc Method for type: " + getDataGetterReturnType().getName());
60      }
61      
62      try {
63        // Get the getter for the Callable Statement
64        javaObject = transformIn(javaSqlMethod.getCsGetter().invoke(cs, getIndex()));
65      } catch (IllegalAccessException iae) {
66        logger.debug("Error Invoking CallableStatement Method: " + ExceptionHelper.getLocalizedMessage(iae));
67        throw new CpoException(iae);
68      } catch (InvocationTargetException ite) {
69        logger.debug("Error Invoking CallableStatement Method: " + ExceptionHelper.getLocalizedMessage(ite));
70        throw new CpoException(ite.getCause());
71      }
72  
73      return javaObject;
74    }
75  
76    @Override
77    public void invokeSetter(Object instanceObject) throws CpoException {
78      Logger localLogger = instanceObject == null ? logger : LoggerFactory.getLogger(instanceObject.getClass());
79      CpoAttribute cpoAttribute = getCpoAttribute();
80      Object param = transformOut(cpoAttribute.invokeGetter(instanceObject));
81      JavaSqlMethod<?> javaSqlMethod = JavaSqlMethods.getJavaSqlMethod(getDataSetterParamType());
82      if (javaSqlMethod == null) {
83        throw new CpoException("Error Retrieveing Jdbc Method for type: " + getDataSetterParamType().getName());
84      }
85      localLogger.info(cpoAttribute.getDataName() + "=" + param);
86      try {
87        switch (javaSqlMethod.getMethodType()) {
88          case JavaSqlMethod.METHOD_TYPE_BASIC:
89            javaSqlMethod.getCsSetter().invoke(jcsf.getCallableStatement(), getIndex(), param);
90            break;
91          case JavaSqlMethod.METHOD_TYPE_STREAM:
92            CpoByteArrayInputStream cbais = CpoByteArrayInputStream.getCpoStream((InputStream) param);
93            // Get the length of the InputStream in param
94            javaSqlMethod.getCsSetter().invoke(jcsf.getCallableStatement(), getIndex(), cbais, cbais.getLength());
95            break;
96          case JavaSqlMethod.METHOD_TYPE_READER:
97            CpoCharArrayReader ccar = CpoCharArrayReader.getCpoReader((Reader) param);
98            // Get the length of the Reader in param
99            javaSqlMethod.getCsSetter().invoke(jcsf.getCallableStatement(), getIndex(), ccar, ccar.getLength());
100           break;
101       }
102     } catch (Exception e) {
103       throw new CpoException("Error Invoking Jdbc Method: " + javaSqlMethod.getPsSetter().getName() + ":" + ExceptionHelper.getLocalizedMessage(e));
104     }
105   }
106   
107   @Override
108   public Object transformOut(Object attributeObject) throws CpoException {
109     Object retObj = attributeObject;
110     CpoTransform cpoTransform = getCpoAttribute().getCpoTransform();
111     
112     if (cpoTransform != null) {
113       if (cpoTransform instanceof JdbcCpoTransform) {
114         retObj = ((JdbcCpoTransform)cpoTransform).transformOut(jcsf, attributeObject);
115       } else {
116         retObj = cpoTransform.transformOut(attributeObject);
117       }
118     }
119     return retObj;
120   }
121 
122 }