001/*
002 *  Copyright 2020 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.odf.db;
017
018import java.sql.Types;
019import java.util.ArrayList;
020import java.util.HashSet;
021import java.util.List;
022import java.util.Set;
023
024import org.apache.commons.lang3.tuple.Pair;
025
026import org.ametys.odf.ose.db.ParameterizableQuery;
027import org.ametys.odf.ose.db.column.Column;
028import org.ametys.odf.ose.db.column.DefaultColumn;
029import org.ametys.odf.ose.db.column.ForeignKeyColumn;
030import org.ametys.odf.ose.db.column.SourceIdColumn;
031import org.ametys.odf.ose.db.parameter.QueryParameter;
032import org.ametys.odf.ose.db.parameter.ValuedQueryParameter;
033import org.ametys.odf.ose.export.ExportUtils;
034
035/**
036 * Helper to build queries to manage the TYPE_INTERVENTION_EP table.
037 */
038public final class TypeInterventionEPHelper
039{
040    private static final String __TABLE_NAME = "TYPE_INTERVENTION_EP";
041    
042    private TypeInterventionEPHelper()
043    {
044        // Helper
045    }
046
047    /**
048     * Queries to create the table and associated view.
049     * @return List of queries for table and view creation
050     */
051    public static List<ParameterizableQuery> initialize()
052    {
053        Set<Column> columns = new HashSet<>();
054        columns.add(new DefaultColumn("ID", "VARCHAR2(100 CHAR)", false, "SOURCE_CODE"));
055        columns.add(new ForeignKeyColumn("TYPE_INTERVENTION_ID", "VARCHAR2(10 CHAR)", false, Pair.of("TYPE_INTERVENTION", "CODE"), false));
056        columns.add(new ForeignKeyColumn("ELEMENT_PEDAGOGIQUE_ID", "VARCHAR2(100 CHAR)", false, Pair.of("ELEMENT_PEDAGOGIQUE", "SOURCE_CODE"), true));
057        columns.add(new DefaultColumn("ANNEE_ID", "NUMBER(38, 0)", false, null));
058        columns.add(new SourceIdColumn());
059        return ExportUtils.initializeTableAndView(__TABLE_NAME, columns);
060    }
061
062    /**
063     * Query to delete all lines from the table.
064     * @return The query for deletion
065     */
066    public static ParameterizableQuery deleteFrom()
067    {
068        return ExportUtils.deleteFromAmetys(__TABLE_NAME);
069    }
070
071    /**
072     * Query to insert a line in the table.
073     * @param coursePartCode The course part code
074     * @param coursePartNature The course part nature
075     * @param courseLink the course link
076     * @param oseCatalog The OSE catalog
077     * @return The query to insert values in the table
078     */
079    public static ParameterizableQuery insertInto(String coursePartCode, String coursePartNature, String courseLink, Long oseCatalog)
080    {
081        List<QueryParameter> parameters = new ArrayList<>();
082        parameters.add(new ValuedQueryParameter("ID", coursePartCode, Types.VARCHAR));
083        parameters.add(new ValuedQueryParameter("TYPE_INTERVENTION_ID", coursePartNature, Types.VARCHAR));
084        parameters.add(new ValuedQueryParameter("ELEMENT_PEDAGOGIQUE_ID", courseLink, Types.VARCHAR));
085        parameters.add(new ValuedQueryParameter("ANNEE_ID", oseCatalog, Types.NUMERIC));
086        return ExportUtils.insertIntoAmetys(__TABLE_NAME, parameters);
087    }
088}