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