001package fr.ifremer.adagio.synchro.service; 002 003/* 004 * #%L 005 * SIH-Adagio :: Synchronization 006 * $Id:$ 007 * $HeadURL:$ 008 * %% 009 * Copyright (C) 2012 - 2014 Ifremer 010 * %% 011 * This program is free software: you can redistribute it and/or modify 012 * it under the terms of the GNU Affero General Public License as published by 013 * the Free Software Foundation, either version 3 of the License, or 014 * (at your option) any later version. 015 * 016 * This program is distributed in the hope that it will be useful, 017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 019 * GNU General Public License for more details. 020 * 021 * You should have received a copy of the GNU Affero General Public License 022 * along with this program. If not, see <http://www.gnu.org/licenses/>. 023 * #L% 024 */ 025 026import java.util.List; 027 028import javax.sql.DataSource; 029 030import org.apache.commons.logging.Log; 031import org.apache.commons.logging.LogFactory; 032import org.hibernate.dialect.Dialect; 033 034import com.google.common.base.Preconditions; 035import com.google.common.collect.Lists; 036 037import fr.ifremer.adagio.synchro.config.SynchroConfiguration; 038import fr.ifremer.adagio.synchro.dao.SynchroTableDao; 039import fr.ifremer.adagio.synchro.intercept.SynchroInterceptor; 040import fr.ifremer.adagio.synchro.intercept.SynchroInterceptorBase; 041import fr.ifremer.adagio.synchro.intercept.SynchroInterceptorUtils; 042import fr.ifremer.adagio.synchro.intercept.internal.RemoteIdWriteInterceptor; 043import fr.ifremer.adagio.synchro.meta.SynchroDatabaseMetadata; 044import fr.ifremer.adagio.synchro.meta.SynchroJoinMetadata; 045import fr.ifremer.adagio.synchro.meta.SynchroTableMetadata; 046 047public class SynchroBaseService { 048 049 /** Logger. */ 050 private static final Log log = 051 LogFactory.getLog(SynchroBaseService.class); 052 053 protected final SynchroConfiguration config; 054 055 protected final int batchSize; 056 057 protected final DataSource dataSource; 058 059 public SynchroBaseService(DataSource dataSource, SynchroConfiguration config) { 060 Preconditions.checkNotNull(config); 061 062 this.dataSource = dataSource; 063 this.config = config; 064 this.batchSize = config.getImportJdbcBatchSize(); 065 } 066 067 public SynchroBaseService() { 068 this.config = SynchroConfiguration.getInstance(); 069 Preconditions.checkNotNull(this.config, 070 String.format("%s instance not initialized. Make sure 'setInstance()' is called first.", SynchroConfiguration.class.getName())); 071 this.dataSource = null; 072 this.batchSize = config.getImportJdbcBatchSize(); 073 } 074 075 protected List<SynchroInterceptor> daoInterceptors = null; 076 077 public List<SynchroInterceptor> getInterceptors(SynchroContext context) { 078 if (daoInterceptors == null) { 079 daoInterceptors = SynchroInterceptorUtils.load(SynchroInterceptor.class, context); 080 } 081 return daoInterceptors; 082 } 083 084 public SynchroInterceptor getInterceptor(SynchroTableMetadata table, SynchroContext context) { 085 List<SynchroInterceptor> interceptors = getInterceptors(context); 086 087 List<SynchroInterceptor> filteredInterceptors = Lists.newArrayList(); 088 filteredInterceptors.addAll(SynchroInterceptorUtils.filter(interceptors, 089 table.getDatabaseMetadata(), table.getDelegate())); 090 091 if (table.hasJoins()) { 092 093 for (SynchroJoinMetadata join : table.getJoins()) { 094 if (join.needRemoteIdInterceptor()) { 095 String pkTableName = join.getTargetTable().getName().toLowerCase(); 096 String fkColumnName = join.getSourceColumn().getName().toLowerCase(); 097 int fkColumnIndex = join.getSourceTable().getColumnIndex(fkColumnName); 098 099 RemoteIdWriteInterceptor remoteIdInterceptor = new RemoteIdWriteInterceptor( 100 pkTableName, 101 fkColumnIndex, 102 join.getSourceColumn().isNullable()); 103 104 filteredInterceptors.add(remoteIdInterceptor); 105 } 106 } 107 } 108 109 SynchroInterceptor result = SynchroInterceptorUtils.chain(filteredInterceptors, SynchroInterceptorBase.class); 110 111 return result; 112 } 113 114 protected void reportProgress(SynchroResult result, SynchroTableDao dao, int countR, String tablePrefix) { 115 if (countR % batchSize == 0) { 116 result.getProgressionModel().increments(batchSize); 117 } 118 119 if (countR % (batchSize * 10) == 0) { 120 if (log.isInfoEnabled()) { 121 log.info(String.format("%s Done: %s (inserts: %s, updates: %s)", tablePrefix, countR, dao.getInsertCount(), dao.getUpdateCount())); 122 } 123 } 124 } 125 126}