From 566e4d5671039b09c64a4da509edd7668d236dd3 Mon Sep 17 00:00:00 2001 From: Santiago Revilla Date: Thu, 29 Aug 2019 16:04:55 -0300 Subject: [PATCH] Add String to SysConfig converter --- .../jpos/qi/sysconfig/SysConfigConverter.java | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 modules/qi-sysconfig/src/main/java/org/jpos/qi/sysconfig/SysConfigConverter.java diff --git a/modules/qi-sysconfig/src/main/java/org/jpos/qi/sysconfig/SysConfigConverter.java b/modules/qi-sysconfig/src/main/java/org/jpos/qi/sysconfig/SysConfigConverter.java new file mode 100644 index 0000000000..f3497a3188 --- /dev/null +++ b/modules/qi-sysconfig/src/main/java/org/jpos/qi/sysconfig/SysConfigConverter.java @@ -0,0 +1,53 @@ +package org.jpos.qi.sysconfig; + +import com.vaadin.data.Converter; +import com.vaadin.data.Result; +import com.vaadin.data.ValueContext; +import org.jpos.ee.DB; +import org.jpos.ee.SysConfig; +import org.jpos.ee.SysConfigManager; +import org.jpos.qi.QI; + +public class SysConfigConverter implements Converter { + private String prefix; + private boolean useValue; + + public SysConfigConverter () { + this(null, false); + } + + public SysConfigConverter (String prefix) { + this (prefix, false); + } + public SysConfigConverter (String prefix, boolean useValue) { + super(); + this.prefix = prefix; + this.useValue = useValue; + } + + @Override + public Result convertToModel(SysConfig value, ValueContext context) { + if (value == null) + return Result.error("NULL value"); + else { + String id = prefix != null ? value.getId().substring(prefix.length()) : value.getId(); + String modelToSave = useValue ? value.getValue() : id; + return Result.ok(modelToSave); + } + } + + @Override + public SysConfig convertToPresentation(String value, ValueContext context) { + if (value != null) { + try { + return DB.exec( (db) -> { + SysConfigManager mgr = new SysConfigManager(db); + return mgr.getObject(value); + }); + } catch (Exception e) { + QI.getQI().getLog().error(e); + } + } + return null; + } +}