001/* 002 * Copyright 2021 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.plugins.joboffer.observer; 017 018import java.util.Map; 019import java.util.Set; 020 021import org.apache.avalon.framework.context.Context; 022import org.apache.avalon.framework.context.ContextException; 023import org.apache.avalon.framework.context.Contextualizable; 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026import org.apache.avalon.framework.service.Serviceable; 027import org.apache.cocoon.components.ContextHelper; 028import org.apache.cocoon.environment.Request; 029 030import org.ametys.cms.ObservationConstants; 031import org.ametys.cms.contenttype.ContentTypesHelper; 032import org.ametys.cms.repository.Content; 033import org.ametys.core.observation.Event; 034import org.ametys.core.observation.Observer; 035import org.ametys.plugins.joboffer.JobOfferConstants; 036import org.ametys.plugins.repository.AmetysObjectResolver; 037import org.ametys.runtime.plugin.component.AbstractLogEnabled; 038 039 040/** 041 * Abstract observer for job offer 042 * 043 */ 044public abstract class AbstractJobOfferObserver extends AbstractLogEnabled implements Observer, Serviceable, Contextualizable 045{ 046 /** The key for persons in charge in transvient vars */ 047 protected static final String REQUEST_ATTR_PERSON_IN_CHARGE = AbstractJobOfferObserver.class.getName() + "$personIncharge"; 048 049 /** The content type helper */ 050 protected ContentTypesHelper _cTypeHelper; 051 /** The Ametys object resolver */ 052 protected AmetysObjectResolver _resolver; 053 054 private Context _context; 055 056 public void service(ServiceManager smanager) throws ServiceException 057 { 058 _cTypeHelper = (ContentTypesHelper) smanager.lookup(ContentTypesHelper.ROLE); 059 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 060 } 061 062 public void contextualize(Context context) throws ContextException 063 { 064 _context = context; 065 } 066 067 public boolean supports(Event event) 068 { 069 return _getSupportedEventIds().contains(event.getId()) && getJobOffer(event) != null; 070 } 071 072 /** 073 * Get the current request 074 * @return the request 075 */ 076 protected Request _getRequest() 077 { 078 return ContextHelper.getRequest(_context); 079 } 080 /** 081 * Get the id of supported events 082 * @return the id in a Set 083 */ 084 protected abstract Set<String> _getSupportedEventIds(); 085 086 public int getPriority(Event event) 087 { 088 return MIN_PRIORITY; 089 } 090 091 public void observe(Event event, Map<String, Object> transientVars) throws Exception 092 { 093 Content content = getJobOffer(event); 094 if (content != null) 095 { 096 _internalObserver(event, content, transientVars); 097 } 098 } 099 100 /** 101 * Internal observer 102 * @param event the event 103 * @param content the job offer content. Cannot be null. 104 * @param transientVars The transient variables 105 * @throws Exception if an error occurred 106 */ 107 protected abstract void _internalObserver(Event event, Content content, Map<String, Object> transientVars) throws Exception; 108 109 /** 110 * Get the job offer concerned by the event 111 * @param event the event 112 * @return the job offer content or null 113 */ 114 protected Content getJobOffer(Event event) 115 { 116 Map<String, Object> args = event.getArguments(); 117 118 if (args.containsKey(ObservationConstants.ARGS_CONTENT_ID)) 119 { 120 String contentId = (String) args.get(ObservationConstants.ARGS_CONTENT_ID); 121 122 Content content = _resolver.resolveById(contentId); 123 return _cTypeHelper.isInstanceOf(content, JobOfferConstants.JOB_OFFER_CONTENT_TYPE) ? content : null; 124 } 125 126 return null; 127 } 128}