001/*
002 *  Copyright 2018 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.core.model.type;
017
018import java.util.ArrayList;
019import java.util.List;
020import java.util.Optional;
021import java.util.stream.Stream;
022
023import org.apache.commons.lang3.StringUtils;
024import org.apache.commons.lang3.tuple.Triple;
025
026import org.ametys.runtime.model.compare.DataChangeType;
027import org.ametys.runtime.model.compare.DataChangeTypeDetail;
028import org.ametys.runtime.model.type.DataContext;
029
030/**
031 * Abstract class for string element types
032 */
033public abstract class AbstractStringElementType extends AbstractElementType<String>
034{
035    @Override
036    public String convertValue(Object value)
037    {
038        return value.toString();
039    }
040    
041    @Override
042    public String toString(String value)
043    {
044        return value;
045    }
046
047    public Object fromJSONForClient(Object json, DataContext context)
048    {
049        if (json == null || json instanceof String)
050        {
051            return json;
052        }
053        else if (json instanceof List)
054        {
055            @SuppressWarnings("unchecked")
056            List<String> jsonList = (List<String>) json;
057            return jsonList.toArray(new String[jsonList.size()]);
058        }
059        else
060        {
061            throw new IllegalArgumentException("Try to convert the non String JSON object '" + json + "' into a String");
062        }
063    }
064    
065    /**
066     * Specific implementation of valueToJSONForClient used for passwords
067     * @param value the value to convert
068     * @return The value as JSON
069     */
070    protected Object passwordValueToJSONForClient(Object value)
071    {
072        if (value == null)
073        {
074            return null;
075        }
076        if (value instanceof String)
077        {
078            String stringValue = (String) value;
079            return !StringUtils.isEmpty(stringValue) ? "PASSWORD" : stringValue;
080        }
081        else if (value instanceof String[])
082        {
083            List<Object> values = new ArrayList<>();
084            for (String singleValue : (String[]) value)
085            {
086                values.add(!StringUtils.isEmpty(singleValue) ? "PASSWORD" : singleValue);
087            }
088            return values;
089        }
090        else
091        {
092            throw new IllegalArgumentException("Try to convert the non String value '" + value + "' to JSON");
093        }
094    }
095    
096    @Override
097    protected Stream<Triple<DataChangeType, DataChangeTypeDetail, String>> _compareSingleValues(String value1, String value2)
098    {
099        return Stream.of(ModelItemTypeHelper.compareSingleStrings(value1, value2, StringUtils.EMPTY))
100                     .filter(Optional::isPresent)
101                     .map(Optional::get);
102    }
103    
104    public boolean isSimple()
105    {
106        return true;
107    }
108}