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.odfsync.apogee.scc.operator;
017
018import java.util.List;
019import java.util.Map;
020
021import org.apache.commons.lang.StringUtils;
022import org.slf4j.Logger;
023
024import org.ametys.odf.courselist.CourseList;
025import org.ametys.odf.courselist.CourseList.ChoiceType;
026
027import com.google.common.collect.ImmutableList;
028
029/**
030 * Get mapped values from Apogée to Ametys for CourseList.
031 */
032public class ApogeeSynchronizingCourseListOperator extends ApogeeSynchronizingContentOperator
033{
034    @Override
035    public Map<String, List<Object>> transform(Map<String, List<Object>> remoteValues, Logger logger)
036    {
037        Map<String, List<Object>> result = super.transform(remoteValues, logger);
038
039        String min = _getFirstValue(remoteValues.get(CourseList.METADATA_MIN_COURSES));
040        String choiceType = _getFirstValue(remoteValues.get(CourseList.METADATA_CHOICE_TYPE));
041        
042        if (choiceType.equalsIgnoreCase("O"))
043        {
044            choiceType = ChoiceType.MANDATORY.toString();
045            result.put(CourseList.METADATA_MIN_COURSES, ImmutableList.of(Long.valueOf(0)));
046            result.put(CourseList.METADATA_MAX_COURSES, ImmutableList.of(Long.valueOf(0)));
047        }
048        else if (StringUtils.isNotEmpty(min))
049        {
050            choiceType = ChoiceType.CHOICE.toString();
051            result.put(CourseList.METADATA_MIN_COURSES, ImmutableList.of(Long.valueOf(min)));
052            String max = _getFirstValue(remoteValues.get(CourseList.METADATA_MAX_COURSES));
053            if (StringUtils.isNotEmpty(max))
054            {
055                result.put(CourseList.METADATA_MAX_COURSES, ImmutableList.of(Long.valueOf(max)));
056            }
057            else
058            {
059                result.remove(CourseList.METADATA_MAX_COURSES);
060            }
061        }
062        else
063        {
064            choiceType = ChoiceType.OPTIONAL.toString();
065            result.remove(CourseList.METADATA_MIN_COURSES);
066            result.remove(CourseList.METADATA_MAX_COURSES);
067        }
068
069        result.put(CourseList.METADATA_CHOICE_TYPE, ImmutableList.of(choiceType));
070
071        return result;
072    }
073}