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.web.indexing.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.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.apache.avalon.framework.service.Serviceable; 026import org.apache.cocoon.components.ContextHelper; 027import org.apache.cocoon.environment.Request; 028 029import org.ametys.cms.content.indexing.solr.SolrIndexer; 030import org.ametys.cms.content.indexing.solr.observation.ObserverHelper; 031import org.ametys.cms.repository.Content; 032import org.ametys.cms.repository.RequestAttributeWorkspaceSelector; 033import org.ametys.core.observation.Event; 034import org.ametys.core.observation.Observer; 035import org.ametys.plugins.repository.AmetysObjectResolver; 036import org.ametys.plugins.repository.RepositoryConstants; 037import org.ametys.runtime.plugin.component.AbstractLogEnabled; 038import org.ametys.web.ObservationConstants; 039import org.ametys.web.WebConstants; 040import org.ametys.web.repository.content.WebContent; 041import org.ametys.web.repository.page.ZoneItem.ZoneType; 042 043/** 044 * Observes when a content is made orphan or reaffected to a page and update the index accordingly. 045 */ 046public class ContentOrphanStatusObserver extends AbstractLogEnabled implements Observer, Serviceable, Contextualizable 047{ 048 /** The Solr indexer. */ 049 protected SolrIndexer _solrIndexer; 050 /** The Ametys object resolver */ 051 protected AmetysObjectResolver _resolver; 052 /** The avalon context */ 053 protected Context _context; 054 055 @Override 056 public void service(ServiceManager manager) throws ServiceException 057 { 058 _solrIndexer = (SolrIndexer) manager.lookup(SolrIndexer.ROLE); 059 _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE); 060 } 061 062 @Override 063 public void contextualize(Context context) throws ContextException 064 { 065 _context = context; 066 } 067 068 @Override 069 public boolean supports(Event event) 070 { 071 return event.getId().equals(ObservationConstants.EVENT_ZONEITEM_DELETED) 072 || event.getId().equals(ObservationConstants.EVENT_ZONEITEM_ADDED); 073 } 074 075 @Override 076 public int getPriority(Event event) 077 { 078 // Will be processed AFTER live synchronization observers, to have up-to-date pages in live, in order to have correct indexation data 079 return Observer.MAX_PRIORITY + 4000; 080 } 081 082 @Override 083 public void observe(Event event, Map<String, Object> transientVars) throws Exception 084 { 085 if (ObserverHelper.isNotSuspendedObservationForIndexation()) 086 { 087 ZoneType zoneType = (ZoneType) event.getArguments().get(ObservationConstants.ARGS_ZONE_TYPE); 088 if (zoneType == ZoneType.CONTENT) 089 { 090 Content content = (Content) event.getArguments().get(ObservationConstants.ARGS_ZONE_ITEM_CONTENT); 091 092 if (content != null) 093 { 094 // default workspace 095 _updateProperties(content, RepositoryConstants.DEFAULT_WORKSPACE); 096 097 // live workspace 098 Request request = ContextHelper.getRequest(_context); 099 String currentWsp = RequestAttributeWorkspaceSelector.getForcedWorkspace(request); 100 try 101 { 102 // Force the workspace. 103 RequestAttributeWorkspaceSelector.setForcedWorkspace(request, WebConstants.LIVE_WORKSPACE); 104 if (_resolver.hasAmetysObjectForId(content.getId())) 105 { 106 Content liveContent = _resolver.resolveById(content.getId(), null); 107 _updateProperties(liveContent, WebConstants.LIVE_WORKSPACE); 108 } 109 } 110 finally 111 { 112 // Restore workspace 113 RequestAttributeWorkspaceSelector.setForcedWorkspace(request, currentWsp); 114 } 115 } 116 } 117 } 118 } 119 120 private void _updateProperties(Content content, String workspace) throws Exception 121 { 122 if (content instanceof WebContent) 123 { 124 // Update orphan status 125 _solrIndexer.updateSystemProperty(content, "orphan", workspace, false); 126 // Update pageIds 127 _solrIndexer.updateSystemProperty(content, "pages", workspace, false); 128 129 _solrIndexer.commit(workspace); 130 } 131 } 132}