Search in sources :

Example 6 with ComplexTypeMetadata

use of org.talend.mdm.commmon.metadata.ComplexTypeMetadata in project tmdm-common by Talend.

the class ComplexTypeMetadataImpl method _getField.

private static FieldMetadata _getField(ComplexTypeMetadata type, String path) {
    // $NON-NLS-1$
    String fieldName = StringUtils.substringBefore(StringUtils.substringBefore(path, "/"), "[");
    // $NON-NLS-1$
    String remainingPath = StringUtils.substringAfter(path, "/");
    if (type.hasField(fieldName)) {
        FieldMetadata field = type.getField(fieldName);
        if (!remainingPath.isEmpty()) {
            TypeMetadata fieldType = field.getType();
            if (fieldType instanceof ComplexTypeMetadata) {
                return _getField((ComplexTypeMetadata) fieldType, remainingPath);
            } else {
                // Simple type field shouldn't have remaining path, this is dead end.
                return null;
            }
        } else {
            return field;
        }
    } else {
        // Handle xsi:type in XPath query
        if (fieldName.contains("xsi:type")) {
            // $NON-NLS-1$
            // $NON-NLS-1$
            String reusableTypeName = StringUtils.substringAfter(fieldName, "@xsi:type").replace('=', ' ').replace(']', ' ').trim();
            if (reusableTypeName.isEmpty()) {
                // $NON-NLS-1$ //$NON-NLS-2$
                throw new IllegalArgumentException("Reusable type could not be null for fieldName '" + fieldName + "'");
            }
            if (!type.getName().equals(reusableTypeName)) {
                // Look real type in sub types
                boolean foundRealType = false;
                for (TypeMetadata subType : type.getSubTypes()) {
                    if (subType instanceof ComplexTypeMetadata && subType.getName().equals(reusableTypeName)) {
                        type = (ComplexTypeMetadata) subType;
                        foundRealType = true;
                        break;
                    }
                }
                // xsi:type not found, assume type is default field type.
                if (!foundRealType) {
                    LOGGER.error("Type '" + reusableTypeName + "' does not exist. Assuming '" + type.getName() + "' has field type.");
                }
            }
            return _getField(type, path);
        } else {
            // Or implicit assumption on a field accessible from sub types.
            for (ComplexTypeMetadata subType : type.getSubTypes()) {
                FieldMetadata subTypeField = _getField(subType, path);
                if (subTypeField != null) {
                    return subTypeField;
                }
            }
        }
        // Not found
        return null;
    }
}
Also used : ComplexTypeMetadata(org.talend.mdm.commmon.metadata.ComplexTypeMetadata) ComplexTypeMetadata(org.talend.mdm.commmon.metadata.ComplexTypeMetadata)

Example 7 with ComplexTypeMetadata

use of org.talend.mdm.commmon.metadata.ComplexTypeMetadata in project tmdm-common by Talend.

the class ComplexTypeMetadataImpl method copy.

public ComplexTypeMetadata copy() {
    ComplexTypeMetadataImpl copy = new ComplexTypeMetadataImpl(getNamespace(), getName(), allowWrite, denyCreate, hideUsers, physicalDelete, logicalDelete, schematron, primaryKeyInfo, Collections.<FieldMetadata>emptyList(), isInstantiable, isAbstract, workflowAccessRights);
    Collection<FieldMetadata> fields = getFields();
    for (FieldMetadata field : fields) {
        FieldMetadata fieldCopy = field.copy();
        fieldCopy.setContainingType(copy);
        copy.addField(fieldCopy);
    }
    for (TypeMetadata superType : superTypes) {
        copy.addSuperType(superType);
        if (superType instanceof ComplexTypeMetadata) {
            ((ComplexTypeMetadata) superType).registerSubType(copy);
        }
    }
    for (ComplexTypeMetadata subType : subTypes) {
        copy.subTypes.add((ComplexTypeMetadata) subType.copy());
    }
    // Copy key fields
    // Need to clear due to use of addField(...) during field copy.
    copy.keyFields.clear();
    Collection<FieldMetadata> typeKeyFields = getKeyFields();
    for (FieldMetadata typeKeyField : typeKeyFields) {
        FieldMetadata fieldCopy = typeKeyField.copy();
        fieldCopy.setContainingType(copy);
        copy.registerKey(fieldCopy);
    }
    copy.isFrozen = false;
    copy.localeToLabel.putAll(localeToLabel);
    copy.localeToDescription.putAll(localeToDescription);
    if (dataMap != null) {
        copy.dataMap = new HashMap<String, Object>(dataMap);
    }
    copy.usages.addAll(usages);
    return copy;
}
Also used : ComplexTypeMetadata(org.talend.mdm.commmon.metadata.ComplexTypeMetadata) ComplexTypeMetadata(org.talend.mdm.commmon.metadata.ComplexTypeMetadata)

Example 8 with ComplexTypeMetadata

use of org.talend.mdm.commmon.metadata.ComplexTypeMetadata in project tmdm-common by Talend.

the class ComplexTypeMetadataImpl method freeze.

public TypeMetadata freeze() {
    if (isFrozen) {
        return this;
    }
    // Gets fields from super types.
    if (!superTypes.isEmpty()) {
        Collection<FieldMetadata> thisTypeFields = new LinkedList<FieldMetadata>(fieldMetadata.values());
        fieldMetadata.clear();
        List<TypeMetadata> thisSuperTypes = new LinkedList<TypeMetadata>(superTypes);
        superTypes.clear();
        // TODO Type should use declareUsage iso. super type for this!
        if (thisSuperTypes.size() > 1) {
            Iterator<TypeMetadata> iterator = thisSuperTypes.iterator();
            while (iterator.hasNext()) {
                if (iterator.next().isInstantiable() != isInstantiable) {
                    iterator.remove();
                }
            }
        }
        for (TypeMetadata superType : thisSuperTypes) {
            if (isInstantiable() == superType.isInstantiable()) {
                superType = superType.freeze();
                if (superType instanceof ComplexTypeMetadata) {
                    ((ComplexTypeMetadata) superType).registerSubType(this);
                    usages.addAll(((ComplexTypeMetadata) superType).getUsages());
                }
                superTypes.add(superType);
            } else {
                superType = superType.freeze();
            }
            if (superType instanceof ComplexTypeMetadata) {
                ((ComplexTypeMetadata) superType).registerSubType(this);
                Collection<FieldMetadata> superTypeFields = ((ComplexTypeMetadata) superType).getFields();
                for (FieldMetadata superTypeField : superTypeFields) {
                    superTypeField.adopt(this);
                }
            }
        }
        for (FieldMetadata thisTypeField : thisTypeFields) {
            fieldMetadata.put(thisTypeField.getName(), thisTypeField);
        }
    }
    isFrozen = true;
    // Freeze all fields.
    Collection<FieldMetadata> values = new LinkedList<FieldMetadata>(fieldMetadata.values());
    for (FieldMetadata value : values) {
        FieldMetadata frozenFieldDeclaration = value.freeze();
        fieldMetadata.put(value.getName(), frozenFieldDeclaration);
        if (keyFields.containsKey(value.getName()) && !frozenFieldDeclaration.isKey()) {
            frozenFieldDeclaration.promoteToKey();
            FieldMetadata keyField = keyFields.get(value.getName());
            if (!keyField.equals(frozenFieldDeclaration)) {
                registerKey(frozenFieldDeclaration);
            }
        }
    }
    for (Map.Entry<String, FieldMetadata> keyField : keyFields.entrySet()) {
        keyField.setValue(keyField.getValue().freeze());
    }
    // Freeze primary info
    List<FieldMetadata> frozenPrimaryKeyInfo = new LinkedList<FieldMetadata>();
    for (FieldMetadata pkInfo : primaryKeyInfo) {
        FieldMetadata freeze = pkInfo.freeze();
        if (freeze != null) {
            frozenPrimaryKeyInfo.add(freeze);
        }
    }
    primaryKeyInfo = frozenPrimaryKeyInfo;
    // Freeze lookup fields
    List<FieldMetadata> frozenLookupFields = new LinkedList<FieldMetadata>();
    for (FieldMetadata lookupField : lookupFields) {
        FieldMetadata freeze = lookupField.freeze();
        if (freeze != null) {
            frozenLookupFields.add(freeze);
        }
    }
    lookupFields = frozenLookupFields;
    return this;
}
Also used : ComplexTypeMetadata(org.talend.mdm.commmon.metadata.ComplexTypeMetadata) ComplexTypeMetadata(org.talend.mdm.commmon.metadata.ComplexTypeMetadata) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedList(java.util.LinkedList)

Example 9 with ComplexTypeMetadata

use of org.talend.mdm.commmon.metadata.ComplexTypeMetadata in project tmdm-common by Talend.

the class Compare method compareTypesChange.

@SuppressWarnings("unused")
private static void compareTypesChange(MetadataRepository left, MetadataRepository right, DiffResults diffResults) {
    List<TypeMetadata> unusedLeftTypes = new ArrayList<TypeMetadata>();
    List<TypeMetadata> unusedRightTypes = new ArrayList<TypeMetadata>();
    Set<TypeMetadata> complexTypeSet = new HashSet<TypeMetadata>();
    complexTypeSet.addAll(left.getNonInstantiableTypes());
    complexTypeSet.addAll(right.getNonInstantiableTypes());
    for (TypeMetadata ctm : complexTypeSet) {
        if (left.getNonInstantiableType(ctm.getNamespace(), ctm.getName()) != null && right.getNonInstantiableType(ctm.getNamespace(), ctm.getName()) == null) {
            if (left.getNonInstantiableType(ctm.getNamespace(), ctm.getName()) instanceof ComplexTypeMetadata && MetadataUtils.countEntityUsageCount((ComplexTypeMetadata) left.getNonInstantiableType(ctm.getNamespace(), ctm.getName())) > 0) {
                unusedLeftTypes.add(ctm);
            }
        } else if (left.getNonInstantiableType(ctm.getNamespace(), ctm.getName()) == null && right.getNonInstantiableType(ctm.getNamespace(), ctm.getName()) != null) {
            if (right.getNonInstantiableType(ctm.getNamespace(), ctm.getName()) instanceof ComplexTypeMetadata && MetadataUtils.countEntityUsageCount((ComplexTypeMetadata) right.getNonInstantiableType(ctm.getNamespace(), ctm.getName())) > 0) {
                unusedRightTypes.add(ctm);
            }
        }
    }
    if (unusedLeftTypes != null && unusedLeftTypes.size() > 0) {
        for (TypeMetadata tm : unusedLeftTypes) {
            // Right type does not exist
            diffResults.removeChanges.add(new RemoveChange(tm));
            if (LOGGER.isDebugEnabled()) {
                // $NON-NLS-1$//$NON-NLS-2$
                LOGGER.debug("[REMOVED] Type " + tm + " no longer exist.");
            }
        }
    }
    if (unusedRightTypes != null && unusedRightTypes.size() > 0) {
        for (TypeMetadata tm : unusedRightTypes) {
            // Added Right type element (only exist in right, not in left).
            diffResults.addChanges.add(new AddChange(tm));
            if (LOGGER.isDebugEnabled()) {
                // $NON-NLS-1$//$NON-NLS-2$
                LOGGER.debug("[ADDED] " + tm + " was added.");
            }
        }
    }
}
Also used : ComplexTypeMetadata(org.talend.mdm.commmon.metadata.ComplexTypeMetadata) SimpleTypeMetadata(org.talend.mdm.commmon.metadata.SimpleTypeMetadata) TypeMetadata(org.talend.mdm.commmon.metadata.TypeMetadata) ContainedComplexTypeMetadata(org.talend.mdm.commmon.metadata.ContainedComplexTypeMetadata) ArrayList(java.util.ArrayList) ComplexTypeMetadata(org.talend.mdm.commmon.metadata.ComplexTypeMetadata) ContainedComplexTypeMetadata(org.talend.mdm.commmon.metadata.ContainedComplexTypeMetadata) HashSet(java.util.HashSet)

Aggregations

ComplexTypeMetadata (org.talend.mdm.commmon.metadata.ComplexTypeMetadata)9 ArrayList (java.util.ArrayList)4 ContainedComplexTypeMetadata (org.talend.mdm.commmon.metadata.ContainedComplexTypeMetadata)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 ContainedTypeFieldMetadata (org.talend.mdm.commmon.metadata.ContainedTypeFieldMetadata)2 FieldMetadata (org.talend.mdm.commmon.metadata.FieldMetadata)2 SimpleTypeMetadata (org.talend.mdm.commmon.metadata.SimpleTypeMetadata)2 TypeMetadata (org.talend.mdm.commmon.metadata.TypeMetadata)2 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 StringTokenizer (java.util.StringTokenizer)1 XSDAnnotation (org.eclipse.xsd.XSDAnnotation)1 XSDElementDeclaration (org.eclipse.xsd.XSDElementDeclaration)1 EnumerationFieldMetadata (org.talend.mdm.commmon.metadata.EnumerationFieldMetadata)1 MetadataVisitable (org.talend.mdm.commmon.metadata.MetadataVisitable)1 ReferenceFieldMetadata (org.talend.mdm.commmon.metadata.ReferenceFieldMetadata)1 SimpleTypeFieldMetadata (org.talend.mdm.commmon.metadata.SimpleTypeFieldMetadata)1 Element (org.w3c.dom.Element)1