Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions codbex-uoms/api/ConverterController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package api;

import gen.codbex_uoms.data.settings.UoMEntity;
import gen.codbex_uoms.data.settings.UoMRepository;

import org.eclipse.dirigible.engine.java.annotations.Documentation;
import org.eclipse.dirigible.engine.java.annotations.Inject;
import org.eclipse.dirigible.engine.java.annotations.http.Controller;
import org.eclipse.dirigible.engine.java.annotations.http.Get;
import org.eclipse.dirigible.engine.java.annotations.http.PathParam;

import org.springframework.http.HttpStatus;
import org.springframework.web.server.ResponseStatusException;

import java.util.List;
import java.util.Map;
/**
* Converts Source UoM to Target UoM the given Value
* Example: http://host:port/services/ts/codbex-uoms/api/ConverterController.ts/KGM/GRM/50
*/

@Controller
@Documentation("codbex-uoms - Converter Controller")
public class ConverterController {

@Inject
private UoMRepository repository;

@Get("/{source}/{target}/{value}")
@Documentation("Convert value from source UoM to target UoM")
public Double convertValue(
@PathParam("source") String source,
@PathParam("target") String target,
@PathParam("value") Double value) {

UoMEntity entitySource = findByISO(source);
UoMEntity entityTarget = findByISO(target);

if (entitySource.Dimension == null || entityTarget.Dimension == null) {
throw new ResponseStatusException(
HttpStatus.BAD_REQUEST,
"Invalid unit dimension configuration");
}

if (!entitySource.Dimension.equals(entityTarget.Dimension)) {
throw new ResponseStatusException(
HttpStatus.BAD_REQUEST,
"Both Source and Target Unit of Measures should have the same Dimension");
}

if (entitySource.Numerator == null
|| entitySource.Denominator == null
|| entityTarget.Numerator == null
|| entityTarget.Denominator == null) {

throw new ResponseStatusException(
HttpStatus.BAD_REQUEST,
"Invalid conversion factors defined");
}

if (entitySource.Denominator == 0
|| entityTarget.Numerator == 0) {

throw new ResponseStatusException(
HttpStatus.BAD_REQUEST,
"Invalid conversion configuration (division by zero)");
}

double valueBase =
value * entitySource.Numerator.doubleValue()
/ entitySource.Denominator.doubleValue();

double valueTarget =
valueBase * entityTarget.Denominator.doubleValue()
/ entityTarget.Numerator.doubleValue();

return valueTarget;
}

private UoMEntity findByISO(String iso) {

List < UoMEntity > result = repository.query(
"from UoMEntity e where e.ISO = :iso",
Map.of("iso", iso));

if (result.isEmpty()) {
throw new ResponseStatusException(
HttpStatus.NOT_FOUND,
"Unit of Measure not found: [" + iso + "]");
}

return result.get(0);
}
}
```
91 changes: 0 additions & 91 deletions codbex-uoms/api/ConverterController.ts

This file was deleted.

28 changes: 22 additions & 6 deletions codbex-uoms/codbex-uoms.gen
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
{
"tablePrefix": "CODBEX_",
"brand": "codbex",
"brandUrl": "https://www.codbex.com",
"title": "Units of Measurement Mangement Application",
"description": "Managing units of measurements data",
"brandUrl": "https://www.codbex.com/",
"title": "UoMs Management Module",
"description": "Managing UoMs Data",
"projectName": "codbex-uoms",
"workspaceName": "workspace",
"filePath": "codbex-uoms.model",
"templateId": "template-application-angular-v2/template/template.js",
"templateId": "template-application-angular-java/template/template.js",
"fileName": "codbex-uoms",
"genFolderName": "codbex-uoms",
"javaRuntime": true,
"javaGenFolderName": "codbex_uoms",
"dataSource": "DefaultDB",
"perspectives": {
"Settings": {
Expand Down Expand Up @@ -66,6 +68,7 @@
"widgetDropdownControllerUrl": "",
"dataTypeJava": "int",
"dataTypeTypescript": "number",
"dataTypeJavaClass": "Integer",
"inputRule": ""
},
{
Expand Down Expand Up @@ -93,6 +96,7 @@
"widgetDropdownControllerUrl": "",
"dataTypeJava": "string",
"dataTypeTypescript": "string",
"dataTypeJavaClass": "String",
"minLength": 0,
"maxLength": 100,
"inputRule": ""
Expand Down Expand Up @@ -122,6 +126,7 @@
"widgetDropdownControllerUrl": "",
"dataTypeJava": "string",
"dataTypeTypescript": "string",
"dataTypeJavaClass": "String",
"minLength": 0,
"maxLength": 20,
"inputRule": ""
Expand Down Expand Up @@ -154,6 +159,7 @@
"tooltip": "Dimension",
"type": "SETTING",
"dataSource": "DefaultDB",
"javaPerspectiveName": "settings",
"referencedProjections": [],
"primaryKeys": [
"Id"
Expand Down Expand Up @@ -186,6 +192,7 @@
"widgetDropdownControllerUrl": "",
"dataTypeJava": "int",
"dataTypeTypescript": "number",
"dataTypeJavaClass": "Integer",
"inputRule": ""
},
{
Expand Down Expand Up @@ -213,6 +220,7 @@
"widgetDropdownControllerUrl": "",
"dataTypeJava": "string",
"dataTypeTypescript": "string",
"dataTypeJavaClass": "String",
"minLength": 0,
"maxLength": 100,
"inputRule": ""
Expand Down Expand Up @@ -242,6 +250,7 @@
"widgetDropdownControllerUrl": "",
"dataTypeJava": "string",
"dataTypeTypescript": "string",
"dataTypeJavaClass": "String",
"minLength": 0,
"maxLength": 20,
"inputRule": ""
Expand Down Expand Up @@ -272,10 +281,11 @@
"isCalculatedProperty": false,
"isReadOnlyProperty": false,
"widgetLabel": "Dimension",
"widgetDropdownUrl": "/services/ts/codbex-uoms/gen/codbex-uoms/api/Settings/DimensionService.ts",
"widgetDropdownControllerUrl": "/services/ts/codbex-uoms/gen/codbex-uoms/api/Settings/DimensionController.ts",
"widgetDropdownUrl": "/services/java/codbex-uoms/gen/codbex_uoms/api/settings/DimensionController",
"widgetDropdownControllerUrl": "/services/java/codbex-uoms/gen/codbex_uoms/api/settings/DimensionController",
"dataTypeJava": "int",
"dataTypeTypescript": "number",
"dataTypeJavaClass": "Integer",
"inputRule": ""
},
{
Expand Down Expand Up @@ -307,6 +317,7 @@
"widgetDropdownControllerUrl": "",
"dataTypeJava": "string",
"dataTypeTypescript": "string",
"dataTypeJavaClass": "String",
"minLength": 0,
"maxLength": 20,
"inputRule": ""
Expand Down Expand Up @@ -335,6 +346,7 @@
"widgetDropdownControllerUrl": "",
"dataTypeJava": "long",
"dataTypeTypescript": "number",
"dataTypeJavaClass": "Long",
"inputRule": ""
},
{
Expand All @@ -361,6 +373,7 @@
"widgetDropdownControllerUrl": "",
"dataTypeJava": "long",
"dataTypeTypescript": "number",
"dataTypeJavaClass": "Long",
"inputRule": ""
},
{
Expand Down Expand Up @@ -388,6 +401,7 @@
"widgetDropdownControllerUrl": "",
"dataTypeJava": "int",
"dataTypeTypescript": "number",
"dataTypeJavaClass": "Integer",
"inputRule": ""
},
{
Expand Down Expand Up @@ -415,6 +429,7 @@
"widgetDropdownControllerUrl": "",
"dataTypeJava": "boolean",
"dataTypeTypescript": "boolean",
"dataTypeJavaClass": "Boolean",
"inputRule": ""
}
],
Expand Down Expand Up @@ -445,6 +460,7 @@
"tooltip": "Unit of Measures",
"type": "SETTING",
"dataSource": "DefaultDB",
"javaPerspectiveName": "settings",
"referencedProjections": [],
"primaryKeys": [
"Id"
Expand Down
Loading
Loading