java - Custom Mapping with orika -
i using orika mapper mapping fields of source , destination class.
i can one-to-one mapping perfectly.
i have 2 fields dateofdeparture , dateofarrival in source class.
i have calculate difference between these dates , map field "travelduration" in destination class.
following mapper class.
package com.tcs.orika; import java.text.parseexception; import java.text.simpledateformat; import java.util.date; import java.util.hashmap; import java.util.map; import java.util.concurrent.timeunit; import ma.glasnost.orika.mapperfacade; import ma.glasnost.orika.mapperfactory; import ma.glasnost.orika.converter.converterfactory; import ma.glasnost.orika.impl.configurablemapper; import ma.glasnost.orika.impl.defaultmapperfactory; public class mapper extends configurablemapper { public static void main(string[] args) throws parseexception { /*date calculation*/ simpledateformat dateformat = new simpledateformat("dd-m-yyyy hh:mm:ss"); string dateofdeparture = "31-08-1982 10:20:56"; string dateofarrival = "31-08-1983 10:20:56"; date date1 = dateformat.parse(dateofdeparture); date date2 = dateformat.parse(dateofarrival); long diff = date2.gettime() - date1.gettime(); int noofdays = (int) timeunit.days.convert(diff, timeunit.milliseconds); /*set values source object*/ orikamapfrom objectmapfrom = new orikamapfrom(); orikamapto objectmapto = new orikamapto(); objectmapfrom.setsource("delhi"); objectmapfrom.setdestination("amsterdam"); objectmapfrom.setdateofdeparture(date1); objectmapfrom.setdateofarrival(date2); /*name mapping when source , destination names different*/ mapperfactory mapperfactory = new defaultmapperfactory.builder().build(); mapperfactory.classmap(orikamapfrom.class, orikamapto.class) .field("source", "sourceplace") .field("destination","destinationplace") .field(noofdays,"travelduration")--------------->facing error on line .register(); /*value mapping*/ mapperfacade mapper = mapperfactory.getmapperfacade(); objectmapto = mapper.map(objectmapfrom, orikamapto.class); objectmapto.settravelduration(noofdays); system.out.println(objectmapto.getsourceplace()); system.out.println(objectmapto.getdestinationplace()); system.exit(0); } }
please advice how map (noofdays,"travelduration").
here way case:
public class mapper { public static void main(string[] args) throws parseexception { /*date calculation*/ simpledateformat dateformat = new simpledateformat("dd-m-yyyy hh:mm:ss"); string dateofdeparture = "31-08-1982 10:20:56"; string dateofarrival = "31-08-1983 10:20:56"; date date1 = dateformat.parse(dateofdeparture); date date2 = dateformat.parse(dateofarrival); /*set values source object*/ orikamapfrom objectmapfrom = new orikamapfrom(); orikamapto objectmapto = new orikamapto(); objectmapfrom.setsource("delhi"); objectmapfrom.setdestination("amsterdam"); objectmapfrom.setdateofdeparture(date1); objectmapfrom.setdateofarrival(date2); /*name mapping when source , destination names different*/ mapperfactory mapperfactory = new defaultmapperfactory.builder().build(); mapperfactory.classmap(orikamapfrom.class, orikamapto.class) .field("source", "sourceplace") .field("destination","destinationplace") .customize(new custommapper<orikamapfrom,orikamapto>() { @override public void mapatob(orikamapfrom a, orikamapto b, mappingcontext mappingcontext) { long diff = a.getdateofarrival().gettime() - a.getdateofdeparture().gettime(); b.settravelduration((int)timeunit.days.convert(diff, timeunit.milliseconds)); } }) //.field(noofdays,"travelduration")--------------->facing error on line .register(); /*value mapping*/ mapperfacade mapper = mapperfactory.getmapperfacade(); objectmapto = mapper.map(objectmapfrom, orikamapto.class); objectmapto.settravelduration(noofdays); system.out.println(objectmapto.getsourceplace()); system.out.println(objectmapto.getdestinationplace()); system.exit(0); } }
hope can help.
Comments
Post a Comment