diff --git a/codbex-uoms/api/ConverterController.java b/codbex-uoms/api/ConverterController.java new file mode 100644 index 0000000..6cbe9a3 --- /dev/null +++ b/codbex-uoms/api/ConverterController.java @@ -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); + } +} +``` diff --git a/codbex-uoms/api/ConverterController.ts b/codbex-uoms/api/ConverterController.ts deleted file mode 100644 index c03cefd..0000000 --- a/codbex-uoms/api/ConverterController.ts +++ /dev/null @@ -1,91 +0,0 @@ -import { Controller, Get, Documentation } from "@aerokit/sdk/http" -import { Operator } from "@aerokit/sdk/db" -import { UoMRepository } from "../gen/codbex-uoms/data/Settings/UoMRepository" -import { HttpUtils } from "@aerokit/sdk/http/utils" - -/** - * Converts Source UoM to Target UoM the given Value - * Example: http://host:port/services/ts/codbex-uoms/api/ConverterController.ts/KGM/GRM/50 - */ - -@Documentation('codbex-uoms - Converter Controller') -@Controller -class ConverterController { - - private readonly repository = new UoMRepository(); - - @Get("/:source/:target/:value") - public convertValue(_: any, ctx: any): number | undefined { - try { - const source = ctx.pathParameters.source; - const target = ctx.pathParameters.target; - const value = parseFloat(ctx.pathParameters.value); - const entitySource = this.repository.findAll({ - conditions: [ - { - operator: Operator.EQ, - propertyName: 'ISO', - value: source - } - ] - })[0]; - - const entityTarget = this.repository.findAll({ - conditions: [ - { - operator: Operator.EQ, - propertyName: 'ISO', - value: target - } - ] - })[0]; - - if (entitySource && entityTarget) { - if (entitySource.Dimension !== entityTarget.Dimension) { - HttpUtils.sendResponseBadRequest( - "Both Source and Target Unit of Measures should have the same Dimension" - ); - return; - } - - if ( - entitySource.Numerator == null || - entitySource.Denominator == null || - entityTarget.Numerator == null || - entityTarget.Denominator == null - ) { - HttpUtils.sendResponseBadRequest( - "Invalid conversion factors defined" - ); - return; - } - - if (entitySource.Denominator === 0 || entityTarget.Numerator === 0) { - HttpUtils.sendResponseBadRequest( - "Invalid conversion configuration (division by zero)" - ); - return; - } - - const valueBase = value * entitySource.Numerator / entitySource.Denominator; - const valueTarget = valueBase * entityTarget.Denominator / entityTarget.Numerator; - - return valueTarget; - } else { - HttpUtils.sendResponseNotFound("Unit of Measures not found: [" + source + "] and/or [" + target + "]"); - } - } catch (error: any) { - this.handleError(error); - } - } - - private handleError(error: any) { - if (error.name === "ForbiddenError") { - HttpUtils.sendForbiddenRequest(error.message); - } else if (error.name === "ValidationError") { - HttpUtils.sendResponseBadRequest(error.message); - } else { - HttpUtils.sendInternalServerError(error.message); - } - } -} diff --git a/codbex-uoms/codbex-uoms.gen b/codbex-uoms/codbex-uoms.gen index 6d553e0..8d5410b 100644 --- a/codbex-uoms/codbex-uoms.gen +++ b/codbex-uoms/codbex-uoms.gen @@ -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": { @@ -66,6 +68,7 @@ "widgetDropdownControllerUrl": "", "dataTypeJava": "int", "dataTypeTypescript": "number", + "dataTypeJavaClass": "Integer", "inputRule": "" }, { @@ -93,6 +96,7 @@ "widgetDropdownControllerUrl": "", "dataTypeJava": "string", "dataTypeTypescript": "string", + "dataTypeJavaClass": "String", "minLength": 0, "maxLength": 100, "inputRule": "" @@ -122,6 +126,7 @@ "widgetDropdownControllerUrl": "", "dataTypeJava": "string", "dataTypeTypescript": "string", + "dataTypeJavaClass": "String", "minLength": 0, "maxLength": 20, "inputRule": "" @@ -154,6 +159,7 @@ "tooltip": "Dimension", "type": "SETTING", "dataSource": "DefaultDB", + "javaPerspectiveName": "settings", "referencedProjections": [], "primaryKeys": [ "Id" @@ -186,6 +192,7 @@ "widgetDropdownControllerUrl": "", "dataTypeJava": "int", "dataTypeTypescript": "number", + "dataTypeJavaClass": "Integer", "inputRule": "" }, { @@ -213,6 +220,7 @@ "widgetDropdownControllerUrl": "", "dataTypeJava": "string", "dataTypeTypescript": "string", + "dataTypeJavaClass": "String", "minLength": 0, "maxLength": 100, "inputRule": "" @@ -242,6 +250,7 @@ "widgetDropdownControllerUrl": "", "dataTypeJava": "string", "dataTypeTypescript": "string", + "dataTypeJavaClass": "String", "minLength": 0, "maxLength": 20, "inputRule": "" @@ -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": "" }, { @@ -307,6 +317,7 @@ "widgetDropdownControllerUrl": "", "dataTypeJava": "string", "dataTypeTypescript": "string", + "dataTypeJavaClass": "String", "minLength": 0, "maxLength": 20, "inputRule": "" @@ -335,6 +346,7 @@ "widgetDropdownControllerUrl": "", "dataTypeJava": "long", "dataTypeTypescript": "number", + "dataTypeJavaClass": "Long", "inputRule": "" }, { @@ -361,6 +373,7 @@ "widgetDropdownControllerUrl": "", "dataTypeJava": "long", "dataTypeTypescript": "number", + "dataTypeJavaClass": "Long", "inputRule": "" }, { @@ -388,6 +401,7 @@ "widgetDropdownControllerUrl": "", "dataTypeJava": "int", "dataTypeTypescript": "number", + "dataTypeJavaClass": "Integer", "inputRule": "" }, { @@ -415,6 +429,7 @@ "widgetDropdownControllerUrl": "", "dataTypeJava": "boolean", "dataTypeTypescript": "boolean", + "dataTypeJavaClass": "Boolean", "inputRule": "" } ], @@ -445,6 +460,7 @@ "tooltip": "Unit of Measures", "type": "SETTING", "dataSource": "DefaultDB", + "javaPerspectiveName": "settings", "referencedProjections": [], "primaryKeys": [ "Id" diff --git a/codbex-uoms/gen/codbex-uoms/api/Settings/DimensionController.ts b/codbex-uoms/gen/codbex-uoms/api/Settings/DimensionController.ts deleted file mode 100644 index f2a2382..0000000 --- a/codbex-uoms/gen/codbex-uoms/api/Settings/DimensionController.ts +++ /dev/null @@ -1,181 +0,0 @@ -import { Controller, Get, Post, Put, Delete, Documentation, request, response } from '@aerokit/sdk/http' -import { HttpUtils } from "@aerokit/sdk/http/utils"; -import { ValidationError } from '@aerokit/sdk/http/errors' -import { ForbiddenError } from '@aerokit/sdk/http/errors' -import { user } from '@aerokit/sdk/security' -import { Options } from '@aerokit/sdk/db' -import { Extensions } from "@aerokit/sdk/extensions" -import { Injected, Inject } from '@aerokit/sdk/component' -import { DimensionRepository } from '../../data/Settings/DimensionRepository' -import { DimensionEntity } from '../../data/Settings/DimensionEntity' - -const validationModules = await Extensions.loadExtensionModules('codbex-uoms-Settings-Dimension', ['validate']); - -@Controller -@Documentation('codbex-uoms - Dimension Controller') -@Injected() -class DimensionController { - - @Inject('DimensionRepository') - private readonly repository!: DimensionRepository; - - @Get('/') - @Documentation('Get All Dimension') - public getAll(_: any, ctx: any): DimensionEntity[] { - try { - this.checkPermissions('read'); - const options: Options = { - limit: ctx.queryParameters["$limit"] ? parseInt(ctx.queryParameters["$limit"]) : 20, - offset: ctx.queryParameters["$offset"] ? parseInt(ctx.queryParameters["$offset"]) : 0, - language: request.getLocale()?.split("_")[0] - }; - - return this.repository.findAll(options); - } catch (error: any) { - this.handleError(error); - } - return undefined as any; - } - - @Post('/') - @Documentation('Create Dimension') - public create(entity: DimensionEntity): DimensionEntity { - try { - this.checkPermissions('write'); - this.validateEntity(entity); - entity.Id = this.repository.create(entity) as any; - response.setHeader('Content-Location', '/services/ts/codbex-uoms/gen/codbex-uoms/api/Settings/DimensionController.ts/' + entity.Id); - response.setStatus(response.CREATED); - return entity; - } catch (error: any) { - this.handleError(error); - } - return undefined as any; - } - - @Get('/count') - @Documentation('Count Dimension') - public count(): { count: number } { - try { - this.checkPermissions('read'); - return { count: this.repository.count() }; - } catch (error: any) { - this.handleError(error); - } - return undefined as any; - } - - @Post('/count') - @Documentation('Count Dimension with filter') - public countWithFilter(filter: any): { count: number } { - try { - this.checkPermissions('read'); - return { count: this.repository.count(filter) }; - } catch (error: any) { - this.handleError(error); - } - return undefined as any; - } - - @Post('/search') - @Documentation('Search Dimension') - public search(filter: any): DimensionEntity[] { - try { - this.checkPermissions('read'); - return this.repository.findAll(filter); - } catch (error: any) { - this.handleError(error); - } - return undefined as any; - } - - @Get('/:id') - @Documentation('Get Dimension by id') - public getById(_: any, ctx: any): DimensionEntity { - try { - this.checkPermissions('read'); - const id = parseInt(ctx.pathParameters.id); - const options: Options = { - language: request.getLocale()?.split("_")[0] - }; - const entity = this.repository.findById(id, options); - if (entity) { - return entity; - } else { - HttpUtils.sendResponseNotFound('Dimension not found'); - } - } catch (error: any) { - this.handleError(error); - } - return undefined as any; - } - - @Put('/:id') - @Documentation('Update Dimension by id') - public update(entity: DimensionEntity, ctx: any): DimensionEntity { - try { - this.checkPermissions('write'); - const id = parseInt(ctx.pathParameters.id); - entity.Id = id; - this.validateEntity(entity); - this.repository.update(entity); - return entity; - } catch (error: any) { - this.handleError(error); - } - return undefined as any; - } - - @Delete('/:id') - @Documentation('Delete Dimension by id') - public deleteById(_: any, ctx: any): void { - try { - this.checkPermissions('write'); - const id = parseInt(ctx.pathParameters.id); - const entity = this.repository.findById(id); - if (entity) { - this.repository.deleteById(id); - HttpUtils.sendResponseNoContent(); - } else { - HttpUtils.sendResponseNotFound('Dimension not found'); - } - } catch (error: any) { - this.handleError(error); - } - } - - private handleError(error: any) { - if (error.name === 'ForbiddenError') { - HttpUtils.sendForbiddenRequest(error.message); - } else if (error.name === 'ValidationError') { - HttpUtils.sendResponseBadRequest(error.message); - } else { - HttpUtils.sendInternalServerError(error.message); - } - } - - private checkPermissions(operationType: string) { - if (operationType === 'read' && !(user.isInRole('codbex-uoms.Dimensions.DimensionReadOnly') || user.isInRole('codbex-uoms.Dimensions.DimensionFullAccess'))) { - throw new ForbiddenError(); - } - if (operationType === 'write' && !user.isInRole('codbex-uoms.Dimensions.DimensionFullAccess')) { - throw new ForbiddenError(); - } - } - - private validateEntity(entity: any): void { - if (entity.Name === null || entity.Name === undefined) { - throw new ValidationError(`The 'Name' property is required, provide a valid value`); - } - if (entity.Name?.length > 100) { - throw new ValidationError(`The 'Name' exceeds the maximum length of [100] characters`); - } - if (entity.SAP?.length > 20) { - throw new ValidationError(`The 'SAP' exceeds the maximum length of [20] characters`); - } - for (const next of validationModules) { - next.validate(entity); - } - } - -} diff --git a/codbex-uoms/gen/codbex-uoms/api/Settings/UoMController.ts b/codbex-uoms/gen/codbex-uoms/api/Settings/UoMController.ts deleted file mode 100644 index 248a88d..0000000 --- a/codbex-uoms/gen/codbex-uoms/api/Settings/UoMController.ts +++ /dev/null @@ -1,199 +0,0 @@ -import { Controller, Get, Post, Put, Delete, Documentation, request, response } from '@aerokit/sdk/http' -import { HttpUtils } from "@aerokit/sdk/http/utils"; -import { ValidationError } from '@aerokit/sdk/http/errors' -import { ForbiddenError } from '@aerokit/sdk/http/errors' -import { user } from '@aerokit/sdk/security' -import { Options } from '@aerokit/sdk/db' -import { Extensions } from "@aerokit/sdk/extensions" -import { Injected, Inject } from '@aerokit/sdk/component' -import { UoMRepository } from '../../data/Settings/UoMRepository' -import { UoMEntity } from '../../data/Settings/UoMEntity' - -const validationModules = await Extensions.loadExtensionModules('codbex-uoms-Settings-UoM', ['validate']); - -@Controller -@Documentation('codbex-uoms - UoM Controller') -@Injected() -class UoMController { - - @Inject('UoMRepository') - private readonly repository!: UoMRepository; - - @Get('/') - @Documentation('Get All UoM') - public getAll(_: any, ctx: any): UoMEntity[] { - try { - this.checkPermissions('read'); - const options: Options = { - limit: ctx.queryParameters["$limit"] ? parseInt(ctx.queryParameters["$limit"]) : 20, - offset: ctx.queryParameters["$offset"] ? parseInt(ctx.queryParameters["$offset"]) : 0, - language: request.getLocale()?.split("_")[0] - }; - - return this.repository.findAll(options); - } catch (error: any) { - this.handleError(error); - } - return undefined as any; - } - - @Post('/') - @Documentation('Create UoM') - public create(entity: UoMEntity): UoMEntity { - try { - this.checkPermissions('write'); - this.validateEntity(entity); - entity.Id = this.repository.create(entity) as any; - response.setHeader('Content-Location', '/services/ts/codbex-uoms/gen/codbex-uoms/api/Settings/UoMController.ts/' + entity.Id); - response.setStatus(response.CREATED); - return entity; - } catch (error: any) { - this.handleError(error); - } - return undefined as any; - } - - @Get('/count') - @Documentation('Count UoM') - public count(): { count: number } { - try { - this.checkPermissions('read'); - return { count: this.repository.count() }; - } catch (error: any) { - this.handleError(error); - } - return undefined as any; - } - - @Post('/count') - @Documentation('Count UoM with filter') - public countWithFilter(filter: any): { count: number } { - try { - this.checkPermissions('read'); - return { count: this.repository.count(filter) }; - } catch (error: any) { - this.handleError(error); - } - return undefined as any; - } - - @Post('/search') - @Documentation('Search UoM') - public search(filter: any): UoMEntity[] { - try { - this.checkPermissions('read'); - return this.repository.findAll(filter); - } catch (error: any) { - this.handleError(error); - } - return undefined as any; - } - - @Get('/:id') - @Documentation('Get UoM by id') - public getById(_: any, ctx: any): UoMEntity { - try { - this.checkPermissions('read'); - const id = parseInt(ctx.pathParameters.id); - const options: Options = { - language: request.getLocale()?.split("_")[0] - }; - const entity = this.repository.findById(id, options); - if (entity) { - return entity; - } else { - HttpUtils.sendResponseNotFound('UoM not found'); - } - } catch (error: any) { - this.handleError(error); - } - return undefined as any; - } - - @Put('/:id') - @Documentation('Update UoM by id') - public update(entity: UoMEntity, ctx: any): UoMEntity { - try { - this.checkPermissions('write'); - const id = parseInt(ctx.pathParameters.id); - entity.Id = id; - this.validateEntity(entity); - this.repository.update(entity); - return entity; - } catch (error: any) { - this.handleError(error); - } - return undefined as any; - } - - @Delete('/:id') - @Documentation('Delete UoM by id') - public deleteById(_: any, ctx: any): void { - try { - this.checkPermissions('write'); - const id = parseInt(ctx.pathParameters.id); - const entity = this.repository.findById(id); - if (entity) { - this.repository.deleteById(id); - HttpUtils.sendResponseNoContent(); - } else { - HttpUtils.sendResponseNotFound('UoM not found'); - } - } catch (error: any) { - this.handleError(error); - } - } - - private handleError(error: any) { - if (error.name === 'ForbiddenError') { - HttpUtils.sendForbiddenRequest(error.message); - } else if (error.name === 'ValidationError') { - HttpUtils.sendResponseBadRequest(error.message); - } else { - HttpUtils.sendInternalServerError(error.message); - } - } - - private checkPermissions(operationType: string) { - if (operationType === 'read' && !(user.isInRole('codbex-uoms.UnitsOfMeasures.UoMReadOnly') || user.isInRole('codbex-uoms.UnitsOfMeasures.UoMFullAccess'))) { - throw new ForbiddenError(); - } - if (operationType === 'write' && !user.isInRole('codbex-uoms.UnitsOfMeasures.UoMFullAccess')) { - throw new ForbiddenError(); - } - } - - private validateEntity(entity: any): void { - if (entity.Name === null || entity.Name === undefined) { - throw new ValidationError(`The 'Name' property is required, provide a valid value`); - } - if (entity.Name?.length > 100) { - throw new ValidationError(`The 'Name' exceeds the maximum length of [100] characters`); - } - if (entity.ISO === null || entity.ISO === undefined) { - throw new ValidationError(`The 'ISO' property is required, provide a valid value`); - } - if (entity.ISO?.length > 20) { - throw new ValidationError(`The 'ISO' exceeds the maximum length of [20] characters`); - } - if (entity.Dimension === null || entity.Dimension === undefined) { - throw new ValidationError(`The 'Dimension' property is required, provide a valid value`); - } - if (entity.SAP?.length > 20) { - throw new ValidationError(`The 'SAP' exceeds the maximum length of [20] characters`); - } - if (entity.Numerator === null || entity.Numerator === undefined) { - throw new ValidationError(`The 'Numerator' property is required, provide a valid value`); - } - if (entity.Denominator === null || entity.Denominator === undefined) { - throw new ValidationError(`The 'Denominator' property is required, provide a valid value`); - } - if (entity.Rounding === null || entity.Rounding === undefined) { - throw new ValidationError(`The 'Rounding' property is required, provide a valid value`); - } - for (const next of validationModules) { - next.validate(entity); - } - } - -} diff --git a/codbex-uoms/gen/codbex-uoms/data/Settings/DimensionEntity.ts b/codbex-uoms/gen/codbex-uoms/data/Settings/DimensionEntity.ts deleted file mode 100644 index 6818844..0000000 --- a/codbex-uoms/gen/codbex-uoms/data/Settings/DimensionEntity.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { Entity, Table, Id, Generated, Column, Documentation, CreatedAt, CreatedBy, UpdatedAt, UpdatedBy} from '@aerokit/sdk/db' - -@Entity('DimensionEntity') -@Table('CODBEX_DIMENSION') -@Documentation('Dimension entity mapping') -export class DimensionEntity { - - @Id() - @Generated('sequence') - @Documentation('Id') - @Column({ - name: 'DIMENSION_ID', - type: 'integer', - }) - public Id?: number; - - @Documentation('Name') - @Column({ - name: 'DIMENSION_NAME', - type: 'string', - length: 100, - }) - public Name!: string; - - @Documentation('SAP') - @Column({ - name: 'DIMENSION_SAP', - type: 'string', - length: 20, - nullable: true, - }) - public SAP?: string; - -} - -(new DimensionEntity()); diff --git a/codbex-uoms/gen/codbex-uoms/data/Settings/DimensionRepository.ts b/codbex-uoms/gen/codbex-uoms/data/Settings/DimensionRepository.ts deleted file mode 100644 index 5d66224..0000000 --- a/codbex-uoms/gen/codbex-uoms/data/Settings/DimensionRepository.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { Repository, EntityEvent, EntityConstructor, Options } from '@aerokit/sdk/db' -import { Component } from '@aerokit/sdk/component' -import { Producer } from '@aerokit/sdk/messaging' -import { Extensions } from '@aerokit/sdk/extensions' -import { DimensionEntity } from './DimensionEntity' - -@Component('DimensionRepository') -export class DimensionRepository extends Repository { - - constructor() { - super((DimensionEntity as EntityConstructor)); - } - - protected override async triggerEvent(data: EntityEvent): Promise { - const triggerExtensions = await Extensions.loadExtensionModules('codbex-uoms-Settings-Dimension', ['trigger']); - triggerExtensions.forEach(triggerExtension => { - try { - triggerExtension.trigger(data); - } catch (error) { - console.error(error); - } - }); - Producer.topic('codbex-uoms-Settings-Dimension').send(JSON.stringify(data)); - } -} diff --git a/codbex-uoms/gen/codbex-uoms/data/Settings/UoMEntity.ts b/codbex-uoms/gen/codbex-uoms/data/Settings/UoMEntity.ts deleted file mode 100644 index 164faa3..0000000 --- a/codbex-uoms/gen/codbex-uoms/data/Settings/UoMEntity.ts +++ /dev/null @@ -1,81 +0,0 @@ -import { Entity, Table, Id, Generated, Column, Documentation, CreatedAt, CreatedBy, UpdatedAt, UpdatedBy} from '@aerokit/sdk/db' - -@Entity('UoMEntity') -@Table('CODBEX_UOM') -@Documentation('UoM entity mapping') -export class UoMEntity { - - @Id() - @Generated('sequence') - @Documentation('Id') - @Column({ - name: 'UOM_ID', - type: 'integer', - }) - public Id?: number; - - @Documentation('Name') - @Column({ - name: 'UOM_NAME', - type: 'string', - length: 100, - }) - public Name!: string; - - @Documentation('ISO') - @Column({ - name: 'UOM_ISO', - type: 'string', - length: 20, - }) - public ISO!: string; - - @Documentation('Dimension') - @Column({ - name: 'UOM_DIMENSION', - type: 'integer', - }) - public Dimension!: number; - - @Documentation('SAP') - @Column({ - name: 'UOM_SAP', - type: 'string', - length: 20, - nullable: true, - }) - public SAP?: string; - - @Documentation('Numerator') - @Column({ - name: 'UOM_NUMERATOR', - type: 'long', - }) - public Numerator!: number; - - @Documentation('Denominator') - @Column({ - name: 'UOM_DENOMINATOR', - type: 'long', - }) - public Denominator!: number; - - @Documentation('Rounding') - @Column({ - name: 'UOM_ROUNDING', - type: 'integer', - defaultValue: `0`, - }) - public Rounding?: number; - - @Documentation('Base') - @Column({ - name: 'UOM_BASE', - type: 'boolean', - defaultValue: `false`, - }) - public Base?: boolean; - -} - -(new UoMEntity()); diff --git a/codbex-uoms/gen/codbex-uoms/data/Settings/UoMRepository.ts b/codbex-uoms/gen/codbex-uoms/data/Settings/UoMRepository.ts deleted file mode 100644 index 0f38122..0000000 --- a/codbex-uoms/gen/codbex-uoms/data/Settings/UoMRepository.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { Repository, EntityEvent, EntityConstructor, Options } from '@aerokit/sdk/db' -import { Component } from '@aerokit/sdk/component' -import { Producer } from '@aerokit/sdk/messaging' -import { Extensions } from '@aerokit/sdk/extensions' -import { UoMEntity } from './UoMEntity' - -@Component('UoMRepository') -export class UoMRepository extends Repository { - - constructor() { - super((UoMEntity as EntityConstructor)); - } - - protected override async triggerEvent(data: EntityEvent): Promise { - const triggerExtensions = await Extensions.loadExtensionModules('codbex-uoms-Settings-UoM', ['trigger']); - triggerExtensions.forEach(triggerExtension => { - try { - triggerExtension.trigger(data); - } catch (error) { - console.error(error); - } - }); - Producer.topic('codbex-uoms-Settings-UoM').send(JSON.stringify(data)); - } -} diff --git a/codbex-uoms/gen/codbex-uoms/odata/codbex-uoms.odata b/codbex-uoms/gen/codbex-uoms/odata/codbex-uoms.odata deleted file mode 100644 index f149140..0000000 --- a/codbex-uoms/gen/codbex-uoms/odata/codbex-uoms.odata +++ /dev/null @@ -1,4 +0,0 @@ -{ - "namespace": "", - "entities": [] -} diff --git a/codbex-uoms/gen/codbex-uoms/ui/Settings/Dimension/controller.js b/codbex-uoms/gen/codbex-uoms/ui/Settings/Dimension/controller.js index 3bebf58..0424f8c 100644 --- a/codbex-uoms/gen/codbex-uoms/ui/Settings/Dimension/controller.js +++ b/codbex-uoms/gen/codbex-uoms/ui/Settings/Dimension/controller.js @@ -1,6 +1,6 @@ angular.module('page', ['blimpKit', 'platformView', 'platformLocale', 'EntityService']) .config(['EntityServiceProvider', (EntityServiceProvider) => { - EntityServiceProvider.baseUrl = '/services/ts/codbex-uoms/gen/codbex-uoms/api/Settings/DimensionController.ts'; + EntityServiceProvider.baseUrl = '/services/java/codbex-uoms/gen/codbex_uoms/api/settings/DimensionController'; }]) .controller('PageController', ($scope, EntityService, Extensions, LocaleService, ButtonStates) => { const Dialogs = new DialogHub(); diff --git a/codbex-uoms/gen/codbex-uoms/ui/Settings/Dimension/dialog-window/controller.js b/codbex-uoms/gen/codbex-uoms/ui/Settings/Dimension/dialog-window/controller.js index 8abf6bc..9b93446 100644 --- a/codbex-uoms/gen/codbex-uoms/ui/Settings/Dimension/dialog-window/controller.js +++ b/codbex-uoms/gen/codbex-uoms/ui/Settings/Dimension/dialog-window/controller.js @@ -1,6 +1,6 @@ angular.module('page', ['blimpKit', 'platformView', 'platformLocale', 'EntityService']) .config(['EntityServiceProvider', (EntityServiceProvider) => { - EntityServiceProvider.baseUrl = '/services/ts/codbex-uoms/gen/codbex-uoms/api/Settings/DimensionController.ts'; + EntityServiceProvider.baseUrl = '/services/java/codbex-uoms/gen/codbex_uoms/api/settings/DimensionController'; }]) .controller('PageController', ($scope, $http, ViewParameters, LocaleService, EntityService) => { const Dialogs = new DialogHub(); diff --git a/codbex-uoms/gen/codbex-uoms/ui/Settings/UoM/controller.js b/codbex-uoms/gen/codbex-uoms/ui/Settings/UoM/controller.js index 4a1ad71..44cf9d5 100644 --- a/codbex-uoms/gen/codbex-uoms/ui/Settings/UoM/controller.js +++ b/codbex-uoms/gen/codbex-uoms/ui/Settings/UoM/controller.js @@ -1,6 +1,6 @@ angular.module('page', ['blimpKit', 'platformView', 'platformLocale', 'EntityService']) .config(['EntityServiceProvider', (EntityServiceProvider) => { - EntityServiceProvider.baseUrl = '/services/ts/codbex-uoms/gen/codbex-uoms/api/Settings/UoMController.ts'; + EntityServiceProvider.baseUrl = '/services/java/codbex-uoms/gen/codbex_uoms/api/settings/UoMController'; }]) .controller('PageController', ($scope, $http, EntityService, Extensions, LocaleService, ButtonStates) => { const Dialogs = new DialogHub(); @@ -100,7 +100,7 @@ angular.module('page', ['blimpKit', 'platformView', 'platformLocale', 'EntitySer if (optionsDimensionHasMore) { const optionsDimensionSearchValues = Array.from(new Set(response.data.map(e => e.Dimension))); if (optionsDimensionSearchValues.length > 0) { - $http.post('/services/ts/codbex-uoms/gen/codbex-uoms/api/Settings/DimensionController.ts/search', { + $http.post('/services/java/codbex-uoms/gen/codbex_uoms/api/settings/DimensionController/search', { conditions: [ { propertyName: 'Id', operator: 'IN', value: optionsDimensionSearchValues } ] @@ -231,9 +231,9 @@ angular.module('page', ['blimpKit', 'platformView', 'platformLocale', 'EntitySer let optionsDimensionHasMore = true; - $http.get('/services/ts/codbex-uoms/gen/codbex-uoms/api/Settings/DimensionController.ts/count').then((response) => { + $http.get('/services/java/codbex-uoms/gen/codbex_uoms/api/settings/DimensionController/count').then((response) => { const optionsDimensionCount = response.data.count; - $http.get('/services/ts/codbex-uoms/gen/codbex-uoms/api/Settings/DimensionController.ts').then((response) => { + $http.get('/services/java/codbex-uoms/gen/codbex_uoms/api/settings/DimensionController').then((response) => { $scope.optionsDimension = response.data.map(e => ({ value: e.Id, text: e.Name diff --git a/codbex-uoms/gen/codbex-uoms/ui/Settings/UoM/dialog-filter/controller.js b/codbex-uoms/gen/codbex-uoms/ui/Settings/UoM/dialog-filter/controller.js index 7a023e4..6f56304 100644 --- a/codbex-uoms/gen/codbex-uoms/ui/Settings/UoM/dialog-filter/controller.js +++ b/codbex-uoms/gen/codbex-uoms/ui/Settings/UoM/dialog-filter/controller.js @@ -106,7 +106,7 @@ angular.module('page', ['blimpKit', 'platformView', 'platformLocale']).controlle $scope.loadMoreOptionsDimension = () => { const limit = 20; $scope.optionsDimensionLoading = true; - $http.get(`/services/ts/codbex-uoms/gen/codbex-uoms/api/Settings/DimensionController.ts?$limit=${limit}&$offset=${++loadMoreOptionsDimensionCounter * limit}`) + $http.get(`/services/java/codbex-uoms/gen/codbex_uoms/api/settings/DimensionController?$limit=${limit}&$offset=${++loadMoreOptionsDimensionCounter * limit}`) .then((response) => { const optionValues = allValuesDimension.map(e => e.value); const resultValues = response.data.map(e => ({ @@ -156,7 +156,7 @@ angular.module('page', ['blimpKit', 'platformView', 'platformLocale']).controlle } }) if (!cacheHit) { - $http.post('/services/ts/codbex-uoms/gen/codbex-uoms/api/Settings/DimensionController.ts/search', { + $http.post('/services/java/codbex-uoms/gen/codbex_uoms/api/settings/DimensionController/search', { conditions: [ { propertyName: 'Name', operator: 'LIKE', value: `${event.originalEvent.target.value}%` } ] diff --git a/codbex-uoms/gen/codbex-uoms/ui/Settings/UoM/dialog-window/controller.js b/codbex-uoms/gen/codbex-uoms/ui/Settings/UoM/dialog-window/controller.js index 758f681..ce5572f 100644 --- a/codbex-uoms/gen/codbex-uoms/ui/Settings/UoM/dialog-window/controller.js +++ b/codbex-uoms/gen/codbex-uoms/ui/Settings/UoM/dialog-window/controller.js @@ -1,6 +1,6 @@ angular.module('page', ['blimpKit', 'platformView', 'platformLocale', 'EntityService']) .config(['EntityServiceProvider', (EntityServiceProvider) => { - EntityServiceProvider.baseUrl = '/services/ts/codbex-uoms/gen/codbex-uoms/api/Settings/UoMController.ts'; + EntityServiceProvider.baseUrl = '/services/java/codbex-uoms/gen/codbex_uoms/api/settings/UoMController'; }]) .controller('PageController', ($scope, $http, ViewParameters, LocaleService, EntityService) => { const Dialogs = new DialogHub(); @@ -81,11 +81,11 @@ angular.module('page', ['blimpKit', 'platformView', 'platformLocale', 'EntitySer }); }; - $scope.serviceDimension = '/services/ts/codbex-uoms/gen/codbex-uoms/api/Settings/DimensionController.ts'; + $scope.serviceDimension = '/services/java/codbex-uoms/gen/codbex_uoms/api/settings/DimensionController'; $scope.optionsDimension = []; - $http.get('/services/ts/codbex-uoms/gen/codbex-uoms/api/Settings/DimensionController.ts').then((response) => { + $http.get('/services/java/codbex-uoms/gen/codbex_uoms/api/settings/DimensionController').then((response) => { $scope.optionsDimension = response.data.map(e => ({ value: e.Id, text: e.Name @@ -109,7 +109,7 @@ angular.module('page', ['blimpKit', 'platformView', 'platformLocale', 'EntitySer $scope.loadMoreOptionsDimension = () => { const limit = 20; $scope.optionsDimensionLoading = true; - $http.get(`/services/ts/codbex-uoms/gen/codbex-uoms/api/Settings/DimensionController.ts?$limit=${limit}&$offset=${++loadMoreOptionsDimensionCounter * limit}`) + $http.get(`/services/java/codbex-uoms/gen/codbex_uoms/api/settings/DimensionController?$limit=${limit}&$offset=${++loadMoreOptionsDimensionCounter * limit}`) .then((response) => { const optionValues = allValuesDimension.map(e => e.value); const resultValues = response.data.map(e => ({ @@ -159,7 +159,7 @@ angular.module('page', ['blimpKit', 'platformView', 'platformLocale', 'EntitySer } }) if (!cacheHit) { - $http.post('/services/ts/codbex-uoms/gen/codbex-uoms/api/Settings/DimensionController.ts/search', { + $http.post('/services/java/codbex-uoms/gen/codbex_uoms/api/settings/DimensionController/search', { conditions: [ { propertyName: 'Name', operator: 'LIKE', value: `${event.originalEvent.target.value}%` } ] diff --git a/codbex-uoms/gen/codbex_uoms/api/settings/DimensionController.java b/codbex-uoms/gen/codbex_uoms/api/settings/DimensionController.java new file mode 100644 index 0000000..deac7c1 --- /dev/null +++ b/codbex-uoms/gen/codbex_uoms/api/settings/DimensionController.java @@ -0,0 +1,173 @@ +package gen.codbex_uoms.api.settings; + +import gen.codbex_uoms.data.settings.DimensionEntity; +import gen.codbex_uoms.data.settings.DimensionRepository; + +import org.eclipse.dirigible.components.api.security.UserFacade; +import org.eclipse.dirigible.engine.java.annotations.Documentation; +import org.eclipse.dirigible.engine.java.annotations.Inject; +import org.eclipse.dirigible.engine.java.annotations.http.Body; +import org.eclipse.dirigible.engine.java.annotations.http.Controller; +import org.eclipse.dirigible.engine.java.annotations.http.Delete; +import org.eclipse.dirigible.engine.java.annotations.http.Get; +import org.eclipse.dirigible.engine.java.annotations.http.PathParam; +import org.eclipse.dirigible.engine.java.annotations.http.Post; +import org.eclipse.dirigible.engine.java.annotations.http.Put; +import org.eclipse.dirigible.engine.java.annotations.http.QueryParam; +import org.springframework.http.HttpStatus; +import org.springframework.web.server.ResponseStatusException; + +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Set; + +@Controller +@Documentation("codbex-uoms - Dimension Controller") +public class DimensionController { + + private static final Set FILTER_FIELDS = Set.of("Id", "Name", "SAP"); + + @Inject + private DimensionRepository repository; + + @Get + @Documentation("List Dimension") + public List getAll(@QueryParam("$limit") Integer limit, + @QueryParam("$offset") Integer offset) { + checkPermissions("read"); + int actualLimit = limit != null ? limit.intValue() : 20; + int actualOffset = offset != null ? offset.intValue() : 0; + List result = repository.findAll(actualLimit, actualOffset); + return result; + } + + @Get("/count") + @Documentation("Count Dimension") + public Map count() { + checkPermissions("read"); + return Map.of("count", repository.count()); + } + + @Post("/count") + @Documentation("Count Dimension with filter") + public Map countWithFilter(@Body Map filter) { + checkPermissions("read"); + return Map.of("count", (long) runFilter(filter).size()); + } + + @Post("/search") + @Documentation("Search Dimension") + public List search(@Body Map filter) { + checkPermissions("read"); + List result = runFilter(filter); + return result; + } + + @Get("/{id}") + @Documentation("Get Dimension by id") + public DimensionEntity getById(@PathParam("id") Integer id) { + checkPermissions("read"); + DimensionEntity entity = repository.findOne(id) + .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Dimension not found")); + return entity; + } + + @Post + @Documentation("Create Dimension") + public DimensionEntity create(@Body DimensionEntity entity) { + checkPermissions("write"); + validate(entity); + return repository.save(entity); + } + + @Put("/{id}") + @Documentation("Update Dimension by id") + public DimensionEntity update(@PathParam("id") Integer id, @Body DimensionEntity entity) { + checkPermissions("write"); + entity.Id = id; + validate(entity); + return repository.update(entity); + } + + @Delete("/{id}") + @Documentation("Delete Dimension by id") + public void deleteById(@PathParam("id") Integer id) { + checkPermissions("write"); + if (repository.findOne(id).isEmpty()) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Dimension not found"); + } + repository.deleteById(id); + } + + private List runFilter(Map filter) { + StringBuilder hql = new StringBuilder("from DimensionEntity e"); + Map params = new LinkedHashMap<>(); + boolean first = true; + if (filter != null && filter.get("equals") instanceof Map equals) { + for (Map.Entry entry : equals.entrySet()) { + String field = requireKnownField(String.valueOf(entry.getKey())); + String paramName = "p" + params.size(); + hql.append(first ? " where e." : " and e.").append(field).append(" = :").append(paramName); + params.put(paramName, entry.getValue()); + first = false; + } + } + if (filter != null && filter.get("conditions") instanceof List conditions) { + for (Object raw : conditions) { + if (!(raw instanceof Map condition)) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid filter condition"); + } + String field = requireKnownField(String.valueOf(condition.get("propertyName"))); + String operator = String.valueOf(condition.get("operator")).toUpperCase(Locale.ROOT); + Object value = condition.get("value"); + String paramName = "p" + params.size(); + String clause = switch (operator) { + case "EQ" -> "e." + field + " = :" + paramName; + case "IN" -> { + if (!(value instanceof Collection)) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "IN value must be a list for field: " + field); + } + yield "e." + field + " in (:" + paramName + ")"; + } + case "LIKE" -> "e." + field + " like :" + paramName; + default -> throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Unsupported operator: " + operator); + }; + hql.append(first ? " where " : " and ").append(clause); + params.put(paramName, value); + first = false; + } + } + return repository.query(hql.toString(), params); + } + + private static String requireKnownField(String field) { + if (!FILTER_FIELDS.contains(field)) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Unknown filter field: " + field); + } + return field; + } + + private void checkPermissions(String op) { + if ("read".equals(op) && !(UserFacade.isInRole("codbex-uoms.Dimensions.DimensionReadOnly") || UserFacade.isInRole("codbex-uoms.Dimensions.DimensionFullAccess"))) { + throw new ResponseStatusException(HttpStatus.FORBIDDEN); + } + if ("write".equals(op) && !UserFacade.isInRole("codbex-uoms.Dimensions.DimensionFullAccess")) { + throw new ResponseStatusException(HttpStatus.FORBIDDEN); + } + } + + private static void validate(DimensionEntity entity) { + if (entity.Name == null) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "The 'Name' property is required"); + } + if (entity.Name != null && entity.Name.length() > 100) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "The 'Name' exceeds the maximum length of 100"); + } + if (entity.SAP != null && entity.SAP.length() > 20) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "The 'SAP' exceeds the maximum length of 20"); + } + } +} diff --git a/codbex-uoms/gen/codbex_uoms/api/settings/UoMController.java b/codbex-uoms/gen/codbex_uoms/api/settings/UoMController.java new file mode 100644 index 0000000..be7d109 --- /dev/null +++ b/codbex-uoms/gen/codbex_uoms/api/settings/UoMController.java @@ -0,0 +1,191 @@ +package gen.codbex_uoms.api.settings; + +import gen.codbex_uoms.data.settings.UoMEntity; +import gen.codbex_uoms.data.settings.UoMRepository; + +import org.eclipse.dirigible.components.api.security.UserFacade; +import org.eclipse.dirigible.engine.java.annotations.Documentation; +import org.eclipse.dirigible.engine.java.annotations.Inject; +import org.eclipse.dirigible.engine.java.annotations.http.Body; +import org.eclipse.dirigible.engine.java.annotations.http.Controller; +import org.eclipse.dirigible.engine.java.annotations.http.Delete; +import org.eclipse.dirigible.engine.java.annotations.http.Get; +import org.eclipse.dirigible.engine.java.annotations.http.PathParam; +import org.eclipse.dirigible.engine.java.annotations.http.Post; +import org.eclipse.dirigible.engine.java.annotations.http.Put; +import org.eclipse.dirigible.engine.java.annotations.http.QueryParam; +import org.springframework.http.HttpStatus; +import org.springframework.web.server.ResponseStatusException; + +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Set; + +@Controller +@Documentation("codbex-uoms - UoM Controller") +public class UoMController { + + private static final Set FILTER_FIELDS = Set.of("Id", "Name", "ISO", "Dimension", "SAP", "Numerator", "Denominator", "Rounding", "Base"); + + @Inject + private UoMRepository repository; + + @Get + @Documentation("List UoM") + public List getAll(@QueryParam("$limit") Integer limit, + @QueryParam("$offset") Integer offset) { + checkPermissions("read"); + int actualLimit = limit != null ? limit.intValue() : 20; + int actualOffset = offset != null ? offset.intValue() : 0; + List result = repository.findAll(actualLimit, actualOffset); + return result; + } + + @Get("/count") + @Documentation("Count UoM") + public Map count() { + checkPermissions("read"); + return Map.of("count", repository.count()); + } + + @Post("/count") + @Documentation("Count UoM with filter") + public Map countWithFilter(@Body Map filter) { + checkPermissions("read"); + return Map.of("count", (long) runFilter(filter).size()); + } + + @Post("/search") + @Documentation("Search UoM") + public List search(@Body Map filter) { + checkPermissions("read"); + List result = runFilter(filter); + return result; + } + + @Get("/{id}") + @Documentation("Get UoM by id") + public UoMEntity getById(@PathParam("id") Integer id) { + checkPermissions("read"); + UoMEntity entity = repository.findOne(id) + .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "UoM not found")); + return entity; + } + + @Post + @Documentation("Create UoM") + public UoMEntity create(@Body UoMEntity entity) { + checkPermissions("write"); + validate(entity); + return repository.save(entity); + } + + @Put("/{id}") + @Documentation("Update UoM by id") + public UoMEntity update(@PathParam("id") Integer id, @Body UoMEntity entity) { + checkPermissions("write"); + entity.Id = id; + validate(entity); + return repository.update(entity); + } + + @Delete("/{id}") + @Documentation("Delete UoM by id") + public void deleteById(@PathParam("id") Integer id) { + checkPermissions("write"); + if (repository.findOne(id).isEmpty()) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "UoM not found"); + } + repository.deleteById(id); + } + + private List runFilter(Map filter) { + StringBuilder hql = new StringBuilder("from UoMEntity e"); + Map params = new LinkedHashMap<>(); + boolean first = true; + if (filter != null && filter.get("equals") instanceof Map equals) { + for (Map.Entry entry : equals.entrySet()) { + String field = requireKnownField(String.valueOf(entry.getKey())); + String paramName = "p" + params.size(); + hql.append(first ? " where e." : " and e.").append(field).append(" = :").append(paramName); + params.put(paramName, entry.getValue()); + first = false; + } + } + if (filter != null && filter.get("conditions") instanceof List conditions) { + for (Object raw : conditions) { + if (!(raw instanceof Map condition)) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid filter condition"); + } + String field = requireKnownField(String.valueOf(condition.get("propertyName"))); + String operator = String.valueOf(condition.get("operator")).toUpperCase(Locale.ROOT); + Object value = condition.get("value"); + String paramName = "p" + params.size(); + String clause = switch (operator) { + case "EQ" -> "e." + field + " = :" + paramName; + case "IN" -> { + if (!(value instanceof Collection)) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "IN value must be a list for field: " + field); + } + yield "e." + field + " in (:" + paramName + ")"; + } + case "LIKE" -> "e." + field + " like :" + paramName; + default -> throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Unsupported operator: " + operator); + }; + hql.append(first ? " where " : " and ").append(clause); + params.put(paramName, value); + first = false; + } + } + return repository.query(hql.toString(), params); + } + + private static String requireKnownField(String field) { + if (!FILTER_FIELDS.contains(field)) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Unknown filter field: " + field); + } + return field; + } + + private void checkPermissions(String op) { + if ("read".equals(op) && !(UserFacade.isInRole("codbex-uoms.UnitsOfMeasures.UoMReadOnly") || UserFacade.isInRole("codbex-uoms.UnitsOfMeasures.UoMFullAccess"))) { + throw new ResponseStatusException(HttpStatus.FORBIDDEN); + } + if ("write".equals(op) && !UserFacade.isInRole("codbex-uoms.UnitsOfMeasures.UoMFullAccess")) { + throw new ResponseStatusException(HttpStatus.FORBIDDEN); + } + } + + private static void validate(UoMEntity entity) { + if (entity.Name == null) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "The 'Name' property is required"); + } + if (entity.Name != null && entity.Name.length() > 100) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "The 'Name' exceeds the maximum length of 100"); + } + if (entity.ISO == null) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "The 'ISO' property is required"); + } + if (entity.ISO != null && entity.ISO.length() > 20) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "The 'ISO' exceeds the maximum length of 20"); + } + if (entity.Dimension == null) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "The 'Dimension' property is required"); + } + if (entity.SAP != null && entity.SAP.length() > 20) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "The 'SAP' exceeds the maximum length of 20"); + } + if (entity.Numerator == null) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "The 'Numerator' property is required"); + } + if (entity.Denominator == null) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "The 'Denominator' property is required"); + } + if (entity.Rounding == null) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "The 'Rounding' property is required"); + } + } +} diff --git a/codbex-uoms/gen/codbex-uoms/data/Settings/Dimension.extensionpoint b/codbex-uoms/gen/codbex_uoms/data/settings/Dimension.extensionpoint similarity index 98% rename from codbex-uoms/gen/codbex-uoms/data/Settings/Dimension.extensionpoint rename to codbex-uoms/gen/codbex_uoms/data/settings/Dimension.extensionpoint index 78ace43..e4a11dd 100644 --- a/codbex-uoms/gen/codbex-uoms/data/Settings/Dimension.extensionpoint +++ b/codbex-uoms/gen/codbex_uoms/data/settings/Dimension.extensionpoint @@ -1,4 +1,4 @@ { "name": "codbex-uoms-Settings-Dimension", "description": "Extension Point for the codbex-uoms-Settings-Dimension entity" -} \ No newline at end of file +} diff --git a/codbex-uoms/gen/codbex_uoms/data/settings/DimensionEntity.java b/codbex-uoms/gen/codbex_uoms/data/settings/DimensionEntity.java new file mode 100644 index 0000000..5ff291d --- /dev/null +++ b/codbex-uoms/gen/codbex_uoms/data/settings/DimensionEntity.java @@ -0,0 +1,34 @@ +package gen.codbex_uoms.data.settings; + +import org.eclipse.dirigible.engine.java.annotations.Column; +import org.eclipse.dirigible.engine.java.annotations.CreatedAt; +import org.eclipse.dirigible.engine.java.annotations.CreatedBy; +import org.eclipse.dirigible.engine.java.annotations.Documentation; +import org.eclipse.dirigible.engine.java.annotations.Entity; +import org.eclipse.dirigible.engine.java.annotations.GeneratedValue; +import org.eclipse.dirigible.engine.java.annotations.GenerationType; +import org.eclipse.dirigible.engine.java.annotations.Id; +import org.eclipse.dirigible.engine.java.annotations.Table; +import org.eclipse.dirigible.engine.java.annotations.UpdatedAt; +import org.eclipse.dirigible.engine.java.annotations.UpdatedBy; + +@Entity +@Table(name = "CODBEX_DIMENSION") +@Documentation("Dimension entity mapping") +public class DimensionEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "DIMENSION_ID") + @Documentation("Id") + public Integer Id; + + @Column(name = "DIMENSION_NAME", length = 100, nullable = false, unique = true) + @Documentation("Name") + public String Name; + + @Column(name = "DIMENSION_SAP", length = 20, nullable = true, unique = true) + @Documentation("SAP") + public String SAP; + +} diff --git a/codbex-uoms/gen/codbex_uoms/data/settings/DimensionRepository.java b/codbex-uoms/gen/codbex_uoms/data/settings/DimensionRepository.java new file mode 100644 index 0000000..4337418 --- /dev/null +++ b/codbex-uoms/gen/codbex_uoms/data/settings/DimensionRepository.java @@ -0,0 +1,12 @@ +package gen.codbex_uoms.data.settings; + +import org.eclipse.dirigible.components.data.store.java.repository.JavaRepository; +import org.eclipse.dirigible.engine.java.annotations.Repository; + +@Repository +public class DimensionRepository extends JavaRepository { + + public DimensionRepository() { + super(DimensionEntity.class); + } +} diff --git a/codbex-uoms/gen/codbex-uoms/data/Settings/UoM.extensionpoint b/codbex-uoms/gen/codbex_uoms/data/settings/UoM.extensionpoint similarity index 98% rename from codbex-uoms/gen/codbex-uoms/data/Settings/UoM.extensionpoint rename to codbex-uoms/gen/codbex_uoms/data/settings/UoM.extensionpoint index d1a4785..c826b74 100644 --- a/codbex-uoms/gen/codbex-uoms/data/Settings/UoM.extensionpoint +++ b/codbex-uoms/gen/codbex_uoms/data/settings/UoM.extensionpoint @@ -1,4 +1,4 @@ { "name": "codbex-uoms-Settings-UoM", "description": "Extension Point for the codbex-uoms-Settings-UoM entity" -} \ No newline at end of file +} diff --git a/codbex-uoms/gen/codbex_uoms/data/settings/UoMEntity.java b/codbex-uoms/gen/codbex_uoms/data/settings/UoMEntity.java new file mode 100644 index 0000000..9cc0842 --- /dev/null +++ b/codbex-uoms/gen/codbex_uoms/data/settings/UoMEntity.java @@ -0,0 +1,58 @@ +package gen.codbex_uoms.data.settings; + +import org.eclipse.dirigible.engine.java.annotations.Column; +import org.eclipse.dirigible.engine.java.annotations.CreatedAt; +import org.eclipse.dirigible.engine.java.annotations.CreatedBy; +import org.eclipse.dirigible.engine.java.annotations.Documentation; +import org.eclipse.dirigible.engine.java.annotations.Entity; +import org.eclipse.dirigible.engine.java.annotations.GeneratedValue; +import org.eclipse.dirigible.engine.java.annotations.GenerationType; +import org.eclipse.dirigible.engine.java.annotations.Id; +import org.eclipse.dirigible.engine.java.annotations.Table; +import org.eclipse.dirigible.engine.java.annotations.UpdatedAt; +import org.eclipse.dirigible.engine.java.annotations.UpdatedBy; + +@Entity +@Table(name = "CODBEX_UOM") +@Documentation("UoM entity mapping") +public class UoMEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "UOM_ID") + @Documentation("Id") + public Integer Id; + + @Column(name = "UOM_NAME", length = 100, nullable = false, unique = true) + @Documentation("Name") + public String Name; + + @Column(name = "UOM_ISO", length = 20, nullable = false, unique = true) + @Documentation("ISO") + public String ISO; + + @Column(name = "UOM_DIMENSION", nullable = false) + @Documentation("Dimension") + public Integer Dimension; + + @Column(name = "UOM_SAP", length = 20, nullable = true, unique = true) + @Documentation("SAP") + public String SAP; + + @Column(name = "UOM_NUMERATOR", nullable = false) + @Documentation("Numerator") + public Long Numerator; + + @Column(name = "UOM_DENOMINATOR", nullable = false) + @Documentation("Denominator") + public Long Denominator; + + @Column(name = "UOM_ROUNDING", nullable = false) + @Documentation("Rounding") + public Integer Rounding; + + @Column(name = "UOM_BASE", nullable = false) + @Documentation("Base") + public Boolean Base; + +} diff --git a/codbex-uoms/gen/codbex_uoms/data/settings/UoMRepository.java b/codbex-uoms/gen/codbex_uoms/data/settings/UoMRepository.java new file mode 100644 index 0000000..73105b6 --- /dev/null +++ b/codbex-uoms/gen/codbex_uoms/data/settings/UoMRepository.java @@ -0,0 +1,12 @@ +package gen.codbex_uoms.data.settings; + +import org.eclipse.dirigible.components.data.store.java.repository.JavaRepository; +import org.eclipse.dirigible.engine.java.annotations.Repository; + +@Repository +public class UoMRepository extends JavaRepository { + + public UoMRepository() { + super(UoMEntity.class); + } +} diff --git a/codbex-uoms/gen/codbex-uoms/roles/default-roles.roles b/codbex-uoms/gen/codbex_uoms/roles/default-roles.roles similarity index 99% rename from codbex-uoms/gen/codbex-uoms/roles/default-roles.roles rename to codbex-uoms/gen/codbex_uoms/roles/default-roles.roles index 94238ce..56f3dae 100644 --- a/codbex-uoms/gen/codbex-uoms/roles/default-roles.roles +++ b/codbex-uoms/gen/codbex_uoms/roles/default-roles.roles @@ -15,4 +15,4 @@ "name": "codbex-uoms.UnitsOfMeasures.UoMFullAccess", "description": "A role that grants full access for UoM." } -] \ No newline at end of file +]