i have a java enum which have to have some default values like
for the
String some default values
int some degault values
etc
i have created a enum like the following
package com.hexgen.tools;
public enum DefaultParamsValues {
STRING ("HEXGEN"),
INTEGER(2013),
DATE(new org.joda.time.LocalDate()),
BOOLEAN(true),
BIGINTEGER(BigInteger.valueOf(Long.MAX_VALUE)),
LONG("1898.48");
private String defaultString;
private int defaultInteger;
private LocalDate defaultDate;
private boolean defaultBoolean;
private long defaultLong;
private BigInteger defaultBigInteger;
public DefaultParamsValues(String strValue,int intValue,LocalDate date,boolean booleanValue,long longValue,BigInteger bigintVlaue){
this.defaultString = strValue;
this.defaultInteger = intValue;
this.defaultDate = date;
this.defaultBoolean = booleanValue;
this.defaultLong=longValue;
this.defaultBigInteger = bigintVlaue;
}
}
but it is giving so many issues, would some one help me to create a enum with basic values for primitive types?
EDIT: this is how i solved it:
package com.test.poc;
import java.math.BigInteger;
import org.joda.time.LocalDate;
public enum DefaultParamValues {
STRING("HEXGEN"),
INTEGER(123),
DATE(new LocalDate()),
BOOLEAN(true),
LONGVALUE(123123),
BIGINTEGER(BigInteger.valueOf(Long.MAX_VALUE));
private String defaultString;
private int defaultInteger;
private LocalDate defaultDate;
private boolean defaultBoolean;
private long defaultLong;
private BigInteger defaultBigInteger;
private DefaultParamValues(String strDefaultValue) {
defaultString = strDefaultValue;
}
private DefaultParamValues(int intDefaultValue) {
defaultInteger = intDefaultValue;
}
private DefaultParamValues(LocalDate dateDefaultValue) {
defaultDate = dateDefaultValue;
}
private DefaultParamValues(boolean booleanDefaultValue) {
defaultBoolean = booleanDefaultValue;
}
private DefaultParamValues(long longDefaultValue) {
defaultLong = longDefaultValue;
}
private DefaultParamValues(BigInteger bigIntegerDefaultValue) {
defaultBigInteger = bigIntegerDefaultValue;
}
public String getDefaultString() {
return defaultString;
}
public int getDefaultInt() {
return defaultInteger;
}
public LocalDate getDefaultDate() {
return defaultDate;
}
public boolean getDefaultBoolean() {
return defaultBoolean;
}
public long getDefaultLong() {
return defaultLong;
}
public BigInteger getDefaultBigInteger() {
return defaultBigInteger;
}
}
Thanks
First, an enum is probably not right for your purpose. You should use a final Class with constants.
But if you want it inefficient and cumbersome you could do it this way:
public enum GenericEnum {
STRING("HEXGEN"),
INTEGER(2013),
DATE(new Date()),
BOOLEAN(true),
BIGINTEGER(BigInteger.valueOf(Long.MAX_VALUE)),
LONG("1898.48");
private String defaultString;
private int defaultInteger;
private Date defaultDate;
private boolean defaultBoolean;
private long defaultLong;
private BigInteger defaultBigInteger;
GenericEnum(Object value) {
if(String.class.isAssignableFrom(value.getClass())) {
this.defaultString = (String) value;
} else if (Integer.class.isAssignableFrom(value.getClass())) {
this.defaultInteger = (Integer) value;
}
[...]
}
}
or overload the constructor:
private GenericEnum(String val) {
this.defaultString = val;
}
private GenericEnum(int val) {
this.defaultInteger = val;
}
[...]
Related
So I'm making an API using Java Spring-boot and JBDC with PostgreSQL as the database and Postman for testing. All the basic CRUD function is already implemented, but I'm trying to validates if a value in the POST request is already in the database or not so there are no duplicate data.
Here's the model:
package com.akuntansi.akuntansi.model;
import java.sql.Timestamp;
import jakarta.persistence.Id;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
public class mAkuntansi {
private Integer id_md_jurnal_Integer;
private Integer id_dd_wilayah_kerja;
private String kode_file;
private String file_id;
private String tanggal;
private String debet_kredit;
private String kode_coa;
private long nominal;
private Integer flag;
private Integer id_akmt_subledger;
private Integer id_referensi;
private String no_bukti;
private Timestamp created_date;
public mAkuntansi(){}
public mAkuntansi(Integer id_md_jurnal_Integer, Integer id_dd_wilayah_kerja, String kode_file, String file_id, String tanggal, String debet_kredit, String kode_coa,
long nominal, Integer flag, Integer id_akmt_subledger, Integer id_referensi, String no_bukti, Timestamp created_date){
this.id_md_jurnal_Integer = id_md_jurnal_Integer;
this.id_dd_wilayah_kerja = id_dd_wilayah_kerja;
this.kode_file = kode_file;
this.file_id = file_id;
this.tanggal = tanggal;
this.debet_kredit = debet_kredit;
this.kode_coa = kode_coa;
this.nominal = nominal;
this.flag = flag;
this.id_akmt_subledger = id_akmt_subledger;
this.id_referensi = id_referensi;
this.no_bukti = no_bukti;
this.created_date = created_date;
}
public mAkuntansi(Integer id_dd_wilayah_kerja, String kode_file, String file_id, String tanggal, String debet_kredit, String kode_coa,
long nominal, Integer flag, Integer id_akmt_subledger, Integer id_referensi, String no_bukti, Timestamp created_date){
this.id_dd_wilayah_kerja = id_dd_wilayah_kerja;
this.kode_file = kode_file;
this.file_id = file_id;
this.tanggal = tanggal;
this.debet_kredit = debet_kredit;
this.kode_coa = kode_coa;
this.nominal = nominal;
this.flag = flag;
this.id_akmt_subledger = id_akmt_subledger;
this.id_referensi = id_referensi;
this.no_bukti = no_bukti;
this.created_date = created_date;
}
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer getid_md_jurnal_int(){
return id_md_jurnal_Integer;
}
public void setid_md_jurnal_int(Integer id_md_jurnal_Integer){
this.id_md_jurnal_Integer = id_md_jurnal_Integer;
}
public Integer getid_dd_wilayah_kerja(){
return id_dd_wilayah_kerja;
}
public void setid_dd_wilayah_kerja(Integer id_dd_wilayah_kerja){
this.id_dd_wilayah_kerja = id_dd_wilayah_kerja;
}
public String getkode_file(){
return kode_file;
}
public void setkode_file(String kode_file){
this.kode_file = kode_file;
}
public String getfile_id(){
return file_id;
}
public void setfile_id(String file_id){
this.file_id = file_id;
}
public String gettanggal(){
return tanggal;
}
public void settanggal(String tanggal){
this.tanggal = tanggal;
}
public String getdebet_kredit(){
return debet_kredit;
}
public void setdebet_kredit(String debet_kredit){
this.debet_kredit = debet_kredit;
}
public String getkode_coa(){
return kode_coa;
}
public void setkode_coa(String kode_coa){
this.kode_coa = kode_coa;
}
public long getnominal(){
return nominal;
}
public void setnominal(long nominal){
this.nominal = nominal;
}
public Integer getflag(){
return flag;
}
public void setflag(Integer flag){
this.flag = flag;
}
public Integer getid_akmt_subledger(){
return id_akmt_subledger;
}
public void setid_akmt_subledger(Integer id_akmt_subledger){
this.id_akmt_subledger = id_akmt_subledger;
}
public Integer getid_referensi(){
return id_referensi;
}
public void setid_referensi(Integer id_refrensi){
this.id_referensi = id_refrensi;
}
public String getno_bukti(){
return no_bukti;
}
public void setno_bukti(String no_bukti){
this.no_bukti = no_bukti;
}
public Timestamp getcreated_date(){
return created_date;
}
public void setcreated_date(Timestamp created_date){
this.created_date = created_date;
}
}
Let say for example I want to validates the kode_coa. There's a POST request with kode_coa of K1 but in the database, a data with value K1 already exist, how do you prevent a duplicate data from getting inserted into the database?
I have 3 class(techspecs, packages, and features) objects where they all share the same fields. The fields are big and instead of repeating setting the fields of each field 3 times(which ends up looking like duplicates), I would like to pass the class objects into one method that uses the generic object to setting the object fields.
I tried passing the class object as a generic but then i dont have access to its members. This is what i tried
Packages packagesFeatures = new Packages();
TechSpecs techSpecsFeature = new TechSpecs();
packagesFeatures = addFeatures(Packages.class, packagesFeatures, vehFeatures);
techSpecsFeature = addFeatures(TechSpecs.class, techSpecsFeature, vehFeatures);
Then
private <T> T addFeatures(Class<T> clazz, T obj, VehicleFeature vehFeatures) {
T inst = null;
try {
inst = clazz.getDeclaredConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
}
if (inst instanceof Packages) {
obj = (T) new Packages();
}
if(inst instanceof TechSpecs){
obj = (T) new TechSpecs();
}
if(inst instanceof Features){
obj = (T) new Features();
}
//then somthing like:
//obj.setFeatureId(vehFeatures.getFeatureId());
// obj.setFeatureKey(vehFeatures.getFeatureKey());
// obj.setFeatureCode(vehFeatures.getFeatureCode());
return obj;
EDIT
Each of the 3 classes extend BaseFeatures
public abstract class BaseFeatures {
private String featureId;
private String featureKey;
private String featureCode;
private String subSectionId;
private String subSectionName;
private String featureIdName;
private Integer subSectionRank;
private Integer featureImgClassificationId;
private String featureImgClassification;
private boolean has3DAnimation;
private String sectionId;
private String searchKeys;
private String description;
private String featureName;
private double featureRank;
private String geoId;
private String ecc;
private String specSegments;
private String featureIconType;
private String featureIconText;
private double featureValue;
private boolean standardCertain;
private boolean built;
private List<String> featureKeyAnswers;
private boolean isNumeric;
private boolean adasFeature;
private List<String> icCodeAnswers;
private String featureKeyNoBrand;
private List<StyleInfo> styles;
private List<String> optionCodes;
private List<String> changeOptions;
//getters and setters.
Here is one of the classes.
public class TechSpecs extends BaseFeatures {
private String techSpecs;
public void setTechSpecs(String techSpecs) {
this.techSpecs = techSpecs;
}
public String getTechSpecs(){
return techSpecs;
}
}
All of these fields need to be set in the class object of all 3 classes
EDIT 2
VehicleFeature Class is a standalone class
#JsonInclude(JsonInclude.Include.NON_NULL)
public class VehicleFeature {
private String section;
private String subSection;
private String featureName;
private String subSectionId;
private String sectionName;
private String subSectionName;
If it were me, I would simplify your addFeatures(...) method to something like:
private <T> T addFeatures(Class<T> clazz, BaseFeatures theseFeatures) {
T obj = null;
try {
obj = clazz.getDeclaredConstructor(BaseFeatures.class).newInstance(theseFeatures);
} catch (ReflectiveOperationException roe) {
roe.printStackTrace();
}
return obj;
}
I'd add these two constructors to BaseFeatures:
public abstract class BaseFeatures{
protected String featureId;
protected String featureKey;
protected String featureCode;
/*...*/
protected BaseFeatures(String featureId, String featureKey, String featureCode){
this.featureId = featureId;
this.featureKey = featureKey;
this.featureCode = featureCode;
}
protected BaseFeatures(BaseFeatures features){
this.featureId = features.featureId ;
this.featureKey = features.featureKey;
this.featureCode = features.featureCode;
}
/*...*/
}
You can see how that implementation would actually work, here:
public class BigAssFields {
/* ... */
static public void main(String ... args){
BigAssFields bLike = new BigAssFields();
VehicleFeature vehFeatures = new VehicleFeature("what", "the actual", "Feature");
TechSpecs bigTechSpecs = bLike.addFeatures(TechSpecs.class, vehFeatures);
}
/* ... */
}
Just trying to learn and understand Jaxb and how xml binding works, so thank you very very much! I do not understand how or if it does blocks or sections that reoccur? in below, the MM block can be 1 or many, how does this get asorbed to pojo? how does Jaxb or other engines bind multiple sections, say the MM section? thank you!!!
I have an XML file I need to UnMarshal to Java POJO's.
A 018912111835030108XXXXXXXXXX
<RTR>
<HEAD>
<IDCMS pn="ABF31A6FNCL0004" vers="4.0"/>
<DA>2018/12/11 01:07:37</DA>
<ACD MSN="0149" tail="N503DN" type="A350-900"/>
<TID>C00000011</TID>
</HEAD>
<HEADRTR>
<FROM>KDTW</FROM>
<TO>ZBAA</TO>
<FNBR>DAL189 </FNBR>
</HEADRTR>
<ITD>
<CB>A</CB>
<NFDE>
<MC>3166X001</MC>
<MD>INDICATING-OIS CAPT-"OIS NOT AVAIL" displayed instead of the requested video</MD>
</NFDE>
<MM hdi="y" fo="A" occ="1">
<MC>4612F11W</MC>
<MD>VGM-1(107TC1) / AVM-1(103TC1) / OSFC RESET</MD>
<DA>2018/12/11 00:57:39</DA>
<ATA>4612</ATA>
<FPH>8</FPH>
<FCL>1</FCL>
<PRIO>high</PRIO>
<STA>1</STA>
<SYS name="ASF" bite_id="107" side="0"/>
</MM>
<MM hdi="y" occ="1">
<MC>3166F8TT</MC>
<MD>VGM-1(107TC1) / CMV-2(1TE2) / WRG TO CMV-2(1TE2) PIN AEDD</MD>
<DA>2018/12/11 00:57:36</DA>
<CDA>2018/12/11 00:57:37</CDA>
<ATA>3166</ATA>
<FPH>8</FPH>
<FCL>1</FCL>
<PRIO>low</PRIO>
<STA>2</STA>
<SYS name="CMV2" bite_id="31" side="2"/>
</MM>
</ITD>
</RTR>
created the classes for the XML structure minus how to consume reoccuring sections. I won't post them all. I think help on one will do the others. thanks!
#XmlRootElement( name = "MM" )
public class Mm {
private String mc;
private String md;
private String da;
private String ata;
private String fph;
private String prio;
Mm(){
super();
}
Mm( #XmlElement(name="MC") String mc,
#XmlElement(name="MD") String md,
#XmlElement(name="DA") String da,
#XmlElement(name="FPH") String fph,
#XmlElement(name="PRIO") String prio) {
this.mc = mc;
this.md = md;
this.da = da;
this.fph = fph;
this.prio = prio;
}
public String getMc() {
return mc;
}
public String getMd() {
return md;
}
public String getDa() {
return da;
}
public String getAta() {
return ata;
}
public String getFph() {
return fph;
}
public String getPrio() {
return prio;
}
#Override
public String toString() {
final StringBuilder sb = new StringBuilder("MC=");
sb.append(getMc()).append(",");
sb.append("MD=").append(getMd()).append(",");
sb.append("DA=").append(getDa()).append(",");
sb.append("ATA=").append(getAta()).append(",");
sb.append("FPH=").append(getFph()).append(",");
sb.append("PRIO=").append(getPrio());
return sb.toString();
}
}
#XmlRootElement( name = "FDE" )
public class Fde {
private String mc;
private String md;
private String da;
private String fph;
Fde(){
super();
}
Fde( #XmlElement(name="MC") String mc,
#XmlElement(name="MD") String md,
#XmlElement(name="DA") String da,
#XmlElement(name="FPH") String fph) {
this.mc = mc;
this.md = md;
this.da = da;
this.fph = fph;
}
public String getMc() {
return mc;
}
public String getMd() {
return md;
}
public String getDa() {
return da;
}
public String getFph() {
return fph;
}
#Override
public String toString() {
final StringBuilder sb = new StringBuilder("MC=");
sb.append(getMc()).append(",");
sb.append("MD=").append(getMd()).append(",");
sb.append("DA=").append(getDa()).append(",");
sb.append("FPH=").append(getFph());
return sb.toString();
}
}
#XmlRootElement( name = "HEADRTR" )
public class HeadRtr {
private String from;
private String to;
private String fnbr;
HeadRtr(){
super();
}
HeadRtr(#XmlElement(name="FROM") String from,
#XmlElement(name="TO") String to,
#XmlElement(name="FNBR") String fnbr) {
this.from = from;
this.to = to;
this.fnbr = fnbr;
}
public String getFrom() {
return from;
}
public String getTo() {
return to;
}
public String getFnbr() {
return fnbr;
}
#Override
public String toString() {
final StringBuilder sb = new StringBuilder("FROM=");
sb.append(getFrom()).append(",");
sb.append("TO=").append(getTo()).append(",");
sb.append("FNBR=").append(getFnbr());
return sb.toString();
}
}
I'm thinking a list of these objects
From the look of the xml.
You'd want ITD, FDE, MM as objects. with ITD having a List, and FDE as a member.
Thanks to Mavriksc here is the result to decode an HTML XML document.
This is a sample HTML XML document we need to Unmarshal.
<RTR>
<HEAD>
<IDCMS pn="ABF31A6FNCL0004" vers="4.0"/>
<DA>2018/12/11 00:15:42</DA>
<ACD MSN="0160" tail="N504DN" type="A350-900"/>
<TID>C00000023</TID>
</HEAD>
<HEADRTR>
<FROM>ZSPD</FROM>
<TO>KLAX</TO>
<FNBR>DAL88 </FNBR>
</HEADRTR>
<ITD>
<CB>A</CB>
<FDE disp="n" dm="y">
<MC>3031H050</MC>
<MD>A-ICE // > A-ICE SIDESLIP PROBE 2 HEATG</MD>
<DA>2018/12/11 00:12:42</DA>
<CDA>2018/12/11 00:14:17</CDA>
<FPH>8</FPH>
<LAB>1</LAB>
</FDE>
<MM fo="A" occ="5">
<MC>3413F7EV</MC>
<MD>PROBE-SSA,2(11FP2)</MD>
<DA>2018/12/11 00:08:43</DA>
<ATA>3413</ATA>
<FPH>8</FPH>
<FCL>1</FCL>
<PRIO>high</PRIO>
<STA>1</STA>
<SYS name="ADR2" bite_id="3" side="2"/>
</MM>
</ITD>
</RTR>
here is the Jaxb POJO's mapping the XML to POJO.
RTR
#XmlRootElement(name = "RTR", namespace="com.ge.ip.infra.fadec.jaxb.decoder")
public class Rtr implements Serializable {
private static final long serialVersionUID = 1L;
public Rtr() {
super();
}
private Itd itd;
private HeadRtr headRtr;
private Head head;
public Itd getItd() {
return itd;
}
public void setItd(Itd itd) {
this.itd = itd;
}
public HeadRtr getHeadRtr() {
return headRtr;
}
public void setHeadRtr(HeadRtr headRtr) {
this.headRtr = headRtr;
}
public Head getHead() {
return head;
}
public void setHead(Head head) {
this.head = head;
}
#Override
public String toString()
{
return "RTR ITD = " + itd + ", HEADRTR = " + headRtr + ", HEAD = " + head;
}
}
HEAD
#XmlRootElement(name = "HEAD")
public class Head implements Serializable {
private static final long serialVersionUID = 1L;
#XmlElement(name = "IDCMS") private Idcms idcms;
#XmlElement(name = "DA") private String da;
#XmlElement(name = "ACD") private Acd acd;
#XmlElement(name = "TID") private String tid;
public String getDa() {
return da;
}
public String getTid() {
return tid;
}
public void setDa(String da) {
this.da = da;
}
public void setTid(String tid) {
this.tid = tid;
}
public Acd getAcd () {
return acd;
}
public void setAcd (Acd acd) {
this.acd = acd;
}
public Idcms getIdcms () {
return idcms;
}
public void setIdcms (Idcms idcms) {
this.idcms = idcms;
}
#Override
public String toString() {
final StringBuilder sb = new StringBuilder("DA=");
sb.append(getDa()).append(",");
sb.append("TID=").append(getTid()).append(",");
sb.append(acd.toString());
sb.append(idcms.toString());
return sb.toString();
}
//}
public class Idcms implements Serializable {
private static final long serialVersionUID = 1L;
#XmlAttribute(name = "pn") private String pn;
#XmlAttribute(name = "vers") private String vers;
public String getPn() {
return pn;
}
public String getVers() {
return vers;
}
public void setPn(String pn) {
this.pn = pn;
}
public void setVers(String vers) {
this.vers = vers;
}
#Override
public String toString() {
final StringBuilder sb = new StringBuilder("PN=");
sb.append(getPn()).append(",");
sb.append("VERS=").append(getVers());
return sb.toString();
}
}
public class Acd implements Serializable {
private static final long serialVersionUID = 1L;
#XmlAttribute(name = "MSN") private String msn;
#XmlAttribute(name = "tail") private String tail;
#XmlAttribute(name = "type") private String type;
public String getMsn() {
return msn;
}
public String getTail() {
return tail;
}
public String getType() {
return type;
}
public void setMsn(String msn) {
this.msn = msn;
}
public void setTail(String tail) {
this.tail = tail;
}
public void setType(String type) {
this.type = type;
}
#Override
public String toString() {
final StringBuilder sb = new StringBuilder("MSN=");
sb.append(getMsn()).append(",");
sb.append("TAIL=").append(getTail()).append(",");
sb.append("TYPE=").append(getType());
return sb.toString();
}
}
}
HEADRTR
#XmlRootElement( name = "HEADRTR" )
public class HeadRtr implements Serializable {
private static final long serialVersionUID = 1L;
private String from;
private String to;
private String fnbr;
HeadRtr(){
super();
}
HeadRtr(#XmlElement(name="FROM") String from,
#XmlElement(name="TO") String to,
#XmlElement(name="FNBR") String fnbr) {
this.from = from;
this.to = to;
this.fnbr = fnbr;
}
public String getFrom() {
return from;
}
public String getTo() {
return to;
}
public String getFnbr() {
return fnbr;
}
#Override
public String toString() {
final StringBuilder sb = new StringBuilder("FROM=");
sb.append(getFrom()).append(",");
sb.append("TO=").append(getTo()).append(",");
sb.append("FNBR=").append(getFnbr());
return sb.toString();
}
}
ITD
#XmlRootElement( name = "ITD" )
#XmlAccessorType (XmlAccessType.FIELD)
public class Itd implements Serializable {
private static final long serialVersionUID = 1L;
private Mm mm;
private Fde fde;
private Nfde nfde;
public Mm getMm() {
return mm;
}
public void setMm (Mm mm)
{
this.mm = mm;
}
#XmlElement(name="CB")
private String cb;
public String getCb() {
return cb;
}
public void setCb(String cb) {
this.cb = cb;
}
public Fde getFde ()
{
return fde;
}
public void setFde (Fde fde)
{
this.fde = fde;
}
public Nfde getNfde() {
return nfde;
}
public void setNfde (Nfde nfde)
{
this.nfde = nfde;
}
#Override
public String toString()
{
return "ITD MM = " + mm + ", CB = " + cb + ", FDE = " + fde + ", NFDE = " + nfde;
}
}
NFDE
#XmlRootElement( name = "NFDE" )
#XmlAccessorType (XmlAccessType.FIELD)
public class Nfde implements Serializable {
private static final long serialVersionUID = 1L;
private String mc;
private String md;
public String getMc() {
if ( !mc.isEmpty() && null != mc ) {
return mc;
} else {
return "NFDE";
}
}
public String getMd() {
if ( !md.isEmpty() && null != md ) {
return md;
} else {
return "NFDE";
}
}
#XmlElement(name="MC")
public void setMc(String mc) {
this.mc = mc;
}
#XmlElement(name="MD")
public void setMd(String md) {
this.md = md;
}
}
FDE
#XmlRootElement( name = "FDE" )
#XmlAccessorType (XmlAccessType.FIELD)
public class Fde implements Serializable {
private static final long serialVersionUID = 1L;
private String mc;
private String md;
private String da;
private String fph;
Fde(){
super();
}
Fde( #XmlElement(name="MC") String mc,
#XmlElement(name="MD") String md,
#XmlElement(name="DA") String da,
#XmlElement(name="FPH") String fph) {
this.mc = mc;
this.md = md;
this.da = da;
this.fph = fph;
}
public String getMc() {
return mc;
}
public String getMd() {
return md;
}
public String getDa() {
return da;
}
public String getFph() {
return fph;
}
#Override
public String toString() {
final StringBuilder sb = new StringBuilder("MC=");
sb.append(getMc()).append(",");
sb.append("MD=").append(getMd()).append(",");
sb.append("DA=").append(getDa()).append(",");
sb.append("FPH=").append(getFph());
return sb.toString();
}
}
MM
#XmlRootElement( name = "MM" )
#XmlAccessorType (XmlAccessType.FIELD)
public class Mm implements Serializable {
private static final long serialVersionUID = 1L;
private String mc;
private String md;
private String da;
private String ata;
private String fph;
private String prio;
Mm(){
super();
}
Mm( #XmlElement(name="MC") String mc,
#XmlElement(name="MD") String md,
#XmlElement(name="DA") String da,
#XmlElement(name="FPH") String fph,
#XmlElement(name="PRIO") String prio) {
this.mc = mc;
this.md = md;
this.da = da;
this.fph = fph;
this.prio = prio;
}
public String getMc() {
return mc;
}
public String getMd() {
return md;
}
public String getDa() {
return da;
}
public String getAta() {
return ata;
}
public String getFph() {
return fph;
}
public String getPrio() {
return prio;
}
#Override
public String toString() {
final StringBuilder sb = new StringBuilder("MC=");
sb.append(getMc()).append(",");
sb.append("MD=").append(getMd()).append(",");
sb.append("DA=").append(getDa()).append(",");
sb.append("ATA=").append(getAta()).append(",");
sb.append("FPH=").append(getFph()).append(",");
sb.append("PRIO=").append(getPrio());
return sb.toString();
}
}
Decoder POJO
public class A350Decoder implements Serializable {
private static final long serialVersionUID = 1L;
public A350Decoder() {
super();
}
private Rtr rtr;
public Rtr getRtr() {
return rtr;
}
public void setRtr(Rtr rtr) {
this.rtr = rtr;
}
#Override
public String toString()
{
return "A350Decoder RTR = " + rtr;
}
}
I have an issue with Room not recognizing my converter. Error:
Cannot figure out how to save this field into database. You can consider adding a type converter for it.
I need to store some maps and sets in my database. What am I doing wrong? Does room not like interfaces or generics?
code: (sorry for all the fields and class name, they are a mixture of English and Czech to not have same names as some java classes):
Converter (only part)
public class MyConverter {
/**
* makes a string like 1;2;3;5;4;8;1;6;8;4 from a collection of integers.
*/
#TypeConverter
public static #NonNull String toString(#NonNull Collection<Integer> c) {
StringBuilder sb = new StringBuilder();
for (Integer item : c) {
sb.append(item.toString() + ";");
}
sb.delete(sb.length()-1,sb.length()-1);
return sb.toString();
}
/**
* makes a Set<Integer> from string like 1;2;3;4;5;6
* #throws NumberFormatException on incorrect input
*/
#TypeConverter
public static#NonNull Set<Integer> toIntegerSet(#NonNull String s) {
Set<Integer> set = new LinkedHashSet<>();
String[] split = s.split(";");
try {
for (String item : split) {
set.add(Integer.parseInt(item));
}
}catch (NumberFormatException e){
throw new NumberFormatException("Could not make set of integers (like 1;2;3;8;7) from \"" + s +"\"");
}
return set;
}
}
Database:
#Database(entities = {SQLUkol.class,SQLPredmet.class,SQLList.class},version = 1)
#TypeConverters({MyConverter.class})
public abstract class AppDatabase extends RoomDatabase {
public abstract MojeDAO mojeDao();
}
One of the entities (getters, setters and constructors not included):
#Entity(primaryKeys = {"id", "list_id"},
indices = {#Index("list_id")},
foreignKeys = #ForeignKey(entity = SQLList.class, parentColumns = "id",
childColumns = "list_id", onDelete = ForeignKey.CASCADE),
tableName = "ukols")
public class SQLUkol implements Comparable<SQLUkol> {
#ColumnInfo(name = "list_id")
private final int listID;
private final int id;
private String title;
#ColumnInfo(name = "title_changed")
private boolean titleChanged = false;
private String notes;
#ColumnInfo(name = "notes_changed")
private boolean notesChanged = false;
private boolean completed;
#ColumnInfo(name = "completed_changed")
private boolean completedChanged = false;
private LocalDate date;
#ColumnInfo(name = "date_changed")
private boolean dateChanged = false;
#Embedded
private final SQLData data;
}
Room does not like generics much. I had to do this:
#TypeConverter
public static String toString1(Map<String, String> m){
...
}
#TypeConverter
public static String toString2(Map<Integer, String> m){
...
}
#TypeConverter
public static String toString3(Set<Integer> s){
...
}
#TypeConverter
public static String toString4(List<Integer> l){
...
}
not just
#TypeConverter
public static String toString(Map m){
...
}
#TypeConverter
public static String toString(Collection<Integer> c){
...
}
I have an object with these attributes:
public final class CaseNote {
private final Long caseNoteId;
private final Long subGroupId;
private final String title;
private final String caseNoteTypeCode;
private final Date contactDate;
private final Date completedDateTime;
private final Long personVisitId;
private final Date createdDateTime;
private final Long createdByWorkerId;
private final Long createdByTeamId;
private final List<CaseNoteDetailsDTO> noteDetails = new ArrayList<CaseNoteDetailsDTO>();
private final List<GroupMemberDetailsDTO> selectedMembers = new ArrayList<GroupMemberDetailsDTO>();
private final ReferenceProvider referenceProvider;
private final Date timeOutDate;
public CaseNote(final CaseNotesDTO caseNoteDto, final List<CaseNoteDetailsDTO> noteDetails,
final List<GroupMemberDetailsDTO> selectedMembers, final ReferenceProvider referenceProvider) {
this.caseNoteId = caseNoteDto.getCaseNoteId();
this.subGroupId = caseNoteDto.getSubGroupId();
this.title = caseNoteDto.getTitle();
this.caseNoteTypeCode = caseNoteDto.getCaseNoteTypeCode();
this.contactDate = caseNoteDto.getContactDateTime();
this.completedDateTime = caseNoteDto.getCompletedDateTime();
this.personVisitId = caseNoteDto.getPersonVisitId();
this.createdDateTime = caseNoteDto.getCreatedDateTime();
this.createdByWorkerId = caseNoteDto.getCreatedByWorkerId();
this.createdByTeamId = caseNoteDto.getCreatedByTeamId();
this.timeOutDate = caseNoteDto.getTimeOutDate();
this.noteDetails.clear();
this.selectedMembers.clear();
this.noteDetails.addAll(noteDetails);
Collections.sort(this.noteDetails, new CaseNoteDetailCreatedDateComparator());
this.selectedMembers.addAll(selectedMembers);
this.referenceProvider = referenceProvider;
}
private class CaseNoteDetailCreatedDateComparator implements Comparator<CaseNoteDetailsDTO> {
#Override
public int compare(final CaseNoteDetailsDTO firstCaseNoteDetail, final CaseNoteDetailsDTO secondCaseNoteDetail) {
return firstCaseNoteDetail.getCreatedDateTime().compareTo(secondCaseNoteDetail.getCreatedDateTime());
}
}
public Long getCaseNoteId() {
return caseNoteId;
}
public Long getSubGroupId() {
return subGroupId;
}
public String getTitle() {
return title;
}
public String getCaseNoteTypeCode() {
return caseNoteTypeCode;
}
public Date getContactDate() {
return contactDate;
}
public Date getCompletedDateTime() {
return completedDateTime;
}
public Long getPersonVisitId() {
return personVisitId;
}
public Date getCreatedDateTime() {
return createdDateTime;
}
public Long getCreatedByWorkerId() {
return createdByWorkerId;
}
public Long getCreatedByTeamId() {
return createdByTeamId;
}
public List<CaseNoteDetailsDTO> getNoteDetails() {
return Collections.unmodifiableList(noteDetails);
}
public List<GroupMemberDetailsDTO> getSelectedMembers() {
return Collections.unmodifiableList(selectedMembers);
}
public boolean isSignificant() {
boolean significantEvent = false;
for (final CaseNoteDetailsDTO detail : this.getNoteDetails()) {
significantEvent = significantEvent || detail.isSignificantEvent();
}
return significantEvent;
}
public String getCaseNoteTypeDescription() {
return referenceProvider.provide(ReferenceDomain.CASENOTE_TYPE, getCaseNoteTypeCode());
}
public CaseNoteDetailsDTO getRootNoteDetails() {
validateCaseNoteDetailsExists();
return getNoteDetails().get(0);
}
public List<CaseNoteDetailsDTO> getAppendments() {
validateCaseNoteDetailsExists();
return getNoteDetails().subList(1, getNoteDetails().size());
}
private void validateCaseNoteDetailsExists() {
if (getNoteDetails() == null || getNoteDetails().isEmpty()) {
throw new IllegalStateException("No case note details found");
}
}
public List<String> getMemberNames() {
final List<String> memberNames = new ArrayList<String>();
final List<GroupMemberDetailsDTO> selectedMembers = getSelectedMembers();
for (final GroupMemberDetailsDTO memberDetails : selectedMembers) {
memberNames.add(memberDetails.getName());
}
return memberNames;
}
public Date getTimeOutDate() {
return timeOutDate;
}
public boolean isTimedOut() {
return completedDateTime == null && new Date().after(this.timeOutDate);
}
}
From a JSP file, I would like to print the attribute 'createdWorkerId', but it's not working. I tried to print the title and it works, but not with the createdWorkerId. The line is the following:
<span class="highlighted"><%=noteClassDescription%>: <c:out value="${casenote.createdByWorkerId}"/> </span>
Should I parse the createdWorkerId to a String before or the problem is other? Any help appreciated.
Thanks.