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.Collection;
019import java.util.Locale;
020
021import org.apache.commons.lang3.LocaleUtils;
022
023import org.ametys.cms.data.type.AbstractMultilingualStringElementType;
024import org.ametys.plugins.repository.RepositoryConstants;
025import org.ametys.plugins.repository.data.repositorydata.ModifiableRepositoryData;
026import org.ametys.plugins.repository.data.repositorydata.RepositoryData;
027import org.ametys.plugins.repository.data.type.ComplexRepositoryElementType;
028import org.ametys.plugins.repository.metadata.MultilingualString;
029
030/**
031 * Class for multilingual string type of elements stored in the repository
032 */
033public class MultilingualStringRepositoryElementType extends AbstractMultilingualStringElementType implements ComplexRepositoryElementType<MultilingualString>
034{
035    public boolean isSingleValueEmpty(RepositoryData multilingualStringData)
036    {
037        return multilingualStringData.getDataNames()
038                                     .stream()
039                                     .noneMatch(this::_isAValidLocale);
040    }
041    
042    public MultilingualString readSingleValue(RepositoryData multilingualStringData)
043    {
044        Collection<String> localesAsString = multilingualStringData.getDataNames();
045        if (!localesAsString.isEmpty())
046        {
047            MultilingualString multilingualString = new MultilingualString();
048            
049            for (String localeAsString : localesAsString)
050            {
051                Locale locale = LocaleUtils.toLocale(localeAsString);
052                multilingualString.add(locale, multilingualStringData.getString(localeAsString));
053            }
054            
055            return multilingualString;
056        }
057        else
058        {
059            return null;
060        }
061    }
062    
063    public void writeSingleValue(ModifiableRepositoryData parentData, String name, MultilingualString value)
064    {
065        ModifiableRepositoryData multilingualStringData = parentData.addRepositoryData(name, getRepositoryDataType());
066        
067        if (value != null)
068        {
069            for (Locale locale : value.getLocales())
070            {
071                multilingualStringData.setValue(locale.toString(), value.getValue(locale));
072            }
073        }
074    }
075    
076    public String getRepositoryDataType()
077    {
078        return RepositoryConstants.MULTILINGUAL_STRING_METADATA_NODETYPE;
079    }
080    
081    /**
082     * Checks if the given string is an available {@link Locale}
083     * @param candidate the string to check
084     * @return <code>true</code> if the given string is an available locale, <code>false</code> otherwise
085     */
086    protected boolean _isAValidLocale(String candidate)
087    {
088        try
089        {
090            LocaleUtils.toLocale(candidate);
091            return true;
092        }
093        catch (IllegalArgumentException e)
094        {
095            // the given candidate is not a locale
096            return false;
097        }
098    }
099}