View Javadoc
1   package fr.ifremer.quadrige2.synchro.intercept;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 Synchro Core
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 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 com.google.common.base.Preconditions;
27  import com.google.common.collect.ImmutableSet;
28  import com.google.common.collect.Sets;
29  import fr.ifremer.common.synchro.intercept.SynchroInterceptorBase;
30  import fr.ifremer.common.synchro.meta.SynchroDatabaseMetadata;
31  import fr.ifremer.common.synchro.service.SynchroDatabaseConfiguration;
32  import fr.ifremer.quadrige2.synchro.service.AbstractSynchroDatabaseConfiguration;
33  import fr.ifremer.quadrige2.synchro.service.SynchroDirection;
34  import org.hibernate.tool.hbm2ddl.TableMetadata;
35  
36  import java.util.Arrays;
37  import java.util.Set;
38  
39  /**
40   * <p>
41   * Abstract AbstractSynchroInterceptor class.
42   * </p>
43   * 
44   */
45  public abstract class AbstractSynchroInterceptor<T extends AbstractSynchroDatabaseConfiguration> extends SynchroInterceptorBase {
46  
47  	private SynchroDatabaseMetadata meta;
48  	private Integer userId;
49  	private Set<SynchroDirection> allowDirections = Sets.newHashSet();
50  	private Set<String> allowTables;
51  
52  	/**
53  	 * <p>
54  	 * Constructor for AbstractSynchroInterceptor.
55  	 * </p>
56  	 */
57  	public AbstractSynchroInterceptor() {
58  		// All direction are allowed
59  		this.allowDirections = null;
60  		// All table are allowed
61  		this.allowTables = null;
62  	}
63  
64  	/**
65  	 * <p>
66  	 * Constructor for AbstractSynchroInterceptor.
67  	 * </p>
68  	 * 
69  	 * @param directions
70  	 *            a {@link fr.ifremer.quadrige2.synchro.service.SynchroDirection} object.
71  	 */
72  	public AbstractSynchroInterceptor(SynchroDirection... directions) {
73  		Preconditions.checkArgument(directions != null && directions.length > 0);
74  
75  		// Fill allowed directions
76  		this.allowDirections = ImmutableSet.copyOf(directions);
77  		// All table are allowed
78  		this.allowTables = null;
79  	}
80  
81  	/**
82  	 * <p>
83  	 * Constructor for AbstractSynchroInterceptor.
84  	 * </p>
85  	 * 
86  	 * @param tableIncludes
87  	 *            a {@link java.util.Set} object.
88  	 * @param directions
89  	 *            a {@link fr.ifremer.quadrige2.synchro.service.SynchroDirection} object.
90  	 */
91  	public AbstractSynchroInterceptor(Set<String> tableIncludes, SynchroDirection... directions) {
92  		Preconditions.checkArgument(tableIncludes != null && tableIncludes.size() > 0);
93  		Preconditions.checkArgument(directions != null && directions.length > 0);
94  
95  		this.allowTables = ImmutableSet.copyOf(tableIncludes);
96  
97  		// Fill allowed directions
98  		this.allowDirections = ImmutableSet.copyOf(directions);
99  	}
100 
101 	/**
102 	 * <p>
103 	 * Constructor for AbstractSynchroInterceptor.
104 	 * </p>
105 	 * 
106 	 * @param tableIncludes
107 	 *            a {@link java.util.Set} object.
108 	 */
109 	public AbstractSynchroInterceptor(Set<String> tableIncludes) {
110 		Preconditions.checkArgument(tableIncludes != null && tableIncludes.size() > 0);
111 
112 		this.allowTables = ImmutableSet.copyOf(tableIncludes);
113 
114 		// All direction are allowed
115 		this.allowDirections = null;
116 	}
117 
118 	/** {@inheritDoc} */
119 	@Override
120 	public SynchroInterceptorBase clone() {
121 		AbstractSynchroInterceptor result = (AbstractSynchroInterceptor) super.clone();
122 		result.meta = this.meta;
123 		result.userId = this.userId;
124 		result.allowDirections = allowDirections;
125 		result.allowTables = allowTables;
126 		return result;
127 	}
128 
129 	/** {@inheritDoc} */
130 	@Override
131 	public final boolean apply(SynchroDatabaseConfiguration config) {
132 		boolean result = getConfigClass().isInstance(config)
133 				/* super is need to store the config into a field */
134 				&& super.apply(config);
135 
136 		if (result) {
137 			// Init the interceptor
138 			init((T) config);
139 		}
140 		return result;
141 	}
142 
143 	/**
144 	 * <p>
145 	 * getConfigClass.
146 	 * </p>
147 	 * 
148 	 * @return a {@link java.lang.Class} object.
149 	 */
150 	protected abstract Class<T> getConfigClass();
151 
152 	/** {@inheritDoc} */
153 	@Override
154 	public final boolean apply(SynchroDatabaseMetadata meta, TableMetadata table) {
155 		Preconditions.checkNotNull(meta);
156 		if (this.meta != null) {
157 			Preconditions.checkState(this.meta == meta);
158 		}
159 
160 		// Store meta and configuration
161 		else {
162 			this.meta = meta;
163 
164 			// Retrieve the user id
165 			userId = getConfig().getUserId();
166 		}
167 
168 		return (allowDirections == null || allowDirections.contains(getConfig().getDirection()))
169 				&& (allowTables == null || allowTables.contains(table.getName()))
170 				&& doApply(meta, table);
171 	}
172 
173 	/**
174 	 * <p>
175 	 * doApply.
176 	 * </p>
177 	 * 
178 	 * @param meta
179 	 *            a {@link fr.ifremer.common.synchro.meta.SynchroDatabaseMetadata} object.
180 	 * @param table
181 	 *            a {@link org.hibernate.tool.hbm2ddl.TableMetadata} object.
182 	 * @return a boolean.
183 	 */
184 	public abstract boolean doApply(SynchroDatabaseMetadata meta, TableMetadata table);
185 
186 	/**
187 	 * <p>
188 	 * checkAndGetUserId.
189 	 * </p>
190 	 * 
191 	 * @return a int.
192 	 */
193 	public int checkAndGetUserId() {
194 		Preconditions
195 				.checkNotNull(
196 						userId,
197 						String.format(
198 								"Could not retrieve user id (in database configuration). %s need a not null userId.",
199 								getClass().getSimpleName()
200 								));
201 		return userId;
202 	}
203 
204 	/* -- delegate methods -- */
205 
206 	/**
207 	 * <p>
208 	 * getConfig.
209 	 * </p>
210 	 * 
211 	 * @return a T object.
212 	 */
213 	public T getConfig() {
214 		return (T) getDefaultDatabaseConfiguration();
215 	}
216 
217 	/* -- internal methods -- */
218 
219 	/**
220 	 * <p>
221 	 * init.
222 	 * </p>
223 	 * 
224 	 * @param config
225 	 *            a T object.
226 	 */
227 	protected void init(T config) {
228 		// could be override
229 	}
230 
231 	/**
232 	 * <p>
233 	 * isInDirections.
234 	 * </p>
235 	 * 
236 	 * @param allowDirections
237 	 *            a {@link fr.ifremer.quadrige2.synchro.service.SynchroDirection} object.
238 	 * @return a boolean.
239 	 */
240 	protected boolean isInDirections(SynchroDirection... allowDirections) {
241 		return Arrays.asList(allowDirections).contains(getConfig().getDirection());
242 	}
243 }