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.*;
27
28 import java.sql.*;
29 import java.util.*;
30
31
32
33
34
35
36
37 public class JdbcCallableStatementFactory implements CpoReleasible {
38
39
40
41
42 private static final long serialVersionUID = 1L;
43
44
45
46 private static final Logger logger = LoggerFactory.getLogger(JdbcCallableStatementFactory.class);
47 private CallableStatement cs_ = null;
48
49 @SuppressWarnings("unused")
50 private JdbcCallableStatementFactory() {
51 }
52 private List<CpoReleasible> releasibles = new ArrayList<CpoReleasible>();
53 private List<CpoArgument> outArguments = new ArrayList<CpoArgument>();
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 public JdbcCallableStatementFactory(Connection conn, JdbcCpoAdapter jca, CpoFunction function, Object obj) throws CpoException {
70 CallableStatement cstmt;
71 JdbcCpoAttribute attribute;
72 Logger localLogger = obj == null ? logger : LoggerFactory.getLogger(obj.getClass());
73
74 try {
75 outArguments = function.getArguments();
76
77 localLogger.debug("SQL = <" + function.getExpression() + ">");
78
79
80 cstmt = conn.prepareCall(function.getExpression());
81 setCallableStatement(cstmt);
82
83 int j = 1;
84 for (CpoArgument argument : outArguments) {
85 JdbcCpoArgument jdbcArgument = (JdbcCpoArgument) argument;
86 attribute = (JdbcCpoAttribute) argument.getAttribute();
87
88 if (jdbcArgument.isInParameter()) {
89 CpoData cpoData = new CallableStatementCpoData(this, attribute, j);
90 cpoData.invokeSetter(obj);
91 }
92
93 if (jdbcArgument.isOutParameter()) {
94 localLogger.debug("Setting OUT parameter " + j + " as Type " + attribute.getJavaSqlType());
95 if (jdbcArgument.getTypeInfo()!=null)
96 cstmt.registerOutParameter(j, attribute.getJavaSqlType(), jdbcArgument.getTypeInfo());
97 else
98 cstmt.registerOutParameter(j, attribute.getJavaSqlType());
99 }
100 j++;
101 }
102
103 } catch (Exception e) {
104 localLogger.error("Error Instantiating JdbcCallableStatementFactory" + ExceptionHelper.getLocalizedMessage(e));
105 throw new CpoException(e);
106 }
107
108 }
109
110
111
112
113 public CallableStatement getCallableStatement() {
114 return cs_;
115 }
116
117 protected void setCallableStatement(CallableStatement cs) {
118 cs_ = cs;
119 }
120
121
122
123
124
125 public List<CpoArgument> getOutArguments() {
126 return outArguments;
127 }
128
129
130
131
132
133
134 public void AddReleasible(CpoReleasible releasible) {
135 if (releasible != null) {
136 releasibles.add(releasible);
137 }
138
139 }
140
141
142
143
144
145 @Override
146 public void release() throws CpoException {
147 for (CpoReleasible releasible : releasibles) {
148 try {
149 releasible.release();
150 } catch (CpoException ce) {
151 logger.error("Error Releasing Callable Statement Transform Object", ce);
152 throw ce;
153 }
154 }
155 }
156 }