| /** |
| * |
| * 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; |
| |
| import java.util.ArrayList; |
| import java.util.List; |
| |
| import org.eclipse.osbp.dsl.semantic.entity.LEntity; |
| import org.eclipse.osbp.dsl.semantic.entity.LEntityAttribute; |
| import org.eclipse.osbp.dsl.semantic.entity.LEntityReference; |
| import org.eclipse.osbp.ui.api.datamart.IDataMart; |
| |
| public class DatamartDefinitionUtil { |
| |
| public static String getEntityNameForIdColumnName(String columnName) { |
| if (isEntityIdColumnName(columnName)) { |
| return columnName.substring(IDataMart.DATAMART_ID_COLUMN_PREFIX.length(), columnName.length()-IDataMart.DATAMART_ID_COLUMN_POSTFIX.length()); |
| } |
| else { |
| return null; |
| } |
| } |
| |
| public static boolean isEntityIdColumnName(String columnName) { |
| return columnName.startsWith(IDataMart.DATAMART_ID_COLUMN_PREFIX) && columnName.endsWith(IDataMart.DATAMART_ID_COLUMN_POSTFIX); |
| } |
| |
| public static String getEntityIdColumnName(LEntity entityRef) { |
| LEntityAttribute idAttribute = entityRef.getPrimaryKeyAttribute(); |
| if (idAttribute == null) { |
| return null; |
| } |
| else { |
| return entityRef.getName()+"."+idAttribute.getName(); |
| } |
| } |
| |
| private static boolean hasSuperType(LEntity entityRef) { |
| return entityRef.getSuperType() != null && |
| !entityRef.getSuperType().isMappedSuperclass() && |
| !entityRef.getSuperType().equals(entityRef); |
| } |
| |
| public static String getEntityIdAliasName(LEntity entityRef) { |
| LEntityAttribute idAttribute = entityRef.getPrimaryKeyAttribute(); |
| return getEntityIdAliasName(entityRef, idAttribute); |
| } |
| |
| |
| public static String getEntityIdAliasName(LEntity entityRef, LEntityAttribute idAttribute) { |
| if (idAttribute == null) { |
| return null; |
| } |
| else { |
| if(hasSuperType(entityRef)) { |
| return IDataMart.DATAMART_ID_COLUMN_PREFIX+entityRef.getSuperType().getName()+IDataMart.DATAMART_ID_COLUMN_POSTFIX; |
| } else { |
| return IDataMart.DATAMART_ID_COLUMN_PREFIX+entityRef.getName()+IDataMart.DATAMART_ID_COLUMN_POSTFIX; |
| } |
| } |
| } |
| |
| public static List<LEntityAttribute> getEntityRefIdAttributes(LEntity entity) { |
| List<LEntityAttribute> entityRefIds = new ArrayList<>(); |
| for (LEntityReference ref : entity.getAllReferences()) { |
| LEntity entityRef = ref.getEntity(); |
| for (LEntityAttribute attribute : entityRef.getAllAttributes()) { |
| if (attribute.isId() || attribute.isUuid()) { |
| entityRefIds.add(attribute); |
| } |
| } |
| } |
| return entityRefIds; |
| } |
| |
| public static List<String> getEntityRefIdAliasNames(LEntity entity) { |
| List<String> entityRefIdNames = new ArrayList<>(); |
| for (LEntityReference ref : entity.getAllReferences()) { |
| LEntity entityRef = ref.getOpposite().getEntity(); |
| for (LEntityAttribute attribute : entityRef.getAllAttributes()) { |
| if (attribute.isId() || attribute.isUuid()) { |
| entityRefIdNames.add(getEntityIdAliasName(entityRef, attribute)); |
| } |
| } |
| } |
| return entityRefIdNames; |
| } |
| |
| public static String getEntityIdDatatype(LEntity entityRef) { |
| for (LEntityAttribute attribute : entityRef.getAllAttributes()) { |
| if (attribute.isId() || attribute.isUuid()) { |
| return attribute.getType().getName(); |
| } |
| } |
| return null; |
| } |
| |
| // public static EType getEntityIdEType(LEntity entityRef) { |
| // for (LEntityAttribute attribute : entityRef.getAllAttributes()) { |
| // if (attribute.isId() || attribute.isUuid()) { |
| // return (new DataType()).getBasicType(attribute); |
| // } |
| // } |
| // return null; |
| // } |
| } |