View Javadoc
1   package fr.ifremer.adagio.synchro.service;
2   
3   /*
4    * #%L
5    * Tutti :: Persistence API
6    * $Id: ReferentialSynchroResult.java 1486 2014-01-15 08:43:26Z tchemit $
7    * $HeadURL: http://svn.forge.codelutin.com/svn/tutti/trunk/tutti-persistence/src/main/java/fr/ifremer/adagio/core/service/technical/synchro/ReferentialSynchroResult.java $
8    * %%
9    * Copyright (C) 2012 - 2013 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.sql.Timestamp;
27  import java.util.Map;
28  import java.util.Set;
29  
30  import com.google.common.collect.ImmutableMap;
31  import com.google.common.collect.ImmutableSet;
32  import com.google.common.collect.Maps;
33  import com.google.common.collect.Sets;
34  
35  import fr.ifremer.adagio.synchro.type.ProgressionModel;
36  
37  /**
38   * Result of a referential synchronize operation.
39   * 
40   * @author tchemit <chemit@codelutin.com>
41   * @since 3.5.2
42   */
43  public class SynchroResult {
44  
45  	protected Exception error;
46  
47  	/**
48  	 * Number of rows detected to update (per table).
49  	 * 
50  	 * @since 1.0
51  	 */
52  	protected final Map<String, Integer> rowHits = Maps.newTreeMap();
53  
54  	/**
55  	 * Number of insert done (per table).
56  	 * 
57  	 * @since 1.0
58  	 */
59  	protected final Map<String, Integer> insertHits = Maps.newTreeMap();
60  
61  	/**
62  	 * Number of update done (per table).
63  	 * 
64  	 * @since 1.0
65  	 */
66  	protected final Map<String, Integer> updateHits = Maps.newTreeMap();
67  
68  	/**
69  	 * timestamp of last update date (per table).
70  	 * 
71  	 * @since 1.0
72  	 */
73  	protected final Map<String, Timestamp> updateDateHits = Maps.newTreeMap();
74  
75  	/**
76  	 * All table treated.
77  	 * 
78  	 * @since 1.0
79  	 */
80  	protected final Set<String> tableNames = Sets.newHashSet();
81  
82  	/**
83  	 * Columns not to synchronize (per table).
84  	 * 
85  	 * @since 3.5.3
86  	 */
87  	protected final Map<String, Set<String>> missingOptionalColumnNames = Maps.newTreeMap();
88  
89  	protected String targetUrl;
90  
91  	protected String sourceUrl;
92  
93  	protected final ProgressionModel progressionModel = new ProgressionModel(this);
94  
95  	public SynchroResult() {
96  	}
97  
98  	public SynchroResult(String targetUrl, String sourceUrl) {
99  		this.targetUrl = targetUrl;
100 		this.sourceUrl = sourceUrl;
101 	}
102 
103 	public void setLocalUrl(String targetUrl) {
104 		this.targetUrl = targetUrl;
105 	}
106 
107 	public void setRemoteUrl(String sourceUrl) {
108 		this.sourceUrl = sourceUrl;
109 	}
110 
111 	public boolean isSuccess() {
112 		return error == null;
113 	}
114 
115 	public Exception getError() {
116 		return error;
117 	}
118 
119 	public void setError(Exception error) {
120 		this.error = error;
121 	}
122 
123 	public ProgressionModel getProgressionModel() {
124 		return progressionModel;
125 	}
126 
127 	public Set<String> getTableNames() {
128 		return ImmutableSet.copyOf(tableNames);
129 	}
130 
131 	public int getTotalRows() {
132 		int result = 0;
133 		for (Integer nb : rowHits.values()) {
134 			result += nb;
135 		}
136 		return result;
137 	}
138 
139 	public int getTotalInserts() {
140 		int result = 0;
141 		for (Integer nb : insertHits.values()) {
142 			result += nb;
143 		}
144 		return result;
145 	}
146 
147 	public int getTotalUpdates() {
148 		int result = 0;
149 		for (Integer nb : updateHits.values()) {
150 			result += nb;
151 		}
152 		return result;
153 	}
154 
155 	public int getNbRows(String tableName) {
156 		Integer result = rowHits.get(tableName);
157 		if (result == null) {
158 			result = 0;
159 		}
160 		return result;
161 	}
162 
163 	public int getNbInserts(String tableName) {
164 		Integer result = insertHits.get(tableName);
165 		if (result == null) {
166 			result = 0;
167 		}
168 		return result;
169 	}
170 
171 	public int getNbUpdates(String tableName) {
172 		Integer result = updateHits.get(tableName);
173 		if (result == null) {
174 			result = 0;
175 		}
176 		return result;
177 	}
178 
179 	public void addRows(String tableName, int nb) {
180 		if (nb > 0) {
181 			rowHits.put(tableName, getNbRows(tableName) + nb);
182 		}
183 	}
184 
185 	public void addUpdates(String tableName, int nb) {
186 		if (nb > 0) {
187 			updateHits.put(tableName, getNbUpdates(tableName) + nb);
188 		}
189 	}
190 
191 	public void addInserts(String tableName, int nb) {
192 		if (nb > 0) {
193 			insertHits.put(tableName, getNbInserts(tableName) + nb);
194 		}
195 	}
196 
197 	public Timestamp getUpdateDate(String tableName) {
198 		return updateDateHits.get(tableName);
199 	}
200 
201 	public void setUpdateDate(String tableName, Timestamp t) {
202 		updateDateHits.put(tableName, t);
203 	}
204 
205 	public void addTableName(String tableName) {
206 		tableNames.add(tableName);
207 	}
208 
209 	public String getLocalUrl() {
210 		return targetUrl;
211 	}
212 
213 	public String getRemoteUrl() {
214 		return sourceUrl;
215 	}
216 
217 	public void addMissingOptionalColumnName(String tableName, String columnName) {
218 		Set<String> columnNames = missingOptionalColumnNames.get(tableName);
219 		if (columnNames == null) {
220 			columnNames = Sets.newHashSet();
221 			missingOptionalColumnNames.put(tableName, columnNames);
222 		}
223 		columnNames.add(columnName);
224 	}
225 
226 	public Set<String> getMissingOptionalColumnNames(String tableName) {
227 		return missingOptionalColumnNames.get(tableName);
228 	}
229 
230 	public Map<String, Set<String>> getMissingOptionalColumnNameMaps() {
231 		return ImmutableMap.copyOf(missingOptionalColumnNames);
232 	}
233 }