001/*
002 *  Copyright 2018 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.runtime.model;
017
018import java.util.ArrayList;
019import java.util.Collections;
020import java.util.HashMap;
021import java.util.HashSet;
022import java.util.List;
023import java.util.Map;
024import java.util.Set;
025
026import org.apache.cocoon.ProcessingException;
027
028import org.ametys.runtime.i18n.I18nizableText;
029import org.ametys.runtime.model.checker.ItemCheckerDescriptor;
030
031/**
032 * Abstract class for model items
033 */
034public abstract class AbstractModelItem implements ModelItem
035{
036    private String _name;
037    private I18nizableText _label;
038    private I18nizableText _description;
039    private Set<ItemCheckerDescriptor> _itemCheckers = new HashSet<>();
040    
041    private Model _model;
042    private ModelItemGroup _parent;
043    
044    public String getName()
045    {
046        return _name;
047    }
048    
049    public void setName(String name)
050    {
051        _name = name;
052    }
053    
054    public I18nizableText getLabel()
055    {
056        return _label;
057    }
058
059    public void setLabel(I18nizableText label)
060    {
061        _label = label;
062    }
063
064    public I18nizableText getDescription()
065    {
066        return _description;
067    }
068
069    public void setDescription(I18nizableText description)
070    {
071        _description = description;
072    }
073    
074    public void addItemChecker(ItemCheckerDescriptor itemChecker)
075    {
076        _itemCheckers.add(itemChecker);
077    }
078    
079    public Set<ItemCheckerDescriptor> getItemCheckers()
080    {
081        return Collections.unmodifiableSet(_itemCheckers);
082    }
083
084    public String getPath()
085    {
086        if (getName() == null)
087        {
088            return null;
089        }
090        
091        StringBuilder path = new StringBuilder();
092        
093        ModelItemGroup parent = getParent();
094        if (parent != null && parent.getPath() != null)
095        {
096            path.append(parent.getPath()).append(ITEM_PATH_SEPARATOR);
097        }
098        
099        path.append(getName());
100        return path.toString();
101    }
102
103    public Model getModel()
104    {
105        return _model;
106    }
107    
108    public void setModel(Model model)
109    {
110        _model = model;
111    }
112
113    public ModelItemGroup getParent()
114    {
115        return _parent;
116    }
117    
118    public void setParent(ModelItemGroup parent)
119    {
120        _parent = parent;
121    }
122    
123    public Map<String, Object> toJSON() throws ProcessingException
124    {
125        Map<String, Object> result = new HashMap<>();
126        
127        result.put("name", getName());
128        result.put("label", getLabel());
129        result.put("description", getDescription());
130        
131        if (!getItemCheckers().isEmpty())
132        {
133            List<Map<String, Object>> checkers2json = new ArrayList<>();
134            for (ItemCheckerDescriptor paramChecker : getItemCheckers())
135            {
136                checkers2json.add(paramChecker.toJSON());
137            }
138            
139            result.put("field-checker", checkers2json);
140        }
141        
142        return result;
143    }
144    
145    public int compareTo(ModelItem item)
146    {
147        if (item == null)
148        {
149            return 1;
150        }
151        
152        String name = getName();
153        if (name != null)
154        {
155            return name.compareTo(item.getName());
156        }
157        
158        I18nizableText label = getLabel();
159        if (label != null)
160        {
161            I18nizableText otherLabel = item.getLabel();
162            if (otherLabel == null)
163            {
164                return 1;
165            }
166            return label.toString().compareTo(otherLabel.toString());
167        }
168        return 0;
169    }
170    
171    @Override
172    public boolean equals(Object obj)
173    {
174        if (obj == null)
175        {
176            return false;
177        }
178        
179        if (!(obj instanceof ModelItem))
180        {
181            return false;
182        }
183        
184        ModelItem item = (ModelItem) obj;
185        
186        if (getName() != null)
187        {
188            return getName().equals(item.getName());
189        }
190        
191        if (getLabel() != null)
192        {
193            return getLabel().equals(item.getLabel());
194        }
195        
196        return true;
197    }
198
199    @Override
200    public int hashCode()
201    {
202        if (getName() != null)
203        {
204            return getName().hashCode();
205        }
206        
207        return getLabel().hashCode();
208    }
209    
210    @Override
211    public String toString()
212    {
213        if (getName() != null)
214        {
215            return getName();
216        }
217        return getLabel().toString();
218    }
219}