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