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.ametys.plugins.repository.AmetysObject;
023import org.ametys.plugins.repository.AmetysObjectIterable;
024import org.ametys.plugins.repository.AmetysRepositoryException;
025import org.ametys.plugins.repository.CopiableAmetysObject;
026import org.ametys.plugins.repository.ModifiableTraversableAmetysObject;
027import org.ametys.plugins.repository.RepositoryConstants;
028import org.ametys.plugins.repository.TraversableAmetysObject;
029import org.ametys.plugins.repository.jcr.DefaultTraversableAmetysObject;
030import org.ametys.web.repository.page.ModifiablePage;
031import org.ametys.web.repository.page.ModifiableZone;
032import org.ametys.web.repository.page.ModifiableZoneItem;
033import org.ametys.web.repository.page.Page;
034import org.ametys.web.repository.page.Zone;
035
036/**
037 * JCR implementation of an {@link Zone}.
038 */
039public class DefaultZone extends DefaultTraversableAmetysObject<DefaultZoneFactory> implements ModifiableZone, CopiableAmetysObject
040{
041    /** Constant for zone items node name */
042    public static final String ZONEITEMS_NODE_NAME = RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":zoneItems";
043    /** Constant for zone item node name */
044    public static final String ZONEITEM_NODE_NAME = RepositoryConstants.NAMESPACE_PREFIX + ":zoneItem";
045    /** Constant for zone items node type */
046    public static final String ZONEITEM_NODETYPE_NAME = RepositoryConstants.NAMESPACE_PREFIX + ":zoneItem";
047
048    
049    /**
050     * Creates a {@link DefaultZone}.
051     * 
052     * @param node the node backing this {@link AmetysObject}.
053     * @param parentPath the parent path in the Ametys hierarchy.
054     * @param factory the {@link DefaultZoneFactory} which creates the AmetysObject.
055     */
056    public DefaultZone(Node node, String parentPath, DefaultZoneFactory factory)
057    {
058        super(node, parentPath, factory);
059    }
060
061    @Override
062    public AmetysObjectIterable<ModifiableZoneItem> getZoneItems() throws AmetysRepositoryException
063    {
064        return ((TraversableAmetysObject) getChild(ZONEITEMS_NODE_NAME)).getChildren(); 
065    }
066    
067    @Override
068    public ModifiableZoneItem addZoneItem() throws AmetysRepositoryException
069    {
070        return ((ModifiableTraversableAmetysObject) getChild(ZONEITEMS_NODE_NAME)).createChild(ZONEITEM_NODE_NAME, ZONEITEM_NODETYPE_NAME); 
071    }
072    
073    @Override
074    public Page getPage()
075    {
076        return getParent().getParent();
077    }
078    
079    @Override
080    public ModifiableZone copyTo(ModifiableTraversableAmetysObject parent, String name) throws AmetysRepositoryException
081    {
082        if (parent instanceof ModifiablePage)
083        {
084            ModifiableZone cZone = ((ModifiablePage) parent).createZone(getName());
085            for (ModifiableZoneItem zoneItem : getZoneItems())
086            {
087                if (zoneItem instanceof CopiableAmetysObject)
088                {
089                    ((CopiableAmetysObject) zoneItem).copyTo((ModifiableTraversableAmetysObject) cZone, null);
090                }
091            }
092            return cZone;
093        }
094        else
095        {
096            throw new AmetysRepositoryException("Can not copy zone " + getId() + " of type DefaultZone to something that is not a ModifiablePage " + parent.getId());
097        }
098    }
099    
100    @Override
101    public AmetysObject copyTo(ModifiableTraversableAmetysObject parent, String name, List<String> restrictTo) throws AmetysRepositoryException
102    {
103        return copyTo(parent, name);
104    }
105}