001/*
002 *  Copyright 2017 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.plugins.extraction.execution;
017
018import java.util.ArrayList;
019import java.util.List;
020import java.util.Optional;
021
022import org.ametys.core.user.UserIdentity;
023import org.ametys.plugins.extraction.component.ExtractionComponent;
024
025/**
026 * Object representing the extraction definition file content
027 */
028public class Extraction
029{    
030    /**
031     * Rights profiles
032     */
033    public enum ExtractionProfile
034    {
035        /** Read access */
036        READ_ACCESS,
037        /** Write access */
038        WRITE_ACCESS,
039        /** Right access */
040        RIGHT_ACCESS;
041        
042        @Override
043        public String toString()
044        {
045            return name().toLowerCase();
046        }
047        
048    }
049    
050    /**
051     * Types of clauses variables
052     */
053    public enum ClausesVariableType
054    {
055        /** Solr request type: the user will give a solr request to select the contents */
056        SOLR_REQUEST("solrRequest"),
057        /** Select contents type: the user will select contents in dropdown list */ 
058        SELECT_CONTENTS("selectContents");
059        
060        private final String _stringValue;
061        
062        ClausesVariableType(String stringValue)
063        {
064            _stringValue = stringValue;
065        }
066        
067        /**
068         * Retrieves the enum's value
069         * @return the enum's value
070         */
071        public String getStringValue()
072        {
073            return _stringValue;
074        }
075        
076        /**
077         * Retrieves the {@link ClausesVariableType} corresponding to the given string value
078         * @param stringValue the string value
079         * @return the {@link ClausesVariableType} corresponding to the given string value
080         * @throws IllegalArgumentException if the given string value is not an available type
081         */
082        public static ClausesVariableType fromStringValue(String stringValue) throws IllegalArgumentException
083        {
084            for (ClausesVariableType type : ClausesVariableType.values())
085            {
086                if (type.getStringValue().equals(stringValue))
087                {
088                    return type;
089                }
090            }
091            
092            throw new IllegalArgumentException("'" + stringValue + "' is not an available type of clauses variable");
093        }
094    }
095    
096    private String _fileName;
097    private String _descriptionId;
098    private List<ExtractionComponent> _extractionComponents;
099    private List<String> _displayOptionalColumnsNames;
100    private List<ClausesVariable> _clausesVariables;
101    private UserIdentity _author;
102    
103    /**
104     * Constructor
105     * @param fileName The file name
106     */
107    public Extraction(String fileName)
108    {
109        _fileName = fileName;
110    }
111    
112    /**
113     * Stores a clauses variable data
114     * @param name the name of the variable
115     * @param type the type of the variable (SOLR_REQUEST or SELECT_CONTENTS)
116     * @param contentTypeIds the content type identifiers to restrict the contents to select. Only one content type should be provided for SELECT_CONTENT type of variables
117     * @param solrRequest the solr request use to limit contents to select
118     */
119    public record ClausesVariable(String name, ClausesVariableType type, List<String> contentTypeIds, Optional<String> solrRequest) { /* empty */ }
120    
121    /**
122     * Get the file name of this extraction
123     * @return the file name
124     */
125    public String getFileName()
126    {
127        return _fileName;
128    }
129    
130    /**
131     * Retrieves the identifier of the extraction's description
132     * @return the identifier of the extraction's description
133     */
134    public String getDescriptionId()
135    {
136        return _descriptionId;
137    }
138    
139    /**
140     * Set the identifier of the extraction's description
141     * @param descriptionId the identifier to set
142     */
143    public void setDescriptionId(String descriptionId)
144    {
145        _descriptionId = descriptionId;
146    }
147    
148    /**
149     * Retrieves the list of the extraction components
150     * @return The extraction components 
151     */
152    public List<ExtractionComponent> getExtractionComponents()
153    {
154        return null != _extractionComponents ? _extractionComponents : new ArrayList<>();
155    }
156    
157    /**
158     * Add an extraction component 
159     * @param extractionComponent The extraction component to add
160     */
161    public void addExtractionComponent(ExtractionComponent extractionComponent)
162    {
163        if (null == _extractionComponents)
164        {
165            _extractionComponents = new ArrayList<>();
166        }
167        _extractionComponents.add(extractionComponent);
168    }
169    
170    /**
171     * Retrieves the list of variables names controlling display of optional columns
172     * @return The variables names
173     */
174    public List<String> getDisplayOptionalColumnsNames()
175    {
176        return null != _displayOptionalColumnsNames ? _displayOptionalColumnsNames : new ArrayList<>();
177    }
178    
179    /**
180     * Set variables names controlling display of optional columns
181     * @param displayOptionalColumnsNames the variables for the optional columns to set
182     */
183    public void setDisplayOptionalColumnsNames(List<String> displayOptionalColumnsNames)
184    {
185        _displayOptionalColumnsNames = displayOptionalColumnsNames;
186    }
187    
188    /**
189     * Retrieves the list of variables to use in clauses
190     * @return A List containing clauses variables
191     */
192    public List<ClausesVariable> getClausesVariables()
193    {
194        return null != _clausesVariables ? _clausesVariables : new ArrayList<>();
195    }
196    
197    /**
198     * Set variables to use in clauses
199     * @param clausesVariables A Map containing variables to set
200     */
201    public void setClausesVariables(List<ClausesVariable> clausesVariables)
202    {
203        _clausesVariables = clausesVariables;
204    }
205
206    /**
207     * Get the author of the extraction
208     * @return The author
209     */
210    public UserIdentity getAuthor ()
211    {
212        return _author;
213    }
214    
215    /**
216     * Set the author of this extraction.
217     * @param author the author
218     */
219    public void setAuthor(UserIdentity author)
220    {
221        _author = author;
222    }
223}