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 import java.lang.reflect.InvocationTargetException;
32 import java.sql.CallableStatement;
33
34
35
36
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
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
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
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 }