package gw.exportimport uses gw.api.locale.DisplayKey uses java.lang.IllegalArgumentException uses java.util.Collections uses java.util.List /** * Container to hold information about the entity, e.g. all related columns. */ @ReadOnly class EntityInfo { /** * EntityInfo Registry. */ static var _registry : EntityInfoRegistry as readonly Registry = XmlEntityInfoRegistry.Instance var _columns : List as readonly Columns var _parentEntityType : Type as readonly ParentEntityType construct(theColumns : List) { this(theColumns, null) } construct(theColumns : List, theParentEntityType : Type) { /* at most a single action column... */ if ( theColumns.countWhere(\ cInfo -> cInfo.FlagsAction ) > 1 ) { throw new IllegalArgumentException(DisplayKey.get("Import.Configuration.Errors.NonUnaryAction")) } /* at most a single entity identifier column... */ if ( theColumns.Empty or theColumns.countWhere(\ cInfo -> cInfo.FlagsEntityId ) > 1 ) { throw new IllegalArgumentException(DisplayKey.get("Import.Configuration.Errors.NonUnaryId")) } /* parent entity type validation */ if (theParentEntityType != null) { /* must have a single parent entity identifier column... */ var cnt = theColumns.Empty ? 0 : theColumns.countWhere(\ cInfo -> cInfo.FlagsParentEntityId) if (cnt == 0) { throw new IllegalArgumentException(DisplayKey.get("Import.Configuration.Errors.RequiresParentEntityIdColumn", T, theParentEntityType)) } if (cnt > 1) { throw new IllegalArgumentException(DisplayKey.get("Import.Configuration.Errors.NonUnaryId")) } } _columns = Collections.unmodifiableList(theColumns) _parentEntityType = theParentEntityType } /** * The type of this entity. */ property get EntityType() : Type { return T } /** * A List of required ColumnInfo's for this EntityInfo. * * Required columns must be present in an export or import of the entity. */ property get RequiredColumns() : List { return Columns.where(\ colInfo -> isColumnRequired(colInfo)) } /** * A List of optional ColumnInfo's for this EntityInfo. */ property get OptionalColumns() : List { return Columns.where(\ colInfo -> !isColumnRequired(colInfo)) } /** * A List of columns excluded from template for this EntityInfo. */ property get ExcludedFromTemplateColumns() : List { return Columns.where(\ colInfo -> colInfo.ExcludeFromTemplate) } /** * Return whether the specified ColumnInfo is required for input. */ function isColumnRequired(colInfo : ColumnInfo) : boolean { return (colInfo != null) and (colInfo.FlagsEntityId or colInfo.RequiredForImport or colInfo.FlagsParentEntityId) } /** * Return a list of ColumnInfo's for the specified reference entity key. * * Finds and returns a locked copy of the ColumnInfo's for the EntityInfo * that have the matching path prefix with it removed from the path * for each copy. */ function getColumnInfoCopiesLockedFor(key : String) : List { var pathPrefix = key return this.Columns .where(\ cInfo -> cInfo.Path != null and cInfo.Path.startsWith(pathPrefix)) .map(\ cInfo -> {return cInfo.copyLockedWithRootPathRemoved(pathPrefix)}) } //inteded to be used for tests only internal static property set Registry(aRegistry : EntityInfoRegistry) { _registry = aRegistry } }