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.cms.contenttype;
017
018import java.util.ArrayList;
019import java.util.Collections;
020import java.util.List;
021
022/**
023 * {@link MetadataSet} element.
024 */
025public abstract class AbstractMetadataSetElement
026{
027    private List<AbstractMetadataSetElement> _elements = new ArrayList<>();
028
029    /**
030     * Retrieves the elements.
031     * @return the elements.
032     */
033    public List<AbstractMetadataSetElement> getElements()
034    {
035        return Collections.unmodifiableList(_elements);
036    }
037    
038    /**
039     * Add a new metadata set element.
040     * @param metadataSetElement the sub metadata set.
041     * @return <code>true</code> if the metadata set element has been added,
042     *         <code>false</code> if this metadata set element already exists.
043     */
044    public boolean addElement(AbstractMetadataSetElement metadataSetElement)
045    {
046        if (metadataSetElement instanceof MetadataDefinitionReference)
047        {
048            String metadataName = ((MetadataDefinitionReference) metadataSetElement).getMetadataName();
049            
050            for (AbstractMetadataSetElement element : _elements)
051            {
052                if (element instanceof MetadataDefinitionReference)
053                {
054                    if (((MetadataDefinitionReference) element).getMetadataName().equals(metadataName))
055                    {
056                        // Already existing metadata definition reference
057                        return false;
058                    }
059                }
060            }
061            
062        }
063        
064        _elements.add(metadataSetElement);
065        return true; 
066    }
067
068    /**
069     * Determines if contains the metadata definition
070     * @param metadataName The metadata name
071     * @return <code>true</code> if contains the metadata definition
072     */
073    public boolean hasMetadataDefinitionReference (String metadataName)
074    {
075        for (AbstractMetadataSetElement element : _elements)
076        {
077            if (element instanceof MetadataDefinitionReference)
078            {
079                if (((MetadataDefinitionReference) element).getMetadataName().equals(metadataName))
080                {
081                    return true;
082                }
083            }
084            else if (element instanceof Fieldset)
085            {
086                if (element.hasMetadataDefinitionReference(metadataName))
087                {
088                    return true;
089                }
090            }
091        }
092        
093        return false;
094    }
095    
096    /**
097     * Get the reference to a sub-metadata definition if exits or null if not exists.
098     * @param metadataName the metadata name
099     * @return the reference to the metadata definition or null if not exists.
100     */
101    public MetadataDefinitionReference getMetadataDefinitionReference (String metadataName)
102    {
103        for (AbstractMetadataSetElement element : _elements)
104        {
105            if (element instanceof MetadataDefinitionReference)
106            {
107                if (((MetadataDefinitionReference) element).getMetadataName().equals(metadataName))
108                {
109                    // Metadata definition reference exists
110                    return (MetadataDefinitionReference) element;
111                }
112            }
113        }
114        
115        return null;
116    }
117}