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