001/*
002 *  Copyright 2015 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.content.properties;
017
018import java.util.Arrays;
019import java.util.HashSet;
020import java.util.Set;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025
026import org.ametys.cms.model.properties.Property;
027import org.ametys.cms.repository.Content;
028import org.ametys.cms.search.content.ContentSearchHelper;
029import org.ametys.runtime.model.type.ElementType;
030import org.ametys.runtime.model.type.ModelItemTypeConstants;
031
032/**
033 * {@link Property} referencing multiple string values in one property.
034 * String values can come from attributes of different types like string, content, double, ...
035 * Each value is converted as string.
036 */
037public class MultiStringValuesProperty extends AbstractMultiValuesProperty<String> implements Serviceable
038{
039    /** The content search helper. */
040    protected ContentSearchHelper _contentSearchHelper;
041    
042    public void service(ServiceManager manager) throws ServiceException
043    {
044        _contentSearchHelper = (ContentSearchHelper) manager.lookup(ContentSearchHelper.ROLE);
045    }
046    
047    @Override
048    @SuppressWarnings("unchecked")
049    public Object getValue(Content content)
050    {
051        Set<String> valuesAsString = new HashSet<>();
052        for (String reference : _references)
053        {
054            Object value = content.getValue(reference, true);
055            if (value != null)
056            {
057                ElementType type = content.getType(reference);
058                
059                if (type.getManagedClassArray().isInstance(value))
060                {
061                    Arrays.stream((Object[]) value)
062                          .map(type::toString)
063                          .forEach(valueAsString -> valuesAsString.add(valueAsString));
064                }
065                else
066                {
067                    String valueAsString = type.toString(value);
068                    valuesAsString.add(valueAsString);
069                }
070            }
071        }
072        
073        return valuesAsString.toArray(new String[valuesAsString.size()]);
074    }
075    
076    @Override
077    protected String _getTypeId()
078    {
079        return ModelItemTypeConstants.STRING_TYPE_ID;
080    }
081}