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 org.apache.avalon.framework.service.ServiceException; 019import org.apache.avalon.framework.service.ServiceManager; 020 021import org.ametys.cms.indexing.explorer.AbstractSolrIndexResourceObserver; 022import org.ametys.plugins.explorer.resources.Resource; 023import org.ametys.plugins.explorer.resources.ResourceCollection; 024import org.ametys.plugins.repository.AmetysObject; 025import org.ametys.plugins.repository.RepositoryConstants; 026import org.ametys.web.WebConstants; 027import org.ametys.web.indexing.solr.SolrPageIndexer; 028import org.ametys.web.repository.page.Page; 029 030/** 031 * Listener on page attachments events 032 */ 033public class PageAttachmentsSolrObserver extends AbstractSolrIndexResourceObserver 034{ 035 /** The Solr page indexer */ 036 protected SolrPageIndexer _solrPageIndexer; 037 038 private Page _page; 039 040 @Override 041 public void service(ServiceManager serviceManager) throws ServiceException 042 { 043 super.service(serviceManager); 044 _solrPageIndexer = (SolrPageIndexer) serviceManager.lookup(SolrPageIndexer.ROLE); 045 } 046 047 @Override 048 protected String[] getWorkspacesToIndex() 049 { 050 return new String[] {RepositoryConstants.DEFAULT_WORKSPACE, WebConstants.LIVE_WORKSPACE}; 051 } 052 053 @Override 054 protected boolean isHandledResource(AmetysObject object) 055 { 056 AmetysObject parent = object.getParent(); 057 while (parent != null) 058 { 059 if (parent instanceof Page) 060 { 061 _page = (Page) parent; 062 return true; 063 } 064 parent = parent.getParent(); 065 } 066 067 return false; 068 } 069 070 @Override 071 protected void onResourceCreated(Resource resource, String workspaceName) throws Exception 072 { 073 if (_page != null) 074 { 075 // _page must be != null because #isHandledResource is called just before 076 _solrPageIndexer.indexPageAttachment(resource, _page); 077 _page = null; 078 } 079 } 080 081 @Override 082 protected void onResourceUpdated(Resource resource, String workspaceName) throws Exception 083 { 084 if (_page != null) 085 { 086 // _page must be != null because #isHandledResource is called just before 087 _solrPageIndexer.indexPageAttachment(resource, _page); 088 _page = null; 089 } 090 } 091 092 @Override 093 protected void onCollectionRenamedOrMoved(ResourceCollection resourceCollection, String workspaceName) throws Exception 094 { 095 if (_page != null) 096 { 097 // _page must be != null because #isHandledResource is called just before 098 _solrPageIndexer.indexPageAttachments(resourceCollection, _page); 099 _page = null; 100 } 101 } 102}