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 searchModelId the search model identifier to use for select contents
118     * @param solrRequest the solr request use to limit contents to select
119     */
120    public record ClausesVariable(String name, ClausesVariableType type, List<String> contentTypeIds, Optional<String> searchModelId, Optional<String> solrRequest) { /* empty */ }
121    
122    /**
123     * Get the file name of this extraction
124     * @return the file name
125     */
126    public String getFileName()
127    {
128        return _fileName;
129    }
130    
131    /**
132     * Retrieves the identifier of the extraction's description
133     * @return the identifier of the extraction's description
134     */
135    public String getDescriptionId()
136    {
137        return _descriptionId;
138    }
139    
140    /**
141     * Set the identifier of the extraction's description
142     * @param descriptionId the identifier to set
143     */
144    public void setDescriptionId(String descriptionId)
145    {
146        _descriptionId = descriptionId;
147    }
148    
149    /**
150     * Retrieves the list of the extraction components
151     * @return The extraction components 
152     */
153    public List<ExtractionComponent> getExtractionComponents()
154    {
155        return null != _extractionComponents ? _extractionComponents : new ArrayList<>();
156    }
157    
158    /**
159     * Add an extraction component 
160     * @param extractionComponent The extraction component to add
161     */
162    public void addExtractionComponent(ExtractionComponent extractionComponent)
163    {
164        if (null == _extractionComponents)
165        {
166            _extractionComponents = new ArrayList<>();
167        }
168        _extractionComponents.add(extractionComponent);
169    }
170    
171    /**
172     * Retrieves the list of variables names controlling display of optional columns
173     * @return The variables names
174     */
175    public List<String> getDisplayOptionalColumnsNames()
176    {
177        return null != _displayOptionalColumnsNames ? _displayOptionalColumnsNames : new ArrayList<>();
178    }
179    
180    /**
181     * Set variables names controlling display of optional columns
182     * @param displayOptionalColumnsNames the variables for the optional columns to set
183     */
184    public void setDisplayOptionalColumnsNames(List<String> displayOptionalColumnsNames)
185    {
186        _displayOptionalColumnsNames = displayOptionalColumnsNames;
187    }
188    
189    /**
190     * Retrieves the list of variables to use in clauses
191     * @return A List containing clauses variables
192     */
193    public List<ClausesVariable> getClausesVariables()
194    {
195        return null != _clausesVariables ? _clausesVariables : new ArrayList<>();
196    }
197    
198    /**
199     * Set variables to use in clauses
200     * @param clausesVariables A Map containing variables to set
201     */
202    public void setClausesVariables(List<ClausesVariable> clausesVariables)
203    {
204        _clausesVariables = clausesVariables;
205    }
206
207    /**
208     * Get the author of the extraction
209     * @return The author
210     */
211    public UserIdentity getAuthor ()
212    {
213        return _author;
214    }
215    
216    /**
217     * Set the author of this extraction.
218     * @param author the author
219     */
220    public void setAuthor(UserIdentity author)
221    {
222        _author = author;
223    }
224}