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  *
  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  *
  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  *
 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  *
 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  *
 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  *
 235  * @author  Jonathan Bruce
 236  * @since 1.5
 237  */
 238 
 239 public interface SyncResolver extends RowSet {
 240     /**
 241      * Indicates that a conflict occurred while the <code>RowSet</code> object was
 242      * attempting to update a row in the data source.
 243      * The values in the data source row to be updated differ from the
 244      * <code>RowSet</code> object's original values for that row, which means that
 245      * the row in the data source has been updated or deleted since the last
 246      * synchronization.
 247      */
 248      public static int UPDATE_ROW_CONFLICT = 0;
 249 
 250     /**
 251      * Indicates that a conflict occurred while the <code>RowSet</code> object was
 252      * attempting to delete a row in the data source.
 253      * The values in the data source row to be updated differ from the
 254      * <code>RowSet</code> object's original values for that row, which means that
 255      * the row in the data source has been updated or deleted since the last
 256      * synchronization.
 257      */
 258     public static int DELETE_ROW_CONFLICT = 1;
 259 
 260    /**
 261     * Indicates that a conflict occurred while the <code>RowSet</code> object was
 262     * attempting to insert a row into the data source.  This means that a
 263     * row with the same primary key as the row to be inserted has been inserted
 264     * into the data source since the last synchronization.
 265     */
 266     public static int INSERT_ROW_CONFLICT = 2;
 267 
 268     /**
 269      * Indicates that <b>no</b> conflict occurred while the <code>RowSet</code> object
 270      * was attempting to update, delete or insert a row in the data source. The values in
 271      * the <code>SyncResolver</code> will contain <code>null</code> values only as an indication
 272      * that no information in pertinent to the conflict resolution in this row.
 273      */
 274     public static int NO_ROW_CONFLICT = 3;
 275 
 276     /**
 277      * Retrieves the conflict status of the current row of this <code>SyncResolver</code>,
 278      * which indicates the operation
 279      * the <code>RowSet</code> object was attempting when the conflict occurred.
 280      *
 281      * @return one of the following constants:
 282      *         <code>SyncResolver.UPDATE_ROW_CONFLICT</code>,
 283      *         <code>SyncResolver.DELETE_ROW_CONFLICT</code>,
 284      *         <code>SyncResolver.INSERT_ROW_CONFLICT</code>, or
 285      *         <code>SyncResolver.NO_ROW_CONFLICT</code>
 286      */
 287     public int getStatus();
 288 
 289     /**
 290      * Retrieves the value in the designated column in the current row of this
 291      * <code>SyncResolver</code> object, which is the value in the data source
 292      * that caused a conflict.
 293      *
 294      * @param index an <code>int</code> designating the column in this row of this
 295      *        <code>SyncResolver</code> object from which to retrieve the value
 296      *        causing a conflict
 297      * @return the value of the designated column in the current row of this
 298      *         <code>SyncResolver</code> object
 299      * @throws SQLException if a database access error occurs
 300      */
 301     public Object getConflictValue(int index) throws SQLException;
 302 
 303     /**
 304      * Retrieves the value in the designated column in the current row of this
 305      * <code>SyncResolver</code> object, which is the value in the data source
 306      * that caused a conflict.
 307      *
 308      * @param columnName a <code>String</code> object designating the column in this row of this
 309      *        <code>SyncResolver</code> object from which to retrieve the value
 310      *        causing a conflict
 311      * @return the value of the designated column in the current row of this
 312      *         <code>SyncResolver</code> object
 313      * @throws SQLException if a database access error occurs
 314      */
 315     public Object getConflictValue(String columnName) throws SQLException;
 316 
 317     /**
 318      * Sets <i>obj</i> as the value in column <i>index</i> in the current row of the
 319      * <code>RowSet</code> object that is being synchronized. <i>obj</i>
 320      * is set as the value in the data source internally.
 321      *
 322      * @param index an <code>int</code> giving the number of the column into which to
 323      *        set the value to be persisted
 324      * @param obj an <code>Object</code> that is the value to be set in the
 325      *        <code>RowSet</code> object and persisted in the data source
 326      * @throws SQLException if a database access error occurs
 327      */
 328     public void setResolvedValue(int index, Object obj) throws SQLException;
 329 
 330     /**
 331      * Sets <i>obj</i> as the value in column <i>columnName</i> in the current row of the
 332      * <code>RowSet</code> object that is being synchronized. <i>obj</i>
 333      * is set as the value in the data source internally.
 334      *
 335      * @param columnName a <code>String</code> object giving the name of the column
 336      *        into which to set the value to be persisted
 337      * @param obj an <code>Object</code> that is the value to be set in the
 338      *        <code>RowSet</code> object and persisted in the data source
 339      * @throws SQLException if a database access error occurs
 340      */
 341     public void setResolvedValue(String columnName, Object obj) throws SQLException;
 342 
 343     /**
 344      * Moves the cursor down from its current position to the next row that contains
 345      * a conflict value. A <code>SyncResolver</code> object's
 346      * cursor is initially positioned before the first conflict row; the first call to the
 347      * method <code>nextConflict</code> makes the first conflict row the current row;
 348      * the second call makes the second conflict row the current row, and so on.
 349      * <p>
 350      * A call to the method <code>nextConflict</code> will implicitly close
 351      * an input stream if one is open and will clear the <code>SyncResolver</code>
 352      * object's warning chain.
 353      *
 354      * @return <code>true</code> if the new current row is valid; <code>false</code>
 355      *         if there are no more rows
 356      * @throws SQLException if a database access error occurs or the result set type
 357      *     is <code>TYPE_FORWARD_ONLY</code>
 358      *
 359      */
 360     public boolean nextConflict() throws SQLException;
 361 
 362     /**
 363      * Moves the cursor up from its current position to the previous conflict
 364      * row in this <code>SyncResolver</code> object.
 365      * <p>
 366      * A call to the method <code>previousConflict</code> will implicitly close
 367      * an input stream if one is open and will clear the <code>SyncResolver</code>
 368      * object's warning chain.
 369      *
 370      * @return <code>true</code> if the cursor is on a valid row; <code>false</code>
 371      *     if it is off the result set
 372      * @throws SQLException if a database access error occurs or the result set type
 373      *     is <code>TYPE_FORWARD_ONLY</code>
 374      */
 375     public boolean previousConflict() throws SQLException;
 376 
 377 }