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.io.File;
019import java.util.ArrayList;
020import java.util.LinkedHashMap;
021import java.util.List;
022import java.util.Map;
023
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.excalibur.source.Source;
027import org.apache.excalibur.source.SourceResolver;
028import org.apache.excalibur.source.impl.FileSource;
029
030import org.ametys.core.ui.Callable;
031import org.ametys.core.ui.StaticClientSideElement;
032import org.ametys.plugins.extraction.ExtractionConstants;
033import org.ametys.plugins.extraction.component.ExtractionComponent;
034import org.ametys.plugins.extraction.edition.SaveExtractionHelper;
035import org.ametys.runtime.i18n.I18nizableText;
036
037/**
038 *  Tool client side element for extraction edition tool
039 */
040public class ExtractionDetailsToolElement extends StaticClientSideElement
041{
042    private ExtractionDefinitionReader _reader;
043    private SourceResolver _sourceResolver;
044    private SaveExtractionHelper _saveHelper;
045    
046    @Override
047    public void service(ServiceManager serviceManager) throws ServiceException
048    {
049        super.service(serviceManager);
050        _reader = (ExtractionDefinitionReader) serviceManager.lookup(ExtractionDefinitionReader.ROLE);
051        _sourceResolver = (SourceResolver) serviceManager.lookup(SourceResolver.ROLE);
052        _saveHelper = (SaveExtractionHelper) serviceManager.lookup(SaveExtractionHelper.ROLE);
053    }
054    
055    /**
056     * Retrieve extraction definition details.
057     * @param definitionFile The extraction's definition file path
058     * @return a <code>Map</code> containing the extraction definition details
059     * @throws Exception if an error occurs
060     */
061    @Callable (right = "Extraction_Rights_ExecuteExtraction")
062    public Map<String, Object> getExtractionDefinitionDetails(String definitionFile) throws Exception
063    {
064        Map<String, Object> extractionDefinitionDetails = new LinkedHashMap<>();
065
066        String definitionFilePath = ExtractionConstants.DEFINITIONS_DIR + definitionFile;
067        Source src = _sourceResolver.resolveURI(definitionFilePath);
068        File file = ((FileSource) src).getFile();
069        
070        if (!file.exists())
071        {
072            if (getLogger().isWarnEnabled())
073            {
074                getLogger().warn("The file " + definitionFilePath + " does not exist.");
075            }
076            
077            extractionDefinitionDetails.put("success", false);
078            extractionDefinitionDetails.put("file-error", "unexisting");
079            return extractionDefinitionDetails;
080        }
081        
082        Extraction extraction = _reader.readExtractionDefinitionFile(file);
083        
084        List<Map<String, Object>> extractionNodes = new ArrayList<>();
085        
086        Map<String, Object> clausesVariablesNode = _getClausesVariablesNode(extraction);
087        extractionNodes.add(clausesVariablesNode);
088        
089        Map<String, Object> optionalColumnsNode = _getOptionalColumnsNode(extraction);
090        extractionNodes.add(optionalColumnsNode);
091        
092        List<Map<String, Object>> componentsNodes = _getComponentNodes(extraction.getExtractionComponents());
093        if (!componentsNodes.isEmpty())
094        {
095            extractionNodes.addAll(componentsNodes);
096        }
097        
098        extractionDefinitionDetails.put("success", true);
099        extractionDefinitionDetails.put("children", extractionNodes);
100        return extractionDefinitionDetails;
101    }
102
103    private Map<String, Object> _getClausesVariablesNode(Extraction extraction)
104    {
105        Map<String, Object> clausesVariablesNode = new LinkedHashMap<>();
106        Map<String, String> clausesVariables = extraction.getQueryVariablesNamesAndContentTypes();
107            
108        List<Map<String, Object>> variables = new ArrayList<>();
109        for (Map.Entry<String, String> clauseVariable : clausesVariables.entrySet())
110        {
111            Map<String, Object> clausesVariableData = new LinkedHashMap<>();
112            clausesVariableData.put("name", clauseVariable.getKey());
113            clausesVariableData.put("contentType", clauseVariable.getValue());
114            variables.add(clausesVariableData);
115        }
116        
117        Map<String, Object> clausesVariablesData = new LinkedHashMap<>();
118        clausesVariablesData.put("variables", variables);
119        
120        clausesVariablesNode.put("text", new I18nizableText(ExtractionConstants.PLUGIN_NAME, "PLUGINS_EXTRACTION_TREE_CLAUSES_VARIABLES_NODE_TEXT"));
121        clausesVariablesNode.put("data", clausesVariablesData);
122        clausesVariablesNode.put("leaf", true);
123        clausesVariablesNode.put("tag", ExtractionConstants.CLAUSES_VARIABLES_TAG);
124        clausesVariablesNode.put("iconCls", "ametysicon-symbol-x");
125        
126        return clausesVariablesNode;
127    }
128
129    private Map<String, Object> _getOptionalColumnsNode(Extraction extraction)
130    {
131        Map<String, Object> optionalColumnsNode = new LinkedHashMap<>();
132        List<String> optionalColumns = extraction.getDisplayOptionalColumnsNames();
133        
134        Map<String, Object> optionalColumnsData = new LinkedHashMap<>();
135        optionalColumnsData.put("names", optionalColumns);
136        
137        optionalColumnsNode.put("text", new I18nizableText(ExtractionConstants.PLUGIN_NAME, "PLUGINS_EXTRACTION_TREE_OPTIONAL_COLUMNS_NODE_TEXT"));
138        optionalColumnsNode.put("data", optionalColumnsData);
139        optionalColumnsNode.put("leaf", true);
140        optionalColumnsNode.put("tag", ExtractionConstants.OPTIONAL_COLUMNS_TAG);
141        optionalColumnsNode.put("iconCls", "ametysicon-table28");
142
143        return optionalColumnsNode;
144    }
145
146    private List<Map<String, Object>> _getComponentNodes(List<ExtractionComponent> components)
147    {
148        List<Map<String, Object>> componentNodes = new ArrayList<>();
149        for (ExtractionComponent component : components)
150        {
151            Map<String, Object> componentNode = component.getComponentDetailsForTree();
152            if (component.getSubComponents().isEmpty())
153            {
154                componentNode.put("leaf", true);
155            }
156            else
157            {
158                
159                componentNode.put("children", _getComponentNodes(component.getSubComponents()));
160            }
161            componentNodes.add(componentNode);
162        }
163        return componentNodes;
164    }
165    
166    /**
167     * Saves modifications on extraction. Creates the definition file if it doesn't exist
168     * @param definitionFileName The extraction definition file name
169     * @param definitionNodes A <code>Map</code> containing definition informations
170     * @return <code>true</code> if extraction saving succeed, <code>false</code> otherwise
171     * @throws Exception if an error occurs
172     */
173    @Callable (right = "Extraction_Rights_EditExtraction")
174    public boolean saveExtraction(String definitionFileName, Map<String, Object> definitionNodes) throws Exception
175    {
176        return _saveHelper.saveExtraction(definitionFileName, definitionNodes);
177    }
178}