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.ArrayUtils;
022
023import org.ametys.core.model.type.AbstractLongElementType;
024import org.ametys.plugins.repository.RepositoryConstants;
025import org.ametys.plugins.repository.data.UnknownDataException;
026import org.ametys.plugins.repository.data.repositorydata.ModifiableRepositoryData;
027import org.ametys.plugins.repository.data.repositorydata.RepositoryData;
028import org.ametys.plugins.repository.data.type.RepositoryElementType;
029import org.ametys.runtime.model.exception.BadItemTypeException;
030
031/**
032 * Class for long type of elements stored in the repository
033 */
034public class LongRepositoryElementType extends AbstractLongElementType implements RepositoryElementType<Long>
035{
036    public Object read(RepositoryData parentData, String name) throws BadItemTypeException
037    {
038        if (!parentData.hasValue(name))
039        {
040            return null;
041        }
042        
043        if (!isCompatible(parentData, name))
044        {
045            throw new BadItemTypeException("Try to get long value from the non long data '" + name + "' on '" + parentData + "'");
046        }
047        
048        if (parentData.isMultiple(name))
049        {
050            return parentData.getLongs(name);
051        }
052        else
053        {
054            return parentData.getLong(name);
055        }
056    }
057    
058    @Override
059    public boolean hasValue(RepositoryData parentData, String name) throws BadItemTypeException
060    {
061        return RepositoryElementType.super.hasValue(parentData, name) || parentData.hasValue(name + EMPTY_METADATA_SUFFIX, RepositoryConstants.NAMESPACE_PREFIX_INTERNAL);
062    }
063    
064    public boolean hasNonEmptyValue(RepositoryData parentData, String name) throws BadItemTypeException
065    {
066        if (!parentData.hasValue(name))
067        {
068            return false;
069        }
070        
071        if (!isCompatible(parentData, name))
072        {
073            throw new BadItemTypeException("Try to check long value from the non long data '" + name + "' on '" + parentData + "'");
074        }
075        
076        if (parentData.isMultiple(name))
077        {
078            return parentData.getLongs(name).length > 0;
079        }
080        else
081        {
082            return true;
083        }
084    }
085
086    public void write(ModifiableRepositoryData parentData, String name, Object value) throws BadItemTypeException
087    {
088        if (value == null)
089        {
090            if (parentData.hasValue(name) && parentData.isMultiple(name))
091            {
092                parentData.setValues(name, new Long[0]);
093            }
094            else
095            {
096                parentData.setValue(name + EMPTY_METADATA_SUFFIX, true, RepositoryConstants.NAMESPACE_PREFIX_INTERNAL);
097
098                if (parentData.hasValue(name))
099                {
100                    parentData.removeValue(name);
101                }
102            }
103        }
104        else if (value instanceof Number)
105        {
106            parentData.setValue(name, ((Number) value).longValue());
107            
108            if (parentData.hasValue(name + EMPTY_METADATA_SUFFIX, RepositoryConstants.NAMESPACE_PREFIX_INTERNAL))
109            {
110                parentData.removeValue(name + EMPTY_METADATA_SUFFIX, RepositoryConstants.NAMESPACE_PREFIX_INTERNAL);
111            }
112        }
113        else if (value instanceof long[])
114        {
115            parentData.setValues(name, ArrayUtils.toObject((long[]) value));
116        }
117        else if (value instanceof int[])
118        {
119            Long[] valueAsLongArray = Arrays.stream(ArrayUtils.toObject((int[]) value))
120                    .map(intValue -> intValue.longValue())
121                    .toArray(Long[]::new);
122
123            parentData.setValues(name, valueAsLongArray);
124        }
125        else if (value instanceof Number[])
126        {
127            Long[] valueAsLongArray = Arrays.stream((Number[]) value)
128                                            .map(v -> Optional.ofNullable(v)
129                                                              .orElseThrow(() -> new IllegalArgumentException("Try to set a null value into the multiple " + getId() + " data '" + name + "' on '" + parentData + "'")))
130                                            .map(number -> number.longValue())
131                                            .toArray(Long[]::new);
132            
133            parentData.setValues(name, valueAsLongArray);
134        }
135        else
136        {
137            throw new BadItemTypeException("Try to set the non long value '" + value + "' to the long data '" + name + "' on '" + parentData + "'");
138        }
139    }
140    
141    @Override
142    public void remove(ModifiableRepositoryData parentData, String name) throws UnknownDataException
143    {
144        if (parentData.hasValue(name + EMPTY_METADATA_SUFFIX, RepositoryConstants.NAMESPACE_PREFIX_INTERNAL))
145        {
146            parentData.removeValue(name + EMPTY_METADATA_SUFFIX, RepositoryConstants.NAMESPACE_PREFIX_INTERNAL);
147
148            if (parentData.hasValue(name))
149            {
150                // If there is an empty metadata AND a value, delete this value. Should never happen
151                parentData.removeValue(name);
152            }
153        }
154        else
155        {
156            RepositoryElementType.super.remove(parentData, name);
157        }
158    }
159    
160    public String getRepositoryDataType()
161    {
162        return RepositoryData.LONG_REPOSITORY_DATA_TYPE;
163    }
164}