2362N/A * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2362N/A * or visit www.oracle.com if you need additional information or have any 0N/A * Defines a framework that allows applications to use a manual decision tree 0N/A * to decide what should be done when a synchronization conflict occurs. 0N/A * Although it is not mandatory for 0N/A * applications to resolve synchronization conflicts manually, this 0N/A * framework provides the means to delegate to the application when conflicts 0N/A * Note that a conflict is a situation where the <code>RowSet</code> object's original 0N/A * values for a row do not match the values in the data source, which indicates that 0N/A * the data source row has been modified since the last synchronization. Note also that 0N/A * a <code>RowSet</code> object's original values are the values it had just prior to the 0N/A * the last synchronization, which are not necessarily its initial values. 0N/A * <H2>Description of a <code>SyncResolver</code> Object</H2> 0N/A * A <code>SyncResolver</code> object is a specialized <code>RowSet</code> object 0N/A * that implements the <code>SyncResolver</code> interface. 0N/A * It <b>may</b> operate as either a connected <code>RowSet</code> object (an 0N/A * implementation of the <code>JdbcRowSet</code> interface) or a connected 0N/A * <code>RowSet</code> object (an implementation of the 0N/A * <code>CachedRowSet</code> interface or one of its subinterfaces). For information 0N/A * on the subinterfaces, see the 0N/A * description. The reference implementation for <code>SyncResolver</code> implements 0N/A * the <code>CachedRowSet</code> interface, but other implementations 0N/A * may choose to implement the <code>JdbcRowSet</code> interface to satisfy 0N/A * After an application has attempted to synchronize a <code>RowSet</code> object with 0N/A * the data source (by calling the <code>CachedRowSet</code> 0N/A * method <code>acceptChanges</code>), and one or more conflicts have been found, 0N/A * a rowset's <code>SyncProvider</code> object creates an instance of 0N/A * <code>SyncResolver</code>. This new <code>SyncResolver</code> object has 0N/A * the same number of rows and columns as the 0N/A * <code>RowSet</code> object that was attempting the synchronization. The 0N/A * <code>SyncResolver</code> object contains the values from the data source that caused 0N/A * the conflict(s) and <code>null</code> for all other values. 0N/A * In addition, it contains information about each conflict. 0N/A * <H2>Getting and Using a <code>SyncResolver</code> Object</H2> 0N/A * When the method <code>acceptChanges</code> encounters conflicts, the 0N/A * <code>SyncProvider</code> object creates a <code>SyncProviderException</code> 0N/A * object and sets it with the new <code>SyncResolver</code> object. The method 0N/A * <code>acceptChanges</code> will throw this exception, which 0N/A * the application can then catch and use to retrieve the 0N/A * <code>SyncResolver</code> object it contains. The following code snippet uses the 0N/A * <code>SyncProviderException</code> method <code>getSyncResolver</code> to get 0N/A * the <code>SyncResolver</code> object <i>resolver</i>. 0N/A * } catch (SyncProviderException spe) { 0N/A * SyncResolver resolver = spe.getSyncResolver(); 0N/A * With <i>resolver</i> in hand, an application can use it to get the information 0N/A * it contains about the conflict or conflicts. A <code>SyncResolver</code> object 0N/A * such as <i>resolver</i> keeps 0N/A * track of the conflicts for each row in which there is a conflict. It also places a 0N/A * lock on the table or tables affected by the rowset's command so that no more 0N/A * conflicts can occur while the current conflicts are being resolved. 0N/A * The following kinds of information can be obtained from a <code>SyncResolver</code> 0N/A * <LI>What operation was being attempted when a conflict occurred<BR> 0N/A * The <code>SyncProvider</code> interface defines four constants 0N/A * describing states that may occur. Three 0N/A * constants describe the type of operation (update, delete, or insert) that a 0N/A * <code>RowSet</code> object was attempting to perform when a conflict was discovered, 0N/A * and the fourth indicates that there is no conflict. 0N/A * These constants are the possible return values when a <code>SyncResolver</code> object 0N/A * calls the method <code>getStatus</code>. 0N/A * int operation = resolver.getStatus(); 0N/A * <LI>The value in the data source that caused a conflict<BR> 0N/A * A conflict exists when a value that a <code>RowSet</code> object has changed 0N/A * and is attempting to write to the data source 0N/A * has also been changed in the data source since the last synchronization. An 0N/A * application can call the <code>SyncResolver</code> method 0N/A * <code>getConflictValue</code > to retrieve the 0N/A * value in the data source that is the cause of the conflict because the values in a 0N/A * <code>SyncResolver</code> object are the conflict values from the data source. 0N/A * java.lang.Object conflictValue = resolver.getConflictValue(2); 0N/A * Note that the column in <i>resolver</i> can be designated by the column number, 0N/A * as is done in the preceding line of code, or by the column name. 0N/A * With the information retrieved from the methods <code>getStatus</code> and 0N/A * <code>getConflictValue</code>, the application may make a determination as to 0N/A * which value should be persisted in the data source. The application then calls the 0N/A * <code>SyncResolver</code> method <code>setResolvedValue</code>, which sets the value 0N/A * to be persisted in the <code>RowSet</code> object and also in the data source. 0N/A * resolver.setResolvedValue("DEPT", 8390426); 0N/A * In the preceding line of code, 0N/A * the column name designates the column in the <code>RowSet</code> object 0N/A * that is to be set with the given value. The column number can also be used to 0N/A * designate the column. 0N/A * An application calls the method <code>setResolvedValue</code> after it has 0N/A * resolved all of the conflicts in the current conflict row and repeats this process 0N/A * for each conflict row in the <code>SyncResolver</code> object. 0N/A * <H2>Navigating a <code>SyncResolver</code> Object</H2> 0N/A * Because a <code>SyncResolver</code> object is a <code>RowSet</code> object, an 0N/A * application can use all of the <code>RowSet</code> methods for moving the cursor 0N/A * to navigate a <code>SyncResolver</code> object. For example, an application can 0N/A * use the <code>RowSet</code> method <code>next</code> to get to each row and then 0N/A * call the <code>SyncResolver</code> method <code>getStatus</code> to see if the row 0N/A * contains a conflict. In a row with one or more conflicts, the application can 0N/A * iterate through the columns to find any non-null values, which will be the values 0N/A * from the data source that are in conflict. 0N/A * To make it easier to navigate a <code>SyncResolver</code> object, especially when 0N/A * there are large numbers of rows with no conflicts, the <code>SyncResolver</code> 0N/A * interface defines the methods <code>nextConflict</code> and 0N/A * <code>previousConflict</code>, which move only to rows 0N/A * that contain at least one conflict value. Then an application can call the 0N/A * <code>SyncResolver</code> method <code>getConflictValue</code>, supplying it 0N/A * with the column number, to get the conflict value itself. The code fragment in the 0N/A * next section gives an example. 0N/A * <H2>Code Example</H2> 0N/A * The following code fragment demonstrates how a disconnected <code>RowSet</code> 0N/A * object <i>crs</i> might attempt to synchronize itself with the 0N/A * underlying data source and then resolve the conflicts. In the <code>try</code> 0N/A * block, <i>crs</i> calls the method <code>acceptChanges</code>, passing it the 0N/A * <code>Connection</code> object <i>con</i>. If there are no conflicts, the 0N/A * changes in <i>crs</i> are simply written to the data source. However, if there 0N/A * is a conflict, the method <code>acceptChanges</code> throws a 0N/A * <code>SyncProviderException</code> object, and the 0N/A * <code>catch</code> block takes effect. In this example, which 0N/A * illustrates one of the many ways a <code>SyncResolver</code> object can be used, 0N/A * the <code>SyncResolver</code> method <code>nextConflict</code> is used in a 0N/A * <code>while</code> loop. The loop will end when <code>nextConflict</code> returns 0N/A * <code>false</code>, which will occur when there are no more conflict rows in the 0N/A * <code>SyncResolver</code> object <i>resolver</i>. In This particular code fragment, 0N/A * <i>resolver</i> looks for rows that have update conflicts (rows with the status 0N/A * <code>SyncResolver.UPDATE_ROW_CONFLICT</code>), and the rest of this code fragment 0N/A * executes only for rows where conflicts occurred because <i>crs</i> was attempting an 0N/A * After the cursor for <i>resolver</i> has moved to the next conflict row that 0N/A * has an update conflict, the method <code>getRow</code> indicates the number of the 0N/A * the cursor for the <code>CachedRowSet</code> object <i>crs</i> is moved to 0N/A * the comparable row in <i>crs</i>. By iterating 0N/A * through the columns of that row in both <i>resolver</i> and <i>crs</i>, the conflicting 0N/A * values can be retrieved and compared to decide which one should be persisted. In this 0N/A * code fragment, the value in <i>crs</i> is the one set as the resolved value, which means 0N/A * that it will be used to overwrite the conflict value in the data source. 0N/A * crs.acceptChanges(con); 0N/A * } catch (SyncProviderException spe) { 0N/A * SyncResolver resolver = spe.getSyncResolver(); 0N/A * Object crsValue; // value in the <code>RowSet</code> object 0N/A * Object resolverValue: // value in the <code>SyncResolver</code> object 0N/A * Object resolvedValue: // value to be persisted 0N/A * while(resolver.nextConflict()) { 0N/A * if(resolver.getStatus() == SyncResolver.UPDATE_ROW_CONFLICT) { 0N/A * int row = resolver.getRow(); 0N/A * crs.absolute(row); 0N/A * int colCount = crs.getMetaData().getColumnCount(); 0N/A * for(int j = 1; j <= colCount; j++) { 0N/A * if (resolver.getConflictValue(j) != null) { 0N/A * crsValue = crs.getObject(j); 0N/A * resolverValue = resolver.getConflictValue(j); 0N/A * // compare crsValue and resolverValue to determine 0N/A * // which should be the resolved value (the value to persist) 0N/A * resolvedValue = crsValue; 0N/A * resolver.setResolvedValue(j, resolvedValue); 0N/A * @author Jonathan Bruce 0N/A * Indicates that a conflict occurred while the <code>RowSet</code> object was 0N/A * attempting to update a row in the data source. 0N/A * The values in the data source row to be updated differ from the 0N/A * <code>RowSet</code> object's original values for that row, which means that 0N/A * the row in the data source has been updated or deleted since the last 0N/A * Indicates that a conflict occurred while the <code>RowSet</code> object was 0N/A * attempting to delete a row in the data source. 0N/A * The values in the data source row to be updated differ from the 0N/A * <code>RowSet</code> object's original values for that row, which means that 0N/A * the row in the data source has been updated or deleted since the last 0N/A * Indicates that a conflict occurred while the <code>RowSet</code> object was 0N/A * attempting to insert a row into the data source. This means that a 0N/A * row with the same primary key as the row to be inserted has been inserted 0N/A * into the data source since the last synchronization. 0N/A * Indicates that <b>no</b> conflict occured while the <code>RowSet</code> object 0N/A * was attempting to update, delete or insert a row in the data source. The values in 0N/A * the <code>SyncResolver</code> will contain <code>null</code> values only as an indication 0N/A * that no information in pertitent to the conflict resolution in this row. 0N/A * Retrieves the conflict status of the current row of this <code>SyncResolver</code>, 0N/A * which indicates the operation 0N/A * the <code>RowSet</code> object was attempting when the conflict occurred. 0N/A * @return one of the following constants: 0N/A * <code>SyncResolver.UPDATE_ROW_CONFLICT</code>, 0N/A * <code>SyncResolver.DELETE_ROW_CONFLICT</code>, 0N/A * <code>SyncResolver.INSERT_ROW_CONFLICT</code>, or 0N/A * <code>SyncResolver.NO_ROW_CONFLICT</code> 0N/A * Retrieves the value in the designated column in the current row of this 0N/A * <code>SyncResolver</code> object, which is the value in the data source 0N/A * that caused a conflict. 0N/A * @param index an <code>int</code> designating the column in this row of this 0N/A * <code>SyncResolver</code> object from which to retrieve the value 0N/A * causing a conflict 0N/A * @return the value of the designated column in the current row of this 0N/A * <code>SyncResolver</code> object 0N/A * @throws SQLException if a database access error occurs 0N/A * Retrieves the value in the designated column in the current row of this 0N/A * <code>SyncResolver</code> object, which is the value in the data source 0N/A * that caused a conflict. 0N/A * @param columnName a <code>String</code> object designating the column in this row of this 0N/A * <code>SyncResolver</code> object from which to retrieve the value 0N/A * causing a conflict 0N/A * @return the value of the designated column in the current row of this 0N/A * <code>SyncResolver</code> object 0N/A * @throws SQLException if a database access error occurs 0N/A * Sets <i>obj</i> as the value in column <i>index</i> in the current row of the 0N/A * <code>RowSet</code> object that is being synchronized. <i>obj</i> 0N/A * is set as the value in the data source internally. 0N/A * @param index an <code>int</code> giving the number of the column into which to 0N/A * set the value to be persisted 0N/A * @param obj an <code>Object</code> that is the value to be set in the 0N/A * <code>RowSet</code> object and persisted in the data source 0N/A * @throws SQLException if a database access error occurs 0N/A * Sets <i>obj</i> as the value in column <i>columnName</i> in the current row of the 0N/A * <code>RowSet</code> object that is being synchronized. <i>obj</i> 0N/A * is set as the value in the data source internally. 0N/A * @param columnName a <code>String</code> object giving the name of the column 0N/A * into which to set the value to be persisted 0N/A * @param obj an <code>Object</code> that is the value to be set in the 0N/A * <code>RowSet</code> object and persisted in the data source 0N/A * @throws SQLException if a database access error occurs 0N/A * Moves the cursor down from its current position to the next row that contains 0N/A * a conflict value. A <code>SyncResolver</code> object's 0N/A * cursor is initially positioned before the first conflict row; the first call to the 0N/A * method <code>nextConflict</code> makes the first conflict row the current row; 0N/A * the second call makes the second conflict row the current row, and so on. 0N/A * A call to the method <code>nextConflict</code> will implicitly close 0N/A * an input stream if one is open and will clear the <code>SyncResolver</code> 0N/A * object's warning chain. 0N/A * @return <code>true</code> if the new current row is valid; <code>false</code> 0N/A * if there are no more rows 0N/A * @throws SQLException if a database access error occurs or the result set type 0N/A * is <code>TYPE_FORWARD_ONLY</code> 0N/A * Moves the cursor up from its current position to the previous conflict 0N/A * row in this <code>SyncResolver</code> object. 0N/A * A call to the method <code>previousConflict</code> will implicitly close 0N/A * an input stream if one is open and will clear the <code>SyncResolver</code> 0N/A * object's warning chain. 0N/A * @return <code>true</code> if the cursor is on a valid row; <code>false</code> 0N/A * if it is off the result set 0N/A * @throws SQLException if a database access error occurs or the result set type 0N/A * is <code>TYPE_FORWARD_ONLY</code>