001/* 002 * Copyright 2024 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.odf.observation; 017 018import java.util.List; 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.content.indexing.solr.observation.ObserverHelper; 027import org.ametys.cms.repository.Content; 028import org.ametys.core.observation.AsyncObserver; 029import org.ametys.core.observation.Event; 030import org.ametys.core.observation.Observer; 031import org.ametys.odf.ODFHelper; 032import org.ametys.odf.ProgramItem; 033import org.ametys.plugins.repository.AmetysObjectResolver; 034import org.ametys.plugins.repository.RepositoryConstants; 035import org.ametys.runtime.plugin.component.AbstractLogEnabled; 036 037 038/** 039 * {@link Observer} when a educational path is no more valid after moving or removing a program item in ODF tree 040 */ 041public class IndexSharedPropertyOnHierarchyChangedObserver extends AbstractLogEnabled implements AsyncObserver, Serviceable 042{ 043 private AmetysObjectResolver _resolver; 044 private SolrIndexer _solrIndexer; 045 private ODFHelper _odfHelper; 046 047 public void service(ServiceManager smanager) throws ServiceException 048 { 049 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 050 _solrIndexer = (SolrIndexer) smanager.lookup(SolrIndexer.ROLE); 051 _odfHelper = (ODFHelper) smanager.lookup(ODFHelper.ROLE); 052 } 053 054 public boolean supports(Event event) 055 { 056 return event.getId().equals(OdfObservationConstants.EVENT_PROGRAM_ITEM_HIERARCHY_CHANGED); 057 } 058 059 public int getPriority(Event event) 060 { 061 return MIN_PRIORITY; 062 } 063 064 public void observe(Event event, Map<String, Object> transientVars) throws Exception 065 { 066 if (ObserverHelper.isNotSuspendedObservationForIndexation()) 067 { 068 ProgramItem programItem = getProgramItem(event); 069 if (programItem != null) 070 { 071 _indexSharedProperty(programItem); 072 } 073 } 074 } 075 076 private void _indexSharedProperty(ProgramItem programItem) throws Exception 077 { 078 // Do a partial update of the shared property 079 _solrIndexer.updateProperty((Content) programItem, ProgramItem.SHARED_PROPERTY, RepositoryConstants.DEFAULT_WORKSPACE); 080 081 List<ProgramItem> childProgramItems = _odfHelper.getChildProgramItems(programItem); 082 for (ProgramItem childProgramItem : childProgramItems) 083 { 084 _indexSharedProperty(childProgramItem); 085 } 086 } 087 088 /** 089 * Get the {@link ProgramItem} concerned by this event 090 * @param event the event 091 * @return the program item content or null 092 */ 093 protected ProgramItem getProgramItem(Event event) 094 { 095 Map<String, Object> args = event.getArguments(); 096 097 if (args.containsKey(OdfObservationConstants.ARGS_PROGRAM_ITEM_ID)) 098 { 099 String programItemId = (String) args.get(OdfObservationConstants.ARGS_PROGRAM_ITEM_ID); 100 return _resolver.resolveById(programItemId); 101 } 102 return null; 103 } 104}