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 Map<String, Object> args = event.getArguments(); 089 String contentId = (String) args.get(ObservationConstants.ARGS_CONTENT_ID); 090 Boolean commit = (Boolean) args.get(ObservationConstants.ARGS_CONTENT_COMMIT); 091 if (commit == null) 092 { 093 // If not specified, let commit argument be 'true' by default, in order to be sure to commit changes. 094 commit = true; 095 } 096 097 Content content = _resolver.resolveById(contentId); 098 099 if (content != null && index(content)) 100 { 101 if (event.getId().equals(ObservationConstants.EVENT_CONTENT_WORKFLOW_CHANGED)) 102 { 103 _solrIndexer.updateSystemProperty(content, "workflowStep", null, commit); 104 if (content instanceof WorkflowAwareContent) 105 { 106 _solrWfIndexer.indexAmetysObjectWorkflow((WorkflowAwareContent) content, RepositoryConstants.DEFAULT_WORKSPACE, commit); 107 } 108 } 109 else 110 { 111 _solrIndexer.indexContent(content.getId(), RepositoryConstants.DEFAULT_WORKSPACE, false, commit); 112 } 113 } 114 } 115 } 116 117 /** 118 * Test whether to index the given content or not. 119 * @param content the content to test. 120 * @return true to index the content, false otherwise. 121 */ 122 protected boolean index(Content content) 123 { 124 return true; 125 } 126 127}