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.cms.data.ContentValue; 033import org.ametys.odf.enumeration.OdfReferenceTableEntry; 034import org.ametys.odf.enumeration.OdfReferenceTableHelper; 035import org.ametys.odf.ose.db.ParameterizableQuery; 036import org.ametys.odf.ose.db.parameter.QueryParameter; 037import org.ametys.odf.ose.db.parameter.StaticQueryParameter; 038import org.ametys.odf.ose.db.parameter.ValuedQueryParameter; 039import org.ametys.odf.ose.export.AbstractOSEExport; 040import org.ametys.odf.ose.export.ExportUtils; 041import org.ametys.runtime.config.Config; 042 043/** 044 * This exports natures like CM, TD, TP to the TYPE_INTERVENTION table. 045 * It's a one-shot operation, it's done in the initialization because it's not synchronizable data. 046 */ 047public class NatureEnseignementExport extends AbstractOSEExport implements Serviceable 048{ 049 /** The name for the ETAPE table */ 050 private static final String __TABLE_NAME = "TYPE_INTERVENTION"; 051 052 /** The ODF enumeration helper */ 053 protected OdfReferenceTableHelper _refTableHelper; 054 055 @Override 056 public void service(ServiceManager manager) throws ServiceException 057 { 058 _refTableHelper = (OdfReferenceTableHelper) manager.lookup(OdfReferenceTableHelper.ROLE); 059 } 060 061 @Override 062 public List<ParameterizableQuery> initializeDb() 063 { 064 List<ParameterizableQuery> queries = new ArrayList<>(); 065 queries.add(_deleteOldData()); 066 queries.addAll(_getInsertQueries()); 067 return queries; 068 } 069 070 @Override 071 protected List<ParameterizableQuery> _populate(String ametysCatalog, Long oseCatalog) 072 { 073 // Nothing to do, only on initialization, data not synchronizable 074 return Collections.EMPTY_LIST; 075 } 076 077 private Long _orderFromCategory(OdfReferenceTableEntry referenceTableEntry) 078 { 079 return referenceTableEntry.getContent() 080 .<ContentValue>getValue("category") 081 .getContent() 082 .getValue("order"); 083 } 084 085 private ParameterizableQuery _deleteOldData() 086 { 087 return ExportUtils.deleteFrom(__TABLE_NAME); 088 } 089 090 private List<ParameterizableQuery> _getInsertQueries() 091 { 092 AtomicInteger order = new AtomicInteger(); 093 String lang = Config.getInstance().getValue("odf.programs.lang"); 094 return _refTableHelper.getItems(OdfReferenceTableHelper.ENSEIGNEMENT_NATURE) 095 .stream() 096 .sequential() 097 .sorted(Comparator 098 .comparingLong(this::_orderFromCategory) 099 .thenComparing(OdfReferenceTableEntry::getCode)) 100 .map(enseignementNature -> _getInsertQuery(enseignementNature, order, lang)) 101 .collect(Collectors.toList()); 102 } 103 104 private ParameterizableQuery _getInsertQuery(OdfReferenceTableEntry enseignementNature, AtomicInteger order, String lang) 105 { 106 String label = enseignementNature.getLabel(lang); 107 108 List<QueryParameter> queryParameters = Arrays.asList( 109 new StaticQueryParameter("ID", "TYPE_INTERVENTION_ID_SEQ.NEXTVAL"), 110 new ValuedQueryParameter("CODE", enseignementNature.getCode(), Types.VARCHAR), 111 new ValuedQueryParameter("LIBELLE", StringUtils.truncate(label, 60), Types.VARCHAR), 112 new ValuedQueryParameter("ORDRE", order.incrementAndGet(), Types.NUMERIC), 113 new StaticQueryParameter("HISTO_CREATEUR_ID", String.valueOf(1)), 114 new StaticQueryParameter("HISTO_MODIFICATEUR_ID", String.valueOf(1)), 115 new StaticQueryParameter("VISIBLE", String.valueOf(1)) 116 ); 117 118 return ExportUtils.insertInto(__TABLE_NAME, queryParameters); 119 } 120}