use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.
the class JavaModule method getCollectionSize.
@SuppressWarnings("rawtypes")
@Override
public int getCollectionSize(AtlasInternalSession session, Field field) throws AtlasException {
// TODO could this use FieldReader?
Object document = session.getSourceDocument(getDocId());
Object collectionObject = ClassHelper.parentObjectForPath(document, new AtlasPath(field.getPath()), false);
if (collectionObject == null) {
throw new AtlasException(String.format("Cannot find collection on sourceObject %s for path: %s", document.getClass().getName(), field.getPath()));
}
if (collectionObject.getClass().isArray()) {
return Array.getLength(collectionObject);
}
return ((List) collectionObject).size();
}
use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.
the class JavaModule method processPreSourceExecution.
@Override
public void processPreSourceExecution(AtlasInternalSession atlasSession) throws AtlasException {
if (atlasSession == null || atlasSession.getMapping() == null || atlasSession.getMapping().getMappings() == null || atlasSession.getMapping().getMappings().getMapping() == null) {
throw new AtlasException("AtlasSession not properly intialized with a mapping that contains field mappings");
}
if (javaInspectionService == null) {
javaInspectionService = new ClassInspectionService();
javaInspectionService.setConversionService(getConversionService());
}
Object sourceDocument = atlasSession.getSourceDocument(getDocId());
if (sourceDocument == null) {
AtlasUtil.addAudit(atlasSession, getDocId(), String.format("Null source document: docId='%s'", getDocId()), null, AuditStatus.WARN, null);
} else {
DocumentJavaFieldReader reader = new DocumentJavaFieldReader();
reader.setConversionService(getConversionService());
reader.setDocument(sourceDocument);
atlasSession.setFieldReader(getDocId(), reader);
}
if (LOG.isDebugEnabled()) {
LOG.debug("{}: processPreSourceExcution completed", getDocId());
}
}
use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.
the class DocumentJavaFieldReader method read.
@Override
public void read(AtlasInternalSession session) throws AtlasException {
try {
Field sourceField = session.head().getSourceField();
Method getter = null;
if (sourceField.getFieldType() == null && (sourceField instanceof JavaField || sourceField instanceof JavaEnumField)) {
getter = resolveGetMethod(sourceDocument, sourceField);
if (getter == null) {
AtlasUtil.addAudit(session, sourceField.getDocId(), String.format("Unable to auto-detect sourceField type path=%s docId=%s", sourceField.getPath(), sourceField.getDocId()), sourceField.getPath(), AuditStatus.WARN, null);
return;
}
Class<?> returnType = getter.getReturnType();
sourceField.setFieldType(conversionService.fieldTypeFromClass(returnType));
if (LOG.isTraceEnabled()) {
LOG.trace("Auto-detected sourceField type p=" + sourceField.getPath() + " t=" + sourceField.getFieldType());
}
}
populateSourceFieldValue(session, sourceField, sourceDocument, getter);
if (LOG.isDebugEnabled()) {
LOG.debug("Processed input field sPath=" + sourceField.getPath() + " sV=" + sourceField.getValue() + " sT=" + sourceField.getFieldType() + " docId: " + sourceField.getDocId());
}
} catch (Exception e) {
throw new AtlasException(e);
}
}
use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.
the class DocumentJavaFieldWriter method getClassForField.
private Class<?> getClassForField(Field field, SegmentContext segmentContext, Object parentObject, boolean unwrapCollectionType) throws AtlasException {
if (LOG.isDebugEnabled()) {
LOG.debug("Looking up class to use for segment: " + segmentContext + "\n\tparentObject: " + parentObject);
}
Class<?> clz = null;
if (LOG.isDebugEnabled()) {
LOG.debug("Looking for configured class for field: " + field + ".");
}
String className = null;
if (field instanceof JavaField) {
className = ((JavaField) field).getClassName();
} else if (field instanceof JavaEnumField) {
className = ((JavaEnumField) field).getClassName();
}
if (className != null) {
try {
clz = className == null ? null : Class.forName(className);
} catch (Exception e) {
throw new AtlasException("Could not find class for '" + className + "', for segment: " + segmentContext + ", on field: " + field, e);
}
}
if (clz == null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Couldn't find class on field. Looking for configured class for segment: " + segmentContext + ".");
}
String normalizedSegment = AtlasPath.removeCollectionIndexes(segmentContext.getSegmentPath());
clz = this.classesForFields.get(normalizedSegment);
}
Type clzType = null;
if (clz == null) {
// attempt to determine it from the parent object.
if (LOG.isDebugEnabled()) {
LOG.debug("Couldn't find configured class for segment: " + segmentContext + ", looking up getter method.");
}
Method m = null;
try {
String methodName = "get" + JavaWriterUtil.capitalizeFirstLetter(AtlasPath.cleanPathSegment(segmentContext.getSegment()));
m = ClassHelper.detectGetterMethod(parentObject.getClass(), methodName);
} catch (NoSuchMethodException e) {
// it's ok, we didnt find a getter.
if (LOG.isDebugEnabled()) {
LOG.debug("Couldn't find getter method for segment: " + segmentContext, e);
}
}
clz = m == null ? null : m.getReturnType();
clzType = m.getGenericReturnType();
}
if (clz == null) {
throw new AtlasException("Could not create object, can't find class to instantiate for segment: " + segmentContext);
}
if (unwrapCollectionType) {
clz = unwrapCollectionType(field, segmentContext, parentObject, clz, clzType);
}
if (LOG.isDebugEnabled()) {
LOG.debug("Found class '" + clz.getName() + "' to use for segment: " + segmentContext);
}
return clz;
}
use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.
the class DocumentJavaFieldWriter method expandCollectionToFitItem.
@SuppressWarnings({ "rawtypes", "unchecked" })
private Object expandCollectionToFitItem(Field field, Object obj, SegmentContext segmentContext, Object parentObject) throws AtlasException {
Object collectionObject = obj;
String segment = segmentContext.getSegment();
if (!collectionHasRoomForIndex(collectionObject, segmentContext)) {
if (LOG.isDebugEnabled()) {
LOG.debug("Collection is not large enough for segment '" + segment + "', expanding the collection.");
}
int index = AtlasPath.indexOfSegment(segment);
if (collectionObject instanceof List) {
List list = (List) collectionObject;
while (list.size() < (index + 1)) {
list.add(null);
}
} else if (collectionObject instanceof Map) {
throw new AtlasException("FIXME: Cannot yet handle adding children to maps");
} else if (collectionObject.getClass().isArray()) {
if (Array.getLength(collectionObject) < (index + 1)) {
// resize the array to fit the item
Object newArray = createObject(field, segmentContext, parentObject, true);
// copy pre-existing items over to new array
for (int i = 0; i < Array.getLength(collectionObject); i++) {
Array.set(newArray, i, Array.get(collectionObject, i));
}
collectionObject = newArray;
}
}
if (LOG.isDebugEnabled()) {
LOG.debug("Finished expanding collection: " + collectionObject);
}
}
return collectionObject;
}
Aggregations