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  
32  /**
33   *
34   * @author dberry
35   */
36  public class PreparedStatementCpoData extends AbstractJdbcCpoData {
37  
38    private static final Logger logger = LoggerFactory.getLogger(PreparedStatementCpoData.class);
39    private JdbcPreparedStatementFactory jpsf = null;
40  
41    public PreparedStatementCpoData(JdbcPreparedStatementFactory jpsf, CpoAttribute cpoAttribute, int index) {
42      super(cpoAttribute, index);
43      this.jpsf = jpsf;
44    }
45  
46    @Override
47    public void invokeSetter(Object instanceObject) throws CpoException {
48      Logger localLogger = instanceObject == null ? logger : LoggerFactory.getLogger(instanceObject.getClass());
49      CpoAttribute cpoAttribute = getCpoAttribute();
50      Object param = transformOut(cpoAttribute.invokeGetter(instanceObject));
51      JavaSqlMethod<?> javaSqlMethod = JavaSqlMethods.getJavaSqlMethod(getDataSetterParamType());
52      if (javaSqlMethod == null) {
53        throw new CpoException("Error Retrieveing Jdbc Method for type: " + getDataSetterParamType().getName());
54      }
55      localLogger.info(cpoAttribute.getDataName() + "=" + param);
56      try {
57        switch (javaSqlMethod.getMethodType()) {
58          case JavaSqlMethod.METHOD_TYPE_BASIC:
59            javaSqlMethod.getPsSetter().invoke(jpsf.getPreparedStatement(), getIndex(), param);
60            break;
61          case JavaSqlMethod.METHOD_TYPE_STREAM:
62            CpoByteArrayInputStream cbais = CpoByteArrayInputStream.getCpoStream((InputStream) param);
63            // Get the length of the InputStream in param
64            javaSqlMethod.getPsSetter().invoke(jpsf.getPreparedStatement(), getIndex(), cbais, cbais.getLength());
65            break;
66          case JavaSqlMethod.METHOD_TYPE_READER:
67            CpoCharArrayReader ccar = CpoCharArrayReader.getCpoReader((Reader) param);
68            // Get the length of the Reader in param
69            javaSqlMethod.getPsSetter().invoke(jpsf.getPreparedStatement(), getIndex(), ccar, ccar.getLength());
70            break;
71        }
72      } catch (Exception e) {
73        throw new CpoException("Error Invoking Jdbc Method: " + javaSqlMethod.getPsSetter().getName() + ":" + ExceptionHelper.getLocalizedMessage(e));
74      }
75    }
76    
77    @Override
78    public Object transformOut(Object attributeObject) throws CpoException {
79      Object retObj = attributeObject;
80      CpoTransform cpoTransform = getCpoAttribute().getCpoTransform();
81      
82      if (cpoTransform != null) {
83        if (cpoTransform instanceof JdbcCpoTransform) {
84          retObj = ((JdbcCpoTransform)cpoTransform).transformOut(jpsf, attributeObject);
85        } else {
86          retObj = cpoTransform.transformOut(attributeObject);
87        }
88      }
89      return retObj;
90    }
91  
92  }