001/*
002 *  Copyright 2011 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.odf.workflow;
017
018import java.util.List;
019import java.util.Map;
020import java.util.Optional;
021import java.util.function.Function;
022
023import org.apache.commons.lang3.StringUtils;
024
025import org.ametys.cms.repository.ModifiableContent;
026import org.ametys.cms.workflow.CreateContentFunction;
027import org.ametys.cms.workflow.EditContentFunction;
028import org.ametys.odf.ProgramItem;
029
030import com.opensymphony.workflow.WorkflowException;
031
032/**
033 * Abstract OSWorkflow function for creating a {@link ProgramItem} content
034 * Populate catalog and code metadata.
035 */
036public abstract class AbstractCreateODFProgramItemFunction extends AbstractCreateODFContentFunction
037{
038    @SuppressWarnings("unchecked")
039    @Override
040    protected void _populateAdditionalData(Map transientVars, ModifiableContent content) throws WorkflowException
041    {
042        super._populateAdditionalData(transientVars, content);
043        
044        if (content instanceof ProgramItem)
045        {
046            Optional<String> catalog = Optional.ofNullable(transientVars.get(CONTENT_CATALOG_KEY))
047                    .map(String.class::cast)
048                    .filter(StringUtils::isNotEmpty);
049            
050            if (!catalog.isPresent())
051            {
052                // Use typed values from the EditContentFunction
053                catalog = Optional.ofNullable(getContextParameters(transientVars))
054                        .map(params -> params.get(EditContentFunction.VALUES_KEY))
055                        .map(Map.class::cast)
056                        .map(values -> values.get(ProgramItem.CATALOG))
057                        .map(String.class::cast)
058                        .filter(StringUtils::isNotEmpty);
059            }
060
061            if (!catalog.isPresent())
062            {
063                // Use raw values from the EditContentFunction
064                catalog = Optional.ofNullable(getContextParameters(transientVars))
065                        .map(params -> params.get(EditContentFunction.FORM_RAW_VALUES))
066                        .map(Map.class::cast)
067                        .map(rawValues -> rawValues.get(EditContentFunction.FORM_ELEMENTS_PREFIX + ProgramItem.CATALOG))
068                        .map(String.class::cast)
069                        .filter(StringUtils::isNotEmpty);
070            }
071            
072            // Used to get the catalog if the program item is created by java code, like for the csv import.
073            if (!catalog.isPresent())
074            {
075                catalog = Optional.ofNullable(transientVars.get(CreateContentFunction.INITIAL_VALUE_SUPPLIER))
076                        .map(Function.class::cast)
077                        .map(function -> function.apply(List.of(ProgramItem.CATALOG)))
078                        .map(String.class::cast)
079                        .filter(StringUtils::isNotEmpty);
080            }
081            
082            catalog.ifPresent(c -> ((ProgramItem) content).setCatalog(c));
083        }
084        
085        String code = content.getValue(ProgramItem.CODE);
086        if (org.apache.commons.lang.StringUtils.isEmpty(code))
087        {
088            content.setValue(ProgramItem.CODE, org.ametys.core.util.StringUtils.generateKey().toUpperCase());
089        }
090    }
091
092}