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 org.ametys.cms.data.Geocode;
019import org.ametys.cms.data.type.AbstractGeocodeElementType;
020import org.ametys.plugins.repository.RepositoryConstants;
021import org.ametys.plugins.repository.data.UnknownDataException;
022import org.ametys.plugins.repository.data.repositorydata.ModifiableRepositoryData;
023import org.ametys.plugins.repository.data.repositorydata.RepositoryData;
024import org.ametys.plugins.repository.data.type.ComplexRepositoryElementType;
025import org.ametys.runtime.model.exception.BadItemTypeException;
026
027/**
028 * Class for geocode type of elements stored in the repository
029 */
030public class GeocodeRepositoryElementType extends AbstractGeocodeElementType implements ComplexRepositoryElementType<Geocode>
031{
032    public boolean isSingleValueEmpty(RepositoryData geocodeData)
033    {
034        return !geocodeData.hasValue(Geocode.LONGITUDE_IDENTIFIER) || !geocodeData.hasValue(Geocode.LATITUDE_IDENTIFIER);
035    }
036    
037    public Geocode readSingleValue(RepositoryData geocodeData) throws BadItemTypeException
038    {
039        Double longitude = _getDoubleValue(geocodeData, Geocode.LONGITUDE_IDENTIFIER);
040        Double latitude = _getDoubleValue(geocodeData, Geocode.LATITUDE_IDENTIFIER);
041        return (longitude != null && latitude != null) ? new Geocode(longitude, latitude) : null;
042    }
043
044    private Double _getDoubleValue(RepositoryData geocodeData, String name)
045    {
046        if (!geocodeData.hasValue(name))
047        {
048            return null;
049        }
050        else if (RepositoryData.DOUBLE_REPOSITORY_DATA_TYPE.equals(geocodeData.getType(name)))
051        {
052            return geocodeData.getDouble(name);
053        }
054        else
055        {
056            throw new BadItemTypeException("Try to get double value from the non double data '" + name + "' on '" + geocodeData + "'");
057        }
058    }
059    
060    public void writeSingleValue(ModifiableRepositoryData parentData, String name, Geocode value)
061    {
062        ModifiableRepositoryData geocodeData = parentData.addRepositoryData(name, getRepositoryDataType());
063        
064        if (value != null)
065        {
066            geocodeData.setValue(Geocode.LONGITUDE_IDENTIFIER, value.getLongitude());
067            geocodeData.setValue(Geocode.LATITUDE_IDENTIFIER, value.getLatitude());
068        }
069    }
070    
071    public boolean isCompatible(RepositoryData parentData, String name) throws UnknownDataException
072    {
073        if (ComplexRepositoryElementType.super.isCompatible(parentData, name))
074        {
075            return true;
076        }
077        // TODO NEWATTRIBUTEAPI: This part of the test is here just to be compatible with the old compositeMetadata type. When all geocodes are migrated, remove this part of the test
078        else if (RepositoryConstants.COMPOSITE_METADTA_NODETYPE.equals(parentData.getType(name)))
079        {
080            RepositoryData possibleGeocode = parentData.getRepositoryData(name);
081            return possibleGeocode.getDataNames().size() == 2
082                && possibleGeocode.hasValue(Geocode.LATITUDE_IDENTIFIER)
083                && possibleGeocode.hasValue(Geocode.LONGITUDE_IDENTIFIER);
084        }
085        else
086        {
087            return false;
088        }
089    }
090    
091    public String getRepositoryDataType()
092    {
093        return RepositoryConstants.GEOCODE_NODETYPE;
094    }
095}