Search in sources :

Example 6 with TargetAddress

use of io.atlasmap.java.test.TargetAddress 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;
}
Also used : TargetFlatPrimitiveClass(io.atlasmap.java.test.TargetFlatPrimitiveClass) TestListOrders(io.atlasmap.java.test.TestListOrders) TargetAddress(io.atlasmap.java.test.TargetAddress) TargetOrder(io.atlasmap.java.test.TargetOrder) TargetTestClass(io.atlasmap.java.test.TargetTestClass) AtlasException(io.atlasmap.api.AtlasException) TargetOrderArray(io.atlasmap.java.test.TargetOrderArray)

Example 7 with TargetAddress

use of io.atlasmap.java.test.TargetAddress 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);
    }
}
Also used : StateEnumClassLong(io.atlasmap.java.test.StateEnumClassLong) TargetFlatPrimitiveClass(io.atlasmap.java.test.TargetFlatPrimitiveClass) TestListOrders(io.atlasmap.java.test.TestListOrders) BaseOrder(io.atlasmap.java.test.BaseOrder) TargetContact(io.atlasmap.java.test.TargetContact) TargetAddress(io.atlasmap.java.test.TargetAddress) TargetOrder(io.atlasmap.java.test.TargetOrder) TargetTestClass(io.atlasmap.java.test.TargetTestClass) AtlasException(io.atlasmap.api.AtlasException) TargetOrderArray(io.atlasmap.java.test.TargetOrderArray)

Example 8 with TargetAddress

use of io.atlasmap.java.test.TargetAddress in project atlasmap by atlasmap.

the class JavaWriterUtilTest method testSetObjectOnParent.

@Test
public void testSetObjectOnParent() throws Exception {
    JavaWriterUtil writerUtil = new JavaWriterUtil(DefaultAtlasConversionService.getInstance());
    reset();
    setupPath("/contact");
    targetTestClassInstance.setContact(null);
    TargetContact testContact = new TargetContact();
    writerUtil.setObjectOnParent(field, lastSegmentContext, targetTestClassInstance, testContact);
    assertTrue(targetTestClassInstance.getContact() == testContact);
    reset();
    setupPath("/address");
    targetTestClassInstance.setAddress(null);
    TargetAddress testAddress = new TargetAddress();
    writerUtil.setObjectOnParent(field, lastSegmentContext, targetTestClassInstance, testAddress);
    assertTrue(targetTestClassInstance.getAddress() == testAddress);
    reset();
    setupPath("/address/addressLine1");
    targetTestClassInstance.getAddress().setAddressLine1(null);
    String addressLine1 = "123 any street";
    writerUtil.setObjectOnParent(field, lastSegmentContext, targetTestClassInstance.getAddress(), addressLine1);
    assertTrue(targetTestClassInstance.getAddress().getAddressLine1() == addressLine1);
    reset();
    setupPath("/listOrders/orders<5>");
    List<TargetOrder> testListOrders = new LinkedList<>();
    targetTestClassInstance.getListOrders().setOrders(null);
    writerUtil.setObjectOnParent(field, lastSegmentContext, targetTestClassInstance.getListOrders(), testListOrders);
    assertTrue(((Object) targetTestClassInstance.getListOrders().getOrders()) == testListOrders);
    reset();
    setupPath("/orderArray/orders[10]");
    targetTestClassInstance.getOrderArray().setOrders(null);
    Object[] testArrayOrders = new TargetOrder[12];
    writerUtil.setObjectOnParent(field, lastSegmentContext, targetTestClassInstance.getOrderArray(), testArrayOrders);
    assertTrue(targetTestClassInstance.getOrderArray().getOrders() == testArrayOrders);
}
Also used : TargetContact(io.atlasmap.java.test.TargetContact) TargetAddress(io.atlasmap.java.test.TargetAddress) TargetOrder(io.atlasmap.java.test.TargetOrder) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 9 with TargetAddress

use of io.atlasmap.java.test.TargetAddress in project atlasmap by atlasmap.

the class BaseDocumentWriterTest method reset.

@Before
public void reset() {
    classLoader = Thread.currentThread().getContextClassLoader();
    writer = new DocumentJavaFieldWriter(conversionService);
    writer.setTargetValueConverter(new TargetValueConverter(classLoader, conversionService) {

        public Object convert(AtlasInternalSession session, LookupTable lookupTable, Field sourceField, Object parentObject, Field targetField) throws AtlasException {
            return targetField.getValue();
        }
    });
    field = null;
    segmentContexts = new LinkedList<>();
    targetTestClassInstance = new TargetTestClass();
    targetTestClassInstance.setContact(new TargetContact());
    targetTestClassInstance.setAddress(new TargetAddress());
    targetOrderListInstance = new TestListOrders();
    targetOrderListInstance.setOrders(new LinkedList<>());
    targetOrderListInstance.getOrders().add(new TargetOrder());
    targetOrderListInstance.getOrders().add(new TargetOrder());
    targetTestClassInstance.setListOrders(targetOrderListInstance);
    targetOrderArrayInstance = new TargetOrderArray();
    targetOrderArrayInstance.setOrders(new BaseOrder[2]);
    targetOrderArrayInstance.getOrders()[0] = new TargetOrder();
    targetOrderArrayInstance.getOrders()[1] = new TargetOrder();
    targetTestClassInstance.setOrderArray(targetOrderArrayInstance);
}
Also used : AtlasInternalSession(io.atlasmap.spi.AtlasInternalSession) TargetContact(io.atlasmap.java.test.TargetContact) TargetAddress(io.atlasmap.java.test.TargetAddress) AtlasException(io.atlasmap.api.AtlasException) TargetOrderArray(io.atlasmap.java.test.TargetOrderArray) Field(io.atlasmap.v2.Field) JavaEnumField(io.atlasmap.java.v2.JavaEnumField) JavaField(io.atlasmap.java.v2.JavaField) TestListOrders(io.atlasmap.java.test.TestListOrders) LookupTable(io.atlasmap.v2.LookupTable) TargetOrder(io.atlasmap.java.test.TargetOrder) TargetTestClass(io.atlasmap.java.test.TargetTestClass) Before(org.junit.Before)

Example 10 with TargetAddress

use of io.atlasmap.java.test.TargetAddress in project atlasmap by atlasmap.

the class JavaConstructServiceComplexArrayTest method testConstructTargetOrderArrayFiltered.

@Test
public void testConstructTargetOrderArrayFiltered() throws Exception {
    Object targetObject = constructService.constructClass(generateOrderArray("Target"), Arrays.asList("orders", "orders/address"));
    assertNotNull(targetObject);
    assertTrue(targetObject instanceof TargetOrderArray);
    TargetOrderArray orderArray = (TargetOrderArray) targetObject;
    assertNotNull(orderArray.getOrders());
    assertTrue(orderArray.getOrders().length > 0);
    assertNull(orderArray.getNumberOrders());
    for (int i = 0; i < orderArray.getOrders().length; i++) {
        TargetOrder order = (TargetOrder) orderArray.getOrders()[i];
        assertNotNull(order.getAddress());
        assertTrue(order.getAddress() instanceof TargetAddress);
        TargetAddress address = (TargetAddress) order.getAddress();
        assertNull(address.getAddressLine1());
        assertNull(address.getAddressLine2());
        assertNull(address.getCity());
        assertNull(address.getState());
        assertNull(address.getZipCode());
        assertNull(order.getContact());
    }
}
Also used : TargetAddress(io.atlasmap.java.test.TargetAddress) TargetOrder(io.atlasmap.java.test.TargetOrder) TargetOrderArray(io.atlasmap.java.test.TargetOrderArray) Test(org.junit.Test)

Aggregations

TargetAddress (io.atlasmap.java.test.TargetAddress)10 TargetOrder (io.atlasmap.java.test.TargetOrder)9 Test (org.junit.Test)7 TargetContact (io.atlasmap.java.test.TargetContact)6 TargetOrderArray (io.atlasmap.java.test.TargetOrderArray)5 AtlasException (io.atlasmap.api.AtlasException)3 TargetTestClass (io.atlasmap.java.test.TargetTestClass)3 TestListOrders (io.atlasmap.java.test.TestListOrders)3 TargetFlatPrimitiveClass (io.atlasmap.java.test.TargetFlatPrimitiveClass)2 JavaClass (io.atlasmap.java.v2.JavaClass)2 BaseOrder (io.atlasmap.java.test.BaseOrder)1 StateEnumClassLong (io.atlasmap.java.test.StateEnumClassLong)1 TargetParentOrder (io.atlasmap.java.test.TargetParentOrder)1 JavaEnumField (io.atlasmap.java.v2.JavaEnumField)1 JavaField (io.atlasmap.java.v2.JavaField)1 AtlasInternalSession (io.atlasmap.spi.AtlasInternalSession)1 Field (io.atlasmap.v2.Field)1 LookupTable (io.atlasmap.v2.LookupTable)1 LinkedList (java.util.LinkedList)1 Before (org.junit.Before)1