001/* 002 * Copyright 2019 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.config; 017 018import java.util.Collection; 019import java.util.Comparator; 020 021import org.ametys.runtime.model.CategorizedElementDefinitionHelper; 022import org.ametys.runtime.model.CategorizedElementDefinitionWrapper; 023import org.ametys.runtime.model.ElementDefinition; 024import org.ametys.runtime.model.ModelItem; 025import org.ametys.runtime.model.ModelItemGroup; 026 027/** 028 * Comparator for the element definitions. Uses the position given by the {@link CategorizedElementDefinitionWrapper}s 029 * If no position is set, compares the definitions themselves 030 */ 031public class ConfigParameterDefinitionComparator implements Comparator<ModelItem> 032{ 033 private final Collection<ConfigParameterDefinitionWrapper> _configParameterDefinitionWrappers; 034 035 /** 036 * Constructor 037 * @param configParameterDefinitionWrappers the wrappers containing the position of the config parameter definitions 038 */ 039 public ConfigParameterDefinitionComparator(Collection<ConfigParameterDefinitionWrapper> configParameterDefinitionWrappers) 040 { 041 _configParameterDefinitionWrappers = configParameterDefinitionWrappers; 042 } 043 044 public int compare(ModelItem item1, ModelItem item2) 045 { 046 if (!(item1 instanceof ElementDefinition) || !(item2 instanceof ElementDefinition)) 047 { 048 throw new IllegalArgumentException("This comparator can only be called with element definitions"); 049 } 050 051 ElementDefinition definition1 = (ElementDefinition) item1; 052 ConfigParameterDefinitionWrapper wrapper1 = getWrapperFromDefinition((ElementDefinition) item1); 053 ElementDefinition definition2 = (ElementDefinition) item2; 054 ConfigParameterDefinitionWrapper wrapper2 = getWrapperFromDefinition((ElementDefinition) item2); 055 056 int positionComparison = CategorizedElementDefinitionHelper.compareWrapperPositions(wrapper1, wrapper2); 057 if (positionComparison != 0) 058 { 059 return positionComparison; 060 } 061 else 062 { 063 return definition1.compareTo(definition2); 064 } 065 } 066 067 /** 068 * Retrieves the {@link ConfigParameterDefinitionWrapper} corresponding to the given definition 069 * @param definition the definition 070 * @return the corresponding {@link ConfigParameterDefinitionWrapper} 071 */ 072 protected ConfigParameterDefinitionWrapper getWrapperFromDefinition(ElementDefinition definition) 073 { 074 ModelItemGroup category = null; 075 ModelItemGroup group = definition.getParent(); 076 if (group != null) 077 { 078 category = group.getParent(); 079 } 080 081 for (ConfigParameterDefinitionWrapper wrapper : _configParameterDefinitionWrappers) 082 { 083 if ((category == null || category.getLabel().equals(wrapper.getDisplayCategory())) 084 && (group == null || group.getLabel().equals(wrapper.getDisplayGroup())) 085 && definition.getName().equals(wrapper.getDefinition().getName())) 086 { 087 return wrapper; 088 } 089 } 090 091 // no wrapper has been found 092 return null; 093 } 094}