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