001/* 002 * Copyright 2016 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.cms.content.indexing.solr.observation; 017 018import java.util.Collection; 019import java.util.Map; 020 021import org.apache.avalon.framework.service.ServiceException; 022import org.apache.avalon.framework.service.ServiceManager; 023import org.apache.avalon.framework.service.Serviceable; 024 025import org.ametys.cms.content.indexing.solr.SolrIndexer; 026import org.ametys.cms.repository.Content; 027import org.ametys.core.ObservationConstants; 028import org.ametys.core.observation.AsyncObserver; 029import org.ametys.core.observation.Event; 030import org.ametys.core.right.RightManager; 031import org.ametys.plugins.repository.AmetysObject; 032import org.ametys.plugins.repository.AmetysObjectResolver; 033import org.ametys.plugins.repository.AmetysRepositoryException; 034import org.ametys.plugins.repository.collection.AmetysObjectCollection; 035import org.ametys.runtime.plugin.component.AbstractLogEnabled; 036 037/** 038 * This observer reindexes the content on which the READER profile affectation has been modified. 039 */ 040public class IndexContentReadAclObserver extends AbstractLogEnabled implements AsyncObserver, Serviceable 041{ 042 /** The solr indexer */ 043 protected SolrIndexer _solrIndexer; 044 /** The ametys object resolver */ 045 protected AmetysObjectResolver _resolver; 046 047 @Override 048 public void service(ServiceManager manager) throws ServiceException 049 { 050 _solrIndexer = (SolrIndexer) manager.lookup(SolrIndexer.ROLE); 051 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 052 } 053 054 @Override 055 public boolean supports(Event event) 056 { 057 Map<String, Object> arguments = event.getArguments(); 058 @SuppressWarnings("unchecked") 059 Collection<String> profileIds = (Collection<String>) arguments.get(ObservationConstants.ARGS_ACL_PROFILES); 060 String contextId = (String) arguments.get(ObservationConstants.ARGS_ACL_CONTEXT_IDENTIFIER); 061 062 try 063 { 064 return ObservationConstants.EVENT_ACL_UPDATED.equals(event.getId()) 065 && profileIds.contains(RightManager.READER_PROFILE_ID) 066 && contextId != null 067 && _resolver.hasAmetysObjectForId(contextId); 068 } 069 catch (AmetysRepositoryException e) 070 { 071 return false; 072 } 073 } 074 075 @Override 076 public int getPriority(Event event) 077 { 078 return MIN_PRIORITY; 079 } 080 081 @Override 082 public void observe(Event event, Map<String, Object> transientVars) throws Exception 083 { 084 if (ObserverHelper.isNotSuspendedObservationForIndexation()) 085 { 086 Map<String, Object> arguments = event.getArguments(); 087 String contextId = (String) arguments.get(ObservationConstants.ARGS_ACL_CONTEXT_IDENTIFIER); 088 AmetysObject context = _resolver.resolveById(contextId); 089 if (context instanceof Content) 090 { 091 _solrIndexer.indexContent(((Content) context).getId()); 092 } 093 else if (context instanceof AmetysObjectCollection) 094 { 095 long time_0 = System.currentTimeMillis(); 096 097 _solrIndexer.indexSubcontents(contextId); 098 099 long time_1 = System.currentTimeMillis(); 100 getLogger().debug("End index child contents in {} ms", time_1 - time_0); 101 } 102 } 103 } 104}