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