001/*
002 *  Copyright 2024 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.cms.search.ui.model.impl;
017
018import java.util.Map;
019
020import org.ametys.cms.search.model.SearchModel;
021import org.ametys.cms.search.ui.model.SearchModelCriterionViewItem;
022import org.ametys.runtime.model.DefinitionContext;
023import org.ametys.runtime.model.ElementDefinition;
024import org.ametys.runtime.model.ViewElement;
025import org.ametys.runtime.model.ViewItem;
026
027/**
028 * Represents a view item referencing a criterion definition of a {@link SearchModel}
029 */
030public class DefaultSearchModelCriterionViewItem extends ViewElement implements SearchModelCriterionViewItem<ElementDefinition>
031{
032    private boolean _hidden;
033
034    public boolean isHidden()
035    {
036        return _hidden;
037    }
038    
039    public void setHidden(boolean hidden)
040    {
041        _hidden = hidden;
042    }
043    
044    @Override
045    public DefaultSearchModelCriterionViewItem createInstance()
046    {
047        return new DefaultSearchModelCriterionViewItem();
048    }
049    
050    @Override
051    public Map<String, Object> toJSON(DefinitionContext context)
052    {
053        Map<String, Object> json = super.toJSON(context);
054
055        if (!json.isEmpty())
056        {
057            json.put("hidden", isHidden());
058        }
059        return json;
060    }
061    
062    @Override
063    public void copyTo(ViewItem item)
064    {
065        super.copyTo(item);
066        
067        assert item instanceof DefaultSearchModelCriterionViewItem;
068        DefaultSearchModelCriterionViewItem criterion = (DefaultSearchModelCriterionViewItem) item;
069        criterion.setHidden(_hidden);
070    }
071}