001/*
002 *  Copyright 2010 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;
017
018import java.util.Collection;
019import java.util.Map;
020import java.util.Optional;
021
022import org.xml.sax.ContentHandler;
023import org.xml.sax.SAXException;
024
025import org.ametys.plugins.repository.data.UnknownDataException;
026import org.ametys.plugins.repository.data.holder.DataHolder;
027import org.ametys.plugins.repository.data.holder.group.Composite;
028import org.ametys.plugins.repository.data.repositorydata.RepositoryData;
029import org.ametys.plugins.repository.data.type.ModelItemTypeExtensionPoint;
030import org.ametys.runtime.model.exception.BadDataPathCardinalityException;
031import org.ametys.runtime.model.exception.BadItemTypeException;
032import org.ametys.runtime.model.exception.NotUniqueTypeException;
033import org.ametys.runtime.model.exception.UndefinedItemPathException;
034import org.ametys.runtime.model.exception.UnknownTypeException;
035import org.ametys.runtime.model.type.DataContext;
036import org.ametys.runtime.model.type.ModelItemType;
037
038/**
039 * Abstract class for {@link AmetysObject}.
040 */
041public abstract class AbstractAmetysObject implements AmetysObject
042{
043    /**
044     * Returns the {@link DataHolder} of this {@link AmetysObject}.
045     * @return the {@link DataHolder} of this {@link AmetysObject}
046     */
047    protected abstract DataHolder getDataHolder();
048    
049    @Override
050    public boolean equals(Object obj)
051    {
052        if (obj == null)
053        {
054            return false;
055        }
056        
057        if (!(obj instanceof AmetysObject))
058        {
059            return false;
060        }
061        
062        AmetysObject ametysObject = (AmetysObject) obj;
063        
064        return getId().equals(ametysObject.getId());
065    }
066    
067    @Override
068    public int hashCode()
069    {
070        return getId().hashCode();
071    }
072    
073    @Override
074    public String toString()
075    {
076        try
077        {
078            StringBuilder object = new StringBuilder();
079
080            object.append("'");
081            object.append(getPath());
082            object.append("' (");
083            object.append(getId());
084            object.append(")");
085            
086            return object.toString();
087        }
088        catch (AmetysRepositoryException e)
089        {
090            return super.toString();
091        }
092    }
093    
094    public Composite getComposite(String compositePath) throws IllegalArgumentException, UnknownTypeException, BadItemTypeException, UndefinedItemPathException, BadDataPathCardinalityException
095    {
096        return getDataHolder().getComposite(compositePath);
097    }
098    
099    public boolean hasValue(String dataPath) throws IllegalArgumentException, BadDataPathCardinalityException
100    {
101        return getDataHolder().hasValue(dataPath);
102    }
103    
104    public boolean hasValue(String dataPath, String dataTypeId) throws IllegalArgumentException, BadDataPathCardinalityException
105    {
106        return getDataHolder().hasValue(dataPath, dataTypeId);
107    }
108    
109    public boolean hasValueOrEmpty(String dataPath) throws IllegalArgumentException, BadDataPathCardinalityException
110    {
111        return getDataHolder().hasValueOrEmpty(dataPath);
112    }
113
114    public Collection<String> getDataNames()
115    {
116        return getDataHolder().getDataNames();
117    }
118    
119    public <T> T getValue(String dataPath) throws IllegalArgumentException, UnknownTypeException, NotUniqueTypeException, BadItemTypeException, UndefinedItemPathException, BadDataPathCardinalityException
120    {
121        return getDataHolder().getValue(dataPath);
122    }
123    
124    public <T> T getValueOrDefault(String dataPath, T defaultValue) throws IllegalArgumentException, UnknownTypeException, NotUniqueTypeException, BadItemTypeException, UndefinedItemPathException, BadDataPathCardinalityException
125    {
126        return getDataHolder().getValueOrDefault(dataPath, defaultValue);
127    }
128
129    public <T> T getValueOfType(String dataPath, String dataTypeId) throws IllegalArgumentException, UnknownTypeException, BadItemTypeException, UndefinedItemPathException, BadDataPathCardinalityException
130    {
131        return getDataHolder().getValueOfType(dataPath, dataTypeId);
132    }
133    
134    public <T> T getValueOfTypeOrDefault(String dataPath, String dataTypeId, T defaultValue) throws IllegalArgumentException, UnknownTypeException, BadItemTypeException, UndefinedItemPathException, BadDataPathCardinalityException
135    {
136        return getDataHolder().getValueOfTypeOrDefault(dataPath, dataTypeId, defaultValue);
137    }
138    
139    public boolean isMultiple(String dataPath) throws IllegalArgumentException, UnknownDataException, UnknownTypeException, NotUniqueTypeException, BadItemTypeException, UndefinedItemPathException
140    {
141        return getDataHolder().isMultiple(dataPath);
142    }
143    
144    public boolean isMultiple(String dataPath, String dataTypeId) throws IllegalArgumentException, UnknownDataException, UnknownTypeException, BadItemTypeException, UndefinedItemPathException
145    {
146        return getDataHolder().isMultiple(dataPath, dataTypeId);
147    }
148    
149    public <X extends ModelItemType> X getType(String dataPath) throws IllegalArgumentException, UnknownDataException, UnknownTypeException, NotUniqueTypeException, UndefinedItemPathException
150    {
151        return getDataHolder().getType(dataPath);
152    }
153    
154    public ModelItemTypeExtensionPoint getModelItemTypeExtensionPoint()
155    {
156        return getDataHolder().getModelItemTypeExtensionPoint();
157    }
158    
159    public void dataToSAX(ContentHandler contentHandler, DataContext context) throws SAXException, UnknownTypeException, NotUniqueTypeException, BadItemTypeException
160    {
161        getDataHolder().dataToSAX(contentHandler, context);
162    }
163    
164    public void dataToSAX(ContentHandler contentHandler, String dataPath, DataContext context) throws SAXException, UnknownTypeException, NotUniqueTypeException, BadItemTypeException, UndefinedItemPathException
165    {
166        getDataHolder().dataToSAX(contentHandler, dataPath, context);
167    }
168    
169    public Map<String, Object> dataToJSON(DataContext context) throws UnknownTypeException, NotUniqueTypeException, BadItemTypeException
170    {
171        return getDataHolder().dataToJSON(context);
172    }
173    
174    public Object dataToJSON(String dataPath, DataContext context) throws UnknownTypeException, NotUniqueTypeException, BadItemTypeException, UndefinedItemPathException
175    {
176        return getDataHolder().dataToJSON(dataPath, context);
177    }
178    
179    public boolean hasDifferences(Map<String, Object> values) throws UnknownTypeException, NotUniqueTypeException, BadItemTypeException
180    {
181        return getDataHolder().hasDifferences(values);
182    }
183    
184    public RepositoryData getRepositoryData()
185    {
186        return getDataHolder().getRepositoryData();
187    }
188    
189    public Optional<? extends DataHolder> getParentDataHolder()
190    {
191        return getDataHolder().getParentDataHolder();
192    }
193    
194    public DataHolder getRootDataHolder()
195    {
196        return getDataHolder().getRootDataHolder();
197    }
198}