Skip to content
Merged
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
84 changes: 84 additions & 0 deletions codbex-uoms/api/ConverterController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package api;

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

import org.eclipse.dirigible.sdk.platform.Documentation;
import org.eclipse.dirigible.sdk.component.Inject;
import org.eclipse.dirigible.sdk.http.Controller;
import org.eclipse.dirigible.sdk.http.Get;
import org.eclipse.dirigible.sdk.http.PathParam;
import org.eclipse.dirigible.sdk.http.Response;

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 || entitySource.Numerator == null || entitySource.Denominator == null
|| entitySource.Numerator == 0 || entitySource.Denominator == 0) {

Response.setStatus(400);
Response.println("Invalid Source configuration");
return null;
}

if (entityTarget.Dimension == null || entityTarget.Numerator == null || entityTarget.Denominator == null
|| entityTarget.Numerator == 0 || entityTarget.Denominator == 0) {

Response.setStatus(400);
Response.println("Invalid Target configuration");
return null;
}

if (!entitySource.Dimension.equals(entityTarget.Dimension)) {
Response.setStatus(400);
Response.println("Dimensions mismatch");
return null;
}

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()) {
Response.setStatus(404);
Response.println("Unit of Measure not found: [" + iso + "]");
return new UoMEntity();
}

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