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.synchronization;
017
018import javax.jcr.ItemNotFoundException;
019import javax.jcr.Node;
020import javax.jcr.RepositoryException;
021import javax.jcr.Session;
022
023import org.apache.commons.collections.Predicate;
024import org.apache.commons.collections.PredicateUtils;
025
026import org.ametys.cms.support.AmetysPredicateUtils;
027import org.ametys.core.observation.Event;
028import org.ametys.core.observation.Observer;
029import org.ametys.plugins.repository.jcr.JCRAmetysObject;
030import org.ametys.web.ObservationConstants;
031import org.ametys.web.repository.page.Page;
032import org.ametys.web.skin.Skin;
033
034/**
035 * {@link Observer} for observing page move in order to synchronize live workspace.
036 */
037public class SynchronizePageRenameObserver extends AbstractSynchronizePageObserver
038{
039    @Override
040    protected boolean _internalSupport(Event event, Page target)
041    {
042        return event.getId().equals(ObservationConstants.EVENT_PAGE_RENAMED);
043    }
044    
045    @Override
046    protected void _internalObservePage(Event event, Page target, Skin skin, Session liveSession) throws RepositoryException
047    {
048        Node pageNode = ((JCRAmetysObject) target).getNode();
049        String uuid = pageNode.getIdentifier();
050        Node livePageNode = null;
051        
052        try
053        {
054            livePageNode = liveSession.getNodeByIdentifier(uuid);
055        }
056        catch (ItemNotFoundException e)
057        {
058            // the page does not exist in the live workspace, no need to rename
059            return;
060        }
061        
062        // Clone all but child pages and zones
063        Predicate childPredicate = PredicateUtils.andPredicate(PredicateUtils.notPredicate(AmetysPredicateUtils.nodeTypePredicate("ametys:page")), 
064                PredicateUtils.notPredicate(AmetysPredicateUtils.nodeTypePredicate("ametys:zones")));
065
066        _synchronizeComponent.cloneNodeAndPreserveUUID(pageNode, livePageNode, PredicateUtils.truePredicate(), childPredicate);
067
068        // rename the node
069        liveSession.move(livePageNode.getPath(), livePageNode.getParent().getPath() + "/" + pageNode.getName());
070        
071        _synchronizeComponent.orderNode(pageNode.getParent(), pageNode.getName(), livePageNode);
072        
073        if (liveSession.hasPendingChanges())
074        {
075            liveSession.save();
076        }
077    }
078}