001/*
002 *  Copyright 2017 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.attachments;
017
018import java.util.Map;
019
020import javax.jcr.RepositoryException;
021import javax.jcr.Session;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025
026import org.ametys.core.observation.Event;
027import org.ametys.plugins.explorer.ObservationConstants;
028import org.ametys.plugins.repository.AmetysObject;
029import org.ametys.plugins.repository.AmetysObjectResolver;
030import org.ametys.plugins.repository.jcr.JCRAmetysObject;
031import org.ametys.web.repository.page.Page;
032import org.ametys.web.skin.Skin;
033import org.ametys.web.skin.SkinsManager;
034import org.ametys.web.synchronization.AbstractSynchronizeObserver;
035
036/**
037 * Listener on page attachments events
038 */
039public class SynchronizePageAttachmentsObserver extends AbstractSynchronizeObserver
040{
041    private AmetysObjectResolver _resolver;
042    private SkinsManager _skinsManager;
043
044    @Override
045    public void service(ServiceManager serviceManager) throws ServiceException
046    {
047        super.service(serviceManager);
048        _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE);
049        _skinsManager = (SkinsManager) serviceManager.lookup(SkinsManager.ROLE);
050    }
051    
052    @Override
053    public boolean supports(Event event)
054    {
055        return event.getId().equals(org.ametys.plugins.explorer.ObservationConstants.EVENT_RESOURCE_CREATED)
056                || event.getId().equals(org.ametys.plugins.explorer.ObservationConstants.EVENT_RESOURCE_UPDATED)
057                || event.getId().equals(org.ametys.plugins.explorer.ObservationConstants.EVENT_RESOURCE_DELETED)
058                || event.getId().equals(org.ametys.plugins.explorer.ObservationConstants.EVENT_RESOURCE_RENAMED)
059                || event.getId().equals(org.ametys.plugins.explorer.ObservationConstants.EVENT_RESOURCE_MOVED)
060                || event.getId().equals(org.ametys.plugins.explorer.ObservationConstants.EVENT_COLLECTION_RENAMED)
061                || event.getId().equals(org.ametys.plugins.explorer.ObservationConstants.EVENT_COLLECTION_MOVED)
062                || event.getId().equals(org.ametys.plugins.explorer.ObservationConstants.EVENT_COLLECTION_DELETED);
063    }
064    
065    @Override
066    protected void _internalObserve(Event event, Session liveSession) throws RepositoryException
067    {
068        Page page = _getPage(event);
069        if (page != null)
070        {
071            Skin skin = null;
072            if (page instanceof JCRAmetysObject)
073            {
074                skin = _skinsManager.getSkin(page.getSite().getSkinId());
075            }
076            
077            _synchronizeComponent.synchronizePage(page, skin, liveSession);
078            
079            if (liveSession.hasPendingChanges())
080            {
081                liveSession.save();
082            }
083        }
084    }
085    
086    private Page _getPage(Event event)
087    {
088        String objectId = null;
089        
090        Map<String, Object> args = event.getArguments();
091        if (event.getId().equals(ObservationConstants.EVENT_RESOURCE_DELETED) || event.getId().equals(ObservationConstants.EVENT_COLLECTION_DELETED) || !args.containsKey(org.ametys.plugins.explorer.ObservationConstants.ARGS_ID))
092        {
093            objectId = (String) args.get(ObservationConstants.ARGS_PARENT_ID);
094        }
095        else
096        {
097            objectId = (String) args.get(org.ametys.plugins.explorer.ObservationConstants.ARGS_ID);
098        }
099
100        AmetysObject object = _resolver.resolveById(objectId);
101        
102        AmetysObject parent = object.getParent();
103        while (parent != null)
104        {
105            if (parent instanceof Page)
106            {
107                return (Page) parent;
108            }
109            parent = parent.getParent();
110        }
111        
112        return null;
113    }
114}