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