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;
}
}
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;
}
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;
}
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.");
}
}
}
}
Aggregations