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.impl; 017 018import java.sql.Types; 019import java.util.ArrayList; 020import java.util.Arrays; 021import java.util.Collections; 022import java.util.Comparator; 023import java.util.List; 024import java.util.concurrent.atomic.AtomicInteger; 025import java.util.stream.Collectors; 026 027import org.apache.avalon.framework.service.ServiceException; 028import org.apache.avalon.framework.service.ServiceManager; 029import org.apache.avalon.framework.service.Serviceable; 030import org.apache.commons.lang3.StringUtils; 031 032import org.ametys.odf.enumeration.OdfReferenceTableEntry; 033import org.ametys.odf.enumeration.OdfReferenceTableHelper; 034import org.ametys.odf.ose.db.ParameterizableQuery; 035import org.ametys.odf.ose.db.parameter.QueryParameter; 036import org.ametys.odf.ose.db.parameter.StaticQueryParameter; 037import org.ametys.odf.ose.db.parameter.ValuedQueryParameter; 038import org.ametys.odf.ose.export.AbstractOSEExport; 039import org.ametys.odf.ose.export.ExportUtils; 040import org.ametys.runtime.config.Config; 041 042/** 043 * This exports period types like pair, impair to the PERIODE table. 044 * It's a one-shot operation, it's done in the initialization because it's not synchronizable data. 045 */ 046public class TypePeriodeExport extends AbstractOSEExport implements Serviceable 047{ 048 /** The name for the ETAPE table */ 049 private static final String __TABLE_NAME = "PERIODE"; 050 051 /** The ODF enumeration helper */ 052 protected OdfReferenceTableHelper _refTableHelper; 053 054 @Override 055 public void service(ServiceManager manager) throws ServiceException 056 { 057 _refTableHelper = (OdfReferenceTableHelper) manager.lookup(OdfReferenceTableHelper.ROLE); 058 } 059 060 @Override 061 public List<ParameterizableQuery> initializeDb() 062 { 063 List<ParameterizableQuery> queries = new ArrayList<>(); 064 queries.add(_deleteOldData()); 065 queries.addAll(_getInsertQueries()); 066 return queries; 067 } 068 069 @Override 070 protected List<ParameterizableQuery> _populate(String ametysCatalog, Long oseCatalog) 071 { 072 // Nothing to do, only on initialization, data not synchronizable 073 return Collections.EMPTY_LIST; 074 } 075 076 private ParameterizableQuery _deleteOldData() 077 { 078 return ExportUtils.deleteFrom(__TABLE_NAME); 079 } 080 081 private List<ParameterizableQuery> _getInsertQueries() 082 { 083 AtomicInteger order = new AtomicInteger(); 084 String lang = Config.getInstance().getValue("odf.programs.lang"); 085 return _refTableHelper.getItems(OdfReferenceTableHelper.PERIOD_TYPE) 086 .stream() 087 .sequential() 088 .sorted(Comparator 089 .comparingLong(OdfReferenceTableEntry::getOrder) 090 .thenComparing(OdfReferenceTableEntry::getCode)) 091 .map(enseignementNature -> _getInsertQuery(enseignementNature, order, lang)) 092 .collect(Collectors.toList()); 093 } 094 095 private ParameterizableQuery _getInsertQuery(OdfReferenceTableEntry typePeriode, AtomicInteger order, String lang) 096 { 097 String label = typePeriode.getLabel(lang); 098 099 List<QueryParameter> queryParameters = Arrays.asList( 100 new StaticQueryParameter("ID", "PERIODE_ID_SEQ.NEXTVAL"), 101 new ValuedQueryParameter("CODE", StringUtils.truncate(typePeriode.getCode(), 3), Types.VARCHAR), 102 new ValuedQueryParameter("LIBELLE_LONG", StringUtils.truncate(label, 40), Types.VARCHAR), 103 new ValuedQueryParameter("LIBELLE_COURT", StringUtils.truncate(label, 15), Types.VARCHAR), 104 new ValuedQueryParameter("ORDRE", order.incrementAndGet(), Types.NUMERIC), 105 new StaticQueryParameter("ENSEIGNEMENT", String.valueOf(1)), 106 new StaticQueryParameter("ECART_MOIS", String.valueOf(0)), 107 new StaticQueryParameter("ECART_MOIS_PAIEMENT", String.valueOf(0)), 108 new StaticQueryParameter("HISTO_CREATEUR_ID", String.valueOf(1)), 109 new StaticQueryParameter("HISTO_MODIFICATEUR_ID", String.valueOf(1)) 110 ); 111 112 return ExportUtils.insertInto(__TABLE_NAME, queryParameters); 113 } 114}