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.plugins.repository.data.ametysobject;
017
018import java.util.Collection;
019import java.util.Optional;
020
021import org.xml.sax.ContentHandler;
022import org.xml.sax.SAXException;
023
024import org.ametys.plugins.repository.AmetysObject;
025import org.ametys.plugins.repository.data.holder.DataHolder;
026import org.ametys.plugins.repository.data.holder.ModifiableDataHolder;
027import org.ametys.plugins.repository.data.holder.group.Composite;
028import org.ametys.plugins.repository.data.repositorydata.RepositoryData;
029import org.ametys.runtime.model.exception.BadItemTypeException;
030import org.ametys.runtime.model.exception.NotUniqueTypeException;
031import org.ametys.runtime.model.exception.UndefinedItemPathException;
032import org.ametys.runtime.model.exception.UnknownTypeException;
033import org.ametys.runtime.model.type.DataContext;
034
035/**
036 * {@link AmetysObject} that can handle data.
037 */
038public interface DataAwareAmetysObject extends AmetysObject, DataHolder
039{
040    /**
041     * Returns the {@link DataHolder} of this {@link AmetysObject}.
042     * @return the {@link DataHolder} of this {@link AmetysObject}
043     */
044    public DataHolder getDataHolder();
045    
046    public default Composite getComposite(String compositePath) throws IllegalArgumentException, BadItemTypeException
047    {
048        return getDataHolder().getComposite(compositePath);
049    }
050    
051    public default boolean hasValue(String dataPath) throws IllegalArgumentException
052    {
053        return getDataHolder().hasValue(dataPath);
054    }
055    
056    public default boolean hasValueOrEmpty(String dataPath) throws IllegalArgumentException
057    {
058        return getDataHolder().hasValueOrEmpty(dataPath);
059    }
060
061    public default Collection<String> getDataNames()
062    {
063        return getDataHolder().getDataNames();
064    }
065    
066    public default void copyTo(ModifiableDataHolder dataHolder) throws UndefinedItemPathException, BadItemTypeException, UnknownTypeException, NotUniqueTypeException
067    {
068        copyTo(dataHolder, DataContext.newInstance());
069    }
070    
071    public default void copyTo(ModifiableDataHolder dataHolder, DataContext context) throws UndefinedItemPathException, BadItemTypeException, UnknownTypeException, NotUniqueTypeException
072    {
073        getDataHolder().copyTo(dataHolder, context);
074    }
075    
076    public default void dataToSAX(ContentHandler contentHandler, String dataPath) throws SAXException
077    {
078        dataToSAX(contentHandler, dataPath, DataContext.newInstance());
079    }
080    
081    public default void dataToSAX(ContentHandler contentHandler, String dataPath, DataContext context) throws SAXException
082    {
083        getDataHolder().dataToSAX(contentHandler, dataPath, context);
084    }
085    
086    public default Object dataToJSON(String dataPath)
087    {
088        return dataToJSON(dataPath, DataContext.newInstance());
089    }
090    
091    public default Object dataToJSON(String dataPath, DataContext context)
092    {
093        return getDataHolder().dataToJSON(dataPath, context);
094    }
095    
096    public default RepositoryData getRepositoryData()
097    {
098        return getDataHolder().getRepositoryData();
099    }
100    
101    public default Optional<? extends DataHolder> getParentDataHolder()
102    {
103        return getDataHolder().getParentDataHolder();
104    }
105    
106    public default DataHolder getRootDataHolder()
107    {
108        return getDataHolder().getRootDataHolder();
109    }
110}