1
2
3
4 package net.sf.provisioner.adapters;
5
6 import java.sql.PreparedStatement;
7 import java.sql.ResultSet;
8 import java.sql.SQLException;
9
10 import java.text.SimpleDateFormat;
11 import java.util.*;
12
13 import net.sf.provisioner.config.DataBase;
14 import net.sf.provisioner.connectors.JDBCConnector;
15 import net.sf.provisioner.core.Operation;
16
17
18
19
20
21
22
23
24
25
26
27
28
29 public class SQLAdapter extends Adapter {
30
31 JDBCConnector db;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public SQLAdapter(DataBase db) { this.db = new JDBCConnector(db); }
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 public String ExecuteCommand(String command) { return new String(); }
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83 public Operation[] getPendingOperations(int maxRows) throws Exception{
84
85 Operation[] operations = {new Operation() , new Operation(), new Operation(), new Operation()};
86
87 ResultSet rs = null;
88
89 try {
90 this.db.statement.setMaxRows(maxRows);
91 rs = this.db.statement.executeQuery("SELECT * FROM operations_queue WHERE \"status\" in ('PEND','RETR') ORDER BY id, insert_date");
92 } catch (SQLException se) {
93 throw se;
94 }
95
96 try {
97
98 if (rs.next()){
99
100 int i = 0;
101 boolean doContinue = true;
102 while (doContinue) {
103
104 if (rs.isLast()) doContinue = false;
105
106 Operation operation = new Operation();
107 operation.setId(rs.getString("id"));
108 operation.setType(rs.getString("type"));
109 operation.setParameters(rs.getString("xml_string"));
110
111 operations[i] = operation;
112
113 rs.next();
114 i++;
115 }
116
117 } else { logger.info("No pending operations"); }
118
119 } catch (Exception ex) { throw ex; }
120
121 return operations;
122 }
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140 public void registerResponse(String response, String operationID) throws Exception {
141 String fecha;
142 PreparedStatement ps = null;
143
144
145 response = response.replaceAll("'", "''");
146
147 try {
148
149 fecha = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").format(new Date());
150
151
152 ps = db.connection.prepareStatement("UPDATE operations_queue SET \"response\" = '" +
153 response + "', \"execution_date\" = '" + fecha +
154 "' WHERE id = " + operationID);
155
156
157
158
159
160 } catch (SQLException se) {
161 throw se;
162 }
163
164 try {
165 ps.executeUpdate();
166 } catch (SQLException se) {
167 throw se;
168 }
169 fecha = null;
170 }
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187 public void updateOperationStatus(String operationID, String status) throws Exception {
188
189 PreparedStatement ps = null;
190
191 try {
192 ps = db.connection.prepareStatement("UPDATE operations_queue SET \"status\" = '" +
193 status +"' WHERE id ="+ operationID);
194
195 } catch (SQLException se) {
196 throw se;
197 }
198
199 try {
200 ps.executeUpdate();
201 } catch (SQLException se) {
202 throw se;
203 }
204
205 }
206 }