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