001/*
002 *  Copyright 2010 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.web.repository.page.jcr;
017
018import java.util.List;
019
020import javax.jcr.Node;
021
022import org.slf4j.Logger;
023import org.slf4j.LoggerFactory;
024
025import org.ametys.plugins.repository.AmetysObject;
026import org.ametys.plugins.repository.AmetysObjectIterable;
027import org.ametys.plugins.repository.AmetysRepositoryException;
028import org.ametys.plugins.repository.CopiableAmetysObject;
029import org.ametys.plugins.repository.ModifiableTraversableAmetysObject;
030import org.ametys.plugins.repository.RepositoryConstants;
031import org.ametys.plugins.repository.TraversableAmetysObject;
032import org.ametys.plugins.repository.data.holder.ModifiableModelLessDataHolder;
033import org.ametys.plugins.repository.data.holder.impl.DefaultModifiableModelLessDataHolder;
034import org.ametys.plugins.repository.data.repositorydata.ModifiableRepositoryData;
035import org.ametys.plugins.repository.data.repositorydata.impl.JCRRepositoryData;
036import org.ametys.plugins.repository.jcr.DefaultTraversableAmetysObject;
037import org.ametys.plugins.repository.metadata.ModifiableCompositeMetadata;
038import org.ametys.web.repository.page.ModifiablePage;
039import org.ametys.web.repository.page.ModifiableZone;
040import org.ametys.web.repository.page.ModifiableZoneItem;
041import org.ametys.web.repository.page.Page;
042import org.ametys.web.repository.page.Zone;
043
044/**
045 * JCR implementation of an {@link Zone}.
046 */
047public class DefaultZone extends DefaultTraversableAmetysObject<DefaultZoneFactory> implements ModifiableZone, CopiableAmetysObject
048{
049    /** Constant for zone items node name */
050    public static final String ZONEITEMS_NODE_NAME = RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":zoneItems";
051    /** Constant for zone item node name */
052    public static final String ZONEITEM_NODE_NAME = RepositoryConstants.NAMESPACE_PREFIX + ":" + ZONEITEM_DATA_NAME;
053    /** Constant for zone items node type */
054    public static final String ZONEITEM_NODETYPE_NAME = RepositoryConstants.NAMESPACE_PREFIX + ":zoneItem";
055
056    private static final Logger __LOGGER = LoggerFactory.getLogger(DefaultZone.class);
057    
058    /**
059     * Creates a {@link DefaultZone}.
060     * 
061     * @param node the node backing this {@link AmetysObject}.
062     * @param parentPath the parent path in the Ametys hierarchy.
063     * @param factory the {@link DefaultZoneFactory} which creates the AmetysObject.
064     */
065    public DefaultZone(Node node, String parentPath, DefaultZoneFactory factory)
066    {
067        super(node, parentPath, factory);
068    }
069
070    @Override
071    public AmetysObjectIterable<ModifiableZoneItem> getZoneItems() throws AmetysRepositoryException
072    {
073        return ((TraversableAmetysObject) getChild(ZONEITEMS_NODE_NAME)).getChildren(); 
074    }
075    
076    @Override
077    public ModifiableZoneItem addZoneItem() throws AmetysRepositoryException
078    {
079        return ((ModifiableTraversableAmetysObject) getChild(ZONEITEMS_NODE_NAME)).createChild(ZONEITEM_NODE_NAME, ZONEITEM_NODETYPE_NAME); 
080    }
081    
082    @Override
083    public Page getPage()
084    {
085        return getParent().getParent();
086    }
087    
088    @Override
089    public ModifiableZone copyTo(ModifiableTraversableAmetysObject parent, String name) throws AmetysRepositoryException
090    {
091        if (parent instanceof ModifiablePage)
092        {
093            ModifiableZone cZone = ((ModifiablePage) parent).createZone(getName());
094            for (ModifiableZoneItem zoneItem : getZoneItems())
095            {
096                if (zoneItem instanceof CopiableAmetysObject)
097                {
098                    ((CopiableAmetysObject) zoneItem).copyTo((ModifiableTraversableAmetysObject) cZone, null);
099                }
100            }
101            return cZone;
102        }
103        else
104        {
105            throw new AmetysRepositoryException("Can not copy zone " + getId() + " of type DefaultZone to something that is not a ModifiablePage " + parent.getId());
106        }
107    }
108    
109    @Override
110    public AmetysObject copyTo(ModifiableTraversableAmetysObject parent, String name, List<String> restrictTo) throws AmetysRepositoryException
111    {
112        return copyTo(parent, name);
113    }
114
115    public ModifiableModelLessDataHolder getDataHolder()
116    {
117        ModifiableRepositoryData repositoryData = new JCRRepositoryData(getNode());
118        return new DefaultModifiableModelLessDataHolder(_getFactory().getPageDataTypeExtensionPoint(), repositoryData);
119    }
120    
121    @Override
122    public ModifiableCompositeMetadata getMetadataHolder()
123    {
124        __LOGGER.warn("The getMetadataHolder method of DefaultZone is deprecated. Use the getDataHolder instead. StackTrace: ", new Exception());
125        return super.getMetadataHolder();
126    }
127}