Path: blob/master/src/java.sql.rowset/share/classes/javax/sql/rowset/spi/SyncResolver.java
40948 views
/*1* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.sql.rowset.spi;2627import javax.sql.RowSet;28import java.sql.SQLException;2930/**31* Defines a framework that allows applications to use a manual decision tree32* to decide what should be done when a synchronization conflict occurs.33* Although it is not mandatory for34* applications to resolve synchronization conflicts manually, this35* framework provides the means to delegate to the application when conflicts36* arise.37* <p>38* Note that a conflict is a situation where the <code>RowSet</code> object's original39* values for a row do not match the values in the data source, which indicates that40* the data source row has been modified since the last synchronization. Note also that41* a <code>RowSet</code> object's original values are the values it had just prior to the42* the last synchronization, which are not necessarily its initial values.43*44*45* <H2>Description of a <code>SyncResolver</code> Object</H2>46*47* A <code>SyncResolver</code> object is a specialized <code>RowSet</code> object48* that implements the <code>SyncResolver</code> interface.49* It <b>may</b> operate as either a connected <code>RowSet</code> object (an50* implementation of the <code>JdbcRowSet</code> interface) or a connected51* <code>RowSet</code> object (an implementation of the52* <code>CachedRowSet</code> interface or one of its subinterfaces). For information53* on the subinterfaces, see the54* <a href="../package-summary.html"><code>javax.sql.rowset</code></a> package55* description. The reference implementation for <code>SyncResolver</code> implements56* the <code>CachedRowSet</code> interface, but other implementations57* may choose to implement the <code>JdbcRowSet</code> interface to satisfy58* particular needs.59* <P>60* After an application has attempted to synchronize a <code>RowSet</code> object with61* the data source (by calling the <code>CachedRowSet</code>62* method <code>acceptChanges</code>), and one or more conflicts have been found,63* a rowset's <code>SyncProvider</code> object creates an instance of64* <code>SyncResolver</code>. This new <code>SyncResolver</code> object has65* the same number of rows and columns as the66* <code>RowSet</code> object that was attempting the synchronization. The67* <code>SyncResolver</code> object contains the values from the data source that caused68* the conflict(s) and <code>null</code> for all other values.69* In addition, it contains information about each conflict.70*71*72* <H2>Getting and Using a <code>SyncResolver</code> Object</H2>73*74* When the method <code>acceptChanges</code> encounters conflicts, the75* <code>SyncProvider</code> object creates a <code>SyncProviderException</code>76* object and sets it with the new <code>SyncResolver</code> object. The method77* <code>acceptChanges</code> will throw this exception, which78* the application can then catch and use to retrieve the79* <code>SyncResolver</code> object it contains. The following code snippet uses the80* <code>SyncProviderException</code> method <code>getSyncResolver</code> to get81* the <code>SyncResolver</code> object <i>resolver</i>.82* <PRE>83* {@code84* } catch (SyncProviderException spe) {85* SyncResolver resolver = spe.getSyncResolver();86* ...87* }88*89* }90* </PRE>91* <P>92* With <i>resolver</i> in hand, an application can use it to get the information93* it contains about the conflict or conflicts. A <code>SyncResolver</code> object94* such as <i>resolver</i> keeps95* track of the conflicts for each row in which there is a conflict. It also places a96* lock on the table or tables affected by the rowset's command so that no more97* conflicts can occur while the current conflicts are being resolved.98* <P>99* The following kinds of information can be obtained from a <code>SyncResolver</code>100* object:101*102* <h3>What operation was being attempted when a conflict occurred</h3>103* The <code>SyncProvider</code> interface defines four constants104* describing states that may occur. Three105* constants describe the type of operation (update, delete, or insert) that a106* <code>RowSet</code> object was attempting to perform when a conflict was discovered,107* and the fourth indicates that there is no conflict.108* These constants are the possible return values when a <code>SyncResolver</code> object109* calls the method <code>getStatus</code>.110* <PRE>111* {@code int operation = resolver.getStatus(); }112* </PRE>113*114* <h3>The value in the data source that caused a conflict</h3>115* A conflict exists when a value that a <code>RowSet</code> object has changed116* and is attempting to write to the data source117* has also been changed in the data source since the last synchronization. An118* application can call the <code>SyncResolver</code> method119* <code>getConflictValue</code > to retrieve the120* value in the data source that is the cause of the conflict because the values in a121* <code>SyncResolver</code> object are the conflict values from the data source.122* <PRE>123* java.lang.Object conflictValue = resolver.getConflictValue(2);124* </PRE>125* Note that the column in <i>resolver</i> can be designated by the column number,126* as is done in the preceding line of code, or by the column name.127* <P>128* With the information retrieved from the methods <code>getStatus</code> and129* <code>getConflictValue</code>, the application may make a determination as to130* which value should be persisted in the data source. The application then calls the131* <code>SyncResolver</code> method <code>setResolvedValue</code>, which sets the value132* to be persisted in the <code>RowSet</code> object and also in the data source.133* <PRE>134* resolver.setResolvedValue("DEPT", 8390426);135* </PRE>136* In the preceding line of code,137* the column name designates the column in the <code>RowSet</code> object138* that is to be set with the given value. The column number can also be used to139* designate the column.140* <P>141* An application calls the method <code>setResolvedValue</code> after it has142* resolved all of the conflicts in the current conflict row and repeats this process143* for each conflict row in the <code>SyncResolver</code> object.144*145*146* <H2>Navigating a <code>SyncResolver</code> Object</H2>147*148* Because a <code>SyncResolver</code> object is a <code>RowSet</code> object, an149* application can use all of the <code>RowSet</code> methods for moving the cursor150* to navigate a <code>SyncResolver</code> object. For example, an application can151* use the <code>RowSet</code> method <code>next</code> to get to each row and then152* call the <code>SyncResolver</code> method <code>getStatus</code> to see if the row153* contains a conflict. In a row with one or more conflicts, the application can154* iterate through the columns to find any non-null values, which will be the values155* from the data source that are in conflict.156* <P>157* To make it easier to navigate a <code>SyncResolver</code> object, especially when158* there are large numbers of rows with no conflicts, the <code>SyncResolver</code>159* interface defines the methods <code>nextConflict</code> and160* <code>previousConflict</code>, which move only to rows161* that contain at least one conflict value. Then an application can call the162* <code>SyncResolver</code> method <code>getConflictValue</code>, supplying it163* with the column number, to get the conflict value itself. The code fragment in the164* next section gives an example.165*166* <H2>Code Example</H2>167*168* The following code fragment demonstrates how a disconnected <code>RowSet</code>169* object <i>crs</i> might attempt to synchronize itself with the170* underlying data source and then resolve the conflicts. In the <code>try</code>171* block, <i>crs</i> calls the method <code>acceptChanges</code>, passing it the172* <code>Connection</code> object <i>con</i>. If there are no conflicts, the173* changes in <i>crs</i> are simply written to the data source. However, if there174* is a conflict, the method <code>acceptChanges</code> throws a175* <code>SyncProviderException</code> object, and the176* <code>catch</code> block takes effect. In this example, which177* illustrates one of the many ways a <code>SyncResolver</code> object can be used,178* the <code>SyncResolver</code> method <code>nextConflict</code> is used in a179* <code>while</code> loop. The loop will end when <code>nextConflict</code> returns180* <code>false</code>, which will occur when there are no more conflict rows in the181* <code>SyncResolver</code> object <i>resolver</i>. In This particular code fragment,182* <i>resolver</i> looks for rows that have update conflicts (rows with the status183* <code>SyncResolver.UPDATE_ROW_CONFLICT</code>), and the rest of this code fragment184* executes only for rows where conflicts occurred because <i>crs</i> was attempting an185* update.186* <P>187* After the cursor for <i>resolver</i> has moved to the next conflict row that188* has an update conflict, the method <code>getRow</code> indicates the number of the189* current row, and190* the cursor for the <code>CachedRowSet</code> object <i>crs</i> is moved to191* the comparable row in <i>crs</i>. By iterating192* through the columns of that row in both <i>resolver</i> and <i>crs</i>, the conflicting193* values can be retrieved and compared to decide which one should be persisted. In this194* code fragment, the value in <i>crs</i> is the one set as the resolved value, which means195* that it will be used to overwrite the conflict value in the data source.196*197* <PRE>198* {@code199* try {200*201* crs.acceptChanges(con);202*203* } catch (SyncProviderException spe) {204*205* SyncResolver resolver = spe.getSyncResolver();206*207* Object crsValue; // value in the RowSet object208* Object resolverValue: // value in the SyncResolver object209* Object resolvedValue: // value to be persisted210*211* while(resolver.nextConflict()) {212* if(resolver.getStatus() == SyncResolver.UPDATE_ROW_CONFLICT) {213* int row = resolver.getRow();214* crs.absolute(row);215*216* int colCount = crs.getMetaData().getColumnCount();217* for(int j = 1; j <= colCount; j++) {218* if (resolver.getConflictValue(j) != null) {219* crsValue = crs.getObject(j);220* resolverValue = resolver.getConflictValue(j);221* . . .222* // compare crsValue and resolverValue to determine223* // which should be the resolved value (the value to persist)224* resolvedValue = crsValue;225*226* resolver.setResolvedValue(j, resolvedValue);227* }228* }229* }230* }231* }232* }</PRE>233*234* @author Jonathan Bruce235* @since 1.5236*/237238public interface SyncResolver extends RowSet {239/**240* Indicates that a conflict occurred while the <code>RowSet</code> object was241* attempting to update a row in the data source.242* The values in the data source row to be updated differ from the243* <code>RowSet</code> object's original values for that row, which means that244* the row in the data source has been updated or deleted since the last245* synchronization.246*/247public static int UPDATE_ROW_CONFLICT = 0;248249/**250* Indicates that a conflict occurred while the <code>RowSet</code> object was251* attempting to delete a row in the data source.252* The values in the data source row to be updated differ from the253* <code>RowSet</code> object's original values for that row, which means that254* the row in the data source has been updated or deleted since the last255* synchronization.256*/257public static int DELETE_ROW_CONFLICT = 1;258259/**260* Indicates that a conflict occurred while the <code>RowSet</code> object was261* attempting to insert a row into the data source. This means that a262* row with the same primary key as the row to be inserted has been inserted263* into the data source since the last synchronization.264*/265public static int INSERT_ROW_CONFLICT = 2;266267/**268* Indicates that <b>no</b> conflict occurred while the <code>RowSet</code> object269* was attempting to update, delete or insert a row in the data source. The values in270* the <code>SyncResolver</code> will contain <code>null</code> values only as an indication271* that no information in pertinent to the conflict resolution in this row.272*/273public static int NO_ROW_CONFLICT = 3;274275/**276* Retrieves the conflict status of the current row of this <code>SyncResolver</code>,277* which indicates the operation278* the <code>RowSet</code> object was attempting when the conflict occurred.279*280* @return one of the following constants:281* <code>SyncResolver.UPDATE_ROW_CONFLICT</code>,282* <code>SyncResolver.DELETE_ROW_CONFLICT</code>,283* <code>SyncResolver.INSERT_ROW_CONFLICT</code>, or284* <code>SyncResolver.NO_ROW_CONFLICT</code>285*/286public int getStatus();287288/**289* Retrieves the value in the designated column in the current row of this290* <code>SyncResolver</code> object, which is the value in the data source291* that caused a conflict.292*293* @param index an <code>int</code> designating the column in this row of this294* <code>SyncResolver</code> object from which to retrieve the value295* causing a conflict296* @return the value of the designated column in the current row of this297* <code>SyncResolver</code> object298* @throws SQLException if a database access error occurs299*/300public Object getConflictValue(int index) throws SQLException;301302/**303* Retrieves the value in the designated column in the current row of this304* <code>SyncResolver</code> object, which is the value in the data source305* that caused a conflict.306*307* @param columnName a <code>String</code> object designating the column in this row of this308* <code>SyncResolver</code> object from which to retrieve the value309* causing a conflict310* @return the value of the designated column in the current row of this311* <code>SyncResolver</code> object312* @throws SQLException if a database access error occurs313*/314public Object getConflictValue(String columnName) throws SQLException;315316/**317* Sets <i>obj</i> as the value in column <i>index</i> in the current row of the318* <code>RowSet</code> object that is being synchronized. <i>obj</i>319* is set as the value in the data source internally.320*321* @param index an <code>int</code> giving the number of the column into which to322* set the value to be persisted323* @param obj an <code>Object</code> that is the value to be set in the324* <code>RowSet</code> object and persisted in the data source325* @throws SQLException if a database access error occurs326*/327public void setResolvedValue(int index, Object obj) throws SQLException;328329/**330* Sets <i>obj</i> as the value in column <i>columnName</i> in the current row of the331* <code>RowSet</code> object that is being synchronized. <i>obj</i>332* is set as the value in the data source internally.333*334* @param columnName a <code>String</code> object giving the name of the column335* into which to set the value to be persisted336* @param obj an <code>Object</code> that is the value to be set in the337* <code>RowSet</code> object and persisted in the data source338* @throws SQLException if a database access error occurs339*/340public void setResolvedValue(String columnName, Object obj) throws SQLException;341342/**343* Moves the cursor down from its current position to the next row that contains344* a conflict value. A <code>SyncResolver</code> object's345* cursor is initially positioned before the first conflict row; the first call to the346* method <code>nextConflict</code> makes the first conflict row the current row;347* the second call makes the second conflict row the current row, and so on.348* <p>349* A call to the method <code>nextConflict</code> will implicitly close350* an input stream if one is open and will clear the <code>SyncResolver</code>351* object's warning chain.352*353* @return <code>true</code> if the new current row is valid; <code>false</code>354* if there are no more rows355* @throws SQLException if a database access error occurs or the result set type356* is <code>TYPE_FORWARD_ONLY</code>357*358*/359public boolean nextConflict() throws SQLException;360361/**362* Moves the cursor up from its current position to the previous conflict363* row in this <code>SyncResolver</code> object.364* <p>365* A call to the method <code>previousConflict</code> will implicitly close366* an input stream if one is open and will clear the <code>SyncResolver</code>367* object's warning chain.368*369* @return <code>true</code> if the cursor is on a valid row; <code>false</code>370* if it is off the result set371* @throws SQLException if a database access error occurs or the result set type372* is <code>TYPE_FORWARD_ONLY</code>373*/374public boolean previousConflict() throws SQLException;375376}377378379