001/*
002 *  Copyright 2025 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.course;
017
018import java.util.Map;
019import java.util.Optional;
020
021import org.apache.avalon.framework.component.Component;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025
026import org.ametys.odf.ODFHelper;
027import org.ametys.odf.ProgramItem;
028import org.ametys.runtime.model.ModelItem;
029import org.ametys.runtime.model.disableconditions.AbstractExternalDisableCondition;
030
031/**
032 * Disable condition used to block attribute on not shared program items
033 */
034public class NotShared2DisableCondition extends AbstractExternalDisableCondition implements Component, Serviceable
035{
036    private ODFHelper _odfHelper;
037    
038    public void service(ServiceManager manager) throws ServiceException
039    {
040        _odfHelper = (ODFHelper) manager.lookup(ODFHelper.ROLE);
041    }
042    
043    public <T> boolean evaluate(ModelItem definition, String dataPath, Optional<String> oldDataPath, Map<String, Object> values, Optional<T> object, Map<String, Object> contextualParameters)
044    {
045        return object.filter(ProgramItem.class::isInstance)
046              .map(ProgramItem.class::cast)
047              .map(_odfHelper::isShared)
048              .map(b -> !b)
049              .orElse(false);
050    }
051}