Search in sources :

Example 1 with AtlasException

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

the class DefaultAtlasContext method process.

/**
 * Process session lifecycle
 */
@Override
public void process(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 process {}", (session == null ? null : session.toString()));
    }
    session.head().unset();
    session.getAudits().getAudit().clear();
    session.getValidations().getValidation().clear();
    processValidation(session);
    for (Validation v : session.getValidations().getValidation()) {
        AtlasUtil.addAudit(session, v);
    }
    if (session.hasErrors()) {
        if (LOG.isDebugEnabled()) {
            LOG.error("Aborting due to {} errors in pre-validation", session.errorCount());
        }
        return;
    }
    for (AtlasModule module : getSourceModules().values()) {
        module.processPreSourceExecution(session);
    }
    for (AtlasModule module : getTargetModules().values()) {
        module.processPreTargetExecution(session);
    }
    if (session.hasErrors()) {
        if (LOG.isDebugEnabled()) {
            LOG.error("Aborting due to {} errors in pre-execution", session.errorCount());
        }
        return;
    }
    for (BaseMapping baseMapping : session.getMapping().getMappings().getMapping()) {
        for (Mapping mapping : extractCollectionMappings(session, baseMapping)) {
            session.head().setMapping(mapping).setLookupTable(lookupTables.get(mapping.getLookupTableName()));
            if (mapping.getOutputField() == null || mapping.getOutputField().isEmpty()) {
                AtlasUtil.addAudit(session, null, String.format("Mapping does not contain at least one output field: alias=%s desc=%s", mapping.getAlias(), mapping.getDescription()), null, AuditStatus.WARN, null);
                continue;
            }
            if (mapping.getInputField() == null || mapping.getInputField().isEmpty()) {
                AtlasUtil.addAudit(session, null, String.format("Mapping does not contain at least one source field: alias=%s desc=%s", mapping.getAlias(), mapping.getDescription()), null, AuditStatus.WARN, null);
            } else {
                processSourceFieldMappings(session, mapping.getInputField());
            }
            processTargetFieldMappings(session, mapping);
        }
    }
    for (AtlasModule module : getSourceModules().values()) {
        module.processPostValidation(session);
    }
    for (AtlasModule module : getTargetModules().values()) {
        module.processPostValidation(session);
    }
    for (AtlasModule module : getSourceModules().values()) {
        module.processPostSourceExecution(session);
    }
    for (AtlasModule module : getTargetModules().values()) {
        module.processPostTargetExecution(session);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("End process {}", session == null ? null : session.toString());
    }
}
Also used : Validation(io.atlasmap.v2.Validation) AtlasModule(io.atlasmap.spi.AtlasModule) BaseMapping(io.atlasmap.v2.BaseMapping) Mapping(io.atlasmap.v2.Mapping) AtlasMapping(io.atlasmap.v2.AtlasMapping) AtlasException(io.atlasmap.api.AtlasException) BaseMapping(io.atlasmap.v2.BaseMapping)

Example 2 with AtlasException

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

the class DefaultAtlasContext method init.

/**
 * TODO: For dynamic re-load. This needs lock()
 *
 * @throws AtlasException
 */
protected void init() throws AtlasException {
    registerJmx(this);
    if (this.atlasMappingUri != null) {
        this.mappingDefinition = factory.getMappingService().loadMapping(this.atlasMappingUri, atlasMappingFormat);
    }
    sourceModules.clear();
    ConstantModule constant = new ConstantModule();
    constant.setConversionService(factory.getConversionService());
    constant.setFieldActionService(factory.getFieldActionService());
    sourceModules.put(CONSTANTS_DOCUMENT_ID, constant);
    PropertyModule propSource = new PropertyModule(factory.getPropertyStrategy());
    propSource.setMode(AtlasModuleMode.SOURCE);
    propSource.setConversionService(factory.getConversionService());
    propSource.setFieldActionService(factory.getFieldActionService());
    sourceModules.put(PROPERTIES_DOCUMENT_ID, propSource);
    targetModules.clear();
    lookupTables.clear();
    if (mappingDefinition.getLookupTables() != null && mappingDefinition.getLookupTables().getLookupTable() != null) {
        for (LookupTable table : mappingDefinition.getLookupTables().getLookupTable()) {
            lookupTables.put(table.getName(), table);
        }
    }
    AtlasModuleInfoRegistry moduleInfoRegistry = factory.getModuleInfoRegistry();
    for (DataSource ds : mappingDefinition.getDataSource()) {
        AtlasModuleInfo moduleInfo = moduleInfoRegistry.lookupByUri(ds.getUri());
        if (moduleInfo == null) {
            LOG.error("Cannot find module info for the DataSource uri '{}'", ds.getUri());
            continue;
        }
        if (ds.getDataSourceType() != DataSourceType.SOURCE && ds.getDataSourceType() != DataSourceType.TARGET) {
            LOG.error("Unsupported DataSource type '{}'", ds.getDataSourceType());
            continue;
        }
        String docId = ds.getId();
        if (docId == null || docId.isEmpty()) {
            docId = ds.getDataSourceType() == DataSourceType.SOURCE ? AtlasConstants.DEFAULT_SOURCE_DOCUMENT_ID : AtlasConstants.DEFAULT_TARGET_DOCUMENT_ID;
        }
        if (ds.getDataSourceType() == DataSourceType.SOURCE && sourceModules.containsKey(docId)) {
            LOG.error("Duplicated {} DataSource ID '{}' was detected, ignoring...", ds.getDataSourceType(), ds.getId());
            continue;
        }
        if (ds.getDataSourceType() == DataSourceType.TARGET && targetModules.containsKey(docId)) {
            LOG.error("Duplicated {} DataSource ID '{}' was detected, ignoring...", ds.getDataSourceType(), docId);
            continue;
        }
        try {
            AtlasModule module = moduleInfo.getModuleClass().newInstance();
            module.setConversionService(factory.getConversionService());
            module.setFieldActionService(factory.getFieldActionService());
            module.setUri(ds.getUri());
            if (ds.getDataSourceType() == DataSourceType.SOURCE) {
                module.setMode(AtlasModuleMode.SOURCE);
                getSourceModules().put(docId, module);
            } else if (ds.getDataSourceType() == DataSourceType.TARGET) {
                module.setMode(AtlasModuleMode.TARGET);
                getTargetModules().put(docId, module);
            }
            module.setDocId(docId);
            module.init();
        } catch (Throwable t) {
            LOG.error("Unable to initialize {} module: {}", ds.getDataSourceType(), moduleInfo.toString());
            LOG.error(t.getMessage(), t);
            throw new AtlasException(String.format("Unable to initialize %s module: %s", ds.getDataSourceType(), moduleInfo.toString()), t);
        }
    }
}
Also used : AtlasModule(io.atlasmap.spi.AtlasModule) AtlasModuleInfo(io.atlasmap.spi.AtlasModuleInfo) AtlasModuleInfoRegistry(io.atlasmap.spi.AtlasModuleInfoRegistry) LookupTable(io.atlasmap.v2.LookupTable) AtlasException(io.atlasmap.api.AtlasException) DataSource(io.atlasmap.v2.DataSource)

Example 3 with AtlasException

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

the class XmlModule method convertDocumentToString.

private String convertDocumentToString(Document document) throws AtlasException {
    DocumentBuilderFactory domFact = DocumentBuilderFactory.newInstance();
    domFact.setNamespaceAware(true);
    StringWriter writer = null;
    try {
        DOMSource domSource = new DOMSource(document);
        writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.transform(domSource, result);
        return writer.toString();
    } catch (TransformerException e) {
        LOG.error(String.format("Error converting Xml document to string msg=%s", e.getMessage()), e);
        throw new AtlasException(e.getMessage(), e);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) AtlasException(io.atlasmap.api.AtlasException) TransformerException(javax.xml.transform.TransformerException)

Example 4 with AtlasException

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

the class XmlFieldReader method read.

public void read(AtlasInternalSession session) throws AtlasException {
    if (document == null) {
        throw new AtlasException(new IllegalArgumentException("'document' cannot be null"));
    }
    Field field = session.head().getSourceField();
    if (field == null) {
        throw new AtlasException(new IllegalArgumentException("Argument 'field' cannot be null"));
    }
    seedDocumentNamespaces(document);
    XmlField xmlField = XmlField.class.cast(field);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Reading source value for field: " + xmlField.getPath());
    }
    if (document == null) {
        throw new AtlasException(new IllegalArgumentException("Argument 'document' cannot be null"));
    }
    if (xmlField == null) {
        throw new AtlasException(new IllegalArgumentException("Argument 'xmlField' cannot be null"));
    }
    Element parentNode = document.getDocumentElement();
    for (SegmentContext sc : new XmlPath(xmlField.getPath()).getSegmentContexts(false)) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Now processing segment: " + sc.getSegment());
            LOG.debug("Parent element is currently: " + XmlIOHelper.writeDocumentToString(true, parentNode));
        }
        if (sc.getPrev() == null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Skipping root segment: " + sc);
            }
            // "/XOA/contact<>/firstName", skip.
            continue;
        }
        if (!XmlPath.isAttributeSegment(sc.getSegment())) {
            String childrenElementName = XmlPath.cleanPathSegment(sc.getSegment());
            String namespaceAlias = XmlPath.getNamespace(sc.getSegment());
            if (namespaceAlias != null && !"".equals(namespaceAlias)) {
                childrenElementName = namespaceAlias + ":" + childrenElementName;
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug("Looking for children elements with name: " + childrenElementName);
            }
            List<Element> children = XmlIOHelper.getChildrenWithName(childrenElementName, parentNode);
            if (children == null || children.isEmpty()) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Skipping source value set, couldn't find children with name '" + childrenElementName + "', for segment: " + sc);
                }
                return;
            }
            parentNode = children.get(0);
            if (XmlPath.isCollectionSegment(sc.getSegment())) {
                int index = XmlPath.indexOfSegment(sc.getSegment());
                if (index >= children.size()) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Skipping source value set, children list can't fit index " + index + ", children list size: " + children.size());
                    }
                    return;
                }
                parentNode = children.get(index);
            }
        }
        if (sc.getNext() == null) {
            // last segment.
            String value = parentNode.getTextContent();
            if (XmlPath.isAttributeSegment(sc.getSegment())) {
                String attributeName = XmlPath.getAttribute(sc.getSegment());
                value = parentNode.getAttribute(attributeName);
            }
            if (value == null) {
                return;
            }
            if (xmlField.getFieldType() == null) {
                xmlField.setValue(value);
                xmlField.setFieldType(FieldType.STRING);
            } else {
                Object convertedValue;
                try {
                    convertedValue = conversionService.convertType(value, xmlField.getFormat(), xmlField.getFieldType(), null);
                    xmlField.setValue(convertedValue);
                } catch (AtlasConversionException e) {
                    AtlasUtil.addAudit(session, xmlField.getDocId(), String.format("Failed to convert field value '%s' into type '%s'", value, xmlField.getFieldType()), xmlField.getPath(), AuditStatus.ERROR, value);
                }
            }
        }
    }
}
Also used : Field(io.atlasmap.v2.Field) XmlField(io.atlasmap.xml.v2.XmlField) SegmentContext(io.atlasmap.core.AtlasPath.SegmentContext) AtlasConversionException(io.atlasmap.api.AtlasConversionException) XmlField(io.atlasmap.xml.v2.XmlField) Element(org.w3c.dom.Element) AtlasException(io.atlasmap.api.AtlasException)

Example 5 with AtlasException

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

the class JsonModule method getCollectionSize.

@Override
public int getCollectionSize(AtlasInternalSession session, Field field) throws AtlasException {
    // TODO could this use FieldReader?
    Object document = session.getSourceDocument(getDocId());
    // make this a JSON document
    JsonFactory jsonFactory = new JsonFactory();
    ObjectMapper objectMapper = new ObjectMapper();
    try {
        JsonParser parser = jsonFactory.createParser(document.toString());
        JsonNode rootNode = objectMapper.readTree(parser);
        ObjectNode parentNode = (ObjectNode) rootNode;
        String parentSegment = "[root node]";
        for (SegmentContext sc : new AtlasPath(field.getPath()).getSegmentContexts(false)) {
            JsonNode currentNode = JsonFieldWriter.getChildNode(parentNode, parentSegment, sc.getSegment());
            if (currentNode == null) {
                return 0;
            }
            if (AtlasPath.isCollectionSegment(sc.getSegment())) {
                if (currentNode != null && currentNode.isArray()) {
                    return currentNode.size();
                }
                return 0;
            }
            parentNode = (ObjectNode) currentNode;
        }
    } catch (IOException e) {
        throw new AtlasException(e.getMessage(), e);
    }
    return 0;
}
Also used : SegmentContext(io.atlasmap.core.AtlasPath.SegmentContext) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonFactory(com.fasterxml.jackson.core.JsonFactory) AtlasPath(io.atlasmap.core.AtlasPath) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) AtlasException(io.atlasmap.api.AtlasException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser)

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