View Javadoc

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   * <p>
21   * 
22   * .
23   * <p>
24   * 
25   *             
26   * @version $Revision: 1.1.2.1 $, $Date: 2007/11/12 01:57:24 $
27   * @author Gonzalo Espert
28   */
29  public class SQLAdapter extends Adapter {
30  	
31  	JDBCConnector db;
32  	
33  	/**
34       * .
35       * <p>
36       * 
37       * .
38       * <p>
39       * 
40       * 
41       * 
42       * @param 
43       *            
44       * @throws 
45       *            
46       *           
47       */
48  	public SQLAdapter(DataBase db) { this.db = new JDBCConnector(db); }
49  
50  	/**
51       * .
52       * <p>
53       * 
54       * .
55       * <p>
56       * 
57       * 
58       * 
59       * @param 
60       *            
61       * @throws 
62       *            
63       *           
64       */
65  	public String ExecuteCommand(String command) { return new String(); }
66  
67  
68  	/**
69       * .
70       * <p>
71       * 
72       * .
73       * <p>
74       * 
75       * 
76       * 
77       * @param 
78       *            
79       * @throws 
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); // fixed array size
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      * <p>
128      * 
129      * .
130      * <p>
131      * 
132      * 
133      * 
134      * @param 
135      *            
136      * @throws 
137      *            
138      *           
139      */
140 	public void registerResponse(String response, String operationID) throws Exception {
141 		String fecha;
142 		PreparedStatement ps = null;
143 
144 		//escape special characters (http://www.jguru.com/faq/view.jsp?EID=8881)
145 		response = response.replaceAll("'", "''");
146 		
147   		try {
148   			// F.Hevia: date format in compliance with postgreSQL
149   			fecha = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").format(new Date());
150   			
151   			//update operation's response
152 	    	ps = db.connection.prepareStatement("UPDATE operations_queue SET \"response\" = '" +
153 	    		                              response + "', \"execution_date\" = '" + fecha +
154 	    		                              "' WHERE id = " + operationID);
155 	    	
156   			//Windows Vista
157 	    	//ps = db.connection.prepareStatement("UPDATE cola_operaciones SET \"descripcion_respuesta\" = '" +
158             //        response +"' WHERE id =" + operationID);
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      * <p>
175      * 
176      * .
177      * <p>
178      * 
179      * 
180      * 
181      * @param 
182      *            
183      * @throws 
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 }