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.plugins.repository.model;
017
018import java.util.Map;
019
020import org.xml.sax.ContentHandler;
021import org.xml.sax.SAXException;
022
023import org.ametys.plugins.repository.data.holder.ModelAwareDataHolder;
024import org.ametys.plugins.repository.data.holder.impl.DataHolderHelper;
025import org.ametys.runtime.model.DefinitionContext;
026import org.ametys.runtime.model.ModelViewItemGroup;
027import org.ametys.runtime.model.type.DataContext;
028
029/**
030 * View reference to a repeater
031 */
032public class RepeaterViewItem extends ModelViewItemGroup<RepeaterDefinition>
033{
034    @Override
035    public ModelViewItemGroup createInstance()
036    {
037        return new RepeaterViewItem();
038    }
039    
040    @Override
041    protected void additionalDataToSAX(ContentHandler contentHandler, DefinitionContext context) throws SAXException
042    {
043        super.additionalDataToSAX(contentHandler, context);
044        
045        DataHolderHelper.externalDisableConditionsToSAX(_getDataHolderFromContext(context), contentHandler, this, _getDataContext());
046    }
047    
048    @Override
049    public Map<String, Object> toJSON(DefinitionContext context)
050    {
051        Map<String, Object> json = super.toJSON(context);
052        
053        if (!json.isEmpty())
054        {
055            Map<String, Boolean> conditionsValues = DataHolderHelper.getExternalDisableConditionsValues(_getDataHolderFromContext(context), this, _getDataContext());
056            if (!conditionsValues.isEmpty())
057            {
058                json.put(DataHolderHelper.EXTERNAL_DISABLE_CONDITIONS_VALUES, conditionsValues);
059            }
060        }
061        
062        return json;
063    }
064    
065    private ModelAwareDataHolder _getDataHolderFromContext(DefinitionContext context)
066    {
067        return context.getObject()
068                      .filter(ModelAwareDataHolder.class::isInstance)
069                      .map(ModelAwareDataHolder.class::cast)
070                      .orElse(null);
071    }
072    
073    private DataContext _getDataContext()
074    {
075        RepeaterDefinition definition = getDefinition();
076        return DataContext.newInstance()
077                          .withViewItem(this)
078                          .withModelItem(definition)
079                          .withDataPath(definition.getPath());
080    }
081}