use of org.eclipse.vorto.mapping.engine.MappingException in project vorto by eclipse.
the class DataMapperJxpath method mapTarget.
@Override
public Object mapTarget(PropertyValue newValue, Optional<PropertyValue> oldValue, String infomodelProperty) {
FunctionblockModel fbm = this.specification.getFunctionBlock(infomodelProperty);
if (fbm == null) {
throw new IllegalArgumentException("No property with the given name could be found in Information Model");
}
Optional<Stereotype> targetStereotype = newValue.getMeta().getStereotype(STEREOTYPE_TARGET);
if (!targetStereotype.isPresent()) {
throw new MappingException("No mapping rule defined for property");
}
Map<String, Object> jxpathContext = new HashMap<String, Object>();
Map<String, Object> param = new HashMap<String, Object>();
param.put("newValue", newValue.getValue());
param.put("oldValue", oldValue.isPresent() ? oldValue.get().getValue() : null);
jxpathContext.put("ctx", param);
final String functionName = "convert" + newValue.getMeta().getName().substring(0, 1).toUpperCase() + newValue.getMeta().getName().substring(1);
final String xpath = infomodelProperty.toLowerCase() + ":" + functionName + "(ctx)";
JXPathContext context = jxpathHelper.newContext(jxpathContext);
try {
return context.getValue(xpath);
} catch (Exception ex) {
throw new MappingException("Problem occurred during mapping", ex);
}
}
use of org.eclipse.vorto.mapping.engine.MappingException 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.mapping.engine.MappingException in project vorto by eclipse.
the class DataMapperJxpath method mapFunctionBlock.
private FunctionblockValue mapFunctionBlock(ModelProperty fbProperty, JXPathContext context) {
FunctionblockModel fbModel = specification.getFunctionBlock(fbProperty.getName());
if (!matchesCondition(fbModel, context)) {
return null;
}
FunctionblockValue fbData = new FunctionblockValue(fbModel);
for (ModelProperty statusProperty : fbModel.getStatusProperties()) {
try {
Object mapped = this.mapProperty(fbModel, statusProperty, context);
if (mapped != null) {
fbData.withStatusProperty(statusProperty.getName(), mapped);
}
} catch (JXPathNotFoundException ex) {
if (statusProperty.isMandatory()) {
return null;
}
} catch (JXPathInvalidAccessException ex) {
if (ex.getCause() instanceof JXPathNotFoundException) {
if (statusProperty.isMandatory()) {
return null;
}
}
throw new MappingException("A problem occured during mapping", ex);
}
}
for (ModelProperty configProperty : fbModel.getConfigurationProperties()) {
try {
Object mapped = this.mapProperty(fbModel, configProperty, context);
if (mapped != null) {
fbData.withConfigurationProperty(configProperty.getName(), mapped);
}
} catch (JXPathNotFoundException ex) {
if (configProperty.isMandatory()) {
return null;
}
} catch (JXPathInvalidAccessException ex) {
if (ex.getCause() instanceof JXPathNotFoundException) {
if (configProperty.isMandatory()) {
return null;
}
}
throw new MappingException("A problem occured during mapping", ex);
}
}
return onlyReturnIfPopulated(fbData);
}
Aggregations