001/* 002 * Copyright 2015 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.Map; 019 020import org.apache.avalon.framework.context.Context; 021import org.apache.avalon.framework.context.ContextException; 022import org.apache.avalon.framework.context.Contextualizable; 023import org.apache.avalon.framework.logger.AbstractLogEnabled; 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026import org.apache.avalon.framework.service.Serviceable; 027 028import org.ametys.cms.ObservationConstants; 029import org.ametys.cms.content.indexing.solr.SolrIndexer; 030import org.ametys.cms.content.indexing.solr.SolrWorkflowIndexer; 031import org.ametys.cms.repository.Content; 032import org.ametys.cms.repository.WorkflowAwareContent; 033import org.ametys.core.observation.AsyncObserver; 034import org.ametys.core.observation.Event; 035import org.ametys.plugins.repository.AmetysObjectResolver; 036import org.ametys.plugins.repository.RepositoryConstants; 037 038/** 039 * Observer in charge for indexing contents when created or modified. 040 */ 041public class IndexContentObserver extends AbstractLogEnabled implements AsyncObserver, Contextualizable, Serviceable 042{ 043 /** The Solr indexer. */ 044 protected SolrIndexer _solrIndexer; 045 046 /** The solr workflow indexer. */ 047 protected SolrWorkflowIndexer _solrWfIndexer; 048 049 /** The component context. */ 050 protected Context _context; 051 052 private AmetysObjectResolver _resolver; 053 054 @Override 055 public void contextualize(Context context) throws ContextException 056 { 057 _context = context; 058 } 059 060 @Override 061 public void service(ServiceManager serviceManager) throws ServiceException 062 { 063 _solrIndexer = (SolrIndexer) serviceManager.lookup(SolrIndexer.ROLE); 064 _solrWfIndexer = (SolrWorkflowIndexer) serviceManager.lookup(SolrWorkflowIndexer.ROLE); 065 _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE); 066 } 067 068 @Override 069 public boolean supports(Event event) 070 { 071 return event.getId().equals(ObservationConstants.EVENT_CONTENT_MODIFIED) 072 || event.getId().equals(ObservationConstants.EVENT_CONTENT_ADDED) 073 || event.getId().equals(ObservationConstants.EVENT_CONTENT_WORKFLOW_CHANGED) 074 || event.getId().equals(ObservationConstants.EVENT_CONTENT_TAGGED); 075 } 076 077 @Override 078 public int getPriority(Event event) 079 { 080 return MAX_PRIORITY + 3000; 081 } 082 083 @Override 084 public void observe(Event event, Map<String, Object> transientVars) throws Exception 085 { 086 if (ObserverHelper.isNotSuspendedObservationForIndexation()) 087 { 088 String contentId = (String) event.getArguments().get(ObservationConstants.ARGS_CONTENT_ID); 089 Content content = _resolver.resolveById(contentId); 090 091 if (content != null && index(content)) 092 { 093 if (event.getId().equals(ObservationConstants.EVENT_CONTENT_WORKFLOW_CHANGED)) 094 { 095 _solrIndexer.updateSystemProperty(content, "workflowStep", null, true); 096 if (content instanceof WorkflowAwareContent) 097 { 098 _solrWfIndexer.indexAmetysObjectWorkflow((WorkflowAwareContent) content, RepositoryConstants.DEFAULT_WORKSPACE); 099 } 100 } 101 else 102 { 103 _solrIndexer.indexContent(content.getId(), RepositoryConstants.DEFAULT_WORKSPACE, true); 104 } 105 } 106 } 107 } 108 109 /** 110 * Test whether to index the given content or not. 111 * @param content the content to test. 112 * @return true to index the content, false otherwise. 113 */ 114 protected boolean index(Content content) 115 { 116 return true; 117 } 118 119}