View Javadoc
1   package fr.ifremer.adagio.synchro.intercept;
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.sql.SQLException;
27  import java.util.List;
28  import java.util.Map;
29  
30  import org.apache.commons.lang3.StringUtils;
31  import org.hibernate.tool.hbm2ddl.TableMetadata;
32  
33  import com.google.common.base.Preconditions;
34  
35  import fr.ifremer.adagio.synchro.dao.SynchroTableDao;
36  import fr.ifremer.adagio.synchro.meta.SynchroDatabaseMetadata;
37  import fr.ifremer.adagio.synchro.meta.SynchroJoinMetadata;
38  import fr.ifremer.adagio.synchro.meta.SynchroTableMetadata;
39  import fr.ifremer.adagio.synchro.service.SynchroContext;
40  
41  public class SynchroInterceptorBase implements SynchroInterceptorChain {
42  	private SynchroInterceptor next;
43  
44  	private SynchroContext context;
45  
46  	public SynchroInterceptorBase() {
47  		this.next = null;
48  		this.context = null;
49  	}
50  
51  	public SynchroInterceptorBase(SynchroInterceptor next) {
52  		this.next = next;
53  		this.context = null;
54  	}
55  
56  	public boolean apply(SynchroDatabaseMetadata meta, TableMetadata table) {
57  		return false;
58  	}
59  
60  	@Override
61  	public void setNext(SynchroInterceptor next) {
62  		this.next = next;
63  	}
64  
65  	public SynchroInterceptor getNext() {
66  		return this.next;
67  	}
68  
69  	@Override
70  	public final void onRead(Object[] data, SynchroTableDao dao) throws SQLException {
71  		doOnRead(data, dao);
72  
73  		if (next != null) {
74  			next.onRead(data, dao);
75  		}
76  	}
77  
78  	@Override
79  	public final void onWrite(Object[] data, SynchroTableDao dao, SynchroWriteBuffer buffer) throws SQLException {
80  		doOnWrite(data, dao, buffer);
81  
82  		if (next != null) {
83  			next.onWrite(data, dao, buffer);
84  		}
85  	}
86  
87  	@Override
88  	public Map<List<Object>, Object[]> transformExtraLocalData(
89  			SynchroTableDao localDao, SynchroTableDao remoteDao,
90  			Map<List<Object>, Object[]> extraRows) throws SQLException {
91  		Map<List<Object>, Object[]> result = doTransformExtraLocalData(localDao, remoteDao, extraRows);
92  
93  		if (next != null) {
94  			result = next.transformExtraLocalData(localDao, remoteDao, result);
95  		}
96  		return result;
97  	}
98  
99  	@Override
100 	public final String onCreateSelectQuery(SynchroTableMetadata table, String queryName, String sql) {
101 		String newSql = doCreateSelectQuery(table, queryName, sql);
102 
103 		if (next != null) {
104 			newSql = next.onCreateSelectQuery(
105 					table,
106 					queryName,
107 					newSql != null ? newSql : sql);
108 		}
109 		return newSql;
110 	}
111 
112 	public final void onTableLoad(SynchroTableMetadata table) {
113 		doTableLoad(table);
114 		if (next != null) {
115 			next.onTableLoad(table);
116 		}
117 	}
118 
119 	public final void onJoinLoad(SynchroTableMetadata table, SynchroJoinMetadata join) {
120 		doJoinLoad(table, join);
121 		if (next != null) {
122 			next.onJoinLoad(table, join);
123 		}
124 	}
125 
126 	@Override
127 	public boolean enableOnRead() {
128 		return false;
129 	}
130 
131 	@Override
132 	public boolean enableOnWrite() {
133 		return false;
134 	}
135 
136 	@Override
137 	public void setContext(SynchroContext context) {
138 		this.context = context;
139 	}
140 
141 	protected SynchroContext getContext() {
142 		return this.context;
143 	}
144 
145 	/* -- protected method -- */
146 
147 	protected void doTableLoad(SynchroTableMetadata table) {
148 	}
149 
150 	protected void doJoinLoad(SynchroTableMetadata table, SynchroJoinMetadata join) {
151 	}
152 
153 	protected String doCreateSelectQuery(SynchroTableMetadata table, String queryName, String sql) {
154 		String[] clauses = sql.split("(FROM)|(WHERE)");
155 		Preconditions.checkArgument(
156 				clauses.length == 2 || clauses.length == 3,
157 				String.format("Bad sql query: [%s]. Expected: SELECT <...> FROM <...> WHERE <...>", sql));
158 		String selectClause = clauses[0];
159 		String fromClause = clauses[1];
160 		String whereClause = clauses.length == 3 ? clauses[2] : null;
161 		return doCreateSelectQuery(table, selectClause, fromClause, whereClause, queryName);
162 	}
163 
164 	/**
165 	 * Default implementation, that return the same SQL query.
166 	 * This method should be override by subclasses
167 	 * 
168 	 * @param table
169 	 * @param selectClause
170 	 * @param fromClause
171 	 * @param whereClause
172 	 * @param queryName
173 	 * @return
174 	 */
175 	protected String doCreateSelectQuery(SynchroTableMetadata table,
176 			String selectClause,
177 			String fromClause,
178 			String whereClause,
179 			String queryName) {
180 		StringBuilder sb = new StringBuilder(selectClause).append(" FROM ").append(fromClause);
181 		if (StringUtils.isNotBlank(whereClause)) {
182 			sb.append(" WHERE ").append(whereClause);
183 		}
184 		return sb.toString();
185 	}
186 
187 	protected void doOnRead(Object[] data, SynchroTableDao dao) throws SQLException {
188 
189 	}
190 
191 	protected void doOnWrite(Object[] data, SynchroTableDao dao, SynchroWriteBuffer buffer) throws SQLException {
192 
193 	}
194 
195 	protected Map<List<Object>, Object[]> doTransformExtraLocalData(
196 			SynchroTableDao localDao, SynchroTableDao remoteDao,
197 			Map<List<Object>, Object[]> extraRows) throws SQLException {
198 		return extraRows;
199 	}
200 
201 }