use of io.atlasmap.v2.Collection in project atlasmap by atlasmap.
the class DefaultAtlasContext method extractCollectionMappings.
private List<Mapping> extractCollectionMappings(DefaultAtlasSession session, BaseMapping baseMapping) throws AtlasException {
if (LOG.isDebugEnabled()) {
LOG.debug("Generating Source Mappings from mapping: {}", baseMapping);
}
if (!baseMapping.getMappingType().equals(MappingType.COLLECTION)) {
if (LOG.isDebugEnabled()) {
LOG.debug("Mapping is not a collection mapping, not cloning: {}", baseMapping);
}
return Arrays.asList((Mapping) baseMapping);
}
List<Mapping> mappings = new LinkedList<>();
for (BaseMapping m : ((Collection) baseMapping).getMappings().getMapping()) {
Mapping mapping = (Mapping) m;
Field sourceField = mapping.getInputField().get(0);
boolean sourceIsCollection = AtlasPath.isCollection(sourceField.getPath());
if (!sourceIsCollection) {
// just copy it over
if (LOG.isDebugEnabled()) {
LOG.debug("Internal mapping's source field is not a collection, not cloning: {}", mapping);
}
// output object to be created for our copied firstName value
for (Field f : mapping.getOutputField()) {
f.setPath(AtlasPath.overwriteCollectionIndex(f.getPath(), 0));
}
mappings.add(mapping);
continue;
}
AtlasModule module = resolveModule(FieldDirection.SOURCE, sourceField);
int sourceCollectionSize = module.getCollectionSize(session, sourceField);
if (LOG.isDebugEnabled()) {
LOG.debug("Internal mapping's source field is a collection. Cloning it for each item ({} clones): {}", sourceCollectionSize, mapping);
}
for (int i = 0; i < sourceCollectionSize; i++) {
Mapping cloneMapping = (Mapping) AtlasModelFactory.cloneMapping(mapping, false);
for (Field f : mapping.getInputField()) {
Field clonedField = module.cloneField(f);
clonedField.setPath(AtlasPath.overwriteCollectionIndex(clonedField.getPath(), i));
cloneMapping.getInputField().add(clonedField);
}
for (Field f : mapping.getOutputField()) {
Field clonedField = module.cloneField(f);
if (AtlasPath.isCollection(clonedField.getPath())) {
clonedField.setPath(AtlasPath.overwriteCollectionIndex(clonedField.getPath(), i));
}
cloneMapping.getOutputField().add(clonedField);
}
mappings.add(cloneMapping);
}
}
if (LOG.isDebugEnabled()) {
LOG.debug("Generated {} mappings from mapping: {}", mappings.size(), baseMapping);
}
((Collection) baseMapping).getMappings().getMapping().clear();
((Collection) baseMapping).getMappings().getMapping().addAll(mappings);
return mappings;
}
use of io.atlasmap.v2.Collection in project atlasmap by atlasmap.
the class SchemaInspector method printAttributes.
private void printAttributes(XSComplexType xsComplexType, String rootName, XmlComplexType xmlComplexType) {
Collection<? extends XSAttributeUse> c = xsComplexType.getDeclaredAttributeUses();
for (XSAttributeUse aC : c) {
XmlField xmlField = AtlasXmlModelFactory.createXmlField();
XSAttributeDecl attributeDecl = aC.getDecl();
xmlField.setName(getNameNS(attributeDecl));
if (attributeDecl.getDefaultValue() != null) {
xmlField.setValue(attributeDecl.getDefaultValue().value);
} else if (attributeDecl.getFixedValue() != null) {
xmlField.setValue(attributeDecl.getFixedValue().value);
}
xmlField.setPath(rootName + "/" + "@" + getNameNS(attributeDecl));
FieldType attrType = getFieldType(attributeDecl.getType().getName());
xmlField.setFieldType(attrType);
if (xmlField.getFieldType() == null) {
// check the simple types in the schema....
XSSimpleType simpleType = xsComplexType.getRoot().getSimpleType(xsComplexType.getTargetNamespace(), attributeDecl.getType().getName());
if (simpleType != null) {
FieldType fieldType = getFieldType(simpleType.getBaseType().getName());
xmlField.setFieldType(fieldType);
xmlField.setTypeName(attributeDecl.getType().getName());
if (simpleType.asRestriction() != null) {
mapRestrictions(xmlField, simpleType.asRestriction());
}
} else {
// cannot figure it out....
xmlField.setFieldType(FieldType.UNSUPPORTED);
}
}
xmlComplexType.getXmlFields().getXmlField().add(xmlField);
}
}
use of io.atlasmap.v2.Collection in project atlasmap by atlasmap.
the class DocumentJavaFieldReader method extractFromCollection.
private Object extractFromCollection(AtlasInternalSession session, Object source, AtlasPath atlasPath) {
if (source == null) {
return null;
}
String lastSegment = atlasPath.getLastSegment();
if (!AtlasPath.isCollectionSegment(lastSegment) || !atlasPath.isIndexedCollection()) {
return source;
}
Integer index = atlasPath.getCollectionIndex(atlasPath.getLastSegment());
if (AtlasPath.isArraySegment(lastSegment)) {
return Array.get(source, index);
} else if (AtlasPath.isListSegment(lastSegment)) {
return Collection.class.cast(source).toArray()[index];
} else if (AtlasPath.isMapSegment(lastSegment)) {
// TODO support map key
String key = index.toString();
return Map.class.cast(source).get(key);
} else {
Field sourceField = session.head().getSourceField();
AtlasUtil.addAudit(session, sourceField.getDocId(), String.format("Ignoring unknown collection type in path '%s'", sourceField.getPath()), sourceField.getPath(), AuditStatus.WARN, null);
return source;
}
}
use of io.atlasmap.v2.Collection in project atlasmap by atlasmap.
the class JavaFieldWriterUtil method resolveCollectionItemClass.
/**
* Resolves collection item class.
* @param parentObject parent
* @param segmentContext segment
* @return class
* @throws AtlasException unexpected error
*/
public Class<?> resolveCollectionItemClass(Object parentObject, SegmentContext segmentContext) throws AtlasException {
Class<?> itemType = null;
Method getterMethod = resolveGetterMethod(parentObject.getClass(), segmentContext.getName());
try {
Type genericType = null;
if (getterMethod != null) {
genericType = getterMethod.getGenericReturnType();
} else {
java.lang.reflect.Field field = resolveField(parentObject.getClass(), segmentContext.getName());
if (field == null) {
throw new AtlasException(String.format("Failed to create a collection item, parent class={}, field name={}", parentObject.getClass(), segmentContext.getName()));
}
genericType = field.getGenericType();
}
if (genericType instanceof Class) {
if (((Class<?>) genericType).isArray()) {
itemType = ((Class<?>) genericType).getComponentType();
} else {
itemType = Object.class;
}
} else if (genericType instanceof ParameterizedType && ((ParameterizedType) genericType).getActualTypeArguments().length > 0) {
String typeArg = ((ParameterizedType) genericType).getActualTypeArguments()[0].getTypeName();
itemType = classLoader.loadClass(typeArg);
} else {
itemType = Object.class;
}
} catch (Throwable t) {
throw new AtlasException(String.format("Failed to resolve collection item class, parent class={}, field name={}", parentObject.getClass(), segmentContext.getName()), t);
}
return itemType;
}
use of io.atlasmap.v2.Collection in project atlasmap by atlasmap.
the class JavaFieldWriter method prepareParentObject.
/**
* Prepares the parent object.
* @param session session
* @return prepared
* @throws AtlasException unexpected error
*/
public Object prepareParentObject(AtlasInternalSession session) throws AtlasException {
Field targetField = session.head().getTargetField();
if (targetField == null) {
throw new AtlasException("Target field cannot be null");
}
try {
AtlasPath path = new AtlasPath(targetField.getPath());
if (path.isRoot()) {
return null;
}
if (rootObject == null) {
throw new IllegalArgumentException("A root object must be set before process");
}
SegmentContext rootSegment = path.getRootSegment();
Object parentObject = this.rootObject;
if (rootSegment.getCollectionType() != CollectionType.NONE) {
if (collectionItemClass == null) {
throw new AtlasException(String.format("Collection item class must be specified to handle topmost collection, path=", path.toString()));
}
if (rootSegment.getCollectionIndex() == null) {
// Cannot proceed from collection segment without index
return null;
}
parentObject = writerUtil.getCollectionItem(rootObject, rootSegment);
if (parentObject == null) {
this.rootObject = writerUtil.adjustCollectionSize(this.rootObject, rootSegment);
parentObject = writerUtil.createComplexCollectionItem(this.rootObject, collectionItemClass, rootSegment);
}
}
for (SegmentContext segmentContext : path.getSegments(false)) {
if (LOG.isDebugEnabled()) {
LOG.debug("Now processing segment: " + segmentContext);
LOG.debug("Parent object is currently: " + writeDocumentToString(false, parentObject));
}
if (segmentContext == path.getLastSegment()) {
return parentObject;
}
Object childObject = writerUtil.getChildObject(parentObject, segmentContext);
if (childObject == null) {
childObject = writerUtil.createComplexChildObject(parentObject, segmentContext);
}
if (segmentContext.getCollectionType() != CollectionType.NONE) {
if (segmentContext.getCollectionIndex() == null) {
// Cannot proceed from collection segment without index
return null;
}
Object item = writerUtil.getCollectionItem(childObject, segmentContext);
if (item == null) {
Object adjusted = writerUtil.adjustCollectionSize(childObject, segmentContext);
if (adjusted != childObject) {
writerUtil.setChildObject(parentObject, adjusted, segmentContext);
}
item = writerUtil.createComplexCollectionItem(parentObject, adjusted, segmentContext);
}
childObject = item;
}
parentObject = childObject;
}
return null;
} catch (Throwable t) {
if (LOG.isDebugEnabled()) {
LOG.debug("Error occured while preparing parent object for: " + targetField.getPath(), t);
}
if (t instanceof AtlasException) {
throw (AtlasException) t;
}
throw new AtlasException(t);
}
}
Aggregations