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