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; 030import org.apache.commons.lang3.StringUtils; 031 032import org.ametys.runtime.model.AbstractModelItem; 033import org.ametys.runtime.model.DefinitionContext; 034import org.ametys.runtime.model.ModelItem; 035import org.ametys.runtime.model.disableconditions.DisableCondition; 036import org.ametys.runtime.model.disableconditions.DisableCondition.OPERATOR; 037import org.ametys.runtime.model.disableconditions.DisableConditions; 038 039/** 040 * Helper for model items 041 */ 042public class ModelItemHelper implements Component, Serviceable, Contextualizable 043{ 044 /** Avalon ROLE. */ 045 public static final String ROLE = ModelItemHelper.class.getName(); 046 047 public void contextualize(Context context) throws ContextException 048 { 049 AbstractModelItem.setContext(context); 050 } 051 052 public void service(ServiceManager manager) throws ServiceException 053 { 054 AbstractModelItem.setServiceManager(manager); 055 } 056 057 /** 058 * Converts the model item's disable conditions in a JSON map 059 * @param modelItem The model item containing the disable conditions 060 * @param context the definition context 061 * @return The model item's disable conditions as a JSON map 062 */ 063 public Map<String, Object> disableConditionsToJSON(ModelItem modelItem, DefinitionContext context) 064 { 065 DisableConditions<? extends DisableCondition> disableConditions = modelItem.getDisableConditions(); 066 return _disableConditionsToJSON(modelItem, disableConditions, context); 067 } 068 069 /** 070 * Converts the given disable conditions in a JSON map 071 * @param modelItem The model item containing the disable conditions 072 * @param disableConditions the disable conditions to convert 073 * @param context the definition context 074 * @return The disable conditions as a JSON map 075 */ 076 private Map<String, Object> _disableConditionsToJSON(ModelItem modelItem, DisableConditions<? extends DisableCondition> disableConditions, DefinitionContext context) 077 { 078 Map<String, Object> map = new HashMap<>(); 079 080 // Handle simple conditions 081 List<Map<String, String>> disableConditionList = new ArrayList<>(); 082 map.put("condition", disableConditionList); 083 for (DisableCondition disableCondition : disableConditions.getConditions()) 084 { 085 Map<String, String> disableConditionAsMap = disableCondition.isExternal(context) 086 ? externalDisableConditionToJSON(modelItem, disableCondition) 087 : disableConditionToJSON(disableCondition); 088 disableConditionList.add(disableConditionAsMap); 089 } 090 091 // Handle nested conditions 092 List<Map<String, Object>> disableConditionsList = new ArrayList<>(); 093 map.put("conditions", disableConditionsList); 094 for (DisableConditions<? extends DisableCondition> subDisableConditions : disableConditions.getSubConditions()) 095 { 096 Map<String, Object> disableConditionsAsMap = _disableConditionsToJSON(modelItem, subDisableConditions, context); 097 disableConditionsList.add(disableConditionsAsMap); 098 } 099 100 // Handle type 101 map.put("type", disableConditions.getAssociationType().toString().toLowerCase()); 102 103 return map; 104 } 105 106 /** 107 * Converts the given disable condition in a JSON map 108 * @param modelItem The model item containing the disable conditions 109 * @param disableCondition the disable condition to convert 110 * @return The disable condition as a JSON map 111 */ 112 public Map<String, String> externalDisableConditionToJSON(ModelItem modelItem, DisableCondition disableCondition) 113 { 114 Map<String, String> map = new HashMap<>(); 115 116 String id = StringUtils.join(new String[] {DisableCondition.EXTERNAL_CONDITION_ID_PREFIX, disableCondition.getId(), disableCondition.getName()}, "_"); 117 map.put("id", id); 118 119 map.put("operator", OPERATOR.EQ.toString().toLowerCase()); 120 map.put("value", String.valueOf(Boolean.TRUE)); 121 return map; 122 } 123 124 /** 125 * Converts the given disable condition in a JSON map 126 * @param disableCondition the disable condition to convert 127 * @return The disable condition as a JSON map 128 */ 129 public Map<String, String> disableConditionToJSON(DisableCondition disableCondition) 130 { 131 Map<String, String> map = new HashMap<>(); 132 map.put("id", disableCondition.getName()); 133 map.put("operator", disableCondition.getOperator().toString().toLowerCase()); 134 map.put("value", disableCondition.getValue()); 135 return map; 136 } 137}