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;
}
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;
}
}
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);
}
}
}
}
}
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");
}
}
Aggregations