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.core.model; 017 018import java.util.ArrayList; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Map; 022 023import org.apache.avalon.framework.component.Component; 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026import org.apache.avalon.framework.service.Serviceable; 027 028import org.ametys.runtime.model.AbstractModelItem; 029import org.ametys.runtime.model.ModelItem; 030import org.ametys.runtime.model.disableconditions.DisableCondition; 031import org.ametys.runtime.model.disableconditions.DisableConditions; 032 033/** 034 * Helper for model items 035 */ 036public class ModelItemHelper implements Component, Serviceable 037{ 038 /** Avalon ROLE. */ 039 public static final String ROLE = ModelItemHelper.class.getName(); 040 041 public void service(ServiceManager manager) throws ServiceException 042 { 043 AbstractModelItem.setServiceManager(manager); 044 } 045 046 /** 047 * Converts the model item's disable conditions in a JSON map 048 * @param modelItem The model item containing the disable conditions 049 * @return The model item's disable conditions as a JSON map 050 */ 051 public Map<String, Object> disableConditionsToJSON(ModelItem modelItem) 052 { 053 return _disableConditionsToJSON(modelItem.getDisableConditions()); 054 } 055 056 /** 057 * Converts the given disable conditions in a JSON map 058 * @param disableConditions the disable conditions to convert 059 * @return The disable conditions as a JSON map 060 */ 061 private Map<String, Object> _disableConditionsToJSON(DisableConditions disableConditions) 062 { 063 Map<String, Object> map = new HashMap<>(); 064 065 // Handle simple conditions 066 List<Map<String, String>> disableConditionList = new ArrayList<>(); 067 map.put("condition", disableConditionList); 068 for (DisableCondition disableCondition : disableConditions.getConditions()) 069 { 070 Map<String, String> disableConditionAsMap = disableConditionToJSON(disableCondition); 071 disableConditionList.add(disableConditionAsMap); 072 } 073 074 // Handle nested conditions 075 List<Map<String, Object>> disableConditionsList = new ArrayList<>(); 076 map.put("conditions", disableConditionsList); 077 for (DisableConditions subDisableConditions : disableConditions.getSubConditions()) 078 { 079 Map<String, Object> disableConditionsAsMap = _disableConditionsToJSON(subDisableConditions); 080 disableConditionsList.add(disableConditionsAsMap); 081 } 082 083 // Handle type 084 map.put("type", disableConditions.getAssociationType().toString().toLowerCase()); 085 086 return map; 087 } 088 089 /** 090 * Converts the given disable condition in a JSON map 091 * @param disableCondition the disable condition to convert 092 * @return The disable condition as a JSON map 093 */ 094 public Map<String, String> disableConditionToJSON(DisableCondition disableCondition) 095 { 096 Map<String, String> map = new HashMap<>(); 097 map.put("id", disableCondition.getId()); 098 map.put("operator", disableCondition.getOperator().toString().toLowerCase()); 099 map.put("value", disableCondition.getValue()); 100 return map; 101 } 102}