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    
030    private MetadataAwareAmetysObjectHelper()
031    {
032        // Hide default constructor.
033    }
034    
035    /**
036     * Remove the workflow reference.
037     * @param object the JCR ametys object.
038     * @throws AmetysRepositoryException if an error occurs.
039     */
040    public static void removeSubObjects(ModifiableMetadataAwareAmetysObject object) throws AmetysRepositoryException
041    {
042        removeSubObjects(object.getMetadataHolder());
043    }
044    
045    /**
046     * Remove the workflow reference.
047     * @param metadataHolder The metadata holder concerned
048     * @throws AmetysRepositoryException if an error occurred
049     */
050    private static void removeSubObjects(ModifiableCompositeMetadata metadataHolder) throws AmetysRepositoryException
051    {
052        for (String metaName : metadataHolder.getMetadataNames())
053        {
054            if (metadataHolder.getType(metaName) == MetadataType.OBJECT_COLLECTION)
055            {
056                ModifiableTraversableAmetysObject objectCollection = metadataHolder.getObjectCollection(metaName);
057                
058                for (AmetysObject subObject : objectCollection.getChildren())
059                {
060                    if (subObject instanceof RemovableAmetysObject)
061                    {
062                        ((RemovableAmetysObject) subObject).remove();
063                    }
064                }
065            }
066            
067            if (metadataHolder.getType(metaName) == MetadataType.COMPOSITE)
068            {
069                ModifiableCompositeMetadata subMetadataHolder = metadataHolder.getCompositeMetadata(metaName);
070                removeSubObjects(subMetadataHolder);
071            }
072        }
073    }
074}