use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.
the class XmlFieldReader method setDocument.
public void setDocument(String docString, boolean namespaced) throws AtlasException {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// this must be done to use namespaces
dbf.setNamespaceAware(namespaced);
DocumentBuilder b = dbf.newDocumentBuilder();
this.document = b.parse(new ByteArrayInputStream(docString.getBytes("UTF-8")));
} catch (Exception e) {
throw new AtlasException(e);
}
}
use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.
the class JavaWriterUtilMock method getObjectFromParent.
/**
* Retrieve a child object (which may be a complex class or collection class)
* from the given parentObject.
*
* @param field
* - provided for convenience, probably not needed here
* @param ParentObject
* - the object to find the child on
* @param segmentContext
* - the segment of the field's path that references the child object
*/
@Override
public Object getObjectFromParent(Field field, Object parentObject, SegmentContext segmentContext) throws AtlasException {
String segment = segmentContext.getSegment();
if (LOG.isDebugEnabled()) {
LOG.debug("Retrieving child '" + segmentContext.getSegmentPath() + "'.\n\tparentObject: " + parentObject);
}
if (parentObject == null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Cannot find child '" + segmentContext.getSegmentPath() + "', parent is null.");
}
return null;
}
// clean up our segment from something like "@addressLine1" to "addressLine1".
// collection segments like "orders[4]" will be cleaned to "orders"
String cleanedSegment = AtlasPath.cleanPathSegment(segmentContext.getSegment());
Object childObject = null;
if (parentObject instanceof TargetAddress && "addressLine1".equals(cleanedSegment)) {
childObject = ((TargetAddress) parentObject).getAddressLine1();
} else if ("orders".equals(cleanedSegment) && parentObject instanceof TestListOrders) {
childObject = ((TestListOrders) parentObject).getOrders();
} else if ("orders".equals(cleanedSegment) && parentObject instanceof TargetOrderArray) {
childObject = ((TargetOrderArray) parentObject).getOrders();
} else if ("address".equals(cleanedSegment) && parentObject instanceof TargetOrder) {
childObject = ((TargetOrder) parentObject).getAddress();
} else if ("address".equals(cleanedSegment) && parentObject instanceof TargetTestClass) {
childObject = ((TargetTestClass) parentObject).getAddress();
} else if ("listOrders".equals(cleanedSegment) && parentObject instanceof TargetTestClass) {
childObject = ((TargetTestClass) parentObject).getListOrders();
} else if ("orderArray".equals(cleanedSegment) && parentObject instanceof TargetTestClass) {
childObject = ((TargetTestClass) parentObject).getOrderArray();
} else if ("contact".equals(cleanedSegment) && parentObject instanceof TargetOrder) {
childObject = ((TargetOrder) parentObject).getContact();
} else if ("numberOrders".equals(cleanedSegment) && parentObject instanceof TargetOrderArray) {
childObject = ((TargetOrderArray) parentObject).getNumberOrders();
} else if ("primitives".equals(cleanedSegment) && parentObject instanceof TargetTestClass) {
childObject = ((TargetTestClass) parentObject).getPrimitives();
} else if ("intArrayField".equals(cleanedSegment) && parentObject instanceof TargetFlatPrimitiveClass) {
childObject = ((TargetFlatPrimitiveClass) parentObject).getIntArrayField();
} else if ("boxedStringArrayField".equals(cleanedSegment) && parentObject instanceof TargetFlatPrimitiveClass) {
childObject = ((TargetFlatPrimitiveClass) parentObject).getBoxedStringArrayField();
} else if ("contact".equals(cleanedSegment) && parentObject instanceof TargetTestClass) {
childObject = ((TargetTestClass) parentObject).getContact();
} else if ("nothing".equals(cleanedSegment) && parentObject instanceof TargetTestClass) {
childObject = null;
} else {
String clz = parentObject.getClass().getSimpleName();
String getter = "get" + cleanedSegment.substring(0, 1).toUpperCase() + cleanedSegment.substring(1);
String fix = "} else if (\"" + cleanedSegment + "\".equals(cleanedSegment) && parentObject instanceof " + clz + ") {\n " + "childObject = ((" + clz + ")parentObject)." + getter + "();";
LOG.error(fix);
throw new AtlasException("Don't know how to handle get object from parent: " + parentObject + ", segment: " + cleanedSegment);
}
// FIXME: Matt, right? an @ here indicates use the getter, or does @ mean access
// member?
boolean useGetter = AtlasPath.isAttributeSegment(segment);
if (useGetter) {
// FIXME: matt, something like this, but with reflection and what not
// childObject = parentObject.getAddressLine1();
} else {
// FIXME: Matt, something lik this, but with reflection
// childObject = parentObject.addressLine1;
}
if (LOG.isDebugEnabled()) {
if (childObject == null) {
LOG.debug("Could not find child object for path: " + segmentContext.getSegmentPath());
} else {
LOG.debug("Found child object for path '" + segmentContext.getSegmentPath() + "': " + childObject);
}
}
// TODO: matt, should we throw an exception here if null?
return childObject;
}
use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.
the class JavaWriterUtilMock method setObjectOnParent.
/**
* Set the given object within the parentObject.
*
* @param field
* - provided if we need it, I don't think we will since we already
* have the value in hand?
* @param segmentContext
* - current segment for the field's path, this will be the last
* segment in the path.
* @param parentObject
* - the object we're setting the value in
* @param childObject
* - the childObject to set
*/
@SuppressWarnings({ "unchecked" })
@Override
public void setObjectOnParent(Field field, SegmentContext segmentContext, Object parentObject, Object childObject) throws AtlasException {
String segment = segmentContext.getSegment();
if (LOG.isDebugEnabled()) {
LOG.debug("Setting object '" + segmentContext.getSegmentPath() + "'.\n\tchildObject: " + childObject + "\n\tparentObject: " + parentObject);
}
// now the cleanedSegment is a cleaned name such as "addressLine1"
String cleanedSegment = AtlasPath.cleanPathSegment(segment);
if ("addressLine1".equals(cleanedSegment) && parentObject instanceof TargetAddress) {
((TargetAddress) parentObject).setAddressLine1((String) childObject);
} else if ("addressLine1".equals(cleanedSegment) && parentObject instanceof TargetAddress) {
((TargetAddress) parentObject).setAddressLine1((String) childObject);
} else if ("addressLine2".equals(cleanedSegment) && parentObject instanceof TargetAddress) {
((TargetAddress) parentObject).setAddressLine2((String) childObject);
} else if ("city".equals(cleanedSegment) && parentObject instanceof TargetAddress) {
((TargetAddress) parentObject).setCity((String) childObject);
} else if ("state".equals(cleanedSegment) && parentObject instanceof TargetAddress) {
((TargetAddress) parentObject).setState((String) childObject);
} else if ("zipCode".equals(cleanedSegment) && parentObject instanceof TargetAddress) {
((TargetAddress) parentObject).setZipCode((String) childObject);
} else if ("orders".equals(cleanedSegment) && parentObject instanceof TestListOrders) {
((TestListOrders) parentObject).setOrders((List<BaseOrder>) childObject);
} else if ("orders".equals(cleanedSegment) && parentObject instanceof TargetOrderArray) {
((TargetOrderArray) parentObject).setOrders((TargetOrder[]) childObject);
} else if ("address".equals(cleanedSegment) && parentObject instanceof TargetOrder) {
((TargetOrder) parentObject).setAddress((TargetAddress) childObject);
} else if ("name".equals(cleanedSegment) && parentObject instanceof TargetTestClass) {
((TargetTestClass) parentObject).setName((String) childObject);
} else if ("address".equals(cleanedSegment) && parentObject instanceof TargetTestClass) {
((TargetTestClass) parentObject).setAddress((TargetAddress) childObject);
} else if ("listOrders".equals(cleanedSegment) && parentObject instanceof TargetTestClass) {
((TargetTestClass) parentObject).setListOrders((TestListOrders) childObject);
} else if ("orderId".equals(cleanedSegment) && parentObject instanceof TargetOrder) {
((TargetOrder) parentObject).setOrderId((Integer) childObject);
} else if ("orderArray".equals(cleanedSegment) && parentObject instanceof TargetTestClass) {
((TargetTestClass) parentObject).setOrderArray((TargetOrderArray) childObject);
} else if ("numberOrders".equals(cleanedSegment) && parentObject instanceof TargetOrderArray) {
((TargetOrderArray) parentObject).setNumberOrders((Integer) childObject);
} else if ("contact".equals(cleanedSegment) && parentObject instanceof TargetOrder) {
((TargetOrder) parentObject).setContact((TargetContact) childObject);
} else if ("firstName".equals(cleanedSegment) && parentObject instanceof TargetContact) {
((TargetContact) parentObject).setFirstName((String) childObject);
} else if ("lastName".equals(cleanedSegment) && parentObject instanceof TargetContact) {
((TargetContact) parentObject).setLastName((String) childObject);
} else if ("primitives".equals(cleanedSegment) && parentObject instanceof TargetTestClass) {
((TargetTestClass) parentObject).setPrimitives((TargetFlatPrimitiveClass) childObject);
} else if ("intArrayField".equals(cleanedSegment) && parentObject instanceof TargetFlatPrimitiveClass) {
((TargetFlatPrimitiveClass) parentObject).setIntArrayField((int[]) childObject);
} else if ("boxedStringArrayField".equals(cleanedSegment) && parentObject instanceof TargetFlatPrimitiveClass) {
((TargetFlatPrimitiveClass) parentObject).setBoxedStringArrayField((String[]) childObject);
} else if ("statesLong".equals(cleanedSegment) && parentObject instanceof TargetTestClass) {
((TargetTestClass) parentObject).setStatesLong((StateEnumClassLong) childObject);
} else {
String clz = parentObject.getClass().getSimpleName();
String clz2 = childObject.getClass().getSimpleName();
String setter = "set" + cleanedSegment.substring(0, 1).toUpperCase() + cleanedSegment.substring(1);
String fix = "} else if (\"" + cleanedSegment + "\".equals(cleanedSegment) && parentObject instanceof " + clz + ") {\n " + "((" + clz + ") parentObject)." + setter + "((" + clz2 + ")childObject);";
LOG.error(fix);
throw new AtlasException("FIX: " + fix);
}
if (LOG.isDebugEnabled()) {
LOG.debug("Object after value written: " + parentObject);
}
}
use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.
the class DocumentJavaFieldWriter method write.
public void write(AtlasInternalSession session) throws AtlasException {
LookupTable lookupTable = session.head().getLookupTable();
Field sourceField = session.head().getSourceField();
Field targetField = session.head().getTargetField();
try {
if (targetField == null) {
throw new AtlasException(new IllegalArgumentException("Argument 'field' cannot be null"));
}
String targetFieldClassName = (targetField instanceof JavaField) ? ((JavaField) targetField).getClassName() : ((JavaEnumField) targetField).getClassName();
if (LOG.isDebugEnabled()) {
LOG.debug("Now processing field: " + targetField);
LOG.debug("Field type: " + targetField.getFieldType());
LOG.debug("Field path: " + targetField.getPath());
LOG.debug("Field value: " + targetField.getValue());
LOG.debug("Field className: " + targetFieldClassName);
}
processedPaths.add(targetField.getPath());
AtlasPath path = new AtlasPath(targetField.getPath());
Object parentObject = rootObject;
boolean segmentIsComplexSegment = true;
for (SegmentContext segmentContext : path.getSegmentContexts(true)) {
if (LOG.isDebugEnabled()) {
LOG.debug("Now processing segment: " + segmentContext);
LOG.debug("Parent object is currently: " + writeDocumentToString(false, parentObject));
}
if ("/".equals(segmentContext.getSegmentPath())) {
if (rootObject == null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Creating root node: " + segmentContext);
}
rootObject = createParentObject(targetField, parentObject, segmentContext);
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("Root node already exists, skipping segment: " + segmentContext);
}
}
parentObject = rootObject;
continue;
}
// if we're on the last segment, the
boolean segmentIsLastSegment = (segmentContext.getNext() == null);
if (segmentIsLastSegment) {
// detect field type from class name if exists
if (targetField.getFieldType() == null && targetFieldClassName != null && (targetField instanceof JavaField)) {
FieldType fieldTypeFromClass = conversionService.fieldTypeFromClass(targetFieldClassName);
targetField.setFieldType(fieldTypeFromClass);
}
if (FieldType.COMPLEX.equals(targetField.getFieldType())) {
segmentIsComplexSegment = true;
} else {
segmentIsComplexSegment = false;
}
if (targetField instanceof JavaEnumField) {
segmentIsComplexSegment = false;
}
}
if (LOG.isDebugEnabled()) {
if (segmentIsComplexSegment) {
LOG.debug("Now processing complex segment: " + segmentContext);
} else if (targetField instanceof JavaEnumField) {
LOG.debug("Now processing field enum value segment: " + segmentContext);
} else {
LOG.debug("Now processing field value segment: " + segmentContext);
}
}
if (segmentIsComplexSegment) {
// processing parent object
Object childObject = findChildObject(targetField, segmentContext, parentObject);
if (childObject == null) {
childObject = createParentObject(targetField, parentObject, segmentContext);
}
parentObject = childObject;
} else {
// processing field value
if (AtlasPath.isCollectionSegment(segmentContext.getSegment())) {
parentObject = findOrCreateOrExpandParentCollectionObject(targetField, parentObject, segmentContext);
}
Object value = converter.convert(session, lookupTable, sourceField, parentObject, targetField);
addChildObject(targetField, segmentContext, parentObject, value);
}
}
} catch (Throwable t) {
if (LOG.isDebugEnabled()) {
LOG.debug("Error occured while writing field: " + targetField.getPath(), t);
}
if (t instanceof AtlasException) {
throw (AtlasException) t;
}
throw new AtlasException(t);
}
}
use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.
the class TargetValueConverter method convert.
public Object convert(AtlasInternalSession session, LookupTable lookupTable, Field sourceField, Object parentObject, Field targetField) throws AtlasException {
FieldType sourceType = sourceField.getFieldType();
Object sourceValue = sourceField.getValue();
Object targetValue = null;
FieldType targetType = targetField.getFieldType();
if (LOG.isDebugEnabled()) {
LOG.debug("processTargetMapping iPath=" + sourceField.getPath() + " iV=" + sourceValue + " iT=" + sourceType + " oPath=" + targetField.getPath() + " docId: " + targetField.getDocId());
}
if (sourceValue == null) {
// TODO: Finish targetValue = null processing
AtlasUtil.addAudit(session, targetField.getDocId(), String.format("Null sourceValue for targetDocId=%s, targetPath=%s", targetField.getDocId(), targetField.getPath()), targetField.getPath(), AuditStatus.WARN, sourceValue != null ? sourceValue.toString() : null);
return null;
}
String targetClassName = (targetField instanceof JavaField) ? ((JavaField) targetField).getClassName() : null;
targetClassName = (targetField instanceof JavaEnumField) ? ((JavaEnumField) targetField).getClassName() : targetClassName;
if (targetType == null || targetClassName == null) {
try {
Method setter = resolveTargetSetMethod(parentObject, targetField, null);
if (setter != null && setter.getParameterCount() == 1) {
if (targetField instanceof JavaField) {
((JavaField) targetField).setClassName(setter.getParameterTypes()[0].getName());
} else if (targetField instanceof JavaEnumField) {
((JavaEnumField) targetField).setClassName(setter.getParameterTypes()[0].getName());
}
targetType = conversionService.fieldTypeFromClass(setter.getParameterTypes()[0]);
targetField.setFieldType(targetType);
if (LOG.isTraceEnabled()) {
LOG.trace("Auto-detected targetType as {} for class={} path={}", targetType, parentObject.toString(), targetField.getPath());
}
}
} catch (Exception e) {
LOG.debug("Unable to auto-detect targetType for class={} path={}", parentObject.toString(), targetField.getPath());
}
}
if (sourceField instanceof JavaEnumField || targetField instanceof JavaEnumField) {
if (!(sourceField instanceof JavaEnumField) || !(targetField instanceof JavaEnumField)) {
AtlasUtil.addAudit(session, targetField.getDocId(), String.format("Value conversion between enum fields and non-enum fields is not yet supported: sourceType=%s targetType=%s targetPath=%s msg=%s", sourceType, targetType, targetField.getPath()), targetField.getPath(), AuditStatus.ERROR, sourceValue != null ? sourceValue.toString() : null);
}
return populateEnumValue(session, lookupTable, (JavaEnumField) sourceField, (JavaEnumField) targetField);
}
Class<?> targetClazz = null;
if (targetClassName == null) {
if (targetType != null) {
targetClazz = conversionService.classFromFieldType(targetType);
} else {
AtlasUtil.addAudit(session, targetField.getDocId(), String.format("Target field doesn't have fieldType nor className: automatic conversion won't work: targetPath=%s", targetField.getPath()), targetField.getPath(), AuditStatus.WARN, sourceValue != null ? sourceValue.toString() : null);
}
} else if (conversionService.isPrimitive(targetClassName)) {
targetClazz = conversionService.boxOrUnboxPrimitive(targetClassName);
} else {
try {
targetClazz = classLoader.loadClass(targetClassName);
} catch (ClassNotFoundException e) {
AtlasUtil.addAudit(session, targetField.getDocId(), String.format("Target field class '%s' was not found: sourceType=%s targetType=%s targetPath=%s msg=%s", ((JavaField) targetField).getClassName(), sourceType, targetType, targetField.getPath(), e.getMessage()), targetField.getPath(), AuditStatus.ERROR, targetValue != null ? targetValue.toString() : null);
return null;
}
}
if (targetClazz != null) {
targetValue = conversionService.convertType(sourceValue, null, targetClazz, null);
} else {
targetValue = sourceValue;
}
AtlasFieldActionService fieldActionService = session.getAtlasContext().getContextFactory().getFieldActionService();
try {
targetValue = fieldActionService.processActions(targetField.getActions(), targetValue, targetType);
if (targetValue != null) {
if (targetClazz != null) {
targetValue = conversionService.convertType(targetValue, null, targetClazz, null);
} else {
FieldType conversionInputType = conversionService.fieldTypeFromClass(targetValue.getClass());
targetValue = conversionService.convertType(targetValue, conversionInputType, targetType);
}
}
} catch (AtlasConversionException e) {
AtlasUtil.addAudit(session, targetField.getDocId(), String.format("Unable to auto-convert for sourceType=%s targetType=%s targetPath=%s msg=%s", sourceType, targetType, targetField.getPath(), e.getMessage()), targetField.getPath(), AuditStatus.ERROR, targetValue != null ? targetValue.toString() : null);
return null;
}
return targetValue;
}
Aggregations