|
| 1 | +package com.metaobjects.identity; |
| 2 | + |
| 3 | +import com.metaobjects.MetaData; |
| 4 | +import com.metaobjects.attr.BooleanAttribute; |
| 5 | +import com.metaobjects.attr.MetaAttribute; |
| 6 | +import com.metaobjects.attr.StringAttribute; |
| 7 | +import com.metaobjects.registry.MetaDataRegistry; |
| 8 | +import org.slf4j.Logger; |
| 9 | +import org.slf4j.LoggerFactory; |
| 10 | + |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.Collections; |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +import static com.metaobjects.object.MetaObject.ATTR_DESCRIPTION; |
| 16 | + |
| 17 | +/** |
| 18 | + * Reference identity — declares that this entity has fields whose value(s) |
| 19 | + * identify an instance of another entity. |
| 20 | + * |
| 21 | + * Paradigm-neutral: maps to SQL foreign key, document linked reference, |
| 22 | + * graph edge target, or OO pointer/reference. The metamodel itself does |
| 23 | + * not commit to a backend implementation. |
| 24 | + * |
| 25 | + * Attributes: |
| 26 | + * - {@code @fields} — column(s) on THIS entity that hold the reference value(s). |
| 27 | + * - {@code @references} — target entity. Bare name (e.g. "Program") resolves to |
| 28 | + * the target's primary identity. Dotted forms ("Program.id" or |
| 29 | + * "Program.fieldA,fieldB") target an explicit field set on that entity. |
| 30 | + * - {@code @enforce} — optional boolean (default true). When true the backend |
| 31 | + * physically enforces the reference (SQL FK constraint, document validation |
| 32 | + * rule, graph edge guarantee). Set false for navigation/typing/codegen-only |
| 33 | + * references that may dangle at the backend level. |
| 34 | + * |
| 35 | + * Parallel to TypeScript's {@code MetaReferenceIdentity} in |
| 36 | + * {@code @metaobjects/metadata}. |
| 37 | + */ |
| 38 | +public class ReferenceIdentity extends MetaIdentity { |
| 39 | + |
| 40 | + private static final Logger log = LoggerFactory.getLogger(ReferenceIdentity.class); |
| 41 | + |
| 42 | + /** |
| 43 | + * Create a reference identity with the specified name. |
| 44 | + * The subType is automatically set to "reference". |
| 45 | + */ |
| 46 | + public ReferenceIdentity(String name) { |
| 47 | + super(SUBTYPE_REFERENCE, name); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Register ReferenceIdentity type with the MetaDataRegistry. |
| 52 | + * Called by IdentityTypesMetaDataProvider during service discovery. |
| 53 | + */ |
| 54 | + public static void registerTypes(MetaDataRegistry registry) { |
| 55 | + registry.registerType(ReferenceIdentity.class, def -> { |
| 56 | + def.type(TYPE_IDENTITY).subType(SUBTYPE_REFERENCE) |
| 57 | + .description("Reference identity — fields on this entity that identify an instance of another entity") |
| 58 | + .inheritsFrom(MetaData.TYPE_METADATA, MetaData.SUBTYPE_BASE); |
| 59 | + |
| 60 | + def.optionalAttributeWithConstraints(ATTR_FIELDS).ofType(StringAttribute.SUBTYPE_STRING).asArray(); |
| 61 | + def.optionalAttributeWithConstraints(ATTR_REFERENCES).ofType(StringAttribute.SUBTYPE_STRING).asSingle(); |
| 62 | + def.optionalAttributeWithConstraints(ATTR_ENFORCE).ofType(BooleanAttribute.SUBTYPE_BOOLEAN).asSingle(); |
| 63 | + def.optionalAttributeWithConstraints(ATTR_DESCRIPTION).ofType(StringAttribute.SUBTYPE_STRING).asSingle(); |
| 64 | + |
| 65 | + // ACCEPTS ANY ATTRIBUTES (for extensibility from service providers) |
| 66 | + def.optionalChild(MetaAttribute.TYPE_ATTR, "*", "*"); |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Raw {@code @references} attr value, unparsed. Null when the attr is absent. |
| 72 | + */ |
| 73 | + public String getReferencesRaw() { |
| 74 | + return hasMetaAttr(ATTR_REFERENCES) ? |
| 75 | + getMetaAttr(ATTR_REFERENCES).getValueAsString() : null; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Target entity name — the segment before the first dot in {@code @references}, |
| 80 | + * or the whole value when bare. Null if the attr is absent. |
| 81 | + */ |
| 82 | + public String getTargetEntity() { |
| 83 | + String raw = getReferencesRaw(); |
| 84 | + if (raw == null) return null; |
| 85 | + int dot = raw.indexOf('.'); |
| 86 | + return dot == -1 ? raw : raw.substring(0, dot); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Target field names parsed from the dotted form. Empty list when {@code @references} |
| 91 | + * is bare (meaning "use the target entity's primary identity"). Multi-element when |
| 92 | + * the dotted form lists comma-separated fields (compound FK). |
| 93 | + */ |
| 94 | + public List<String> getTargetFields() { |
| 95 | + String raw = getReferencesRaw(); |
| 96 | + if (raw == null) return Collections.emptyList(); |
| 97 | + int dot = raw.indexOf('.'); |
| 98 | + if (dot == -1) return Collections.emptyList(); |
| 99 | + String suffix = raw.substring(dot + 1); |
| 100 | + List<String> out = new ArrayList<>(); |
| 101 | + for (String part : suffix.split(",")) { |
| 102 | + String trimmed = part.trim(); |
| 103 | + if (!trimmed.isEmpty()) out.add(trimmed); |
| 104 | + } |
| 105 | + return out; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Whether the reference is physically enforced by the backend. |
| 110 | + * Default true (hard FK constraint emitted). Explicit {@code @enforce: false} |
| 111 | + * marks the reference as logical-only — codegen skips physical constraints. |
| 112 | + */ |
| 113 | + public boolean isEnforced() { |
| 114 | + if (!hasMetaAttr(ATTR_ENFORCE)) return true; |
| 115 | + MetaAttribute attr = getMetaAttr(ATTR_ENFORCE); |
| 116 | + Object value = attr.getValue(); |
| 117 | + if (value instanceof Boolean) return (Boolean) value; |
| 118 | + // String fallback for value coercion edge cases — explicit "false" means soft. |
| 119 | + return !"false".equalsIgnoreCase(attr.getValueAsString()); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public String toString() { |
| 124 | + return String.format("%s[%s:%s]{%s -> %s}%s", |
| 125 | + getClass().getSimpleName(), |
| 126 | + getType(), |
| 127 | + getSubType(), |
| 128 | + getName(), |
| 129 | + getReferencesRaw(), |
| 130 | + isEnforced() ? "" : " [SOFT]"); |
| 131 | + } |
| 132 | +} |
0 commit comments