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.ScenarioIdColumn;
031import org.ametys.odf.ose.db.column.SourceIdColumn;
032import org.ametys.odf.ose.db.parameter.QueryParameter;
033import org.ametys.odf.ose.db.parameter.StaticQueryParameter;
034import org.ametys.odf.ose.db.parameter.ValuedQueryParameter;
035import org.ametys.odf.ose.export.ExportUtils;
036
037/**
038 * Helper to build queries to manage the SCENARIO_LIEN table.
039 */
040public final class ScenarioLienHelper
041{
042    private static final String __TABLE_NAME = "SCENARIO_LIEN";
043    
044    private ScenarioLienHelper()
045    {
046        // Helper
047    }
048
049    /**
050     * Queries to create the table and associated view.
051     * @return List of queries for table and view creation
052     */
053    public static List<ParameterizableQuery> initialize()
054    {
055        Set<Column> columns = new HashSet<>();
056        columns.add(new DefaultColumn("ID", "VARCHAR2(100 CHAR)", false, "SOURCE_CODE"));
057        columns.add(new ForeignKeyColumn("LIEN_ID", "VARCHAR2(100 CHAR)", false, Pair.of("LIEN", "SOURCE_CODE"), false));
058        columns.add(new DefaultColumn("ACTIF", "NUMBER(1)", false));
059        columns.add(new DefaultColumn("POIDS", "FLOAT", false));
060        columns.add(new DefaultColumn("CHOIX_MINIMUM", "NUMBER", true));
061        columns.add(new DefaultColumn("CHOIX_MAXIMUM", "NUMBER", true));
062        columns.add(new ScenarioIdColumn());
063        columns.add(new SourceIdColumn());
064        return ExportUtils.initializeTableAndView(__TABLE_NAME, columns);
065    }
066
067    /**
068     * Query to delete all lines from the table.
069     * @return The query for deletion
070     */
071    public static ParameterizableQuery deleteFrom()
072    {
073        return ExportUtils.deleteFromAmetys(__TABLE_NAME);
074    }
075
076    /**
077     * Query to insert a line in the table.
078     * @param code The code
079     * @param weight The weight (1 in most case, can be 0 for optional lists and min/nbChildren for choice lists)
080     * @param minChoice The min choice (only for choice lists), otherwise should be null
081     * @param maxChoice The max choice (only for choice lists), otherwise should be null
082     * @return The query to insert values in the table
083     */
084    public static ParameterizableQuery insertInto(String code, Double weight, Long minChoice, Long maxChoice)
085    {
086        List<QueryParameter> parameters = new ArrayList<>();
087        parameters.add(new ValuedQueryParameter("ID", code, Types.VARCHAR));
088        parameters.add(new ValuedQueryParameter("LIEN_ID", code, Types.VARCHAR));
089        parameters.add(new StaticQueryParameter("ACTIF", String.valueOf(1)));
090        parameters.add(new ValuedQueryParameter("POIDS", weight, Types.FLOAT));
091        parameters.add(new ValuedQueryParameter("CHOIX_MINIMUM", minChoice, Types.NUMERIC));
092        parameters.add(new ValuedQueryParameter("CHOIX_MAXIMUM", maxChoice, Types.NUMERIC));
093        return ExportUtils.insertIntoAmetys(__TABLE_NAME, parameters);
094    }
095}