use of io.atlasmap.java.v2.JavaField in project atlasmap by atlasmap.
the class OverloadedFieldActionsTest method generateMappingDayOfWeek.
protected AtlasMapping generateMappingDayOfWeek(Class<?> clazz) {
AtlasMapping m = new AtlasMapping();
DataSource s = new DataSource();
s.setDataSourceType(DataSourceType.SOURCE);
s.setUri("atlas:java?className=io.atlasmap.java.test.SourceFlatPrimitiveClass");
DataSource t = new DataSource();
t.setDataSourceType(DataSourceType.TARGET);
t.setUri("atlas:java?className=io.atlasmap.java.test.TargetFlatPrimitiveClass");
m.getDataSource().add(s);
m.getDataSource().add(t);
Mapping mfm = AtlasModelFactory.createMapping(MappingType.MAP);
JavaField srcF = new JavaField();
Actions acts = new Actions();
srcF.setActions(acts);
JavaField tgtF = new JavaField();
if (clazz.isAssignableFrom(String.class)) {
srcF.setPath("boxedStringField");
srcF.getActions().getActions().add(new DayOfWeekString());
tgtF.setPath("boxedStringField");
} else if (clazz.isAssignableFrom(Integer.class)) {
srcF.setPath("intField");
srcF.getActions().getActions().add(new DayOfWeekInteger());
tgtF.setPath("boxedStringField");
}
mfm.getInputField().add(srcF);
mfm.getOutputField().add(tgtF);
Mappings maps = new Mappings();
maps.getMapping().add(mfm);
m.setMappings(maps);
return m;
}
use of io.atlasmap.java.v2.JavaField in project atlasmap by atlasmap.
the class JavaJavaComplexListTest method testGenerateListMappingBasic.
@Test
public void testGenerateListMappingBasic() throws Exception {
AtlasMapping a = AtlasModelFactory.createAtlasMapping();
a.setName("JavaJavaComplexListBase");
DataSource s = new DataSource();
s.setDataSourceType(DataSourceType.SOURCE);
s.setUri("atlas:java?className=io.atlasmap.java.test.SourceOrderList");
DataSource t = new DataSource();
t.setDataSourceType(DataSourceType.TARGET);
t.setUri("atlas:java?className=io.atlasmap.java.test.TargetOrderList");
JavaField f1 = AtlasJavaModelFactory.createJavaField();
f1.setPath("/numberOrders");
f1.setModifiers(null);
JavaField f2 = AtlasJavaModelFactory.createJavaField();
f2.setPath("/orderBatchNumber");
f2.setModifiers(null);
Mapping m1 = AtlasModelFactory.createMapping(MappingType.MAP);
m1.getInputField().add(f1);
m1.getOutputField().add(f1);
Mapping m2 = AtlasModelFactory.createMapping(MappingType.MAP);
m2.getInputField().add(f2);
m2.getOutputField().add(f2);
JavaCollection cm = new JavaCollection();
cm.setMappingType(MappingType.COLLECTION);
cm.setCollectionType(CollectionType.LIST);
JavaField f3 = AtlasJavaModelFactory.createJavaField();
f3.setPath("/orders<>/orderId");
f3.setModifiers(null);
Mapping m3 = AtlasModelFactory.createMapping(MappingType.MAP);
m3.getInputField().add(f3);
m3.getOutputField().add(f3);
if (cm.getMappings() == null) {
cm.setMappings(new Mappings());
}
cm.getMappings().getMapping().add(m3);
a.getDataSource().addAll(Arrays.asList(s, t));
a.getMappings().getMapping().addAll(Arrays.asList(m1, m2, cm));
AtlasMappingService atlasMappingService = new AtlasMappingService(Arrays.asList("io.atlasmap.v2", "io.atlasmap.java.v2", "io.atlasmap.xml.v2"));
File path = new File("target/reference-mappings/javaToJava");
path.mkdirs();
atlasMappingService.saveMappingAsFile(a, new File(path, "atlasmapping-complex-list-autodetect-base.xml"));
}
use of io.atlasmap.java.v2.JavaField in project atlasmap by atlasmap.
the class JavaJavaDateTimeMappingTest method createJavaField.
private static JavaField createJavaField(String path, FieldType type, String className) {
JavaField javaField = AtlasJavaModelFactory.createJavaField();
javaField.setPath(path);
javaField.setModifiers(null);
javaField.setFieldType(type);
javaField.setClassName(className);
return javaField;
}
use of io.atlasmap.java.v2.JavaField in project atlasmap by atlasmap.
the class JavaJavaDateTimeMappingTest method copyJavaField.
private JavaField copyJavaField(JavaField source) {
JavaField javaField = AtlasJavaModelFactory.createJavaField();
javaField.setPath(source.getPath());
javaField.setModifiers(source.getModifiers());
javaField.setFieldType(source.getFieldType());
javaField.setClassName(source.getClassName());
return javaField;
}
use of io.atlasmap.java.v2.JavaField 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);
}
}
Aggregations