1 /* 2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javax.sql.rowset.spi; 27 28 import javax.sql.RowSet; 29 import java.sql.SQLException; 30 31 /** 32 * Defines a framework that allows applications to use a manual decision tree 33 * to decide what should be done when a synchronization conflict occurs. 34 * Although it is not mandatory for 35 * applications to resolve synchronization conflicts manually, this 36 * framework provides the means to delegate to the application when conflicts 37 * arise. 38 * <p> 39 * Note that a conflict is a situation where the <code>RowSet</code> object's original 40 * values for a row do not match the values in the data source, which indicates that 41 * the data source row has been modified since the last synchronization. Note also that 42 * a <code>RowSet</code> object's original values are the values it had just prior to the 43 * the last synchronization, which are not necessarily its initial values. 44 * <p> 45 * 46 * <H2>Description of a <code>SyncResolver</code> Object</H2> 47 * 48 * A <code>SyncResolver</code> object is a specialized <code>RowSet</code> object 49 * that implements the <code>SyncResolver</code> interface. 50 * It <b>may</b> operate as either a connected <code>RowSet</code> object (an 51 * implementation of the <code>JdbcRowSet</code> interface) or a connected 52 * <code>RowSet</code> object (an implementation of the 53 * <code>CachedRowSet</code> interface or one of its subinterfaces). For information 54 * on the subinterfaces, see the 55 * <a href="../package-summary.html"><code>javax.sql.rowset</code></a> package 56 * description. The reference implementation for <code>SyncResolver</code> implements 57 * the <code>CachedRowSet</code> interface, but other implementations 58 * may choose to implement the <code>JdbcRowSet</code> interface to satisfy 59 * particular needs. 60 * <P> 61 * After an application has attempted to synchronize a <code>RowSet</code> object with 62 * the data source (by calling the <code>CachedRowSet</code> 63 * method <code>acceptChanges</code>), and one or more conflicts have been found, 64 * a rowset's <code>SyncProvider</code> object creates an instance of 65 * <code>SyncResolver</code>. This new <code>SyncResolver</code> object has 66 * the same number of rows and columns as the 67 * <code>RowSet</code> object that was attempting the synchronization. The 68 * <code>SyncResolver</code> object contains the values from the data source that caused 69 * the conflict(s) and <code>null</code> for all other values. 70 * In addition, it contains information about each conflict. 71 * <P> 72 * 73 * <H2>Getting and Using a <code>SyncResolver</code> Object</H2> 74 * 75 * When the method <code>acceptChanges</code> encounters conflicts, the 76 * <code>SyncProvider</code> object creates a <code>SyncProviderException</code> 77 * object and sets it with the new <code>SyncResolver</code> object. The method 78 * <code>acceptChanges</code> will throw this exception, which 79 * the application can then catch and use to retrieve the 80 * <code>SyncResolver</code> object it contains. The following code snippet uses the 81 * <code>SyncProviderException</code> method <code>getSyncResolver</code> to get 82 * the <code>SyncResolver</code> object <i>resolver</i>. 83 * <PRE> 84 * {@code 85 * } catch (SyncProviderException spe) { 86 * SyncResolver resolver = spe.getSyncResolver(); 87 * ... 88 * } 89 * 90 * } 91 * </PRE> 92 * <P> 93 * With <i>resolver</i> in hand, an application can use it to get the information 94 * it contains about the conflict or conflicts. A <code>SyncResolver</code> object 95 * such as <i>resolver</i> keeps 96 * track of the conflicts for each row in which there is a conflict. It also places a 97 * lock on the table or tables affected by the rowset's command so that no more 98 * conflicts can occur while the current conflicts are being resolved. 99 * <P> 100 * The following kinds of information can be obtained from a <code>SyncResolver</code> 101 * object: 102 * <P> 103 * <h3>What operation was being attempted when a conflict occurred</h3> 104 * The <code>SyncProvider</code> interface defines four constants 105 * describing states that may occur. Three 106 * constants describe the type of operation (update, delete, or insert) that a 107 * <code>RowSet</code> object was attempting to perform when a conflict was discovered, 108 * and the fourth indicates that there is no conflict. 109 * These constants are the possible return values when a <code>SyncResolver</code> object 110 * calls the method <code>getStatus</code>. 111 * <PRE> 112 * {@code int operation = resolver.getStatus(); } 113 * </PRE> 114 * <P> 115 * <h3>The value in the data source that caused a conflict</h3> 116 * A conflict exists when a value that a <code>RowSet</code> object has changed 117 * and is attempting to write to the data source 118 * has also been changed in the data source since the last synchronization. An 119 * application can call the <code>SyncResolver</code> method 120 * <code>getConflictValue</code > to retrieve the 121 * value in the data source that is the cause of the conflict because the values in a 122 * <code>SyncResolver</code> object are the conflict values from the data source. 123 * <PRE> 124 * java.lang.Object conflictValue = resolver.getConflictValue(2); 125 * </PRE> 126 * Note that the column in <i>resolver</i> can be designated by the column number, 127 * as is done in the preceding line of code, or by the column name. 128 * <P> 129 * With the information retrieved from the methods <code>getStatus</code> and 130 * <code>getConflictValue</code>, the application may make a determination as to 131 * which value should be persisted in the data source. The application then calls the 132 * <code>SyncResolver</code> method <code>setResolvedValue</code>, which sets the value 133 * to be persisted in the <code>RowSet</code> object and also in the data source. 134 * <PRE> 135 * resolver.setResolvedValue("DEPT", 8390426); 136 * </PRE> 137 * In the preceding line of code, 138 * the column name designates the column in the <code>RowSet</code> object 139 * that is to be set with the given value. The column number can also be used to 140 * designate the column. 141 * <P> 142 * An application calls the method <code>setResolvedValue</code> after it has 143 * resolved all of the conflicts in the current conflict row and repeats this process 144 * for each conflict row in the <code>SyncResolver</code> object. 145 * <P> 146 * 147 * <H2>Navigating a <code>SyncResolver</code> Object</H2> 148 * 149 * Because a <code>SyncResolver</code> object is a <code>RowSet</code> object, an 150 * application can use all of the <code>RowSet</code> methods for moving the cursor 151 * to navigate a <code>SyncResolver</code> object. For example, an application can 152 * use the <code>RowSet</code> method <code>next</code> to get to each row and then 153 * call the <code>SyncResolver</code> method <code>getStatus</code> to see if the row 154 * contains a conflict. In a row with one or more conflicts, the application can 155 * iterate through the columns to find any non-null values, which will be the values 156 * from the data source that are in conflict. 157 * <P> 158 * To make it easier to navigate a <code>SyncResolver</code> object, especially when 159 * there are large numbers of rows with no conflicts, the <code>SyncResolver</code> 160 * interface defines the methods <code>nextConflict</code> and 161 * <code>previousConflict</code>, which move only to rows 162 * that contain at least one conflict value. Then an application can call the 163 * <code>SyncResolver</code> method <code>getConflictValue</code>, supplying it 164 * with the column number, to get the conflict value itself. The code fragment in the 165 * next section gives an example. 166 * 167 * <H2>Code Example</H2> 168 * 169 * The following code fragment demonstrates how a disconnected <code>RowSet</code> 170 * object <i>crs</i> might attempt to synchronize itself with the 171 * underlying data source and then resolve the conflicts. In the <code>try</code> 172 * block, <i>crs</i> calls the method <code>acceptChanges</code>, passing it the 173 * <code>Connection</code> object <i>con</i>. If there are no conflicts, the 174 * changes in <i>crs</i> are simply written to the data source. However, if there 175 * is a conflict, the method <code>acceptChanges</code> throws a 176 * <code>SyncProviderException</code> object, and the 177 * <code>catch</code> block takes effect. In this example, which 178 * illustrates one of the many ways a <code>SyncResolver</code> object can be used, 179 * the <code>SyncResolver</code> method <code>nextConflict</code> is used in a 180 * <code>while</code> loop. The loop will end when <code>nextConflict</code> returns 181 * <code>false</code>, which will occur when there are no more conflict rows in the 182 * <code>SyncResolver</code> object <i>resolver</i>. In This particular code fragment, 183 * <i>resolver</i> looks for rows that have update conflicts (rows with the status 184 * <code>SyncResolver.UPDATE_ROW_CONFLICT</code>), and the rest of this code fragment 185 * executes only for rows where conflicts occurred because <i>crs</i> was attempting an 186 * update. 187 * <P> 188 * After the cursor for <i>resolver</i> has moved to the next conflict row that 189 * has an update conflict, the method <code>getRow</code> indicates the number of the 190 * current row, and 191 * the cursor for the <code>CachedRowSet</code> object <i>crs</i> is moved to 192 * the comparable row in <i>crs</i>. By iterating 193 * through the columns of that row in both <i>resolver</i> and <i>crs</i>, the conflicting 194 * values can be retrieved and compared to decide which one should be persisted. In this 195 * code fragment, the value in <i>crs</i> is the one set as the resolved value, which means 196 * that it will be used to overwrite the conflict value in the data source. 197 * 198 * <PRE> 199 * {@code 200 * try { 201 * 202 * crs.acceptChanges(con); 203 * 204 * } catch (SyncProviderException spe) { 205 * 206 * SyncResolver resolver = spe.getSyncResolver(); 207 * 208 * Object crsValue; // value in the RowSet object 209 * Object resolverValue: // value in the SyncResolver object 210 * Object resolvedValue: // value to be persisted 211 * 212 * while(resolver.nextConflict()) { 213 * if(resolver.getStatus() == SyncResolver.UPDATE_ROW_CONFLICT) { 214 * int row = resolver.getRow(); 215 * crs.absolute(row); 216 * 217 * int colCount = crs.getMetaData().getColumnCount(); 218 * for(int j = 1; j <= colCount; j++) { 219 * if (resolver.getConflictValue(j) != null) { 220 * crsValue = crs.getObject(j); 221 * resolverValue = resolver.getConflictValue(j); 222 * . . . 223 * // compare crsValue and resolverValue to determine 224 * // which should be the resolved value (the value to persist) 225 * resolvedValue = crsValue; 226 * 227 * resolver.setResolvedValue(j, resolvedValue); 228 * } 229 * } 230 * } 231 * } 232 * } 233 * }</PRE> 234 * @author Jonathan Bruce 235 */ 236 237 public interface SyncResolver extends RowSet { 238 /** 239 * Indicates that a conflict occurred while the <code>RowSet</code> object was 240 * attempting to update a row in the data source. 241 * The values in the data source row to be updated differ from the 242 * <code>RowSet</code> object's original values for that row, which means that 243 * the row in the data source has been updated or deleted since the last 244 * synchronization. 245 */ 246 public static int UPDATE_ROW_CONFLICT = 0; 247 248 /** 249 * Indicates that a conflict occurred while the <code>RowSet</code> object was 250 * attempting to delete a row in the data source. 251 * The values in the data source row to be updated differ from the 252 * <code>RowSet</code> object's original values for that row, which means that 253 * the row in the data source has been updated or deleted since the last 254 * synchronization. 255 */ 256 public static int DELETE_ROW_CONFLICT = 1; 257 258 /** 259 * Indicates that a conflict occurred while the <code>RowSet</code> object was 260 * attempting to insert a row into the data source. This means that a 261 * row with the same primary key as the row to be inserted has been inserted 262 * into the data source since the last synchronization. 263 */ 264 public static int INSERT_ROW_CONFLICT = 2; 265 266 /** 267 * Indicates that <b>no</b> conflict occured while the <code>RowSet</code> object 268 * was attempting to update, delete or insert a row in the data source. The values in 269 * the <code>SyncResolver</code> will contain <code>null</code> values only as an indication 270 * that no information in pertitent to the conflict resolution in this row. 271 */ 272 public static int NO_ROW_CONFLICT = 3; 273 274 /** 275 * Retrieves the conflict status of the current row of this <code>SyncResolver</code>, 276 * which indicates the operation 277 * the <code>RowSet</code> object was attempting when the conflict occurred. 278 * 279 * @return one of the following constants: 280 * <code>SyncResolver.UPDATE_ROW_CONFLICT</code>, 281 * <code>SyncResolver.DELETE_ROW_CONFLICT</code>, 282 * <code>SyncResolver.INSERT_ROW_CONFLICT</code>, or 283 * <code>SyncResolver.NO_ROW_CONFLICT</code> 284 */ 285 public int getStatus(); 286 287 /** 288 * Retrieves the value in the designated column in the current row of this 289 * <code>SyncResolver</code> object, which is the value in the data source 290 * that caused a conflict. 291 * 292 * @param index an <code>int</code> designating the column in this row of this 293 * <code>SyncResolver</code> object from which to retrieve the value 294 * causing a conflict 295 * @return the value of the designated column in the current row of this 296 * <code>SyncResolver</code> object 297 * @throws SQLException if a database access error occurs 298 */ 299 public Object getConflictValue(int index) throws SQLException; 300 301 /** 302 * Retrieves the value in the designated column in the current row of this 303 * <code>SyncResolver</code> object, which is the value in the data source 304 * that caused a conflict. 305 * 306 * @param columnName a <code>String</code> object designating the column in this row of this 307 * <code>SyncResolver</code> object from which to retrieve the value 308 * causing a conflict 309 * @return the value of the designated column in the current row of this 310 * <code>SyncResolver</code> object 311 * @throws SQLException if a database access error occurs 312 */ 313 public Object getConflictValue(String columnName) throws SQLException; 314 315 /** 316 * Sets <i>obj</i> as the value in column <i>index</i> in the current row of the 317 * <code>RowSet</code> object that is being synchronized. <i>obj</i> 318 * is set as the value in the data source internally. 319 * 320 * @param index an <code>int</code> giving the number of the column into which to 321 * set the value to be persisted 322 * @param obj an <code>Object</code> that is the value to be set in the 323 * <code>RowSet</code> object and persisted in the data source 324 * @throws SQLException if a database access error occurs 325 */ 326 public void setResolvedValue(int index, Object obj) throws SQLException; 327 328 /** 329 * Sets <i>obj</i> as the value in column <i>columnName</i> in the current row of the 330 * <code>RowSet</code> object that is being synchronized. <i>obj</i> 331 * is set as the value in the data source internally. 332 * 333 * @param columnName a <code>String</code> object giving the name of the column 334 * into which to set the value to be persisted 335 * @param obj an <code>Object</code> that is the value to be set in the 336 * <code>RowSet</code> object and persisted in the data source 337 * @throws SQLException if a database access error occurs 338 */ 339 public void setResolvedValue(String columnName, Object obj) throws SQLException; 340 341 /** 342 * Moves the cursor down from its current position to the next row that contains 343 * a conflict value. A <code>SyncResolver</code> object's 344 * cursor is initially positioned before the first conflict row; the first call to the 345 * method <code>nextConflict</code> makes the first conflict row the current row; 346 * the second call makes the second conflict row the current row, and so on. 347 * <p> 348 * A call to the method <code>nextConflict</code> will implicitly close 349 * an input stream if one is open and will clear the <code>SyncResolver</code> 350 * object's warning chain. 351 * 352 * @return <code>true</code> if the new current row is valid; <code>false</code> 353 * if there are no more rows 354 * @throws SQLException if a database access error occurs or the result set type 355 * is <code>TYPE_FORWARD_ONLY</code> 356 * 357 */ 358 public boolean nextConflict() throws SQLException; 359 360 /** 361 * Moves the cursor up from its current position to the previous conflict 362 * row in this <code>SyncResolver</code> object. 363 * <p> 364 * A call to the method <code>previousConflict</code> will implicitly close 365 * an input stream if one is open and will clear the <code>SyncResolver</code> 366 * object's warning chain. 367 * 368 * @return <code>true</code> if the cursor is on a valid row; <code>false</code> 369 * if it is off the result set 370 * @throws SQLException if a database access error occurs or the result set type 371 * is <code>TYPE_FORWARD_ONLY</code> 372 */ 373 public boolean previousConflict() throws SQLException; 374 375 }