001/* 002 * Copyright 2019 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.odf.ose.export; 017 018import java.sql.Connection; 019import java.sql.PreparedStatement; 020import java.sql.SQLException; 021import java.util.List; 022 023import org.ametys.core.datasource.ConnectionHelper; 024import org.ametys.odf.ose.db.ParameterizableQuery; 025import org.ametys.odf.ose.db.parameter.ValuedQueryParameter; 026import org.ametys.runtime.config.Config; 027import org.ametys.runtime.plugin.component.AbstractLogEnabled; 028 029/** 030 * Abstract class for OSE export. 031 */ 032public abstract class AbstractOSEExport extends AbstractLogEnabled implements OSEExport 033{ 034 @Override 035 public void populate(String ametysCatalog, Long oseCatalog) 036 { 037 _executeQueries(_populate(ametysCatalog, oseCatalog)); 038 } 039 040 /** 041 * Populate the Ametys table(s) with the values from Ametys. 042 * @param ametysCatalog The Ametys catalog to filter data. 043 * @param oseCatalog The OSE catalog to insert into the right year. 044 * @return A {@link List} of queries 045 */ 046 protected abstract List<ParameterizableQuery> _populate(String ametysCatalog, Long oseCatalog); 047 048 private void _executeQueries(List<ParameterizableQuery> queries) 049 { 050 String oseDatasource = Config.getInstance().getValue("ose.datasource"); 051 try (Connection connection = ConnectionHelper.getConnection(oseDatasource)) 052 { 053 for (ParameterizableQuery query : queries) 054 { 055 _executeQuery(query, connection); 056 } 057 } 058 catch (SQLException e) 059 { 060 getLogger().error("Error during connection to the database.", e); 061 } 062 } 063 064 private void _executeQuery(ParameterizableQuery query, Connection connection) 065 { 066 try (PreparedStatement stmt = connection.prepareStatement(query.getQuery())) 067 { 068 if (getLogger().isDebugEnabled()) 069 { 070 getLogger().debug("Will execute {}", query.toReadableString()); 071 } 072 _setStatementParameters(stmt, query); 073 stmt.execute(); 074 } 075 catch (SQLException e) 076 { 077 getLogger().error( 078 "Impossible to execute the {}", 079 query.toReadableString(), 080 e 081 ); 082 } 083 } 084 085 private void _setStatementParameters(PreparedStatement stmt, ParameterizableQuery query) throws SQLException 086 { 087 int index = 1; 088 for (ValuedQueryParameter parameter : query.getParameters()) 089 { 090 stmt.setObject(index, parameter.getValue(), parameter.getType()); 091 index++; 092 } 093 } 094}