View Javadoc
1   package fr.ifremer.adagio.synchro.dao;
2   
3   /*
4    * #%L
5    * SIH-Adagio :: Synchronization
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2014 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import java.io.Closeable;
27  import java.sql.Connection;
28  import java.sql.ResultSet;
29  import java.sql.SQLException;
30  import java.sql.Timestamp;
31  import java.util.Date;
32  import java.util.List;
33  import java.util.Map;
34  import java.util.Set;
35  
36  import fr.ifremer.adagio.synchro.meta.SynchroTableMetadata;
37  import fr.ifremer.adagio.synchro.service.SynchroPendingOperationBuffer;
38  
39  public interface SynchroTableDao extends Closeable {
40  
41  	Connection getConnection();
42  
43  	SynchroTableMetadata getTable();
44  
45  	void setPendingOperationBuffer(SynchroPendingOperationBuffer pendingChangesBuffer);
46  
47  	SynchroPendingOperationBuffer getPendingOperationBuffer();
48  
49  	/**
50  	 * Obtains the max update date found in table's rows
51  	 * 
52  	 * @return
53  	 * @throws SQLException
54  	 */
55  	Timestamp getLastUpdateDate() throws SQLException;
56  
57  	/**
58  	 * Count the number of rows in the table
59  	 * 
60  	 * @return
61  	 * @throws SQLException
62  	 */
63  	long count() throws SQLException;
64  
65  	/**
66  	 * Count the number of rows updated since the given date
67  	 * 
68  	 * @param fromDate
69  	 * @return
70  	 * @throws SQLException
71  	 */
72  	long countDataToUpdate(Date fromDate) throws SQLException;
73  
74  	/**
75  	 * Retrieve a id by a remote id. Only used for data synchronization (not used in referential synchronization)
76  	 * 
77  	 * @param tableName
78  	 * @param remoteId
79  	 * @return the local id, or null if remote_id not found
80  	 */
81  	Integer getIdFromRemoteId(String tableName, Integer remoteId) throws SQLException;
82  
83  	/**
84  	 * Obtains existing remoteIds
85  	 * 
86  	 * @return
87  	 * @throws SQLException
88  	 */
89  	Map<Integer, Integer> getExistingRemoteIdsMap() throws SQLException;
90  
91  	/**
92  	 * Getting all row depending of parent FK values
93  	 * 
94  	 * @param fkColumnName
95  	 *            the FK column name
96  	 * @param fkValues
97  	 *            values of the FK columns
98  	 * @return
99  	 * @throws SQLException
100 	 */
101 	ResultSet getDataByFk(String fkColumnName, Set<Integer> fkValues) throws SQLException;
102 
103 	/**
104 	 * Obtains all data updated since the given date (or all data if the given date is null
105 	 * 
106 	 * @param fromDate
107 	 * @return A Resulset with expected data.
108 	 * @throws SQLException
109 	 */
110 	ResultSet getDataToUpdate(Date fromDate) throws SQLException;
111 
112 	/**
113 	 * Obtains all primary keys of a table.
114 	 * If more than one key in tha table, use a serialization {@link SynchroTableMetadata#toPkStr}
115 	 * 
116 	 * @return
117 	 * @throws SQLException
118 	 */
119 	Set<String> getExistingPrimaryKeys() throws SQLException;
120 
121 	/**
122 	 * Obtains a row, using values of each PK
123 	 * 
124 	 * @param pk
125 	 *            values of PK column
126 	 * @return the existing row in the table
127 	 * @throws SQLException
128 	 */
129 	Object[] findByPk(List<Object> pk) throws SQLException;
130 
131 	/**
132 	 * Obtains a PK from the current ResultSet's row
133 	 * 
134 	 * @param incomingData
135 	 * @param tableBuffer
136 	 * @return
137 	 * @throws SQLException
138 	 */
139 	List<Object> getPk(ResultSet incomingData) throws SQLException;
140 
141 	/**
142 	 * Delete all rows of a table
143 	 * 
144 	 * @throws SQLException
145 	 */
146 	void deleteAll() throws SQLException;
147 
148 	/**
149 	 * Insert into a table the current row of the given {@link ResultSet}
150 	 * 
151 	 * @param pk
152 	 *            the PK of the current row (only need for logging)
153 	 * @param incomingData
154 	 *            a {@link ResultSet} with a row ready to be read ({@link ResultSet#next()} should have been call
155 	 *            before)
156 	 * @throws SQLException
157 	 */
158 	void executeInsert(ResultSet incomingData) throws SQLException;
159 
160 	/**
161 	 * Insert into a table the current row of the given incoming data
162 	 * <p/>
163 	 * Same as {@link #executeInsert(List,ResultSet)}, but using a array instead of a ResultSet.
164 	 * 
165 	 * @param incomingData
166 	 *            a array of object
167 	 * @throws SQLException
168 	 */
169 	void executeInsert(Object[] row) throws SQLException;
170 
171 	Integer executeInsertAndReturnId(ResultSet incomingData) throws SQLException;
172 
173 	/**
174 	 * Update one table's row, using the current row of the given {@link ResultSet}
175 	 * 
176 	 * @param pk
177 	 *            the PK of the row to update
178 	 * @param incomingData
179 	 *            a {@link ResultSet} with a row ready to be read ({@link ResultSet#next()} should have been call
180 	 *            before)
181 	 * @throws SQLException
182 	 */
183 	void executeUpdate(List<Object> pk, ResultSet incomingData) throws SQLException;
184 
185 	/**
186 	 * Update one table's row, using the current row of the given array of values
187 	 * <p/>
188 	 * Same as {@link #executeUpdate(List,ResultSet)}, but using a array instead of a ResultSet.
189 	 * 
190 	 * @param pk
191 	 *            the PK of the row to update
192 	 * @param row
193 	 *            a array of object
194 	 * @throws SQLException
195 	 */
196 	void executeUpdate(List<Object> pk, Object[] row) throws SQLException;
197 
198 	/**
199 	 * Flush all pending updates (i.e. pending batch statement)
200 	 * 
201 	 * @throws SQLException
202 	 */
203 	void flush() throws SQLException;
204 
205 	/**
206 	 * Obtains the number of updated rows
207 	 * 
208 	 * @return the number of updated rows
209 	 */
210 	int getUpdateCount();
211 
212 	/**
213 	 * Obtains the number of inserted rows
214 	 * 
215 	 * @return the number of inserted rows
216 	 */
217 	int getInsertCount();
218 
219 }