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.generators; 017 018import java.io.IOException; 019import java.sql.Types; 020import java.util.ArrayList; 021import java.util.Collections; 022import java.util.List; 023import java.util.stream.Collectors; 024 025import org.apache.avalon.framework.service.ServiceException; 026import org.apache.avalon.framework.service.ServiceManager; 027import org.apache.cocoon.ProcessingException; 028import org.apache.cocoon.generation.ServiceableGenerator; 029import org.apache.cocoon.xml.XMLUtils; 030import org.apache.commons.collections4.ListUtils; 031import org.xml.sax.SAXException; 032 033import org.ametys.odf.ose.db.ParameterizableQuery; 034import org.ametys.odf.ose.db.parameter.ValuedQueryParameter; 035import org.ametys.odf.ose.export.OSEExport; 036import org.ametys.odf.ose.export.OSEExportExtensionPoint; 037import org.ametys.runtime.config.Config; 038 039/** 040 * The generator which generates the SQL initialization file for Ametys connector. 041 */ 042public class ExportInitGenerator extends ServiceableGenerator 043{ 044 /** The OSE export extension point */ 045 protected OSEExportExtensionPoint _oseExportEP; 046 047 @Override 048 public void service(ServiceManager serviceManager) throws ServiceException 049 { 050 super.service(serviceManager); 051 _oseExportEP = (OSEExportExtensionPoint) manager.lookup(OSEExportExtensionPoint.ROLE); 052 } 053 054 @Override 055 public void generate() throws IOException, SAXException, ProcessingException 056 { 057 List<ParameterizableQuery> queries = _retrieveQueries(); 058 _saxQueries(queries); 059 } 060 061 /** 062 * Retrieve the queries to SAX 063 * @return the queries to SAX 064 */ 065 protected List<ParameterizableQuery> _retrieveQueries() 066 { 067 return ListUtils.union( 068 _retrieveStaticQueries(), 069 _retrieveQueriesFromExtensions() 070 ); 071 } 072 073 /** 074 * Retrieve the static queries (hardcoded) to SAX 075 * @return the static queries (hardcoded) to SAX 076 */ 077 protected List<ParameterizableQuery> _retrieveStaticQueries() 078 { 079 List<ParameterizableQuery> queries = new ArrayList<>(); 080 081 String oseSourceCode = Config.getInstance().getValue("ose.source.code"); 082 List<ValuedQueryParameter> queryParameters = Collections.singletonList(new ValuedQueryParameter("SOURCE_CODE", oseSourceCode, Types.VARCHAR)); 083 queries.add(new ParameterizableQuery("DELETE FROM SOURCE WHERE CODE = :SOURCE_CODE", queryParameters)); 084 queries.add(new ParameterizableQuery("INSERT INTO SOURCE (ID, CODE, LIBELLE, IMPORTABLE) VALUES (SOURCE_ID_SEQ.NEXTVAL, :SOURCE_CODE, 'Ametys', 1)", queryParameters)); 085 086 return queries; 087 } 088 089 /** 090 * Retrieve the queries from {@link OSEExportExtensionPoint} to SAX 091 * @return the queries from {@link OSEExportExtensionPoint} to SAX 092 */ 093 protected List<ParameterizableQuery> _retrieveQueriesFromExtensions() 094 { 095 return _oseExportEP.getExtensionsIds() 096 .stream() 097 .map(_oseExportEP::getExtension) 098 .map(OSEExport::initializeDb) 099 .flatMap(List::stream) 100 .collect(Collectors.toList()); 101 } 102 103 /** 104 * SAX queries 105 * @param queries The queries to SAX 106 * @throws SAXException Any SAX exception 107 */ 108 protected void _saxQueries(List<ParameterizableQuery> queries) throws SAXException 109 { 110 contentHandler.startDocument(); 111 XMLUtils.startElement(contentHandler, "queries"); 112 113 for (ParameterizableQuery query : queries) 114 { 115 String queryText = _queryText(query); 116 XMLUtils.createElement(contentHandler, "query", queryText); 117 } 118 119 XMLUtils.endElement(contentHandler, "queries"); 120 contentHandler.endDocument(); 121 } 122 123 /** 124 * Gets the text representation of the query to SAX 125 * @param query The query to SAX 126 * @return The string representation of the query 127 */ 128 protected String _queryText(ParameterizableQuery query) 129 { 130 String queryText = query.getQuery(); 131 for (ValuedQueryParameter parameter : query.getParameters()) 132 { 133 String target = ":" + parameter.getName(); 134 String replacement = "'" + parameter.getValue().toString().replace("'", "''") + "'"; 135 queryText = queryText.replace(target, replacement); 136 } 137 return queryText; 138 } 139 140}