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.plugins.contentio.csv;
017
018import java.io.IOException;
019import java.util.ArrayList;
020import java.util.Arrays;
021import java.util.HashMap;
022import java.util.List;
023import java.util.Locale;
024import java.util.Map;
025import java.util.Map.Entry;
026import java.util.Objects;
027import java.util.Optional;
028import java.util.Set;
029import java.util.function.Function;
030import java.util.stream.Collectors;
031import java.util.stream.Stream;
032
033import org.apache.avalon.framework.component.Component;
034import org.apache.avalon.framework.service.ServiceException;
035import org.apache.avalon.framework.service.ServiceManager;
036import org.apache.avalon.framework.service.Serviceable;
037import org.apache.commons.lang3.StringUtils;
038import org.supercsv.io.ICsvListReader;
039import org.supercsv.util.Util;
040
041import org.ametys.cms.ObservationConstants;
042import org.ametys.cms.contenttype.ContentAttributeDefinition;
043import org.ametys.cms.contenttype.ContentType;
044import org.ametys.cms.contenttype.ContentTypeExtensionPoint;
045import org.ametys.cms.data.ContentValue;
046import org.ametys.cms.data.type.AbstractMultilingualStringElementType;
047import org.ametys.cms.data.type.impl.MultilingualStringRepositoryElementType;
048import org.ametys.cms.indexing.solr.SolrIndexHelper;
049import org.ametys.cms.repository.Content;
050import org.ametys.cms.repository.ContentQueryHelper;
051import org.ametys.cms.repository.ModifiableDefaultContent;
052import org.ametys.cms.repository.ModifiableWorkflowAwareContent;
053import org.ametys.cms.workflow.AbstractContentWorkflowComponent;
054import org.ametys.cms.workflow.ContentWorkflowHelper;
055import org.ametys.cms.workflow.CreateContentFunction;
056import org.ametys.core.util.I18nUtils;
057import org.ametys.plugins.contentio.csv.SynchronizeModeEnumerator.ImportMode;
058import org.ametys.plugins.contentio.in.ContentImportException;
059import org.ametys.plugins.repository.AmetysObjectIterable;
060import org.ametys.plugins.repository.AmetysObjectResolver;
061import org.ametys.plugins.repository.data.holder.group.ModelAwareRepeater;
062import org.ametys.plugins.repository.data.holder.group.ModelAwareRepeaterEntry;
063import org.ametys.plugins.repository.data.holder.group.Repeater;
064import org.ametys.plugins.repository.data.holder.values.SynchronizableRepeater;
065import org.ametys.plugins.repository.data.holder.values.SynchronizableValue;
066import org.ametys.plugins.repository.data.holder.values.SynchronizableValue.Mode;
067import org.ametys.plugins.repository.data.type.ModelItemTypeConstants;
068import org.ametys.plugins.repository.metadata.MultilingualString;
069import org.ametys.plugins.repository.metadata.MultilingualStringHelper;
070import org.ametys.plugins.repository.query.expression.AndExpression;
071import org.ametys.plugins.repository.query.expression.Expression;
072import org.ametys.plugins.repository.query.expression.Expression.Operator;
073import org.ametys.plugins.repository.query.expression.ExpressionContext;
074import org.ametys.plugins.repository.query.expression.MultilingualStringExpression;
075import org.ametys.plugins.repository.query.expression.StringExpression;
076import org.ametys.runtime.model.ElementDefinition;
077import org.ametys.runtime.model.ModelItem;
078import org.ametys.runtime.model.ModelViewItemGroup;
079import org.ametys.runtime.model.View;
080import org.ametys.runtime.model.ViewElement;
081import org.ametys.runtime.model.ViewElementAccessor;
082import org.ametys.runtime.model.ViewItem;
083import org.ametys.runtime.model.ViewItemAccessor;
084import org.ametys.runtime.model.type.ElementType;
085import org.ametys.runtime.plugin.component.AbstractLogEnabled;
086
087import com.opensymphony.workflow.WorkflowException;
088
089/**
090 * Import contents from an uploaded CSV file.
091 */
092public class CSVImporter extends AbstractLogEnabled implements Component, Serviceable
093{
094    
095    /** Avalon Role */
096    public static final String ROLE = CSVImporter.class.getName();
097
098    /** Result key containing updated content's ids */
099    public static final String RESULT_CONTENT_IDS = "contentIds";
100    /** Result key containing number of errors */
101    public static final String RESULT_NB_ERRORS = "nbErrors";
102    /** Result key containing number of warnings */
103    public static final String RESULT_NB_WARNINGS = "nbWarnings";
104
105    private ContentWorkflowHelper _contentWorkflowHelper;
106
107    private ContentTypeExtensionPoint _contentTypeEP;
108    
109    private AmetysObjectResolver _resolver;
110
111    private I18nUtils _i18nUtils;
112
113    private SolrIndexHelper _solrIndexHelper;
114    
115    public void service(ServiceManager smanager) throws ServiceException
116    {
117        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
118        _contentTypeEP = (ContentTypeExtensionPoint) smanager.lookup(ContentTypeExtensionPoint.ROLE);
119        _contentWorkflowHelper = (ContentWorkflowHelper) smanager.lookup(ContentWorkflowHelper.ROLE);
120        _i18nUtils = (I18nUtils) smanager.lookup(I18nUtils.ROLE);
121        _solrIndexHelper = (SolrIndexHelper) smanager.lookup(SolrIndexHelper.ROLE);
122    }
123
124    /**
125     * Extract contents from CSV file
126     * @param mapping mapping of content attributes and CSV file header
127     * @param view View of importing content
128     * @param contentType content type to import
129     * @param listReader mapReader to parse CSV file
130     * @param createAction creation action id
131     * @param editAction edition action id
132     * @param workflowName workflow name
133     * @param language language of created content.
134     * @param importMode The import mode
135     * @return list of created contents
136     * @throws IOException IOException while reading CSV
137     */
138    public Map<String, Object> importContentsFromCSV(Map<String, Object> mapping, View view, ContentType contentType, ICsvListReader listReader, int createAction, int editAction, String workflowName, String language, ImportMode importMode) throws IOException
139    {
140        try
141        {
142            _solrIndexHelper.pauseSolrCommitForEvents(_getIndexationEvents());
143            
144            List<String> contentIds = new ArrayList<>();
145            String[] columns = listReader.getHeader(true);
146            int nbErrors = 0;
147            int nbWarnings = 0;
148            List<String> row = null;
149            
150            while ((row = listReader.read()) != null)
151            {
152                try
153                {
154                    if (listReader.length() != columns.length)
155                    {
156                        getLogger().error("[{}] Import from CSV file: content skipped because of invalid row: {}", contentType.getId(), row);
157                        nbErrors++;
158                        continue;
159                    }
160    
161                    Map<String, String> rowMap = new HashMap<>();
162                    Util.filterListToMap(rowMap, columns, row);
163                    List<ViewItem> errors = new ArrayList<>();
164                    SynchronizeResult synchronizeResult = _processContent(view, rowMap, contentType, mapping, createAction, editAction, workflowName, language, errors, importMode);
165                    Optional<ModifiableWorkflowAwareContent> content = synchronizeResult.content();
166                    
167                    // If we are in CREATE_ONLY mode but content was not created, do not add it to the contents list
168                    if (content.isPresent() && (importMode != ImportMode.CREATE_ONLY || synchronizeResult.isCreated()))
169                    {
170                        contentIds.add(content.get().getId());
171                    }
172                    
173                    if (!errors.isEmpty())
174                    {
175                        nbWarnings++;
176                    }
177                }
178                catch (Exception e)
179                {
180                    nbErrors++;
181                    getLogger().error("[{}] Import from CSV file: error importing the content on line {}", contentType.getId(), listReader.getLineNumber(), e);
182                }
183            }
184            
185            Map<String, Object> results = new HashMap<>();
186            results.put(RESULT_CONTENT_IDS, contentIds);
187            results.put(RESULT_NB_ERRORS, nbErrors);
188            results.put(RESULT_NB_WARNINGS, nbWarnings);
189            return results;
190        }
191        finally
192        {
193            _solrIndexHelper.restartSolrCommitForEvents(_getIndexationEvents());
194        }
195    }
196    
197    private String[] _getIndexationEvents()
198    {
199        return new String[] {
200            ObservationConstants.EVENT_CONTENT_ADDED,
201            ObservationConstants.EVENT_CONTENT_MODIFIED,
202            ObservationConstants.EVENT_CONTENT_WORKFLOW_CHANGED,
203            ObservationConstants.EVENT_CONTENT_VALIDATED
204        };
205    }
206    
207    private SynchronizeResult _processContent(View view, Map<String, String> row, ContentType contentType, Map<String, Object> mapping, int createAction, int editAction, String workflowName, String language, List<ViewItem> errors, ImportMode importMode) throws Exception
208    {
209        @SuppressWarnings("unchecked")
210        List<String> attributeIdNames = (List<String>) mapping.get(ImportCSVFileHelper.NESTED_MAPPING_ID);
211        @SuppressWarnings("unchecked")
212        Map<String, Object> mappingValues = (Map<String, Object>) mapping.get(ImportCSVFileHelper.NESTED_MAPPING_VALUES);
213
214        SynchronizeResult synchronizeResult = _synchronizeContent(row, contentType, view, attributeIdNames, mappingValues, createAction, editAction, workflowName, language, errors, importMode, Optional.empty());
215        return synchronizeResult;
216    }
217
218    private void _editContent(int editAction, Map<String, Object> values, ModifiableWorkflowAwareContent content) throws WorkflowException
219    {
220        if (!values.isEmpty())
221        {
222            _contentWorkflowHelper.editContent(content, values, editAction);
223        }
224    }
225    
226    private boolean _isId(ViewItem viewItem, List<String> attributeIdNames)
227    {
228        if (viewItem instanceof ViewElement)
229        {
230            ViewElement viewElement = (ViewElement) viewItem;
231            ElementDefinition elementDefinition = viewElement.getDefinition();
232            if (!(elementDefinition instanceof ContentAttributeDefinition))
233            {
234                String elementName = elementDefinition.getName();
235                return attributeIdNames.contains(elementName);
236            }
237        }
238        return false;
239    }
240    
241    private Object _getValue(Optional<? extends Content> parentContent, ViewItem viewItem, Map<String, Object> mapping, Map<String, String> row, int createAction, int editAction, String language, List<ViewItem> errors, String prefix, ImportMode importMode) throws Exception
242    {
243        if (viewItem instanceof ViewElement)
244        {
245            ViewElement viewElement = (ViewElement) viewItem;
246            ElementDefinition elementDefinition = viewElement.getDefinition();
247            if (elementDefinition instanceof ContentAttributeDefinition)
248            {
249                return _getContentAttributeDefinitionValues(parentContent, viewItem, mapping, row, createAction, editAction, language, viewElement, elementDefinition, errors, importMode);
250            }
251            else
252            {
253                return _getAttributeDefinitionValues(parentContent, mapping, row, elementDefinition, language, prefix);
254            }
255        }
256        else if (viewItem instanceof ModelViewItemGroup)
257        {
258            ModelViewItemGroup modelViewItemGroup = (ModelViewItemGroup) viewItem;
259            List<ViewItem> children = modelViewItemGroup.getViewItems();
260            @SuppressWarnings("unchecked")
261            Map<String, Object> nestedMap = (Map<String, Object>) mapping.get(viewItem.getName());
262            @SuppressWarnings("unchecked")
263            Map<String, Object> nestedMapValues = (Map<String, Object>) (nestedMap.get(ImportCSVFileHelper.NESTED_MAPPING_VALUES));
264            if (ModelItemTypeConstants.REPEATER_TYPE_ID.equals(modelViewItemGroup.getDefinition().getType().getId()))
265            {
266                return _getRepeaterValues(parentContent, modelViewItemGroup, row, createAction, editAction, language, children, nestedMap, errors, prefix, importMode);
267            }
268            else
269            {
270                return _getCompositeValues(parentContent, viewItem, row, createAction, editAction, language, children, nestedMapValues, errors, prefix, importMode);
271            }
272        }
273        else
274        {
275            errors.add(viewItem);
276            throw new RuntimeException("Import from CSV file: unsupported type of ViewItem for view: " + viewItem.getName());
277        }
278    }
279
280    private Map<String, Object> _getCompositeValues(Optional<? extends Content> parentContent, ViewItem viewItem, Map<String, String> row, int createAction, int editAction,
281            String language, List<ViewItem> children, Map<String, Object> nestedMapValues, List<ViewItem> errors, String prefix, ImportMode importMode)
282    {
283        Map<String, Object> compositeValues = new HashMap<>();
284        for (ViewItem child : children)
285        {
286            try
287            {
288                compositeValues.put(child.getName(), _getValue(parentContent, child, nestedMapValues, row, createAction, editAction, language, errors, prefix + viewItem.getName() + ModelItem.ITEM_PATH_SEPARATOR, importMode));
289            }
290            catch (Exception e)
291            {
292                errors.add(viewItem);
293                getLogger().error("Import from CSV file: error while trying to get values for view: {}", viewItem.getName(), e);
294            }
295        }
296        return compositeValues;
297    }
298
299    private SynchronizableRepeater _getRepeaterValues(Optional<? extends Content> parentContent, ModelViewItemGroup viewItem, Map<String, String> row, int createAction, int editAction, String language,
300            List<ViewItem> children, Map<String, Object> nestedMap, List<ViewItem> errors, String prefix, ImportMode importMode)
301    {
302        @SuppressWarnings("unchecked")
303        Map<String, Object> mappingValues = (Map<String, Object>) nestedMap.get(ImportCSVFileHelper.NESTED_MAPPING_VALUES);
304        @SuppressWarnings("unchecked")
305        List<String> attributeIdNames = (List<String>) nestedMap.getOrDefault(ImportCSVFileHelper.NESTED_MAPPING_ID, List.of());
306        Map<String, Object> repeaterValues = new HashMap<>();
307        List<Map<String, Object>> repeaterValuesList = new ArrayList<>();
308        List<Integer> indexList = new ArrayList<>();
309
310        Optional<ModelAwareRepeater> repeater = parentContent.map(p -> p.getRepeater(prefix + viewItem.getName()));
311        
312        if (repeater.isPresent() && !attributeIdNames.isEmpty() && _allAttributesFilled(mappingValues, attributeIdNames))
313        {
314            indexList = repeater.get()
315                .getEntries()
316                .stream()
317                .filter(entry ->
318                {
319                    return attributeIdNames.stream()
320                        .allMatch(attributeName ->
321                        {
322                            // Get the entry value
323                            String entryValue = entry.getValue(attributeName);
324                            
325                            // Get the row value for the attribute (from CSV file)
326                            Object rowValue = Optional.of(attributeName)
327                                    .map(mappingValues::get)
328                                    .map(String.class::cast)
329                                    .map(row::get)
330                                    .orElse(null);
331                            
332                            // Transform the row value to the right type then compare entry value and row value
333                            return Optional.of(attributeName)
334                                .map(viewItem::getModelViewItem)
335                                .map(ViewElement.class::cast)
336                                .map(ViewElement::getDefinition)
337                                .map(ElementDefinition::getType)
338                                .map(def -> def.castValue(rowValue))
339                                .map(value -> value.equals(entryValue))
340                                .orElse(false);
341                        });
342                })
343                .map(ModelAwareRepeaterEntry::getPosition)
344                .collect(Collectors.toList());
345        }
346        
347        // If entries match with attribute ids, we replace the first entries
348        // Else if the repeater exists, we assume we are on the next model item after the last one
349        // Else (the repeater doesn't exist, we set the rowIndex to first index
350        Integer rowIndex = indexList.isEmpty() ? repeater.map(Repeater::getSize).orElse(1) : indexList.get(0);
351        
352        for (ViewItem child : children)
353        {
354            try
355            {
356                Object entryValues = _getValue(parentContent, child, mappingValues, row, createAction, editAction, language, errors, prefix + viewItem.getName() + "[" + rowIndex + "]" + ModelItem.ITEM_PATH_SEPARATOR, importMode);
357                if (entryValues != null)
358                {
359                    repeaterValues.put(child.getName(), entryValues);
360                }
361            }
362            catch (Exception e)
363            {
364                errors.add(viewItem);
365                getLogger().error("Import from CSV file: error while trying to get values for view: {}", viewItem.getName(), e);
366            }
367        }
368        repeaterValuesList.add(repeaterValues);
369        
370        if (indexList.isEmpty())
371        {
372            return SynchronizableRepeater.appendOrRemove(repeaterValuesList, Set.of());
373        }
374        else
375        {
376            // If several rows match the id, only replace the first but add a warning
377            if (indexList.size() > 1)
378            {
379                errors.add(viewItem);
380            }
381            return SynchronizableRepeater.replace(repeaterValuesList, List.of(rowIndex));
382        }
383    }
384    
385    private Object _getContentAttributeDefinitionValues(Optional<? extends Content> parentContent, ViewItem viewItem, Map<String, Object> mapping, Map<String, String> row,
386            int createAction, int editAction, String language, ViewElement viewElement, ElementDefinition elementDefinition, List<ViewItem> errors, ImportMode importMode) throws Exception
387    {
388        ContentAttributeDefinition contentAttributeDefinition = (ContentAttributeDefinition) elementDefinition;
389        String contentTypeId = contentAttributeDefinition.getContentTypeId();
390        ContentType contentType = _contentTypeEP.getExtension(contentTypeId);
391        @SuppressWarnings("unchecked")
392        Map<String, Object> nestedMap = (Map<String, Object>) mapping.get(viewItem.getName());
393        @SuppressWarnings("unchecked")
394        Map<String, Object> mappingValues = (Map<String, Object>) nestedMap.get(ImportCSVFileHelper.NESTED_MAPPING_VALUES);
395        @SuppressWarnings("unchecked")
396        List<String> attributeIdNames = (List<String>) nestedMap.get(ImportCSVFileHelper.NESTED_MAPPING_ID);
397        if (_allAttributesFilled(mappingValues, attributeIdNames))
398        {
399            SynchronizeResult synchronizeResult = _synchronizeContent(row, contentType, (ViewElementAccessor) viewElement, attributeIdNames, mappingValues, createAction, editAction, null, language, errors, importMode, parentContent);
400            
401            Optional<ModifiableWorkflowAwareContent> attachedContent = synchronizeResult.content();
402            
403            if (synchronizeResult.hasKey && attachedContent.isEmpty() && importMode == ImportMode.UPDATE_ONLY)
404            {
405                errors.add(viewItem);
406            }
407            
408            // If content is multiple, we keep the old value list and we check if content was already inside, and add it otherwise
409            if (attachedContent.isPresent() && contentAttributeDefinition.isMultiple())
410            {
411                Optional<ContentValue[]> multipleContents = parentContent.map(c -> c.getValue(contentAttributeDefinition.getPath()));
412                if (!_containsContent(attachedContent.get(), multipleContents))
413                {
414                    // If there is no list or if it is empty, add it. Otherwise, we have to check if it is inside.
415                    SynchronizableValue syncValue = new SynchronizableValue(List.of(attachedContent.get()));
416                    syncValue.setMode(Mode.APPEND);
417                    return syncValue;
418                }
419                else if (multipleContents.isPresent())
420                {
421                    // return existing values as otherwise the values would be erased
422                    SynchronizableValue syncValue = new SynchronizableValue(Arrays.asList(multipleContents.get()));
423                    syncValue.setMode(Mode.REPLACE);
424                    return syncValue;
425                }
426            }
427            else
428            {
429                return attachedContent.orElse(null);
430            }
431        }
432        return null;
433    }
434    
435    private boolean _containsContent(ModifiableWorkflowAwareContent attachedContent, Optional<ContentValue[]> multipleContents)
436    {
437        return multipleContents
438                .map(Arrays::stream)
439                .orElseGet(Stream::empty)
440                .map(ContentValue::getContentId)
441                .anyMatch(valueFromContent -> valueFromContent.equals(attachedContent.getId()));
442    }
443    
444    private Object _getAttributeDefinitionValues(Optional<? extends Content> parentContent, Map<String, Object> mapping, Map<String, String> row, ElementDefinition elementDefinition, String language, String prefix)
445    {
446        ElementType elementType = elementDefinition.getType();
447        String elementName = elementDefinition.getName();
448        String elementColumn = (String) mapping.get(elementName);
449        String valueAsString = row.get(elementColumn);
450        
451        Object value;
452        if (elementType instanceof AbstractMultilingualStringElementType && !MultilingualStringHelper.matchesMultilingualStringPattern(valueAsString))
453        {
454            MultilingualString multilingualString = new MultilingualString();
455            multilingualString.add(new Locale(language), valueAsString);
456            value = multilingualString;
457        }
458        else
459        {
460            value = elementType.castValue(valueAsString);
461        }
462        
463        if (elementDefinition.isMultiple())
464        {
465            // Build path with index for repeaters.
466            String pathWithIndex = prefix + elementDefinition.getName();
467            
468            Optional<Object[]> values = parentContent.map(c -> c.getValue(pathWithIndex));
469            if (!_containsValue(value, parentContent.map(c -> c.getValue(pathWithIndex))))
470            {
471                // If there is no list or if it is empty, add it. Otherwise, we have to check if it is inside.
472                // If there is no parentContent, still append as we want to fill the values map anyway.
473                SynchronizableValue syncValue = new SynchronizableValue(value != null ? List.of(value) : List.of());
474                syncValue.setMode(Mode.APPEND);
475                return syncValue;
476            }
477            else if (values.isPresent())
478            {
479                // return existing values as otherwise the values would be erased
480                SynchronizableValue syncValue = new SynchronizableValue(Arrays.asList(values.get()));
481                syncValue.setMode(Mode.REPLACE);
482                return syncValue;
483            }
484        }
485        else
486        {
487            return value;
488        }
489        
490        return null;
491    }
492    
493    private boolean _containsValue(Object value, Optional<Object[]> multipleValues)
494    {
495        return multipleValues
496                .map(Arrays::stream)
497                .orElseGet(Stream::empty)
498                .anyMatch(valueFromContent -> valueFromContent.equals(value));
499    }
500
501    private SynchronizeResult _synchronizeContent(Map<String, String> row, ContentType contentType, ViewItemAccessor viewItemAccessor, List<String> attributeIdNames, Map<String, Object> mappingValues, int createAction, int editAction, String workflowName, String language, List<ViewItem> errors, ImportMode importMode, Optional< ? extends Content> parentContent) throws Exception
502    {
503        SynchronizeResult synchronizeResult = _getOrCreateContent(mappingValues, row, contentType, Optional.ofNullable(workflowName), createAction, language, viewItemAccessor, attributeIdNames, parentContent, importMode);
504        Optional<ModifiableWorkflowAwareContent> content = synchronizeResult.content();
505        
506        // If we are on CREATE_ONLY mode and content already exists, or if we are on UPDATE_ONLY mode and content does not exists, stop recursivity
507        if (importMode == ImportMode.CREATE_ONLY && !synchronizeResult.isCreated() || importMode == ImportMode.UPDATE_ONLY && content.isEmpty())
508        {
509            return synchronizeResult;
510        }
511        
512        Map<String, Object> values = _getValues(content, row, viewItemAccessor, attributeIdNames, mappingValues, createAction, editAction, language, errors, importMode);
513        if (!values.isEmpty())
514        {
515            if (content.isEmpty())
516            {
517                // Throw this exception only when values are filled, as an empty content should not trigger any warning
518                throw new ContentImportException("Can't create and fill content of content type '" + contentType.getId() + "' and following values '" + values + "' : at least one of those identifiers is null : " + attributeIdNames);
519            }
520            else
521            {
522                _editContent(editAction, values, content.get());
523            }
524        }
525        
526        return synchronizeResult;
527    }
528    
529    private SynchronizeResult _getOrCreateContent(Map<String, Object> mapping, Map<String, String> row, ContentType contentType, Optional<String> workflowName, int createAction, String language, ViewItemAccessor viewItemAccessor, List<String> attributeIdNames, Optional<? extends Content> parentContent, ImportMode importMode) throws ContentImportException, WorkflowException
530    {
531        List<Expression> idExpressions = new ArrayList<>();
532        List<String> values = new ArrayList<>();
533        
534        for (String attributeName : attributeIdNames)
535        {
536            ViewElement viewElement = (ViewElement) viewItemAccessor.getModelViewItem(attributeName);
537            ElementDefinition elementDefinition = viewElement.getDefinition();
538            String attributePath = (String) mapping.get(attributeName);
539            String value = row.get(attributePath);
540            values.add(value);
541            
542            if (value == null)
543            {
544                return new SynchronizeResult(false, Optional.empty(), false);
545            }
546            
547            // Get content
548            if (elementDefinition.getType() instanceof MultilingualStringRepositoryElementType)
549            {
550                idExpressions.add(new MultilingualStringExpression(attributeName, Operator.EQ, value, language));
551            }
552            else
553            {
554                idExpressions.add(new StringExpression(attributeName, Operator.EQ, value));
555            }
556        }
557
558        idExpressions.add(_contentTypeEP.createHierarchicalCTExpression(contentType.getId()));
559        
560        if (!contentType.isMultilingual())
561        {
562            idExpressions.add(new StringExpression("language", Operator.EQ, language, ExpressionContext.newInstance().withInternal(true)));
563        }
564        
565        Expression expression = new AndExpression(idExpressions.toArray(new Expression[idExpressions.size()]));
566
567        String xPathQuery = ContentQueryHelper.getContentXPathQuery(expression);
568        AmetysObjectIterable<ModifiableDefaultContent> matchingContents = _resolver.query(xPathQuery);
569        if (matchingContents.getSize() > 1)
570        {
571            throw new ContentImportException("More than one content found for type " + contentType.getLabel() + " with "
572                    + attributeIdNames + " as identifier and " + values + " as value");
573        }
574        else if (matchingContents.getSize() == 1)
575        {
576            return new SynchronizeResult(false, Optional.of(matchingContents.iterator().next()), true);
577        }
578        else if (importMode == ImportMode.UPDATE_ONLY)
579        {
580            return new SynchronizeResult(false, Optional.empty(), true);
581        }
582        
583        // Create content
584
585        if (contentType.isAbstract())
586        {
587            throw new ContentImportException("Can not create content for type " + contentType.getLabel() + " with "
588                    + attributeIdNames + " as identifier and " + values + " as value, the content type is abstract");
589        }
590        
591        Map<String, Object> result;
592        String title;
593        if (mapping.containsKey("title"))
594        {
595            title = row.get(mapping.get("title"));
596        }
597        else
598        {
599            title = _i18nUtils.translate(contentType.getDefaultTitle(), language);
600        }
601        
602        
603        String finalWorkflowName = workflowName.or(contentType::getDefaultWorkflowName)
604                                               .orElseThrow(() -> new ContentImportException("No workflow specified for content type " + contentType.getLabel() + " with "
605                                                       + attributeIdNames + " as identifier and " + values + " as value"));
606
607        Map<String, Object> inputs = new HashMap<>();
608        inputs.put(CreateContentFunction.INITIAL_VALUE_SUPPLIER, new Function<List<String>, Object>()
609        {
610            public Object apply(List<String> keys)
611            {
612                // Browse the mapping to find the column related to the attribute
613                Object nestedValue = mapping;
614                for (String key : keys)
615                {
616                    nestedValue = ((Map) nestedValue).get(key);
617                    // If nestedValue is null, the attribute is absent from the map, no value can be found
618                    if (nestedValue == null)
619                    {
620                        return null;
621                    }
622                    // If nestedValue is a map, the key is a complex element such a content or a composite,
623                    // we need to keep browsing the map to find the column
624                    if (nestedValue instanceof Map)
625                    {
626                        nestedValue = ((Map) nestedValue).get(ImportCSVFileHelper.NESTED_MAPPING_VALUES);
627                    }
628                }
629                
630                // Get the value of the attribute for the current row
631                return row.get(nestedValue.toString());
632            }
633        });
634        
635        parentContent.ifPresent(content -> inputs.put(CreateContentFunction.PARENT_CONTEXT_VALUE, content.getId()));
636
637        // CONTENTIO-253 To avoid issue with title starting with a non letter character, we prefix the name with the contentTypeId
638        String prefix = StringUtils.substringAfterLast(contentType.getId(), ".").toLowerCase();
639        String contentName = prefix + "-" + title;
640        
641        if (contentType.isMultilingual())
642        {
643            inputs.put(CreateContentFunction.CONTENT_LANGUAGE_KEY, language);
644            result = _contentWorkflowHelper.createContent(finalWorkflowName, createAction, contentName, Map.of(language, title), new String[] {contentType.getId()}, null, null, null, inputs);
645        }
646        else
647        {
648            result = _contentWorkflowHelper.createContent(finalWorkflowName, createAction, contentName, title, new String[] {contentType.getId()}, null, language, null, null, inputs);
649        }
650        
651        ModifiableWorkflowAwareContent content = (ModifiableWorkflowAwareContent) result.get(AbstractContentWorkflowComponent.CONTENT_KEY);
652        for (String attributeName : attributeIdNames)
653        {
654            ViewElement viewElement = (ViewElement) viewItemAccessor.getModelViewItem(attributeName);
655            ElementDefinition elementDefinition = viewElement.getDefinition();
656            String attributePath = (String) mapping.get(attributeName);
657            final String value = row.get(attributePath);
658            if (org.ametys.cms.data.type.ModelItemTypeConstants.MULTILINGUAL_STRING_ELEMENT_TYPE_ID.equals(elementDefinition.getType().getId()))
659            {
660                MultilingualString multilingualString = new MultilingualString();
661                multilingualString.add(new Locale(language), value);
662                content.setValue(attributeName, multilingualString);
663            }
664            else
665            {
666                content.setValue(attributeName, elementDefinition.getType().castValue(value));
667            }
668        }
669        
670        return new SynchronizeResult(true, Optional.of(content), true);
671    }
672    
673    private Map<String, Object> _getValues(Optional<ModifiableWorkflowAwareContent> content, Map<String, String> row, ViewItemAccessor viewItemAccessor, List<String> attributeIdNames, Map<String, Object> mappingValues, int createAction, int editAction, String language, List<ViewItem> errors, ImportMode importMode)
674    {
675        Map<String, Object> values = new HashMap<>();
676        
677        for (ViewItem viewItem : viewItemAccessor.getViewItems())
678        {
679            try
680            {
681                if (!_isId(viewItem, attributeIdNames))
682                {
683                    Object value = _getValue(content, viewItem, mappingValues, row, createAction, editAction, language, errors, StringUtils.EMPTY, importMode);
684                    if (value != null)
685                    {
686                        values.put(viewItem.getName(), value);
687                    }
688                }
689            }
690            catch (Exception e)
691            {
692                errors.add(viewItem);
693                getLogger().error("Import from CSV file: error while trying to get values for item '{}'", viewItem.getName(), e);
694            }
695        }
696        
697        return values;
698    }
699    
700    private boolean _allAttributesFilled(Map<String, Object> mappingValues, List<String> attributeNames)
701    {
702        return mappingValues.entrySet()
703            .stream()
704            .filter(entry -> attributeNames.contains(entry.getKey()))
705            .map(Entry::getValue)
706            .allMatch(Objects::nonNull);
707    }
708    
709    private record SynchronizeResult(boolean isCreated, Optional<ModifiableWorkflowAwareContent> content, boolean hasKey) { /* empty */ }
710    
711}