Search in sources :

Example 16 with Equals

use of io.atlasmap.v2.Equals in project atlasmap by atlasmap.

the class BaseAtlasModule method processLookupField.

protected void processLookupField(AtlasInternalSession session, LookupTable lookupTable, Object sourceValue, Field targetField) throws AtlasException {
    String lookupValue = null;
    FieldType lookupType = null;
    for (LookupEntry lkp : lookupTable.getLookupEntry()) {
        if (lkp.getSourceValue().equals(sourceValue)) {
            lookupValue = lkp.getTargetValue();
            lookupType = lkp.getTargetType();
            break;
        }
    }
    Object targetValue = null;
    if (lookupType == null || FieldType.STRING.equals(lookupType)) {
        targetValue = lookupValue;
    } else {
        targetValue = atlasConversionService.convertType(lookupValue, FieldType.STRING, lookupType);
    }
    if (targetField.getFieldType() != null && !targetField.getFieldType().equals(lookupType)) {
        targetValue = atlasConversionService.convertType(targetValue, lookupType, targetField.getFieldType());
    }
    targetField.setValue(targetValue);
}
Also used : LookupEntry(io.atlasmap.v2.LookupEntry) FieldType(io.atlasmap.v2.FieldType)

Example 17 with Equals

use of io.atlasmap.v2.Equals in project atlasmap by atlasmap.

the class SchemaInspector method doInspect.

private void doInspect(InputStream is) throws Exception {
    xmlDocument = AtlasXmlModelFactory.createXmlDocument();
    Fields fields = new Fields();
    xmlDocument.setFields(fields);
    namespaceContext = new AtlasXmlNamespaceContext();
    rootNamespace = null;
    XSOMParser parser = new XSOMParser(SAXParserFactory.newInstance());
    parser.setAnnotationParser(new DomAnnotationParserFactory());
    parser.setErrorHandler(new XSOMErrorHandler());
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    Document doc = dbf.newDocumentBuilder().parse(is);
    Element root = doc.getDocumentElement();
    if (root == null) {
        throw new XmlInspectionException("XML schema document is empty");
    } else if ("SchemaSet".equals(root.getLocalName())) {
        XPath xpath = XPathFactory.newInstance().newXPath();
        xpath.setNamespaceContext(namespaceContext);
        NodeList subSchemas = (NodeList) xpath.evaluate(String.format("/%s:SchemaSet/%s:AdditionalSchemas/%s:schema", NS_PREFIX_SCHEMASET, NS_PREFIX_SCHEMASET, NS_PREFIX_XMLSCHEMA), doc, XPathConstants.NODESET);
        for (int i = 0; i < subSchemas.getLength(); i++) {
            Element e = (Element) subSchemas.item(i);
            inheritNamespaces(e, false);
            parser.parse(toInputStream(transformer, e));
        }
        Element rootSchema = (Element) xpath.evaluate(String.format("/%s:SchemaSet/%s:schema", NS_PREFIX_SCHEMASET, NS_PREFIX_XMLSCHEMA), doc, XPathConstants.NODE);
        if (rootSchema == null) {
            throw new XmlInspectionException("The root schema '/SchemaSet/schema' must be specified once and only once");
        }
        rootNamespace = getTargetNamespace(rootSchema);
        if (rootNamespace != null && !rootNamespace.isEmpty()) {
            namespaceContext.add("tns", rootNamespace);
        }
        inheritNamespaces(rootSchema, true);
        parser.parse(toInputStream(transformer, rootSchema));
    } else if ("schema".equals(root.getLocalName())) {
        parser.parse(toInputStream(transformer, root));
        rootNamespace = getTargetNamespace(root);
        if (rootNamespace != null && !rootNamespace.isEmpty()) {
            namespaceContext.add("tns", rootNamespace);
        }
    } else {
        throw new XmlInspectionException(String.format("Unsupported document element '%s': root element must be 'schema' or 'SchemaSet'", root.getLocalName()));
    }
    XSSchemaSet schemaSet = parser.getResult();
    printSchemaSet(schemaSet);
    populateNamespaces();
}
Also used : XPath(javax.xml.xpath.XPath) XSOMParser(com.sun.xml.xsom.parser.XSOMParser) Transformer(javax.xml.transform.Transformer) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) XSSchemaSet(com.sun.xml.xsom.XSSchemaSet) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) XmlDocument(io.atlasmap.xml.v2.XmlDocument) XmlFields(io.atlasmap.xml.v2.XmlFields) Fields(io.atlasmap.v2.Fields) DomAnnotationParserFactory(com.sun.xml.xsom.util.DomAnnotationParserFactory)

Example 18 with Equals

use of io.atlasmap.v2.Equals in project atlasmap by atlasmap.

the class AtlasCoreValidationTest method testMappingNameNull.

@Test
public void testMappingNameNull() throws AtlasException {
    assertNotNull(context);
    assertNotNull(session);
    context.processValidation(session);
    Validations validations = session.getValidations();
    assertNotNull(validations);
    assertNotNull(validations.getValidation());
    assertFalse(validations.getValidation().isEmpty());
    boolean found = false;
    for (Validation v : validations.getValidation()) {
        if ("Mapping name must not be null nor empty".equals(v.getMessage())) {
            found = true;
            assertEquals(ValidationStatus.ERROR, v.getStatus());
            assertEquals(ValidationScope.ALL, v.getScope());
        }
        System.out.println(AtlasValidationTestHelper.validationToString(v));
    }
    assertTrue(found);
}
Also used : Validation(io.atlasmap.v2.Validation) Validations(io.atlasmap.v2.Validations) Test(org.junit.Test) AtlasMappingBaseTest(io.atlasmap.validation.AtlasMappingBaseTest)

Example 19 with Equals

use of io.atlasmap.v2.Equals 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);
    }
}
Also used : AtlasException(io.atlasmap.api.AtlasException) FieldType(io.atlasmap.v2.FieldType) JavaEnumField(io.atlasmap.java.v2.JavaEnumField) Field(io.atlasmap.v2.Field) JavaField(io.atlasmap.java.v2.JavaField) SegmentContext(io.atlasmap.core.AtlasPath.SegmentContext) JavaEnumField(io.atlasmap.java.v2.JavaEnumField) JavaField(io.atlasmap.java.v2.JavaField) LookupTable(io.atlasmap.v2.LookupTable) AtlasPath(io.atlasmap.core.AtlasPath)

Example 20 with Equals

use of io.atlasmap.v2.Equals in project atlasmap by atlasmap.

the class TargetValueConverter method populateEnumValue.

@SuppressWarnings("unchecked")
private Object populateEnumValue(AtlasInternalSession session, LookupTable lookupTable, JavaEnumField sourceField, JavaEnumField targetField) {
    if (sourceField == null || sourceField.getValue() == null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Input enum field or value is null, field: " + sourceField);
        }
        return null;
    }
    String sourceValue = ((Enum<?>) sourceField.getValue()).name();
    String targetValue = sourceValue;
    if (lookupTable != null) {
        for (LookupEntry e : lookupTable.getLookupEntry()) {
            if (e.getSourceValue().equals(sourceValue)) {
                targetValue = e.getTargetValue();
                break;
            }
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Mapped input enum value '" + sourceValue + "' to output enum value '" + targetValue + "'.");
    }
    if (targetValue == null) {
        return null;
    }
    @SuppressWarnings("rawtypes") Class enumClass = null;
    try {
        enumClass = Class.forName(targetField.getClassName());
    } catch (Exception e) {
        AtlasUtil.addAudit(session, targetField.getDocId(), String.format("Could not find class for output field class '%s': %s", targetField.getClassName(), e.getMessage()), targetField.getPath(), AuditStatus.ERROR, targetValue);
        return null;
    }
    try {
        return Enum.valueOf(enumClass, targetValue);
    } catch (IllegalArgumentException e) {
        AtlasUtil.addAudit(session, targetField.getDocId(), String.format("No enum entry found for value '%s': %s", targetValue, e.getMessage()), targetField.getPath(), AuditStatus.ERROR, targetValue);
        return null;
    }
}
Also used : LookupEntry(io.atlasmap.v2.LookupEntry) AtlasException(io.atlasmap.api.AtlasException) AtlasConversionException(io.atlasmap.api.AtlasConversionException)

Aggregations

Field (io.atlasmap.v2.Field)7 Test (org.junit.Test)6 AtlasConversionException (io.atlasmap.api.AtlasConversionException)4 AtlasException (io.atlasmap.api.AtlasException)4 Validation (io.atlasmap.v2.Validation)4 FieldType (io.atlasmap.v2.FieldType)3 LookupTable (io.atlasmap.v2.LookupTable)3 Given (cucumber.api.java.en.Given)2 SegmentContext (io.atlasmap.core.AtlasPath.SegmentContext)2 JavaField (io.atlasmap.java.v2.JavaField)2 ActionDetail (io.atlasmap.v2.ActionDetail)2 AtlasMapping (io.atlasmap.v2.AtlasMapping)2 BaseMapping (io.atlasmap.v2.BaseMapping)2 LookupEntry (io.atlasmap.v2.LookupEntry)2 Mapping (io.atlasmap.v2.Mapping)2 MappingType (io.atlasmap.v2.MappingType)2 SimpleField (io.atlasmap.v2.SimpleField)2 Validations (io.atlasmap.v2.Validations)2 AtlasMappingBaseTest (io.atlasmap.validation.AtlasMappingBaseTest)2 XmlFieldWriter (io.atlasmap.xml.core.XmlFieldWriter)2