001/*
002 *  Copyright 2022 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.odfweb.observation;
017
018import java.util.Map;
019
020import javax.jcr.ItemNotFoundException;
021import javax.jcr.Node;
022import javax.jcr.RepositoryException;
023import javax.jcr.Session;
024
025import org.ametys.cms.ObservationConstants;
026import org.ametys.cms.repository.Content;
027import org.ametys.core.observation.Event;
028import org.ametys.core.observation.Observer;
029import org.ametys.odf.observation.OdfObservationConstants;
030import org.ametys.odf.skill.ODFSkillsHelper;
031import org.ametys.plugins.repository.RepositoryConstants;
032import org.ametys.plugins.repository.jcr.JCRAmetysObject;
033import org.ametys.web.synchronization.AbstractSynchronizeObserver;
034
035
036/**
037 * {@link Observer} for observing skills display change
038 */
039public class SkillsDisplayObserver extends AbstractSynchronizeObserver
040{
041    @Override
042    public boolean supports(Event event)
043    {
044        return event.getId().equals(OdfObservationConstants.ODF_CONTENT_SKILLS_EXCLUSION_CHANGED);
045    }
046
047    @Override
048    protected void _internalObserve(Event event, Session liveSession) throws RepositoryException
049    {
050        Map<String, Object> arguments = event.getArguments();
051        Content content = (Content) arguments.get(ObservationConstants.ARGS_CONTENT);
052        
053        if (!(content instanceof JCRAmetysObject))
054        {
055            return;
056        }
057        
058        Node node = ((JCRAmetysObject) content).getNode();
059        
060        try
061        {
062            Node clonedNode = liveSession.getNodeByIdentifier(node.getIdentifier());
063            
064            String excludedProperty = RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ":" + ODFSkillsHelper.SKILLS_EXCLUDED_INTERNAL_ATTRIBUTE_NAME;
065            clonedNode.setProperty(excludedProperty, node.getProperty(excludedProperty).getValue());
066        }
067        catch (ItemNotFoundException e)
068        {
069            // content does not exist in the live workspace
070        }
071        
072        if (liveSession.hasPendingChanges())
073        {
074            liveSession.save();
075        }
076    }
077}