001/*
002 *  Copyright 2013 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.metadata;
017
018import org.ametys.plugins.repository.AmetysObject;
019import org.ametys.plugins.repository.AmetysRepositoryException;
020import org.ametys.plugins.repository.ModifiableTraversableAmetysObject;
021import org.ametys.plugins.repository.RemovableAmetysObject;
022import org.ametys.plugins.repository.metadata.CompositeMetadata.MetadataType;
023
024/**
025 * Provides helper methods to use the {@link MetadataAwareAmetysObject} API.
026 */
027public final class MetadataAwareAmetysObjectHelper
028{
029    private MetadataAwareAmetysObjectHelper()
030    {
031        // Hide default constructor.
032    }
033    
034    /**
035     * Remove the workflow reference.
036     * @param object the JCR ametys object.
037     * @throws AmetysRepositoryException if an error occurs.
038     */
039    public static void removeSubObjects(ModifiableMetadataAwareAmetysObject object) throws AmetysRepositoryException
040    {
041        removeSubObjects(object.getMetadataHolder());
042    }
043    
044    /**
045     * Remove the workflow reference.
046     * @param metadataHolder The metadata holder concerned
047     * @throws AmetysRepositoryException if an error occurred
048     */
049    private static void removeSubObjects(ModifiableCompositeMetadata metadataHolder) throws AmetysRepositoryException
050    {
051        for (String metaName : metadataHolder.getMetadataNames())
052        {
053            if (metadataHolder.getType(metaName) == MetadataType.OBJECT_COLLECTION)
054            {
055                ModifiableTraversableAmetysObject objectCollection = metadataHolder.getObjectCollection(metaName);
056                
057                for (AmetysObject subObject : objectCollection.getChildren())
058                {
059                    if (subObject instanceof RemovableAmetysObject)
060                    {
061                        ((RemovableAmetysObject) subObject).remove();
062                    }
063                }
064            }
065            
066            if (metadataHolder.getType(metaName) == MetadataType.COMPOSITE)
067            {
068                ModifiableCompositeMetadata subMetadataHolder = metadataHolder.getCompositeMetadata(metaName);
069                removeSubObjects(subMetadataHolder);
070            }
071        }
072    }
073    
074}