1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
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
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 }