Search in sources :

Example 21 with AtlasException

use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.

the class JsonFieldWriter method write.

@Override
public void write(AtlasInternalSession session) throws AtlasException {
    Field targetField = session.head().getTargetField();
    if (targetField == null) {
        throw new AtlasException(new IllegalArgumentException("Argument 'jsonField' cannot be null"));
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Field: " + AtlasModelFactory.toString(targetField));
        LOG.debug("Field type=" + targetField.getFieldType() + " path=" + targetField.getPath() + " v=" + targetField.getValue());
    }
    AtlasPath path = new AtlasPath(targetField.getPath());
    String lastSegment = path.getLastSegment();
    ObjectNode parentNode = this.rootNode;
    String parentSegment = null;
    for (String segment : path.getSegments()) {
        if (!segment.equals(lastSegment)) {
            // this is a parent node.
            if (LOG.isDebugEnabled()) {
                LOG.debug("Now processing parent segment: " + segment);
            }
            JsonNode childNode = getChildNode(parentNode, parentSegment, segment);
            if (childNode == null) {
                childNode = createParentNode(parentNode, parentSegment, segment);
            } else if (childNode instanceof ArrayNode) {
                int index = AtlasPath.indexOfSegment(segment);
                ArrayNode arrayChild = (ArrayNode) childNode;
                if (arrayChild.size() < (index + 1)) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Object Array is too small, resizing to accomodate index: " + index + ", current array: " + arrayChild);
                    }
                    // index available
                    while (arrayChild.size() < (index + 1)) {
                        arrayChild.addObject();
                    }
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Object Array after resizing: " + arrayChild);
                    }
                }
                childNode = arrayChild.get(index);
            }
            parentNode = (ObjectNode) childNode;
            parentSegment = segment;
        } else {
            // this is the last segment of the path, write the value
            if (targetField.getFieldType() == FieldType.COMPLEX) {
                createParentNode(parentNode, parentSegment, segment);
                return;
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug("Now processing field value segment: " + segment);
            }
            writeValue(parentNode, parentSegment, segment, targetField);
        }
    }
}
Also used : Field(io.atlasmap.v2.Field) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) AtlasPath(io.atlasmap.core.AtlasPath) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) AtlasException(io.atlasmap.api.AtlasException)

Example 22 with AtlasException

use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.

the class XmlModule method getCollectionSize.

@Override
public int getCollectionSize(AtlasInternalSession session, Field field) throws AtlasException {
    // TODO could this use FieldReader?
    try {
        Object sourceObject = session.getSourceDocument(getDocId());
        Document document = getDocument((String) sourceObject, false);
        Element parentNode = document.getDocumentElement();
        for (SegmentContext sc : new XmlPath(field.getPath()).getSegmentContexts(false)) {
            if (sc.getPrev() == null) {
                // "/XOA/contact<>/firstName", skip.
                continue;
            }
            String childrenElementName = XmlPath.cleanPathSegment(sc.getSegment());
            String namespaceAlias = XmlPath.getNamespace(sc.getSegment());
            if (namespaceAlias != null && !"".equals(namespaceAlias)) {
                childrenElementName = namespaceAlias + ":" + childrenElementName;
            }
            List<Element> children = XmlIOHelper.getChildrenWithName(childrenElementName, parentNode);
            if (children == null || children.isEmpty()) {
                return 0;
            }
            if (XmlPath.isCollectionSegment(sc.getSegment())) {
                return children.size();
            }
            parentNode = children.get(0);
        }
        return 0;
    } catch (Exception e) {
        throw new AtlasException(e);
    }
}
Also used : XmlPath(io.atlasmap.xml.core.XmlPath) SegmentContext(io.atlasmap.core.AtlasPath.SegmentContext) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) AtlasException(io.atlasmap.api.AtlasException) TransformerException(javax.xml.transform.TransformerException) AtlasConversionException(io.atlasmap.api.AtlasConversionException) AtlasValidationException(io.atlasmap.api.AtlasValidationException) AtlasException(io.atlasmap.api.AtlasException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 23 with AtlasException

use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.

the class AtlasEndpoint method onExchange.

@Override
protected void onExchange(Exchange exchange) throws Exception {
    Message incomingMessage = exchange.getIn();
    String newResourceUri = incomingMessage.getHeader(AtlasConstants.ATLAS_RESOURCE_URI, String.class);
    if (newResourceUri != null) {
        incomingMessage.removeHeader(AtlasConstants.ATLAS_RESOURCE_URI);
        log.debug("{} set to {} creating new endpoint to handle exchange", AtlasConstants.ATLAS_RESOURCE_URI, newResourceUri);
        AtlasEndpoint newEndpoint = findOrCreateEndpoint(getEndpointUri(), newResourceUri);
        newEndpoint.onExchange(exchange);
        return;
    }
    AtlasSession atlasSession = getOrCreateAtlasContext(incomingMessage).createSession();
    populateSourceDocuments(exchange, atlasSession);
    getAtlasContext().process(atlasSession);
    List<Audit> errors = new ArrayList<>();
    for (Audit audit : atlasSession.getAudits().getAudit()) {
        switch(audit.getStatus()) {
            case ERROR:
                errors.add(audit);
                break;
            case WARN:
                LOG.warn("{}: docId='{}', path='{}'", audit.getMessage(), audit.getDocId(), audit.getPath());
                break;
            default:
                LOG.info("{}: docId='{}', path='{}'", audit.getMessage(), audit.getDocId(), audit.getPath());
        }
    }
    if (!errors.isEmpty()) {
        StringBuilder buf = new StringBuilder("Errors: ");
        errors.stream().forEach(a -> buf.append(String.format("[%s: docId='%s', path='%s'], ", a.getMessage(), a.getDocId(), a.getPath())));
        throw new AtlasException(buf.toString());
    }
    populateTargetDocuments(atlasSession, exchange);
}
Also used : Audit(io.atlasmap.v2.Audit) Message(org.apache.camel.Message) ArrayList(java.util.ArrayList) AtlasException(io.atlasmap.api.AtlasException) AtlasSession(io.atlasmap.api.AtlasSession)

Example 24 with AtlasException

use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.

the class DefaultAtlasContext method processValidation.

@Override
public void processValidation(AtlasSession userSession) throws AtlasException {
    if (!(userSession instanceof DefaultAtlasSession)) {
        throw new AtlasException(String.format("Unsupported session class '%s'", userSession.getClass().getName()));
    }
    if (!this.equals(userSession.getAtlasContext())) {
        throw new AtlasException("Cannot execute AtlasSession created by the other AtlasContext");
    }
    DefaultAtlasSession session = (DefaultAtlasSession) userSession;
    if (LOG.isDebugEnabled()) {
        LOG.debug("Begin processValidation {}", session == null ? null : session.toString());
    }
    List<Validation> validations = getContextFactory().getValidationService().validateMapping(session.getMapping());
    if (validations != null && !validations.isEmpty()) {
        session.getValidations().getValidation().addAll(validations);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Detected {} core validation notices", validations.size());
    }
    for (AtlasModule module : getSourceModules().values()) {
        module.processPreValidation(session);
    }
    for (AtlasModule module : getTargetModules().values()) {
        module.processPreValidation(session);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("End processValidation {}", session == null ? null : session.toString());
    }
}
Also used : Validation(io.atlasmap.v2.Validation) AtlasModule(io.atlasmap.spi.AtlasModule) AtlasException(io.atlasmap.api.AtlasException)

Example 25 with AtlasException

use of io.atlasmap.api.AtlasException in project atlasmap by atlasmap.

the class DefaultAtlasFieldActionService method processAction.

protected Object processAction(Action action, ActionDetail actionDetail, Object sourceObject) throws AtlasException {
    Object targetObject = null;
    if (actionDetail != null) {
        Object actionObject = null;
        try {
            Class<?> actionClazz = Class.forName(actionDetail.getClassName());
            actionObject = actionClazz.newInstance();
            Method method = null;
            if (actionDetail.getSourceType() != null) {
                switch(actionDetail.getSourceType()) {
                    case ANY:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Object.class);
                        break;
                    case BIG_INTEGER:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, BigInteger.class);
                        break;
                    case BOOLEAN:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Boolean.class);
                        break;
                    case BYTE:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Byte.class);
                        break;
                    case BYTE_ARRAY:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Byte[].class);
                        break;
                    case CHAR:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Character.class);
                        break;
                    case DATE:
                    case DATE_TIME:
                    case DATE_TZ:
                    case TIME_TZ:
                    case DATE_TIME_TZ:
                    case ANY_DATE:
                        if (sourceObject instanceof Calendar) {
                            sourceObject = DateTimeHelper.toZonedDateTime((Calendar) sourceObject);
                        } else if (sourceObject instanceof Date) {
                            sourceObject = DateTimeHelper.toZonedDateTime((Date) sourceObject, null);
                        } else if (sourceObject instanceof LocalDate) {
                            sourceObject = DateTimeHelper.toZonedDateTime((LocalDate) sourceObject, null);
                        } else if (sourceObject instanceof LocalTime) {
                            sourceObject = DateTimeHelper.toZonedDateTime((LocalTime) sourceObject, null);
                        } else if (sourceObject instanceof LocalDateTime) {
                            sourceObject = DateTimeHelper.toZonedDateTime((LocalDateTime) sourceObject, null);
                        } else if (!(sourceObject instanceof ZonedDateTime)) {
                            LOG.warn(String.format("Unsupported sourceObject type=%s in actionClass=%s", sourceObject.getClass(), actionDetail.getClassName()));
                            break;
                        }
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, ZonedDateTime.class);
                        break;
                    case DECIMAL:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, BigDecimal.class);
                        break;
                    case DOUBLE:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Double.class);
                        break;
                    case FLOAT:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Float.class);
                        break;
                    case INTEGER:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Integer.class);
                        break;
                    case LONG:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Long.class);
                        break;
                    case NUMBER:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Number.class);
                        break;
                    case SHORT:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, Short.class);
                        break;
                    case STRING:
                        method = actionClazz.getMethod(actionDetail.getMethod(), Action.class, String.class);
                        break;
                    default:
                        LOG.warn(String.format("Unsupported sourceType=%s in actionClass=%s", actionDetail.getSourceType().value(), actionDetail.getClassName()));
                        break;
                }
            }
            if (method == null) {
                throw new AtlasException(String.format("Unable to locate field action className=%s method=%s sourceType=%s", actionDetail.getClassName(), actionDetail.getMethod(), actionDetail.getSourceType()));
            }
            if (Modifier.isStatic(method.getModifiers())) {
                targetObject = method.invoke(null, action, sourceObject);
            } else {
                targetObject = method.invoke(actionObject, action, sourceObject);
            }
        } catch (Throwable e) {
            throw new AtlasException(String.format("Error processing action %s", actionDetail.getName()), e);
        }
        return targetObject;
    }
    return sourceObject;
}
Also used : LocalDateTime(java.time.LocalDateTime) AtlasFieldAction(io.atlasmap.api.AtlasFieldAction) Action(io.atlasmap.v2.Action) LocalTime(java.time.LocalTime) Calendar(java.util.Calendar) Method(java.lang.reflect.Method) AtlasException(io.atlasmap.api.AtlasException) LocalDate(java.time.LocalDate) Date(java.util.Date) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) BigInteger(java.math.BigInteger) ZonedDateTime(java.time.ZonedDateTime) BigInteger(java.math.BigInteger)

Aggregations

AtlasException (io.atlasmap.api.AtlasException)34 AtlasPath (io.atlasmap.core.AtlasPath)8 Method (java.lang.reflect.Method)7 AtlasConversionException (io.atlasmap.api.AtlasConversionException)6 JavaEnumField (io.atlasmap.java.v2.JavaEnumField)6 JavaField (io.atlasmap.java.v2.JavaField)6 Field (io.atlasmap.v2.Field)6 SegmentContext (io.atlasmap.core.AtlasPath.SegmentContext)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 TargetAddress (io.atlasmap.java.test.TargetAddress)3 TargetOrder (io.atlasmap.java.test.TargetOrder)3 TargetOrderArray (io.atlasmap.java.test.TargetOrderArray)3 TargetTestClass (io.atlasmap.java.test.TargetTestClass)3 TestListOrders (io.atlasmap.java.test.TestListOrders)3 AtlasModule (io.atlasmap.spi.AtlasModule)3 FieldType (io.atlasmap.v2.FieldType)3 LookupTable (io.atlasmap.v2.LookupTable)3 List (java.util.List)3 Element (org.w3c.dom.Element)3 JsonFactory (com.fasterxml.jackson.core.JsonFactory)2