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; 027import org.apache.solr.client.solrj.SolrClient; 028 029import org.ametys.cms.ObservationConstants; 030import org.ametys.cms.content.indexing.solr.SolrIndexer; 031import org.ametys.cms.content.indexing.solr.SolrWorkflowIndexer; 032import org.ametys.cms.indexing.IndexingObserver; 033import org.ametys.cms.repository.Content; 034import org.ametys.cms.repository.WorkflowAwareContent; 035import org.ametys.cms.search.solr.SolrClientProvider; 036import org.ametys.cms.search.systemprop.WorkflowStepSystemProperty; 037import org.ametys.core.observation.AsyncObserver; 038import org.ametys.core.observation.Event; 039import org.ametys.plugins.repository.AmetysObjectResolver; 040import org.ametys.plugins.repository.RepositoryConstants; 041 042/** 043 * Observer in charge for indexing contents when created or modified. 044 */ 045public class IndexContentObserver extends AbstractLogEnabled implements AsyncObserver, IndexingObserver, Contextualizable, Serviceable 046{ 047 /** The provider of solr clients */ 048 protected SolrClientProvider _solrClientProvider; 049 050 /** The Solr indexer. */ 051 protected SolrIndexer _solrIndexer; 052 053 /** The solr workflow indexer. */ 054 protected SolrWorkflowIndexer _solrWfIndexer; 055 056 /** The component context. */ 057 protected Context _context; 058 059 private AmetysObjectResolver _resolver; 060 061 @Override 062 public void contextualize(Context context) throws ContextException 063 { 064 _context = context; 065 } 066 067 @Override 068 public void service(ServiceManager serviceManager) throws ServiceException 069 { 070 _solrClientProvider = (SolrClientProvider) serviceManager.lookup(SolrClientProvider.ROLE); 071 _solrIndexer = (SolrIndexer) serviceManager.lookup(SolrIndexer.ROLE); 072 _solrWfIndexer = (SolrWorkflowIndexer) serviceManager.lookup(SolrWorkflowIndexer.ROLE); 073 _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE); 074 } 075 076 @Override 077 public boolean supports(Event event) 078 { 079 return event.getId().equals(ObservationConstants.EVENT_CONTENT_MODIFIED) 080 || event.getId().equals(ObservationConstants.EVENT_CONTENT_ADDED) 081 || event.getId().equals(ObservationConstants.EVENT_CONTENT_WORKFLOW_CHANGED) 082 || event.getId().equals(ObservationConstants.EVENT_CONTENT_TAGGED); 083 } 084 085 @Override 086 public int getPriority() 087 { 088 return MAX_PRIORITY + 3000; 089 } 090 091 @Override 092 public void observe(Event event, Map<String, Object> transientVars) throws Exception 093 { 094 if (ObserverHelper.isNotSuspendedObservationForIndexation()) 095 { 096 Map<String, Object> args = event.getArguments(); 097 String contentId = (String) args.get(ObservationConstants.ARGS_CONTENT_ID); 098 Boolean commit = (Boolean) args.get(ObservationConstants.ARGS_CONTENT_COMMIT); 099 if (commit == null) 100 { 101 // If not specified, let commit argument be 'true' by default, in order to be sure to commit changes. 102 commit = true; 103 } 104 String workspaceName = RepositoryConstants.DEFAULT_WORKSPACE; 105 SolrClient solrClient = _solrClientProvider.getUpdateClient(workspaceName, commit); 106 107 Content content = _resolver.resolveById(contentId); 108 109 if (content != null && index(content)) 110 { 111 if (event.getId().equals(ObservationConstants.EVENT_CONTENT_WORKFLOW_CHANGED)) 112 { 113 _solrIndexer.updateSystemProperty(content, WorkflowStepSystemProperty.SYSTEM_PROPERTY_ID, workspaceName, solrClient); 114 if (content instanceof WorkflowAwareContent) 115 { 116 _solrWfIndexer.indexAmetysObjectWorkflow((WorkflowAwareContent) content, workspaceName, solrClient); 117 } 118 } 119 else 120 { 121 _solrIndexer.indexContent(content.getId(), workspaceName, false, solrClient); 122 } 123 } 124 } 125 } 126 127 /** 128 * Test whether to index the given content or not. 129 * @param content the content to test. 130 * @return true to index the content, false otherwise. 131 */ 132 protected boolean index(Content content) 133 { 134 return true; 135 } 136 137}