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.Objects; 025import java.util.Set; 026 027import org.apache.avalon.framework.component.Component; 028import org.apache.avalon.framework.service.ServiceManager; 029import org.apache.cocoon.ProcessingException; 030 031import org.ametys.runtime.i18n.I18nizableText; 032import org.ametys.runtime.model.checker.ItemCheckerDescriptor; 033 034/** 035 * Abstract class for model items 036 */ 037public abstract class AbstractModelItem implements ModelItem 038{ 039 /** The service manager */ 040 protected static ServiceManager __serviceManager; 041 042 private String _name; 043 private I18nizableText _label; 044 private I18nizableText _description; 045 private Set<ItemCheckerDescriptor> _itemCheckers = new HashSet<>(); 046 047 private Model _model; 048 private ModelItemGroup _parent; 049 050 private String _path; 051 052 /** 053 * Default constructor. 054 */ 055 public AbstractModelItem() 056 { 057 // Empty constructor 058 } 059 060 /** 061 * Constructor used to create simple models and items 062 * @param name the name of the item 063 */ 064 public AbstractModelItem(String name) 065 { 066 _name = name; 067 } 068 069 /** 070 * Constructor by copying an existing {@link AbstractModelItem}. 071 * @param modelItemToCopy The {@link AbstractModelItem} to copy 072 */ 073 public AbstractModelItem(AbstractModelItem modelItemToCopy) 074 { 075 setName(modelItemToCopy.getName()); 076 setLabel(modelItemToCopy.getLabel()); 077 setDescription(modelItemToCopy.getDescription()); 078 for (ItemCheckerDescriptor itemChecker : modelItemToCopy.getItemCheckers()) 079 { 080 addItemChecker(itemChecker); 081 } 082 setModel(modelItemToCopy.getModel()); 083 setParent(modelItemToCopy.getParent()); 084 } 085 086 public String getName() 087 { 088 return _name; 089 } 090 091 public void setName(String name) 092 { 093 _name = name; 094 _path = null; 095 } 096 097 public I18nizableText getLabel() 098 { 099 return _label; 100 } 101 102 public void setLabel(I18nizableText label) 103 { 104 _label = label; 105 } 106 107 public I18nizableText getDescription() 108 { 109 return _description; 110 } 111 112 public void setDescription(I18nizableText description) 113 { 114 _description = description; 115 } 116 117 public void addItemChecker(ItemCheckerDescriptor itemChecker) 118 { 119 _itemCheckers.add(itemChecker); 120 } 121 122 public Set<ItemCheckerDescriptor> getItemCheckers() 123 { 124 return Collections.unmodifiableSet(_itemCheckers); 125 } 126 127 public String getPath() 128 { 129 if (_path != null) 130 { 131 return _path; 132 } 133 134 if (getName() == null) 135 { 136 return null; 137 } 138 139 StringBuilder path = new StringBuilder(); 140 141 ModelItemGroup parent = getParent(); 142 if (parent != null && parent.getPath() != null) 143 { 144 path.append(parent.getPath()).append(ITEM_PATH_SEPARATOR); 145 } 146 147 path.append(getName()); 148 _path = path.toString(); 149 return _path; 150 } 151 152 public Model getModel() 153 { 154 return _model; 155 } 156 157 public void setModel(Model model) 158 { 159 _model = model; 160 } 161 162 public ModelItemGroup getParent() 163 { 164 return _parent; 165 } 166 167 public void setParent(ModelItemGroup parent) 168 { 169 _parent = parent; 170 } 171 172 public Map<String, Object> toJSON(DefinitionContext context) throws ProcessingException 173 { 174 if (_shouldJSONBeEmpty(context)) 175 { 176 return Map.of(); 177 } 178 else 179 { 180 return _toJSON(context); 181 } 182 } 183 184 /** 185 * Converts the model item in a JSON map 186 * @param context the context of the definition 187 * @return The model item as a JSON map 188 * @throws ProcessingException If an error occurs when converting the model item 189 */ 190 protected Map<String, Object> _toJSON(DefinitionContext context) throws ProcessingException 191 { 192 Map<String, Object> result = new HashMap<>(); 193 194 result.put("name", getName()); 195 result.put("label", getLabel()); 196 result.put("description", getDescription()); 197 198 if (!getItemCheckers().isEmpty()) 199 { 200 List<Map<String, Object>> checkers2json = new ArrayList<>(); 201 for (ItemCheckerDescriptor paramChecker : getItemCheckers()) 202 { 203 checkers2json.add(paramChecker.toJSON()); 204 } 205 206 result.put("field-checker", checkers2json); 207 } 208 209 return result; 210 } 211 212 /** 213 * Checks if the current definition JSON conversion should return an empty map 214 * @param context the context of the definition 215 * @return <code>true</code> if the JSON conversion should return an empty map, <code>false</code> otherwise 216 */ 217 protected boolean _shouldJSONBeEmpty(DefinitionContext context) 218 { 219 return false; 220 } 221 222 public int compareTo(ModelItem item) 223 { 224 if (item == null) 225 { 226 return 1; 227 } 228 229 String name = getName(); 230 if (name != null) 231 { 232 return name.compareTo(item.getName()); 233 } 234 235 I18nizableText label = getLabel(); 236 if (label != null) 237 { 238 I18nizableText otherLabel = item.getLabel(); 239 if (otherLabel == null) 240 { 241 return 1; 242 } 243 244 return label.toString().compareTo(otherLabel.toString()); 245 } 246 247 return 0; 248 } 249 250 @Override 251 public boolean equals(Object obj) 252 { 253 if (obj == null || !getClass().equals(obj.getClass())) 254 { 255 return false; 256 } 257 258 ModelItem item = (ModelItem) obj; 259 260 // The item's model can be null 261 if (getModel() != item.getModel()) 262 { 263 if (getModel() == null ^ item.getModel() == null) 264 { 265 return false; 266 } 267 268 if (!Objects.equals(getModel().getFamilyId(), item.getModel().getFamilyId()) || !Objects.equals(getModel().getId(), item.getModel().getId())) 269 { 270 return false; 271 } 272 } 273 274 if (getPath() != null || item.getPath() != null) 275 { 276 return Objects.equals(getPath(), item.getPath()); 277 } 278 279 if (getLabel() != null || item.getLabel() != null) 280 { 281 return Objects.equals(getLabel(), item.getLabel()); 282 } 283 284 return false; 285 } 286 287 @Override 288 public int hashCode() 289 { 290 if (getPath() != null) 291 { 292 return getPath().hashCode(); 293 } 294 295 return getLabel().hashCode(); 296 } 297 298 @Override 299 public String toString() 300 { 301 if (getPath() != null) 302 { 303 return getPath(); 304 } 305 306 return getLabel().toString(); 307 } 308 309 /** 310 * Set the service manager 311 * The {@link ServiceManager} is used in the model items creation methods to get the model item type. 312 * {@link ModelItem} is not a {@link Component} and can't have a {@link ServiceManager} itself. Another {@link Component} has to set it 313 * @param manager the service manager to set 314 */ 315 public static void setServiceManager(ServiceManager manager) 316 { 317 __serviceManager = manager; 318 } 319}