| /** |
| * |
| * Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation |
| * |
| */ |
| package org.eclipse.osbp.xtext.datamart.common.sql; |
| |
| import java.lang.reflect.InvocationTargetException; |
| import java.lang.reflect.Method; |
| import java.math.BigDecimal; |
| import java.util.HashMap; |
| import java.util.List; |
| import java.util.Map; |
| |
| import org.apache.commons.lang3.StringUtils; |
| import org.eclipse.osbp.dsl.common.datatypes.IDto; |
| import org.eclipse.osbp.ui.api.datamart.IDataMart.EType; |
| import org.eclipse.osbp.xtext.datamart.common.DatamartDtoMapper; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| public class OperativeDtoContainer { |
| |
| private static final Logger LOGGER = LoggerFactory.getLogger(OperativeDtoContainer.class); |
| |
| private final String operativeDtoIdColumn; |
| private final EType operativeDtoIdDatatype; |
| private final Map<Object, IDto> operativeDtoIdsMap; |
| |
| public OperativeDtoContainer() { |
| operativeDtoIdColumn = null; |
| operativeDtoIdDatatype = null; |
| operativeDtoIdsMap = new HashMap<>(); |
| } |
| |
| public OperativeDtoContainer(DatamartDtoMapper datamartDtoMapper, Class<?> operativeDtoClass, List<IDto> operativeDtos) { |
| operativeDtoIdsMap = new HashMap<>(); |
| if ((operativeDtoClass != null) && datamartDtoMapper.contains(operativeDtoClass.getCanonicalName())) { |
| String dtoFqn = operativeDtoClass.getCanonicalName(); |
| operativeDtoIdColumn = datamartDtoMapper.getEntityIdAliasName(dtoFqn); |
| operativeDtoIdDatatype = datamartDtoMapper.getEntityIdDataTypeClassName(dtoFqn); |
| try { |
| switch (operativeDtoIdDatatype) { |
| case INTEGER: |
| break; |
| case STRING: |
| break; |
| default: |
| throw new IllegalArgumentException("Column "+dtoFqn+"."+datamartDtoMapper.getEntityIdAttribute(dtoFqn)+" must be either int or String"); |
| } |
| if (!operativeDtos.isEmpty()) { |
| String getterName = "get"+StringUtils.capitalize(datamartDtoMapper.getEntityIdAttribute(dtoFqn)); |
| Method getter = operativeDtoClass.getMethod(getterName, (Class[]) null); |
| for (IDto dtoInstance : operativeDtos) { |
| switch (operativeDtoIdDatatype) { |
| case INTEGER: |
| operativeDtoIdsMap.put(new Integer((Integer)getter.invoke(dtoInstance)), dtoInstance); |
| break; |
| case STRING: |
| operativeDtoIdsMap.put((String)getter.invoke(dtoInstance), dtoInstance); |
| break; |
| default: |
| } |
| } |
| } |
| } |
| catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { |
| LOGGER.error(e.getLocalizedMessage(), e); |
| } |
| } |
| else { |
| operativeDtoIdDatatype = null; |
| operativeDtoIdColumn = null; |
| } |
| // analyze columnaxis if contains operative dto object |
| // if so get column label for operative dto id field |
| // add all column values of operative dtos into ids set |
| } |
| |
| public String getOperativeDtoIdColumn() { |
| return operativeDtoIdColumn; |
| } |
| |
| public IDto getOperativeDtoForId(Object id) { |
| switch (operativeDtoIdDatatype) { |
| case INTEGER: |
| if (id instanceof Integer) { |
| return operativeDtoIdsMap.get(id); |
| } |
| else if (id instanceof String) { |
| try { |
| id = ((String)id).split("\\.")[0]; |
| return operativeDtoIdsMap.get(Integer.parseInt((String)id)); |
| } |
| catch (Exception e) { |
| throw new IllegalArgumentException("id '"+id+"' must be INTEGER/BigDecimal or parseable STRING/String"); |
| } |
| } |
| throw new IllegalArgumentException("id must be INTEGER"); |
| case STRING: |
| if (id instanceof String) { |
| return operativeDtoIdsMap.get(id); |
| } |
| throw new IllegalArgumentException("id must be STRING/String"); |
| default: |
| } |
| return null; |
| } |
| } |