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_NOEUD table.
039 */
040public final class ScenarioNoeudHelper
041{
042    private static final String __TABLE_NAME = "SCENARIO_NOEUD";
043    
044    private ScenarioNoeudHelper()
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("NOEUD_ID", "VARCHAR2(100 CHAR)", false, Pair.of("NOEUD", "SOURCE_CODE"), false));
058        columns.add(new DefaultColumn("ASSIDUITE", "FLOAT", false));
059        columns.add(new ScenarioIdColumn());
060        columns.add(new SourceIdColumn());
061        return ExportUtils.initializeTableAndView(__TABLE_NAME, columns);
062    }
063
064    /**
065     * Query to delete all lines from the table.
066     * @return The query for deletion
067     */
068    public static ParameterizableQuery deleteFrom()
069    {
070        return ExportUtils.deleteFromAmetys(__TABLE_NAME);
071    }
072
073    /**
074     * Query to insert a line in the table.
075     * @param nodeId The identifier of the referenced node
076     * @return The query to insert values in the table
077     */
078    public static ParameterizableQuery insertInto(String nodeId)
079    {
080        List<QueryParameter> parameters = new ArrayList<>();
081        parameters.add(new ValuedQueryParameter("ID", nodeId, Types.VARCHAR));
082        parameters.add(new ValuedQueryParameter("NOEUD_ID", nodeId, Types.VARCHAR));
083        parameters.add(new StaticQueryParameter("ASSIDUITE", String.valueOf(1)));
084        return ExportUtils.insertIntoAmetys(__TABLE_NAME, parameters);
085    }
086}