001/*
002 *  Copyright 2020 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.web.parameters.view;
017
018import java.util.Collection;
019import java.util.Map;
020import java.util.stream.Collectors;
021
022import org.ametys.runtime.model.Model;
023import org.ametys.runtime.model.ModelItem;
024import org.ametys.runtime.model.View;
025import org.ametys.web.repository.page.SitemapElement;
026
027/**
028 * Object representing the view parameters as a model
029 */
030public class ViewParametersModel implements Model
031{
032    /** The unique identifier */
033    protected String _id;
034    
035    /** The map of model items */
036    private Map<String, ModelItem> _modelItems;
037    
038    /** The view of the parameters */
039    private View _view;
040    
041    /**
042     * The view parameters constructor
043     * @param id the id
044     * @param view the view
045     * @param modelItems the model items
046     */
047    public ViewParametersModel(String id, View view, Map<String, ModelItem> modelItems)
048    {
049        this._id = id;
050        this._view = view;
051        this._modelItems = modelItems;
052    }
053    
054    /**
055     * Get the view
056     * @return the view
057     */
058    public View getView()
059    {
060        return _view;
061    }
062
063    /**
064     * Set the view
065     * @param view the view
066     */
067    public void setView(View view)
068    {
069        this._view = view;
070    }
071    
072    public Collection<? extends ModelItem> getModelItems()
073    {
074        return _modelItems.values();
075    }
076    
077    /**
078     * Set the model items
079     * @param modelItems the model items
080     */
081    public void setModelItems(Map<String, ModelItem> modelItems)
082    {
083        this._modelItems = modelItems;
084    }
085    
086    public String getId()
087    {
088        return _id;
089    }
090
091    public String getFamilyId()
092    {
093        return getClass().getName();
094    }
095    
096    /**
097     * Returns true if there are model items
098     * @return true if there are model items
099     */
100    public boolean isNotEmpty()
101    {
102        return !_modelItems.isEmpty();
103    }
104    
105    /**
106     * Add a model item
107     * @param modelItem the model item
108     */
109    public void addModelItem(ModelItem modelItem)
110    {
111        _modelItems.putIfAbsent(modelItem.getName(), modelItem);
112    }
113    
114    /**
115     * Get model item with inheritance from the page
116     * @param page the page
117     * @return the list of model item with inheritance
118     */
119    public Collection< ? extends ModelItem> getInheritedModelItems(SitemapElement page)
120    {
121        return _modelItems.values()
122                .stream()
123                .filter(ViewParameter.class::isInstance)
124                .map(ViewParameter.class::cast)
125                .filter(p -> p.hasInheritance(page))
126                .collect(Collectors.toList());
127    }
128}