001    package org.apache.archiva.metadata.model;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *   http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.util.Collection;
023    import java.util.Collections;
024    import java.util.HashMap;
025    import java.util.Map;
026    
027    /**
028     * Base class for metadata that is contains facets for storing extensions by various plugins.
029     */
030    public abstract class FacetedMetadata
031    {
032        /**
033         * The facets to store, keyed by the {@linkplain MetadataFacet#getFacetId() Facet ID} of the metadata.
034         */
035        private Map<String, MetadataFacet> facets = new HashMap<String, MetadataFacet>();
036    
037        /**
038         * Add a new facet to the metadata. If it already exists, it will be replaced.
039         *
040         * @param metadataFacet the facet to add
041         */
042        public void addFacet( MetadataFacet metadataFacet )
043        {
044            this.facets.put( metadataFacet.getFacetId(), metadataFacet );
045        }
046    
047        /**
048         * Get a particular facet of metadata.
049         *
050         * @param facetId the facet ID
051         * @return the facet of the metadata.
052         */
053        public MetadataFacet getFacet( String facetId )
054        {
055            return this.facets.get( facetId );
056        }
057    
058        public MetadataFacet removeFacet( String facetId )
059        {
060            return this.facets.remove( facetId );
061        }
062    
063        /**
064         * Get all the facets available on this metadata.
065         *
066         * @return the facets of the metadata
067         */
068        public Collection<MetadataFacet> getFacetList()
069        {
070            return this.facets.values();
071        }
072    
073        /**
074         * Get all the keys of the facets available on this metadata.
075         *
076         * @return the collection of facet IDs.
077         */
078        public Collection<String> getFacetIds()
079        {
080            return this.facets.keySet();
081        }
082    
083        /**
084         * Get all available facets as a Map (typically used by bean rendering, such as in Archiva's JSPs).
085         *
086         * @return the map of facets
087         * @see #facets
088         */
089        public Map<String, MetadataFacet> getFacets()
090        {
091            return Collections.unmodifiableMap( facets );
092        }
093    }