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.cms.data.type.impl;
017
018import java.util.Arrays;
019import java.util.Optional;
020
021import org.apache.commons.lang3.StringUtils;
022
023import org.ametys.core.model.type.AbstractStringElementType;
024import org.ametys.plugins.repository.data.repositorydata.ModifiableRepositoryData;
025import org.ametys.plugins.repository.data.repositorydata.RepositoryData;
026import org.ametys.plugins.repository.data.type.RepositoryElementType;
027import org.ametys.runtime.model.exception.BadItemTypeException;
028
029/**
030 * Class for string type of elements stored in the repository
031 */
032public class StringRepositoryElementType extends AbstractStringElementType implements RepositoryElementType<String>
033{
034    public Object read(RepositoryData parentData, String name) throws BadItemTypeException
035    {
036        if (!parentData.hasValue(name))
037        {
038            return null;
039        }
040        
041        if (!isCompatible(parentData, name))
042        {
043            throw new BadItemTypeException("Try to get string value from the non string data '" + name + "' on '" + parentData + "'");
044        }
045        
046        if (parentData.isMultiple(name))
047        {
048            return parentData.getStrings(name);
049        }
050        else
051        {
052            return parentData.getString(name);
053        }
054    }
055    
056    public boolean hasNonEmptyValue(RepositoryData parentData, String name) throws BadItemTypeException
057    {
058        if (!parentData.hasValue(name))
059        {
060            return false;
061        }
062        
063        if (!isCompatible(parentData, name))
064        {
065            throw new BadItemTypeException("Try to check string value from the non string data '" + name + "' on '" + parentData + "'");
066        }
067        
068        if (parentData.isMultiple(name))
069        {
070            return parentData.getStrings(name).length > 0;
071        }
072        else
073        {
074            return StringUtils.isNotEmpty(parentData.getString(name));
075        }
076    }
077
078    public void write(ModifiableRepositoryData parentData, String name, Object value) throws BadItemTypeException
079    {
080        if (value == null)
081        {
082            if (parentData.hasValue(name) && parentData.isMultiple(name))
083            {
084                parentData.setValues(name, new String[0]);
085            }
086            else
087            {
088                parentData.setValue(name, StringUtils.EMPTY);
089            }
090        }
091        else if (value instanceof String)
092        {
093            parentData.setValue(name, (String) value);
094        }
095        else if (value instanceof String[])
096        {
097            Arrays.stream((String[]) value)
098                  .forEach(v -> Optional.ofNullable(v)
099                                        .orElseThrow(() -> new IllegalArgumentException("Try to set a null value into the multiple " + getId() + " data '" + name + "' on '" + parentData + "'")));
100            
101            parentData.setValues(name, (String[]) value);
102        }
103        else
104        {
105            throw new BadItemTypeException("Try to set the non string value '" + value + "' to the string data '" + name + "' on '" + parentData + "'");
106        }
107    }
108    
109    public String getRepositoryDataType()
110    {
111        return RepositoryData.STRING_REPOSITORY_DATA_TYPE;
112    }
113}