Search in sources :

Example 1 with IModel

use of org.eclipse.vorto.model.IModel in project vorto by eclipse.

the class DataMapperJxpath method mapProperty.

private Object mapProperty(IModel parent, ModelProperty property, JXPathContext input) {
    Optional<Stereotype> sourceStereotype = property.getStereotype(STEREOTYPE_SOURCE);
    if (sourceStereotype.isPresent() && hasXpath(sourceStereotype.get().getAttributes())) {
        String expression = replacePlaceHolders(sourceStereotype.get().getAttributes().get(ATTRIBUTE_XPATH), sourceStereotype.get().getAttributes());
        if (matchesPropertyCondition(sourceStereotype.get(), input)) {
            return input.getValue(expression);
        }
    } else if (property.getType() instanceof IModel) {
        IModel referencedModel = (IModel) property.getType();
        if (referencedModel instanceof EntityModel) {
            EntityModel entityModel = (EntityModel) referencedModel;
            EntityValue value = new EntityValue(entityModel);
            for (ModelProperty entityProperty : entityModel.getProperties()) {
                try {
                    Object mapped = this.mapProperty(entityModel, entityProperty, input);
                    if (mapped != null) {
                        value.withProperty(entityProperty.getName(), mapped);
                    }
                } catch (JXPathNotFoundException ex) {
                    if (entityProperty.isMandatory()) {
                        return null;
                    }
                } catch (JXPathInvalidAccessException ex) {
                    if (ex.getCause() instanceof JXPathNotFoundException) {
                        if (entityProperty.isMandatory()) {
                            return null;
                        }
                    }
                    throw new MappingException("A problem occured during mapping", ex);
                }
            }
            return onlyReturnIfPopulated(value);
        } else if (referencedModel instanceof EnumModel) {
            EnumModel enumModel = (EnumModel) referencedModel;
            EnumValue value = new EnumValue(enumModel);
            if (sourceStereotype.isPresent() && hasXpath(sourceStereotype.get().getAttributes())) {
                String expression = replacePlaceHolders(sourceStereotype.get().getAttributes().get(ATTRIBUTE_XPATH), sourceStereotype.get().getAttributes());
                if (matchesPropertyCondition(sourceStereotype.get(), input)) {
                    Object mappedEnumValue = input.getValue(expression);
                    if (mappedEnumValue instanceof String) {
                        value.setValue((String) mappedEnumValue);
                    }
                    return value;
                }
            }
        }
    }
    return null;
}
Also used : IModel(org.eclipse.vorto.model.IModel) JXPathNotFoundException(org.apache.commons.jxpath.JXPathNotFoundException) EnumValue(org.eclipse.vorto.model.runtime.EnumValue) EntityModel(org.eclipse.vorto.model.EntityModel) Stereotype(org.eclipse.vorto.model.Stereotype) EntityValue(org.eclipse.vorto.model.runtime.EntityValue) JXPathInvalidAccessException(org.apache.commons.jxpath.JXPathInvalidAccessException) MappingException(org.eclipse.vorto.mapping.engine.MappingException) EnumModel(org.eclipse.vorto.model.EnumModel) ModelProperty(org.eclipse.vorto.model.ModelProperty)

Example 2 with IModel

use of org.eclipse.vorto.model.IModel in project vorto by eclipse.

the class DefaultPayloadMappingService method getModelContentByModelAndMappingId.

private IModel getModelContentByModelAndMappingId(final String _modelId, @PathVariable final String mappingId) {
    final ModelId modelId = ModelId.fromPrettyFormat(_modelId);
    final ModelId mappingModelId = ModelId.fromPrettyFormat(mappingId);
    IModelRepository repository = this.modelRepositoryFactory.getRepositoryByModel(modelId);
    ModelInfo vortoModelInfo = repository.getById(modelId);
    ModelInfo mappingModelInfo = this.modelRepositoryFactory.getRepositoryByModel(mappingModelId).getById(mappingModelId);
    if (vortoModelInfo == null) {
        throw new ModelNotFoundException(String.format("Could not find vorto model with ID: %s", modelId));
    } else if (mappingModelInfo == null) {
        throw new ModelNotFoundException(String.format("Could not find mapping with ID: %s", mappingId));
    }
    IModelWorkspace mappingWorkspace = getWorkspaceForModel(mappingModelInfo.getId());
    Optional<Model> model = mappingWorkspace.get().stream().filter(_model -> ModelUtils.fromEMFModelId(ModelIdFactory.newInstance(_model)).equals(vortoModelInfo.getId())).findFirst();
    if (model.isPresent()) {
        final Model flattenedModel = ModelConversionUtils.convertToFlatHierarchy(model.get());
        return ModelDtoFactory.createResource(flattenedModel, Optional.of((MappingModel) mappingWorkspace.get().stream().filter(_model -> _model instanceof MappingModel && mappingMatchesModelId((MappingModel) _model, vortoModelInfo)).findFirst().get()));
    } else {
        return null;
    }
}
Also used : IModelRepository(org.eclipse.vorto.repository.core.IModelRepository) PathVariable(org.springframework.web.bind.annotation.PathVariable) ModelIdFactory(org.eclipse.vorto.core.api.model.model.ModelIdFactory) DependencyManager(org.eclipse.vorto.repository.core.impl.utils.DependencyManager) IPayloadMappingService(org.eclipse.vorto.repository.mapping.IPayloadMappingService) MappingSpecification(org.eclipse.vorto.mapping.engine.model.spec.MappingSpecification) IUserContext(org.eclipse.vorto.repository.core.IUserContext) ModelNotFoundException(org.eclipse.vorto.repository.core.ModelNotFoundException) MappingModel(org.eclipse.vorto.core.api.model.mapping.MappingModel) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) Infomodel(org.eclipse.vorto.model.Infomodel) ModelIdToModelContentConverter(org.eclipse.vorto.repository.conversion.ModelIdToModelContentConverter) ArrayList(java.util.ArrayList) ModelConversionUtils(org.eclipse.vorto.core.api.model.ModelConversionUtils) Model(org.eclipse.vorto.core.api.model.model.Model) IModelRepository(org.eclipse.vorto.repository.core.IModelRepository) ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) HashSet(java.util.HashSet) Logger(org.apache.log4j.Logger) IWorkflowService(org.eclipse.vorto.repository.workflow.IWorkflowService) InfomodelValue(org.eclipse.vorto.model.runtime.InfomodelValue) IMappingSpecification(org.eclipse.vorto.mapping.engine.model.spec.IMappingSpecification) ByteArrayInputStream(java.io.ByteArrayInputStream) EntityModel(org.eclipse.vorto.model.EntityModel) IModelWorkspace(org.eclipse.vorto.utilities.reader.IModelWorkspace) Service(org.springframework.stereotype.Service) Map(java.util.Map) IModelRepositoryFactory(org.eclipse.vorto.repository.core.IModelRepositoryFactory) MappingSpecificationSerializer(org.eclipse.vorto.mapping.engine.serializer.MappingSpecificationSerializer) ModelProperty(org.eclipse.vorto.model.ModelProperty) IModel(org.eclipse.vorto.model.IModel) FileContent(org.eclipse.vorto.repository.core.FileContent) MappingEngine(org.eclipse.vorto.mapping.engine.MappingEngine) StringEscapeUtils(org.apache.commons.text.StringEscapeUtils) ModelDtoFactory(org.eclipse.vorto.repository.web.core.ModelDtoFactory) ModelId(org.eclipse.vorto.model.ModelId) FunctionblockModel(org.eclipse.vorto.model.FunctionblockModel) Stereotype(org.eclipse.vorto.model.Stereotype) List(java.util.List) ModelContent(org.eclipse.vorto.model.ModelContent) EnumModel(org.eclipse.vorto.model.EnumModel) ModelWorkspaceReader(org.eclipse.vorto.utilities.reader.ModelWorkspaceReader) Optional(java.util.Optional) ModelUtils(org.eclipse.vorto.repository.utils.ModelUtils) WorkflowException(org.eclipse.vorto.repository.workflow.WorkflowException) ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) ModelNotFoundException(org.eclipse.vorto.repository.core.ModelNotFoundException) MappingModel(org.eclipse.vorto.core.api.model.mapping.MappingModel) Model(org.eclipse.vorto.core.api.model.model.Model) EntityModel(org.eclipse.vorto.model.EntityModel) IModel(org.eclipse.vorto.model.IModel) FunctionblockModel(org.eclipse.vorto.model.FunctionblockModel) EnumModel(org.eclipse.vorto.model.EnumModel) ModelId(org.eclipse.vorto.model.ModelId) IModelWorkspace(org.eclipse.vorto.utilities.reader.IModelWorkspace) MappingModel(org.eclipse.vorto.core.api.model.mapping.MappingModel)

Example 3 with IModel

use of org.eclipse.vorto.model.IModel in project vorto by eclipse.

the class DefaultPayloadMappingService method addReferencesRecursive.

/**
 * Adds reference types of the given properties to the mapping Specification (needed for lookup)
 *
 * @param model to traverse properties
 */
private void addReferencesRecursive(IModel model, String targetPlatformKey) {
    if (model instanceof Infomodel) {
        Infomodel infomodel = (Infomodel) model;
        for (ModelProperty property : infomodel.getFunctionblocks()) {
            ModelId referenceModelId = (ModelId) property.getType();
            ModelId mappingId = property.getMappingReference();
            IModel referenceModel = null;
            if (mappingId != null) {
                referenceModel = getModelContentByModelAndMappingId(referenceModelId.getPrettyFormat(), mappingId.getPrettyFormat());
            } else {
                ModelContent modelContent = getModelContent(referenceModelId, targetPlatformKey);
                referenceModel = modelContent.getModels().get(modelContent.getRoot());
            }
            property.setType((FunctionblockModel) referenceModel);
            addReferencesRecursive(referenceModel, targetPlatformKey);
        }
    } else if (model instanceof EntityModel) {
        EntityModel entityModel = (EntityModel) model;
        for (ModelProperty property : entityModel.getProperties()) {
            initStereotypeIfMissing(property);
            if (property.getType() instanceof ModelId) {
                ModelId referenceModelId = (ModelId) property.getType();
                ModelId mappingId = property.getMappingReference();
                IModel referenceModel = null;
                if (mappingId != null) {
                    referenceModel = getModelContentByModelAndMappingId(referenceModelId.getPrettyFormat(), mappingId.getPrettyFormat());
                } else {
                    ModelContent modelContent = getModelContent(referenceModelId, targetPlatformKey);
                    referenceModel = modelContent.getModels().get(modelContent.getRoot());
                }
                if (referenceModel instanceof EntityModel) {
                    property.setType((EntityModel) referenceModel);
                    addReferencesRecursive(referenceModel, targetPlatformKey);
                } else {
                    property.setType((EnumModel) referenceModel);
                }
            }
        }
    } else if (model instanceof FunctionblockModel) {
        FunctionblockModel fbModel = (FunctionblockModel) model;
        for (ModelProperty property : fbModel.getProperties()) {
            initStereotypeIfMissing(property);
            if (property.getType() instanceof ModelId) {
                ModelId referenceModelId = (ModelId) property.getType();
                ModelId mappingId = property.getMappingReference();
                IModel referenceModel = null;
                if (mappingId != null) {
                    referenceModel = getModelContentByModelAndMappingId(referenceModelId.getPrettyFormat(), mappingId.getPrettyFormat());
                } else {
                    ModelContent modelContent = getModelContent(referenceModelId, targetPlatformKey);
                    referenceModel = modelContent.getModels().get(modelContent.getRoot());
                }
                if (referenceModel instanceof EntityModel) {
                    property.setType((EntityModel) referenceModel);
                    addReferencesRecursive(referenceModel, targetPlatformKey);
                } else {
                    property.setType((EnumModel) referenceModel);
                }
            }
        }
    }
}
Also used : FunctionblockModel(org.eclipse.vorto.model.FunctionblockModel) IModel(org.eclipse.vorto.model.IModel) Infomodel(org.eclipse.vorto.model.Infomodel) EnumModel(org.eclipse.vorto.model.EnumModel) ModelContent(org.eclipse.vorto.model.ModelContent) ModelProperty(org.eclipse.vorto.model.ModelProperty) EntityModel(org.eclipse.vorto.model.EntityModel) ModelId(org.eclipse.vorto.model.ModelId)

Example 4 with IModel

use of org.eclipse.vorto.model.IModel in project vorto by eclipse.

the class ModelContentToEcoreConverter method createPropertyType.

private PropertyType createPropertyType(IReferenceType referenceType, ModelBuilder<?> builder, ModelContent context) {
    if (referenceType instanceof PrimitiveType) {
        PrimitivePropertyType primitive = DatatypeFactory.eINSTANCE.createPrimitivePropertyType();
        primitive.setType(org.eclipse.vorto.core.api.model.datatype.PrimitiveType.valueOf(((PrimitiveType) referenceType).name()));
        return primitive;
    } else if (referenceType instanceof org.eclipse.vorto.model.ModelId) {
        IModel referencedModel = context.getModels().get((org.eclipse.vorto.model.ModelId) referenceType);
        Type convertedReference = (Type) convert(referencedModel, context, Optional.empty());
        builder.withReference(ModelIdFactory.newInstance(convertedReference));
        ObjectPropertyType objType = DatatypeFactory.eINSTANCE.createObjectPropertyType();
        objType.setType(convertedReference);
        return objType;
    } else if (referenceType instanceof DictionaryType) {
        DictionaryType dictionaryType = (DictionaryType) referenceType;
        DictionaryPropertyType dictionary = DatatypeFactory.eINSTANCE.createDictionaryPropertyType();
        if (dictionaryType.getKey() != null) {
            dictionary.setKeyType(createPropertyType(dictionaryType.getKey(), builder, context));
        }
        if (dictionaryType.getValue() != null) {
            dictionary.setValueType(createPropertyType(dictionaryType.getValue(), builder, context));
        }
        return dictionary;
    } else {
        throw new UnsupportedOperationException("Reference type is not valie");
    }
}
Also used : IModel(org.eclipse.vorto.model.IModel) ObjectPropertyType(org.eclipse.vorto.core.api.model.datatype.ObjectPropertyType) PrimitivePropertyType(org.eclipse.vorto.core.api.model.datatype.PrimitivePropertyType) DictionaryType(org.eclipse.vorto.model.DictionaryType) ReturnPrimitiveType(org.eclipse.vorto.core.api.model.functionblock.ReturnPrimitiveType) DictionaryPropertyType(org.eclipse.vorto.core.api.model.datatype.DictionaryPropertyType) PrimitivePropertyType(org.eclipse.vorto.core.api.model.datatype.PrimitivePropertyType) ReturnObjectType(org.eclipse.vorto.core.api.model.functionblock.ReturnObjectType) Type(org.eclipse.vorto.core.api.model.datatype.Type) ReturnType(org.eclipse.vorto.core.api.model.functionblock.ReturnType) ReturnDictonaryType(org.eclipse.vorto.core.api.model.functionblock.ReturnDictonaryType) DictionaryType(org.eclipse.vorto.model.DictionaryType) PrimitiveType(org.eclipse.vorto.model.PrimitiveType) ConstraintIntervalType(org.eclipse.vorto.core.api.model.datatype.ConstraintIntervalType) IReferenceType(org.eclipse.vorto.model.IReferenceType) PropertyType(org.eclipse.vorto.core.api.model.datatype.PropertyType) ModelType(org.eclipse.vorto.core.api.model.model.ModelType) ObjectPropertyType(org.eclipse.vorto.core.api.model.datatype.ObjectPropertyType) DictionaryPropertyType(org.eclipse.vorto.core.api.model.datatype.DictionaryPropertyType) ReturnPrimitiveType(org.eclipse.vorto.core.api.model.functionblock.ReturnPrimitiveType) PrimitiveType(org.eclipse.vorto.model.PrimitiveType) Infomodel(org.eclipse.vorto.model.Infomodel) ModelId(org.eclipse.vorto.core.api.model.model.ModelId)

Aggregations

IModel (org.eclipse.vorto.model.IModel)4 EntityModel (org.eclipse.vorto.model.EntityModel)3 EnumModel (org.eclipse.vorto.model.EnumModel)3 Infomodel (org.eclipse.vorto.model.Infomodel)3 ModelProperty (org.eclipse.vorto.model.ModelProperty)3 FunctionblockModel (org.eclipse.vorto.model.FunctionblockModel)2 ModelContent (org.eclipse.vorto.model.ModelContent)2 ModelId (org.eclipse.vorto.model.ModelId)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 JXPathInvalidAccessException (org.apache.commons.jxpath.JXPathInvalidAccessException)1 JXPathNotFoundException (org.apache.commons.jxpath.JXPathNotFoundException)1 StringEscapeUtils (org.apache.commons.text.StringEscapeUtils)1 Logger (org.apache.log4j.Logger)1 ModelConversionUtils (org.eclipse.vorto.core.api.model.ModelConversionUtils)1