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.Objects;
020import java.util.Set;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024
025import org.ametys.cms.ObservationConstants;
026import org.ametys.cms.content.indexing.solr.SolrIndexer;
027import org.ametys.cms.repository.Content;
028import org.ametys.cms.repository.ContentQueryHelper;
029import org.ametys.cms.repository.ContentTypeExpression;
030import org.ametys.core.observation.Event;
031import org.ametys.core.observation.Observer;
032import org.ametys.core.user.UserIdentity;
033import org.ametys.plugins.joboffer.JobOfferConstants;
034import org.ametys.plugins.repository.AmetysObjectIterable;
035import org.ametys.plugins.repository.query.expression.AndExpression;
036import org.ametys.plugins.repository.query.expression.Expression;
037import org.ametys.plugins.repository.query.expression.Expression.Operator;
038import org.ametys.plugins.repository.query.expression.StringExpression;
039
040/**
041 * {@link Observer}  when the job offer has been modified
042 */
043public class ModifiedJobOfferObserver extends AbstractJobOfferObserver
044{
045    private SolrIndexer _solrIndexer;
046
047    @Override
048    public void service(ServiceManager smanager) throws ServiceException
049    {
050        super.service(smanager);
051        _solrIndexer = (SolrIndexer) smanager.lookup(SolrIndexer.ROLE);
052    }
053    
054    @Override
055    protected Set<String> _getSupportedEventIds()
056    {
057        return Set.of(ObservationConstants.EVENT_CONTENT_MODIFIED);
058    }
059
060    @Override
061    protected void _internalObserver(Event event, Content content, Map<String, Object> transientVars) throws Exception
062    {
063        UserIdentity[] personsInCharge = content.getValue(JobOfferConstants.JOB_OFFER_ATTRIBUTE_PATH_PERSON_IN_CHARGE);
064        
065        UserIdentity[] oldPersonsInCharge = (UserIdentity[]) _getRequest().getAttribute(REQUEST_ATTR_PERSON_IN_CHARGE);
066        
067        if (!Objects.deepEquals(personsInCharge, oldPersonsInCharge))
068        {
069            // responsible has changed, update ACL for applications
070            _reIndexApplications(content);
071        }
072    }
073    
074    private void _reIndexApplications(Content content) throws Exception
075    {
076        Expression cTypeExpr = new ContentTypeExpression(Operator.EQ, JobOfferConstants.JOB_APPLICATION_CONTENT_TYPE);
077        Expression jobExpr = new StringExpression(JobOfferConstants.JOB_APPLICATION_ATTRIBUTE_PATH_JOB_OFFER, Operator.EQ, content.getId());
078        
079        Expression finalExpr = new AndExpression(cTypeExpr, jobExpr);
080        
081        String query = ContentQueryHelper.getContentXPathQuery(finalExpr);
082        
083        AmetysObjectIterable<Content> applications = _resolver.query(query);
084        _solrIndexer.updateAclCache(applications);
085    }
086}