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