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.StringUtils;
025import org.apache.commons.lang3.tuple.Pair;
026
027import org.ametys.odf.ose.db.ParameterizableQuery;
028import org.ametys.odf.ose.db.column.Column;
029import org.ametys.odf.ose.db.column.DefaultColumn;
030import org.ametys.odf.ose.db.column.ForeignKeyColumn;
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 NOEUD table.
039 */
040public final class NoeudHelper
041{
042    private static final String __TABLE_NAME = "NOEUD";
043    
044    private NoeudHelper()
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 DefaultColumn("CODE", "VARCHAR2(50 CHAR)", false));
058        columns.add(new DefaultColumn("LIBELLE", "VARCHAR2(255 CHAR)", false));
059        columns.add(new DefaultColumn("LISTE", "NUMBER(1)", false));
060        columns.add(new ForeignKeyColumn("ETAPE_ID", "VARCHAR2(100 CHAR)", true, Pair.of("ETAPE", "SOURCE_CODE"), true));
061        columns.add(new ForeignKeyColumn("ELEMENT_PEDAGOGIQUE_ID", "VARCHAR2(100 CHAR)", true, Pair.of("ELEMENT_PEDAGOGIQUE", "SOURCE_CODE"), true));
062        columns.add(new DefaultColumn("ANNEE_ID", "NUMBER(38, 0)", false));
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 nodeId The identifier (concatenation of the catalog and the code)
079     * @param code The code
080     * @param isEtape <code>true</code> if it is a step, otherwise set it to <code>false</code>
081     * @param title The title
082     * @param oseCatalog The OSE catalog
083     * @return The query to insert values in the table
084     */
085    public static ParameterizableQuery insertInto(String nodeId, String code, boolean isEtape, String title, Long oseCatalog)
086    {
087        List<QueryParameter> parameters = new ArrayList<>();
088        parameters.add(new ValuedQueryParameter("ID", nodeId, Types.VARCHAR));
089        parameters.add(new ValuedQueryParameter("CODE", code, Types.VARCHAR));
090        parameters.add(new ValuedQueryParameter("LIBELLE", StringUtils.truncate(title, 255), Types.VARCHAR));
091        parameters.add(new StaticQueryParameter("LISTE", String.valueOf(0)));
092        parameters.add(new ValuedQueryParameter("ETAPE_ID", isEtape ? code : null, Types.VARCHAR));
093        parameters.add(new ValuedQueryParameter("ELEMENT_PEDAGOGIQUE_ID", isEtape ? null : code, Types.VARCHAR));
094        parameters.add(new ValuedQueryParameter("ANNEE_ID", oseCatalog, Types.NUMERIC));
095        return ExportUtils.insertIntoAmetys(__TABLE_NAME, parameters);
096    }
097}