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 relativeDefinitionFilePath The extraction's definition file path, relative to the base definitions directory 058 * @return a <code>Map</code> containing the extraction definition details 059 * @throws Exception if an error occurs 060 */ 061 @Callable (right = ExtractionConstants.EXECUTE_EXTRACTION_RIGHT_ID) 062 public Map<String, Object> getExtractionDefinitionDetails(String relativeDefinitionFilePath) throws Exception 063 { 064 Map<String, Object> extractionDefinitionDetails = new LinkedHashMap<>(); 065 066 String absoluteDefinitionFilePath = ExtractionConstants.DEFINITIONS_DIR + relativeDefinitionFilePath; 067 Source src = _sourceResolver.resolveURI(absoluteDefinitionFilePath); 068 File file = ((FileSource) src).getFile(); 069 070 if (!file.exists()) 071 { 072 if (getLogger().isWarnEnabled()) 073 { 074 getLogger().warn("The file " + relativeDefinitionFilePath + " 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("descriptionId", extraction.getDescriptionId()); 100 extractionDefinitionDetails.put("children", extractionNodes); 101 return extractionDefinitionDetails; 102 } 103 104 private Map<String, Object> _getClausesVariablesNode(Extraction extraction) 105 { 106 Map<String, Object> clausesVariablesNode = new LinkedHashMap<>(); 107 Map<String, String> clausesVariables = extraction.getQueryVariablesNamesAndContentTypes(); 108 109 List<Map<String, Object>> variables = new ArrayList<>(); 110 for (Map.Entry<String, String> clauseVariable : clausesVariables.entrySet()) 111 { 112 Map<String, Object> clausesVariableData = new LinkedHashMap<>(); 113 clausesVariableData.put("name", clauseVariable.getKey()); 114 clausesVariableData.put("contentType", clauseVariable.getValue()); 115 variables.add(clausesVariableData); 116 } 117 118 Map<String, Object> clausesVariablesData = new LinkedHashMap<>(); 119 clausesVariablesData.put("variables", variables); 120 121 clausesVariablesNode.put("text", new I18nizableText(ExtractionConstants.PLUGIN_NAME, "PLUGINS_EXTRACTION_TREE_CLAUSES_VARIABLES_NODE_TEXT")); 122 clausesVariablesNode.put("data", clausesVariablesData); 123 clausesVariablesNode.put("leaf", true); 124 clausesVariablesNode.put("tag", ExtractionConstants.CLAUSES_VARIABLES_TAG); 125 clausesVariablesNode.put("iconCls", "ametysicon-symbol-x"); 126 127 return clausesVariablesNode; 128 } 129 130 private Map<String, Object> _getOptionalColumnsNode(Extraction extraction) 131 { 132 Map<String, Object> optionalColumnsNode = new LinkedHashMap<>(); 133 List<String> optionalColumns = extraction.getDisplayOptionalColumnsNames(); 134 135 Map<String, Object> optionalColumnsData = new LinkedHashMap<>(); 136 optionalColumnsData.put("names", optionalColumns); 137 138 optionalColumnsNode.put("text", new I18nizableText(ExtractionConstants.PLUGIN_NAME, "PLUGINS_EXTRACTION_TREE_OPTIONAL_COLUMNS_NODE_TEXT")); 139 optionalColumnsNode.put("data", optionalColumnsData); 140 optionalColumnsNode.put("leaf", true); 141 optionalColumnsNode.put("tag", ExtractionConstants.OPTIONAL_COLUMNS_TAG); 142 optionalColumnsNode.put("iconCls", "ametysicon-table28"); 143 144 return optionalColumnsNode; 145 } 146 147 private List<Map<String, Object>> _getComponentNodes(List<ExtractionComponent> components) 148 { 149 List<Map<String, Object>> componentNodes = new ArrayList<>(); 150 for (ExtractionComponent component : components) 151 { 152 Map<String, Object> componentNode = component.getComponentDetailsForTree(); 153 if (component.getSubComponents().isEmpty()) 154 { 155 componentNode.put("leaf", true); 156 } 157 else 158 { 159 160 componentNode.put("children", _getComponentNodes(component.getSubComponents())); 161 } 162 componentNodes.add(componentNode); 163 } 164 return componentNodes; 165 } 166 167 /** 168 * Saves modifications on extraction. Creates the definition file if it doesn't exist 169 * @param definitionFileName The extraction definition file name 170 * @param extraction A <code>Map</code> containing definition information 171 * @return <code>true</code> if extraction saving succeed, <code>false</code> otherwise 172 * @throws Exception if an error occurs 173 */ 174 @Callable (right = ExtractionConstants.MODIFY_EXTRACTION_RIGHT_ID) 175 public boolean saveExtraction(String definitionFileName, Map<String, Object> extraction) throws Exception 176 { 177 return _saveHelper.saveExtraction(definitionFileName, extraction); 178 } 179}