Search in sources :

Example 6 with CollectionType

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

the class DefaultAtlasFieldActionService method createDetailFromFieldActionInfo.

private ActionProcessor createDetailFromFieldActionInfo(final Class<?> clazz, final Method method) {
    AtlasFieldActionInfo annotation = method.getAnnotation(AtlasFieldActionInfo.class);
    if (annotation == null) {
        return null;
    }
    final ActionDetail det = new ActionDetail();
    det.setClassName(clazz.getName());
    det.setMethod(method.getName());
    det.setName(annotation.name());
    det.setSourceType(annotation.sourceType());
    det.setTargetType(annotation.targetType());
    CollectionType sourceCollection = annotation.sourceCollectionType();
    CollectionType targetCollection = annotation.sourceCollectionType();
    if (sourceCollection != null && sourceCollection != CollectionType.NONE) {
        det.setMultiplicity(Multiplicity.MANY_TO_ONE);
    } else if (targetCollection != null && targetCollection != CollectionType.NONE) {
        det.setMultiplicity(Multiplicity.ONE_TO_MANY);
    } else {
        det.setMultiplicity(Multiplicity.ONE_TO_ONE);
    }
    Class<? extends Action> actionClazz;
    try {
        actionClazz = (Class<? extends Action>) Class.forName("io.atlasmap.v2." + annotation.name());
    } catch (Exception e) {
        actionClazz = null;
        det.setCustom(true);
    }
    try {
        det.setActionSchema(actionClazz);
    } catch (Exception e) {
        LOG.error(String.format("Could not get json schema for action=%s msg=%s", annotation.name(), e.getMessage()), e);
    }
    try {
        // TODO https://github.com/atlasmap/atlasmap/issues/538
        if (det.isCustom() == null || !det.isCustom()) {
            det.setParameters(detectFieldActionParameters(actionClazz));
        }
    } catch (ClassNotFoundException e) {
        LOG.error(String.format("Error detecting parameters for field action=%s msg=%s", annotation.name(), e.getMessage()), e);
    }
    if (LOG.isTraceEnabled()) {
        LOG.trace("Loaded FieldAction: " + det.getName());
    }
    Class<? extends Action> finalActionClazz = actionClazz;
    return new ActionProcessor() {

        @Override
        public ActionDetail getActionDetail() {
            return det;
        }

        @Override
        public Class<? extends Action> getActionClass() {
            return finalActionClazz;
        }

        @Override
        public Object process(Action action, Object sourceObject) throws AtlasException {
            Object targetObject = null;
            try {
                Object convertedSourceObject = convertSourceObject(sourceObject);
                if (Modifier.isStatic(method.getModifiers())) {
                    // cf. https://github.com/atlasmap/atlasmap/issues/536
                    if (det.isCustom() != null && det.isCustom()) {
                        targetObject = det.getMultiplicity() == Multiplicity.ZERO_TO_ONE ? method.invoke(null) : method.invoke(null, convertedSourceObject);
                    } else {
                        targetObject = det.getMultiplicity() == Multiplicity.ZERO_TO_ONE ? method.invoke(null, action) : method.invoke(null, action, convertedSourceObject);
                    }
                } else {
                    Object object = clazz.getDeclaredConstructor().newInstance();
                    if (det.isCustom() != null && det.isCustom()) {
                        targetObject = det.getMultiplicity() == Multiplicity.ZERO_TO_ONE ? method.invoke(object) : method.invoke(object, convertedSourceObject);
                    } else {
                        targetObject = det.getMultiplicity() == Multiplicity.ZERO_TO_ONE ? method.invoke(object, action) : method.invoke(object, action, convertedSourceObject);
                    }
                }
            } catch (Throwable e) {
                throw new AtlasException(String.format("Error processing action %s", det.getName()), e);
            }
            return targetObject;
        }

        private Object convertSourceObject(Object sourceObject) throws AtlasConversionException {
            Class<?> paramType;
            int paramCount = method.getParameterCount();
            if (paramCount < 2) {
                return null;
            }
            paramType = method.getParameterTypes()[1];
            if (paramType.isInstance(sourceObject)) {
                return sourceObject;
            }
            return conversionService.convertType(sourceObject, null, paramType, null);
        }
    };
}
Also used : CustomAction(io.atlasmap.v2.CustomAction) AtlasFieldAction(io.atlasmap.spi.AtlasFieldAction) Action(io.atlasmap.v2.Action) ActionDetail(io.atlasmap.v2.ActionDetail) AtlasFieldActionInfo(io.atlasmap.spi.AtlasFieldActionInfo) AtlasException(io.atlasmap.api.AtlasException) AtlasConversionException(io.atlasmap.api.AtlasConversionException) AtlasException(io.atlasmap.api.AtlasException) CollectionType(io.atlasmap.v2.CollectionType) ActionProcessor(io.atlasmap.spi.ActionProcessor) AtlasActionProcessor(io.atlasmap.spi.AtlasActionProcessor)

Example 7 with CollectionType

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

the class JavaModule method processPreTargetExecution.

@Override
public void processPreTargetExecution(AtlasInternalSession atlasSession) throws AtlasException {
    if (atlasSession == null || atlasSession.getMapping() == null || atlasSession.getMapping().getMappings() == null || atlasSession.getMapping().getMappings().getMapping() == null) {
        throw new AtlasException("AtlasSession not properly intialized with a mapping that contains field mappings");
    }
    Object rootObject;
    String targetClassName = AtlasUtil.unescapeFromUri(AtlasUtil.getUriParameterValue(getUri(), "className"));
    String collectionTypeStr = AtlasUtil.unescapeFromUri(AtlasUtil.getUriParameterValue(getUri(), "collectionType"));
    CollectionType collectionType = collectionTypeStr != null ? CollectionType.fromValue(collectionTypeStr) : CollectionType.NONE;
    String collectionClassName = AtlasUtil.unescapeFromUri(AtlasUtil.getUriParameterValue(getUri(), "collectionClassName"));
    JavaFieldWriter writer = new JavaFieldWriter(this.writerUtil);
    Class<?> clazz = writerUtil.loadClass(targetClassName);
    if (collectionType == CollectionType.ARRAY) {
        rootObject = Array.newInstance(clazz, 0);
    } else if (collectionType != CollectionType.NONE) {
        if (collectionClassName != null) {
            rootObject = writerUtil.instantiateObject(writerUtil.loadClass(collectionClassName));
        } else {
            rootObject = writerUtil.instantiateObject(writerUtil.getDefaultCollectionImplClass(collectionType));
        }
        writer.setCollectionItemClass(clazz);
    } else {
        rootObject = writerUtil.instantiateObject(clazz);
    }
    writer.setRootObject(rootObject);
    writer.setTargetValueConverter(targetValueConverter);
    atlasSession.setFieldWriter(getDocId(), writer);
    if (LOG.isDebugEnabled()) {
        LOG.debug("{}: processPreTargetExcution completed", getDocId());
    }
}
Also used : JavaFieldWriter(io.atlasmap.java.core.JavaFieldWriter) CollectionType(io.atlasmap.v2.CollectionType) AtlasException(io.atlasmap.api.AtlasException)

Example 8 with CollectionType

use of io.atlasmap.v2.CollectionType in project arctic-sea by 52North.

the class AbstractWmlEncoderv20 method createWmlGetObservationResponse.

/**
 * Encodes a SOS GetObservationResponse to a single WaterML 2.0 observation
 * or to a WaterML 1.0 ObservationCollection
 *
 * @param getObservationResonse
 *            SOS GetObservationResponse
 * @return Encoded response
 * @throws EncodingException
 *             If an error occurs
 */
protected XmlObject createWmlGetObservationResponse(GetObservationResponse getObservationResonse) throws EncodingException {
    // TODO: set schemaLocation if final
    Map<CodeWithAuthority, String> gmlID4sfIdentifier = Maps.newHashMap();
    int sfIdCounter = 1;
    try {
        if (getObservationResonse.getObservationCollection() != null && !getObservationResonse.getObservationCollection().hasNext()) {
            ObservationStream observations = getObservationResonse.getObservationCollection();
            OmObservation observation = observations.next();
            if (!observations.hasNext()) {
                OMObservationDocument omObservationDoc = OMObservationDocument.Factory.newInstance(getXmlOptions());
                omObservationDoc.setOMObservation(encodeObservation(observation, gmlID4sfIdentifier, sfIdCounter));
                sfIdCounter++;
                return omObservationDoc;
            } else {
                CollectionDocument xmlCollectionDoc = CollectionDocument.Factory.newInstance(getXmlOptions());
                CollectionType wmlCollection = xmlCollectionDoc.addNewCollection();
                wmlCollection.addNewObservationMember().setOMObservation(encodeObservation(observation, gmlID4sfIdentifier, sfIdCounter));
                sfIdCounter++;
                while (observations.hasNext()) {
                    wmlCollection.addNewObservationMember().setOMObservation(encodeObservation(observations.next(), gmlID4sfIdentifier, sfIdCounter));
                    sfIdCounter++;
                }
                return xmlCollectionDoc;
            }
        } else {
            // TODO: HydrologieProfile-Exception
            throw new EncodingException("Combination does not exists!");
        }
    } catch (NoSuchElementException | OwsExceptionReport e) {
        throw new EncodingException(e);
    }
}
Also used : EncodingException(org.n52.svalbard.encode.exception.EncodingException) CollectionDocument(net.opengis.waterml.x20.CollectionDocument) OmObservation(org.n52.shetland.ogc.om.OmObservation) OMObservationDocument(net.opengis.om.x20.OMObservationDocument) WmlMonitoringPoint(org.n52.shetland.ogc.om.series.wml.WmlMonitoringPoint) ObservationStream(org.n52.shetland.ogc.om.ObservationStream) CollectionType(net.opengis.waterml.x20.CollectionType) CodeWithAuthority(org.n52.shetland.ogc.gml.CodeWithAuthority) OwsExceptionReport(org.n52.shetland.ogc.ows.exception.OwsExceptionReport) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

CollectionType (io.atlasmap.v2.CollectionType)7 AtlasException (io.atlasmap.api.AtlasException)3 ArrayList (java.util.ArrayList)3 XSModelGroup (com.sun.xml.xsom.XSModelGroup)2 XSTerm (com.sun.xml.xsom.XSTerm)2 AtlasConversionException (io.atlasmap.api.AtlasConversionException)2 ActionProcessor (io.atlasmap.spi.ActionProcessor)2 AtlasActionProcessor (io.atlasmap.spi.AtlasActionProcessor)2 AtlasFieldAction (io.atlasmap.spi.AtlasFieldAction)2 Action (io.atlasmap.v2.Action)2 ActionDetail (io.atlasmap.v2.ActionDetail)2 CustomAction (io.atlasmap.v2.CustomAction)2 List (java.util.List)2 AtlasPath (io.atlasmap.core.AtlasPath)1 JavaFieldWriter (io.atlasmap.java.core.JavaFieldWriter)1 KafkaConnectComplexType (io.atlasmap.kafkaconnect.v2.KafkaConnectComplexType)1 KafkaConnectEnumField (io.atlasmap.kafkaconnect.v2.KafkaConnectEnumField)1 KafkaConnectEnumFields (io.atlasmap.kafkaconnect.v2.KafkaConnectEnumFields)1 KafkaConnectField (io.atlasmap.kafkaconnect.v2.KafkaConnectField)1 AtlasFieldActionInfo (io.atlasmap.spi.AtlasFieldActionInfo)1