Search in sources :

Example 31 with Field

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

the class DocumentJavaFieldReader method read.

@Override
public void read(AtlasInternalSession session) throws AtlasException {
    try {
        Field sourceField = session.head().getSourceField();
        Method getter = null;
        if (sourceField.getFieldType() == null && (sourceField instanceof JavaField || sourceField instanceof JavaEnumField)) {
            getter = resolveGetMethod(sourceDocument, sourceField);
            if (getter == null) {
                AtlasUtil.addAudit(session, sourceField.getDocId(), String.format("Unable to auto-detect sourceField type path=%s docId=%s", sourceField.getPath(), sourceField.getDocId()), sourceField.getPath(), AuditStatus.WARN, null);
                return;
            }
            Class<?> returnType = getter.getReturnType();
            sourceField.setFieldType(conversionService.fieldTypeFromClass(returnType));
            if (LOG.isTraceEnabled()) {
                LOG.trace("Auto-detected sourceField type p=" + sourceField.getPath() + " t=" + sourceField.getFieldType());
            }
        }
        populateSourceFieldValue(session, sourceField, sourceDocument, getter);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Processed input field sPath=" + sourceField.getPath() + " sV=" + sourceField.getValue() + " sT=" + sourceField.getFieldType() + " docId: " + sourceField.getDocId());
        }
    } catch (Exception e) {
        throw new AtlasException(e);
    }
}
Also used : JavaEnumField(io.atlasmap.java.v2.JavaEnumField) Field(io.atlasmap.v2.Field) JavaField(io.atlasmap.java.v2.JavaField) JavaEnumField(io.atlasmap.java.v2.JavaEnumField) JavaField(io.atlasmap.java.v2.JavaField) Method(java.lang.reflect.Method) AtlasException(io.atlasmap.api.AtlasException) AtlasException(io.atlasmap.api.AtlasException)

Example 32 with Field

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

the class DocumentJavaFieldWriter method getClassForField.

private Class<?> getClassForField(Field field, SegmentContext segmentContext, Object parentObject, boolean unwrapCollectionType) throws AtlasException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Looking up class to use for segment: " + segmentContext + "\n\tparentObject: " + parentObject);
    }
    Class<?> clz = null;
    if (LOG.isDebugEnabled()) {
        LOG.debug("Looking for configured class for field: " + field + ".");
    }
    String className = null;
    if (field instanceof JavaField) {
        className = ((JavaField) field).getClassName();
    } else if (field instanceof JavaEnumField) {
        className = ((JavaEnumField) field).getClassName();
    }
    if (className != null) {
        try {
            clz = className == null ? null : Class.forName(className);
        } catch (Exception e) {
            throw new AtlasException("Could not find class for '" + className + "', for segment: " + segmentContext + ", on field: " + field, e);
        }
    }
    if (clz == null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Couldn't find class on field. Looking for configured class for segment: " + segmentContext + ".");
        }
        String normalizedSegment = AtlasPath.removeCollectionIndexes(segmentContext.getSegmentPath());
        clz = this.classesForFields.get(normalizedSegment);
    }
    Type clzType = null;
    if (clz == null) {
        // attempt to determine it from the parent object.
        if (LOG.isDebugEnabled()) {
            LOG.debug("Couldn't find configured class for segment: " + segmentContext + ", looking up getter method.");
        }
        Method m = null;
        try {
            String methodName = "get" + JavaWriterUtil.capitalizeFirstLetter(AtlasPath.cleanPathSegment(segmentContext.getSegment()));
            m = ClassHelper.detectGetterMethod(parentObject.getClass(), methodName);
        } catch (NoSuchMethodException e) {
            // it's ok, we didnt find a getter.
            if (LOG.isDebugEnabled()) {
                LOG.debug("Couldn't find getter method for segment: " + segmentContext, e);
            }
        }
        clz = m == null ? null : m.getReturnType();
        clzType = m.getGenericReturnType();
    }
    if (clz == null) {
        throw new AtlasException("Could not create object, can't find class to instantiate for segment: " + segmentContext);
    }
    if (unwrapCollectionType) {
        clz = unwrapCollectionType(field, segmentContext, parentObject, clz, clzType);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Found class '" + clz.getName() + "' to use for segment: " + segmentContext);
    }
    return clz;
}
Also used : JavaEnumField(io.atlasmap.java.v2.JavaEnumField) FieldType(io.atlasmap.v2.FieldType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) JavaField(io.atlasmap.java.v2.JavaField) Method(java.lang.reflect.Method) AtlasException(io.atlasmap.api.AtlasException) AtlasException(io.atlasmap.api.AtlasException)

Example 33 with Field

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

the class ClassInspectionService method inspectField.

private JavaField inspectField(ClassLoader classLoader, Field f, Set<String> cachedClasses, String pathPrefix) {
    JavaField s = AtlasJavaModelFactory.createJavaField();
    Class<?> clazz = f.getType();
    s.setName(f.getName());
    if (pathPrefix != null && pathPrefix.length() > 0) {
        s.setPath(pathPrefix + AtlasPath.PATH_SEPARATOR + f.getName());
    } else {
        s.setPath(f.getName());
    }
    if (isMapList(clazz.getCanonicalName())) {
        s.setCollectionType(CollectionType.MAP);
    }
    if (clazz.isArray()) {
        s.setCollectionType(CollectionType.ARRAY);
        s.setArrayDimensions(detectArrayDimensions(clazz));
        clazz = detectArrayClass(clazz);
    } else if (isFieldList(clazz.getCanonicalName())) {
        s.setCollectionType(CollectionType.LIST);
        s.setCollectionClassName(clazz.getCanonicalName());
        try {
            clazz = detectListClass(classLoader, f);
            if (clazz == null) {
                s.setStatus(FieldStatus.ERROR);
                return s;
            }
        } catch (ClassCastException | ClassNotFoundException cce) {
            LOG.debug("Error detecting inner listClass: " + cce.getMessage() + " for field: " + f.getName(), cce);
            s.setStatus(FieldStatus.ERROR);
            return s;
        }
    }
    s.setFieldType(getConversionService().fieldTypeFromClass(clazz));
    if (getConversionService().isPrimitive(clazz) || getConversionService().isBoxedPrimitive(clazz)) {
        s.setPrimitive(true);
        s.setStatus(FieldStatus.SUPPORTED);
    } else if (s.getFieldType() != FieldType.COMPLEX) {
        s.setPrimitive(false);
        s.setStatus(FieldStatus.SUPPORTED);
    } else {
        s.setPrimitive(false);
        Class<?> complexClazz = null;
        JavaClass tmpField = convertJavaFieldToJavaClass(s);
        s = tmpField;
        if (clazz.getCanonicalName() == null) {
            s.setStatus(FieldStatus.UNSUPPORTED);
        } else if (!cachedClasses.contains(clazz.getCanonicalName())) {
            try {
                complexClazz = classLoader.loadClass(clazz.getCanonicalName());
                cachedClasses.add(clazz.getCanonicalName());
                inspectClass(classLoader, complexClazz, tmpField, cachedClasses, s.getPath());
                if (tmpField.getStatus() == null) {
                    s.setStatus(FieldStatus.SUPPORTED);
                }
            } catch (ClassNotFoundException cnfe) {
                s.setStatus(FieldStatus.NOT_FOUND);
            }
        } else {
            s.setStatus(FieldStatus.CACHED);
        }
    }
    s.setClassName(clazz.getCanonicalName());
    s.setSynthetic(f.isSynthetic());
    Annotation[] annotations = f.getAnnotations();
    if (annotations != null) {
        for (Annotation a : annotations) {
            if (s.getAnnotations() == null) {
                s.setAnnotations(new StringList());
            }
            s.getAnnotations().getString().add(a.annotationType().getCanonicalName());
        }
    }
    if (s.getModifiers() == null) {
        s.setModifiers(new ModifierList());
    }
    s.getModifiers().getModifier().addAll(detectModifiers(f.getModifiers()));
    List<String> pTypes = detectParameterizedTypes(f, false);
    if (pTypes != null) {
        if (s.getParameterizedTypes() == null) {
            s.setParameterizedTypes(new StringList());
        }
        s.getParameterizedTypes().getString().addAll(pTypes);
    }
    populateGetterSetter(clazz, f, s);
    return s;
}
Also used : ModifierList(io.atlasmap.java.v2.ModifierList) JavaField(io.atlasmap.java.v2.JavaField) JavaClass(io.atlasmap.java.v2.JavaClass) StringList(io.atlasmap.v2.StringList) JavaClass(io.atlasmap.java.v2.JavaClass) Annotation(java.lang.annotation.Annotation)

Example 34 with Field

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

the class BaseDocumentWriterTest method write.

protected void write(String path, StateEnumClassLong targetValue) throws AtlasException {
    Field field = createEnumField(path, targetValue);
    setTargetValue(targetValue);
    write(field);
}
Also used : Field(io.atlasmap.v2.Field) JavaEnumField(io.atlasmap.java.v2.JavaEnumField) JavaField(io.atlasmap.java.v2.JavaField)

Example 35 with Field

use of io.atlasmap.v2.Field in project syndesis-qe by syndesisio.

the class UiComplexSteps method dbToDbIntegrationWithPeriodMs.

@Given("^db to db \"([^\"]*)\" integration with period (\\d+) ms$")
public void dbToDbIntegrationWithPeriodMs(String integrationName, int ms) throws IOException {
    final Connection dbConnection = connectionsEndpoint.get(getDbConnectionId());
    final Connector dbConnector = connectorsEndpoint.get("sql");
    final String sqlStartQuery = "SELECT * FROM CONTACT";
    final String sqlFinishQuery = "INSERT INTO TODO(task, completed) VALUES (:#TASK, 2)";
    final String datamapperTemplate = "db-db.json";
    // 1.        @Then("^create start DB periodic sql invocation action step with query \"([^\"]*)\" and period \"([^\"]*)\" ms")
    final Action dbAction1 = TestUtils.findConnectorAction(dbConnector, "sql-start-connector");
    final Map<String, String> properties1 = TestUtils.map("query", sqlStartQuery, "schedulerPeriod", ms);
    final ConnectorDescriptor connectorDescriptor1 = getConnectorDescriptor(dbAction1, properties1, dbConnection.getId().get());
    // to be reported: period is not part of .json step (when checked via browser).
    final Step dbStep1 = new Step.Builder().stepKind(StepKind.endpoint).id(UUID.randomUUID().toString()).connection(dbConnection).action(dbAction1).configuredProperties(properties1).build();
    steps.getStepDefinitions().add(new StepDefinition(dbStep1, connectorDescriptor1));
    // 2.A @And("start mapper definition with name: \"([^\"]*)\"")
    String mapperName = "mapping 1";
    final Step mapperStep = new Step.Builder().stepKind(StepKind.mapper).name(mapperName).build();
    steps.getStepDefinitions().add(new StepDefinition(mapperStep, new DataMapperDefinition()));
    // 2.B @Then("MAP using Step (\\d+) and field \"([^\"]*)\" to \"([^\"]*)\"")
    int fromStep = 1;
    String fromField = "first_name";
    String toField = "TASK";
    DataMapperStepDefinition newDmStep = new DataMapperStepDefinition();
    newDmStep.setFromStep(fromStep);
    newDmStep.setInputFields(Arrays.asList(fromField));
    newDmStep.setOutputFields(Arrays.asList(toField));
    newDmStep.setMappingType(MappingType.MAP);
    newDmStep.setStrategy(null);
    steps.getLastStepDefinition().getDataMapperDefinition().get().getDataMapperStepDefinition().add(newDmStep);
    // 3.        @Then("^create finish DB invoke sql action step with query \"([^\"]*)\"")
    final Action dbAction2 = TestUtils.findConnectorAction(dbConnector, "sql-connector");
    final Map<String, String> properties2 = TestUtils.map("query", sqlFinishQuery);
    final ConnectorDescriptor connectorDescriptor2 = getConnectorDescriptor(dbAction2, properties2, dbConnection.getId().get());
    final Step dbStep2 = new Step.Builder().stepKind(StepKind.endpoint).id(UUID.randomUUID().toString()).connection(dbConnection).action(dbAction2).configuredProperties(properties2).build();
    steps.getStepDefinitions().add(new StepDefinition(dbStep2, connectorDescriptor2));
    // 4.    @When("^create integration with name: \"([^\"]*)\"")
    processMapperSteps();
    Integration integration = new Integration.Builder().steps(steps.getSteps()).name(integrationName).description("Awkward UI integration.").build();
    log.info("Creating integration {}", integration.getName());
    String integrationId = integrationsEndpoint.create(integration).getId().get();
    log.info("Publish integration with ID: {}", integrationId);
    publishIntegration(integrationId);
    log.debug("Flushing used steps");
    steps.flushStepDefinitions();
    // 5.        @Then("^wait for integration with name: \"([^\"]*)\" to become active")
    final List<Integration> integrations = integrationsEndpoint.list().stream().filter(item -> item.getName().equals(integrationName)).collect(Collectors.toList());
    final long start = System.currentTimeMillis();
    // wait for activation
    log.info("Waiting until integration \"{}\" becomes active. This may take a while...", integrationName);
    integrationOverviewEndpoint = new IntegrationOverviewEndpoint(integrationId);
    final IntegrationOverview integrationOverview = integrationOverviewEndpoint.getOverview();
    final boolean activated = TestUtils.waitForPublishing(integrationOverviewEndpoint, integrationOverview, TimeUnit.MINUTES, 10);
    Assertions.assertThat(activated).isEqualTo(true);
    log.info("Integration pod has been started. It took {}s to build the integration.", TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - start));
}
Also used : ConnectorDescriptor(io.syndesis.common.model.action.ConnectorDescriptor) Arrays(java.util.Arrays) Action(io.syndesis.common.model.action.Action) Step(io.syndesis.common.model.integration.Step) Autowired(org.springframework.beans.factory.annotation.Autowired) MappingType(io.atlasmap.v2.MappingType) ConnectorsEndpoint(io.syndesis.qe.endpoints.ConnectorsEndpoint) DataMapperStepDefinition(io.syndesis.qe.bdd.entities.DataMapperStepDefinition) IntegrationsDeploymentEndpoint(io.syndesis.qe.endpoints.IntegrationsDeploymentEndpoint) Connection(io.syndesis.common.model.connection.Connection) Map(java.util.Map) Given(cucumber.api.java.en.Given) Assertions(org.assertj.core.api.Assertions) StepKind(io.syndesis.common.model.integration.StepKind) Integration(io.syndesis.common.model.integration.Integration) ConnectionsEndpoint(io.syndesis.qe.endpoints.ConnectionsEndpoint) Connector(io.syndesis.common.model.connection.Connector) AtlasMapperGenerator(io.syndesis.qe.bdd.datamapper.AtlasMapperGenerator) IOException(java.io.IOException) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) IntegrationOverview(io.syndesis.qe.model.IntegrationOverview) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) IntegrationsEndpoint(io.syndesis.qe.endpoints.IntegrationsEndpoint) TestUtils(io.syndesis.qe.utils.TestUtils) DataMapperDefinition(io.syndesis.qe.bdd.entities.DataMapperDefinition) StepsStorage(io.syndesis.qe.bdd.storage.StepsStorage) StepDefinition(io.syndesis.qe.bdd.entities.StepDefinition) IntegrationOverviewEndpoint(io.syndesis.qe.endpoints.IntegrationOverviewEndpoint) Connector(io.syndesis.common.model.connection.Connector) DataMapperStepDefinition(io.syndesis.qe.bdd.entities.DataMapperStepDefinition) Action(io.syndesis.common.model.action.Action) Integration(io.syndesis.common.model.integration.Integration) IntegrationOverviewEndpoint(io.syndesis.qe.endpoints.IntegrationOverviewEndpoint) Connection(io.syndesis.common.model.connection.Connection) Step(io.syndesis.common.model.integration.Step) ConnectorsEndpoint(io.syndesis.qe.endpoints.ConnectorsEndpoint) IntegrationsDeploymentEndpoint(io.syndesis.qe.endpoints.IntegrationsDeploymentEndpoint) ConnectionsEndpoint(io.syndesis.qe.endpoints.ConnectionsEndpoint) IntegrationsEndpoint(io.syndesis.qe.endpoints.IntegrationsEndpoint) IntegrationOverviewEndpoint(io.syndesis.qe.endpoints.IntegrationOverviewEndpoint) ConnectorDescriptor(io.syndesis.common.model.action.ConnectorDescriptor) DataMapperDefinition(io.syndesis.qe.bdd.entities.DataMapperDefinition) IntegrationOverview(io.syndesis.qe.model.IntegrationOverview) DataMapperStepDefinition(io.syndesis.qe.bdd.entities.DataMapperStepDefinition) StepDefinition(io.syndesis.qe.bdd.entities.StepDefinition) Given(cucumber.api.java.en.Given)

Aggregations

Field (io.atlasmap.v2.Field)60 Test (org.junit.Test)27 AtlasMapping (io.atlasmap.v2.AtlasMapping)24 Mapping (io.atlasmap.v2.Mapping)23 JavaField (io.atlasmap.java.v2.JavaField)20 SimpleField (io.atlasmap.v2.SimpleField)17 BaseMapping (io.atlasmap.v2.BaseMapping)15 Validation (io.atlasmap.v2.Validation)14 JavaEnumField (io.atlasmap.java.v2.JavaEnumField)13 AtlasException (io.atlasmap.api.AtlasException)12 FieldType (io.atlasmap.v2.FieldType)12 JsonField (io.atlasmap.json.v2.JsonField)10 AtlasInternalSession (io.atlasmap.spi.AtlasInternalSession)10 XmlField (io.atlasmap.xml.v2.XmlField)9 LookupTable (io.atlasmap.v2.LookupTable)8 AtlasConversionException (io.atlasmap.api.AtlasConversionException)7 ConstantField (io.atlasmap.v2.ConstantField)7 Head (io.atlasmap.spi.AtlasInternalSession.Head)6 PropertyField (io.atlasmap.v2.PropertyField)6 Method (java.lang.reflect.Method)6