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.util.ArrayList; 019import java.util.List; 020import java.util.Set; 021import java.util.stream.Collectors; 022 023import org.apache.commons.lang3.StringUtils; 024 025import org.ametys.odf.ose.db.ParameterizableQuery; 026import org.ametys.odf.ose.db.column.Column; 027import org.ametys.odf.ose.db.parameter.QueryParameter; 028import org.ametys.odf.ose.db.parameter.ValuedQueryParameter; 029 030/** 031 * Utils method for export 032 */ 033public final class ExportUtils 034{ 035 private static final String __TABLE_PREFIX = "AMETYS_"; 036 private static final String __VIEW_PREFIX = "SRC_"; 037 038 private ExportUtils() 039 { 040 // Nothing to do 041 } 042 043 /** 044 * Build a request to insert values into the Ametys table. The baseName will be prefixed by "AMETYS_". 045 * @param baseName The name of the OSE table 046 * @param parameters The list of the parameters with their name and type. 047 * @return A query 048 */ 049 public static ParameterizableQuery insertIntoAmetys(String baseName, List<QueryParameter> parameters) 050 { 051 return insertInto(__TABLE_PREFIX + baseName, parameters); 052 } 053 054 /** 055 * Build a request to insert values into the table. 056 * @param tableName The name of the OSE table 057 * @param parameters The list of the parameters with their name and type. 058 * @return A query 059 */ 060 public static ParameterizableQuery insertInto(String tableName, List<QueryParameter> parameters) 061 { 062 StringBuilder sb = new StringBuilder("INSERT INTO "); 063 sb.append(tableName); 064 sb.append(" ("); 065 sb.append( 066 parameters.stream() 067 .sequential() 068 .map(QueryParameter::getName) 069 .collect(Collectors.joining(", ")) 070 ); 071 sb.append(") VALUES ("); 072 sb.append( 073 parameters.stream() 074 .sequential() 075 .map(QueryParameter::getParameter) 076 .collect(Collectors.joining(", ")) 077 ); 078 sb.append(")"); 079 080 return new ParameterizableQuery( 081 sb.toString(), 082 parameters.stream() 083 .sequential() 084 .filter(ValuedQueryParameter.class::isInstance) 085 .map(ValuedQueryParameter.class::cast) 086 .collect(Collectors.toList()) 087 ); 088 } 089 090 /** 091 * Build the request to delete all the rows from an Ametys table. The baseName will be prefixed by "AMETYS_". 092 * @param baseName The name of the OSE table 093 * @return A query 094 */ 095 public static ParameterizableQuery deleteFromAmetys(String baseName) 096 { 097 return deleteFrom(__TABLE_PREFIX + baseName); 098 } 099 100 /** 101 * Build the request to delete all the rows from a table. 102 * @param tableName The name of the OSE table 103 * @return A query 104 */ 105 public static ParameterizableQuery deleteFrom(String tableName) 106 { 107 StringBuilder sb = new StringBuilder("DELETE FROM "); 108 sb.append(tableName); 109 return new ParameterizableQuery(sb.toString()); 110 } 111 112 /** 113 * Build requests to drop, create and activate import on the OSE table defined by the parameter baseName. 114 * @param baseName The name of the OSE table 115 * @param tableColumns The columns for the Ametys table and the source view 116 * @return A {@link List} of queries 117 */ 118 public static List<ParameterizableQuery> initializeTableAndView(String baseName, Set<Column> tableColumns) 119 { 120 List<ParameterizableQuery> queries = new ArrayList<>(); 121 queries.add(_dropView(baseName)); 122 queries.add(_dropTable(baseName)); 123 queries.add(_createTable(baseName, tableColumns)); 124 queries.add(_createView(baseName, tableColumns)); 125 queries.add(_activateImport(baseName)); 126 return queries; 127 } 128 129 private static ParameterizableQuery _dropTable(String baseName) 130 { 131 StringBuilder sb = new StringBuilder("DROP TABLE "); 132 sb.append(__TABLE_PREFIX); 133 sb.append(baseName); 134 return new ParameterizableQuery(sb.toString()); 135 } 136 137 private static ParameterizableQuery _dropView(String baseName) 138 { 139 StringBuilder sb = new StringBuilder("DROP VIEW "); 140 sb.append(__VIEW_PREFIX); 141 sb.append(baseName); 142 return new ParameterizableQuery(sb.toString()); 143 } 144 145 private static ParameterizableQuery _createTable(String baseName, Set<Column> tableColumns) 146 { 147 StringBuilder sb = new StringBuilder("CREATE TABLE "); 148 sb.append(__TABLE_PREFIX); 149 sb.append(baseName); 150 sb.append(" ("); 151 sb.append(tableColumns.stream() 152 .map(column -> column.getColumnDescription()) 153 .filter(StringUtils::isNotEmpty) 154 .collect(Collectors.joining(", "))); 155 sb.append(")"); 156 return new ParameterizableQuery(sb.toString()); 157 } 158 159 private static ParameterizableQuery _createView(String baseName, Set<Column> tableColumns) 160 { 161 StringBuilder sb = new StringBuilder("CREATE VIEW "); 162 sb.append(__VIEW_PREFIX); 163 sb.append(baseName); 164 sb.append(" AS SELECT "); 165 sb.append(tableColumns.stream() 166 .map(column -> column.getViewDescription()) 167 .filter(StringUtils::isNotEmpty) 168 .collect(Collectors.joining(", "))); 169 sb.append(" FROM "); 170 sb.append(__TABLE_PREFIX); 171 sb.append(baseName); 172 sb.append(" TBL"); 173 174 return new ParameterizableQuery( 175 sb.toString(), 176 tableColumns.stream() 177 .map(Column::getViewParameters) 178 .flatMap(List::stream) 179 .collect(Collectors.toList()) 180 ); 181 } 182 183 private static ParameterizableQuery _activateImport(String baseName) 184 { 185 StringBuilder sb = new StringBuilder("UPDATE IMPORT_TABLES SET SYNC_ENABLED = 1, SYNC_JOB = 'synchro-ametys', SYNC_FILTRE = NULL WHERE TABLE_NAME = '"); 186 sb.append(baseName); 187 sb.append("'"); 188 return new ParameterizableQuery(sb.toString()); 189 } 190} 191