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;
020
021import org.apache.commons.lang3.StringUtils;
022
023import org.ametys.runtime.model.exception.BadItemTypeException;
024
025/**
026 * Abstract class for string element types
027 */
028public abstract class AbstractStringElementType extends AbstractElementType<String>
029{
030    public String castValue(String value)
031    {
032        return value;
033    }
034    
035    @Override
036    public String toString(String value) throws BadItemTypeException
037    {
038        return value;
039    }
040
041    public Object fromJSONForClient(Object json)
042    {
043        if (json == null || json instanceof String)
044        {
045            return json;
046        }
047        else if (json instanceof List)
048        {
049            @SuppressWarnings("unchecked")
050            List<String> jsonList = (List<String>) json;
051            return jsonList.toArray(new String[jsonList.size()]);
052        }
053        else
054        {
055            throw new IllegalArgumentException("Try to convert the non String JSON object '" + json + "' into a String");
056        }
057    }
058    
059    /**
060     * Specific implementation of valueToJSONForClient used for passwords
061     * @param value the value to convert
062     * @return The value as JSON
063     */
064    protected Object passwordValueToJSONForClient(Object value)
065    {
066        if (value == null)
067        {
068            return null;
069        }
070        if (value instanceof String)
071        {
072            String stringValue = (String) value;
073            return !StringUtils.isEmpty(stringValue) ? "PASSWORD" : stringValue;
074        }
075        else if (value instanceof String[])
076        {
077            List<Object> values = new ArrayList<>();
078            for (String singleValue : (String[]) value)
079            {
080                values.add(!StringUtils.isEmpty(singleValue) ? "PASSWORD" : singleValue);
081            }
082            return values;
083        }
084        else
085        {
086            throw new IllegalArgumentException("Try to convert the non String value '" + value + "' to JSON");
087        }
088    }
089    
090    public boolean isSimple()
091    {
092        return true;
093    }
094}