View Javadoc
1   package fr.ifremer.quadrige3.synchro.dao;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 Client 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.collect.ArrayListMultimap;
27  import com.google.common.collect.Multimap;
28  import fr.ifremer.quadrige3.core.config.QuadrigeConfiguration;
29  import fr.ifremer.quadrige3.core.exception.QuadrigeTechnicalException;
30  import fr.ifremer.quadrige3.core.dao.technical.Daos;
31  import fr.ifremer.quadrige3.core.dao.technical.jdbc.OptionalDataSourceJdbcDaoSupport;
32  import org.apache.commons.collections4.CollectionUtils;
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.springframework.beans.factory.annotation.Autowired;
36  import org.springframework.context.annotation.Lazy;
37  import org.springframework.stereotype.Repository;
38  
39  import javax.annotation.Resource;
40  import javax.sql.DataSource;
41  import java.sql.Connection;
42  import java.sql.DatabaseMetaData;
43  import java.sql.ResultSet;
44  import java.sql.SQLException;
45  import java.util.Properties;
46  import java.util.Set;
47  
48  /**
49   * Created by blavenie on 20/08/15.
50   */
51  @Repository("synchroClientDao")
52  @Lazy
53  public class SynchroClientDaoImpl extends OptionalDataSourceJdbcDaoSupport implements SynchroClientDao {
54  
55  	/** Logger. */
56  	private static final Log log = LogFactory.getLog(SynchroClientDaoImpl.class);
57  
58  	@Resource
59  	private QuadrigeConfiguration config;
60  
61  	/**
62  	 * Constructor used by Spring
63  	 * 
64  	 * @param dataSource
65  	 *            a {@link javax.sql.DataSource} object.
66  	 */
67  	@Autowired
68  	public SynchroClientDaoImpl(DataSource dataSource) {
69  		super(dataSource);
70  	}
71  
72  	/**
73  	 * Constructor to use when Spring not started
74  	 */
75  	public SynchroClientDaoImpl() {
76  		super();
77  		config = QuadrigeConfiguration.getInstance();
78  	}
79  
80  	/** {@inheritDoc} */
81  	@Override
82  	public boolean isAllTablesEmpty(Set<String> tableNames) {
83  		return isAllTablesEmpty(null, tableNames);
84  	}
85  
86  	/** {@inheritDoc} */
87  	@Override
88  	public boolean isAllTablesEmpty(Properties connectionProperties, Set<String> tableNames) {
89  		if (CollectionUtils.isEmpty(tableNames)) {
90  			return true;
91  		}
92  
93  		Connection connection = null;
94  		try {
95  			connection = createConnection(connectionProperties);
96  
97  			for (String tableName : tableNames) {
98  				long count = Daos.countTableRows(connection, tableName);
99  				if (count > 0) {
100 					return false;
101 				}
102 			}
103 
104 			return true;
105 		} catch (SQLException e) {
106 			throw new QuadrigeTechnicalException("Error while getting if all tables are empty");
107 		} finally {
108 			closeSilently(connection);
109 		}
110 	}
111 
112 	/** {@inheritDoc} */
113 	@Override
114 	public Multimap<String, String> getExportedKeys(String pkTableName, String pkColumnName) {
115 
116 		// Execute query and fetch into result list
117 		Multimap<String, String> result = ArrayListMultimap.create();
118 
119 		// Exported keys (primary keys referenced by another table)
120 		Connection connection;
121 		ResultSet rs = null;
122 		try {
123 			connection = createConnection(null);
124 			DatabaseMetaData dbMeta = connection.getMetaData();
125 
126 			rs = dbMeta.getExportedKeys(config.getJdbcCatalog(), config.getJdbcSchema(), pkTableName);
127 			while (rs.next()) {
128 				String columnName = rs.getString("PKCOLUMN_NAME").toLowerCase();
129 				if (columnName.equalsIgnoreCase(pkColumnName)) {
130 					String fkTableName = rs.getString("FKTABLE_NAME").toUpperCase();
131 					String fkColumnName = rs.getString("FKCOLUMN_NAME").toLowerCase();
132 					result.put(fkTableName, fkColumnName);
133 				}
134 			}
135 		} catch (SQLException e) {
136 			throw new QuadrigeTechnicalException(String.format("Error while getting exported keys for table [%s]", pkTableName));
137 		} finally {
138 			fr.ifremer.common.synchro.dao.Daos.closeSilently(rs);
139 		}
140 
141 		return result;
142 	}
143 
144 	/* -- Internal methods -- */
145 
146 }