Javabean Mapping Using Dozer Framework: Author: Muffadal Katheria Date: 20-Sep-2010 Project Details
Javabean Mapping Using Dozer Framework: Author: Muffadal Katheria Date: 20-Sep-2010 Project Details
Javabean Mapping Using Dozer Framework: Author: Muffadal Katheria Date: 20-Sep-2010 Project Details
Author: Muffadal Katheria Date: 20-Sep-2010 Project Details: Project Code: BOAIOLP2 Project Na e: Ca!hpro IB": BCM Soft#are $%&iro% e%t: 'a&a Declaration: I here() declare that thi! docu e%t i! (a!ed o% ) per!o%al e*perie%ce+ ,o the (e!t of ) -%o#led.e/ thi! docu e%t doe! %ot co%tai% a%) aterial that i%fri%.e! the cop)ri.ht! of a%) other i%di&idual or or.a%i0atio% i%cludi%. the cu!to er! of I%fo!)!+ Target ea!ers:
Soft#are 1e&eloper!/ ,ech%ical A%al)!t!/ ,ech%ical Lead!/ ,ech%ical Architect!/ tech%ical 2riter! "e# $or!s: 'A3ABea% Mapper/ Bea% Mappi%. 4ra e#or-/ 1o0er/ 2S-"I Pre%re&uisite: It i! re5uired that u!er ha&e a (a!ic u%der!ta%di%. of 'a&a Pro.ra Pre'ace: ,hi! docu e%t pro&ide! a (rief o&er&ie# a%d a #or-i%. applicatio% for #or-i%. #ith 1o0er 4ra e#or- for doi%. 2S-"I 62e( Ser&ice-"I7 ha%dler appi%.8'a&aBea%! to 'a&aBea%! appi%.+ i%.+ appi%.+
(ntro!uction: $hat is Dozer) 1o0er i! a 'a&a Bea% to 'a&a Bea% apper/ #hich recur!i&el) copie! data fro o%e o(ject to a%other+ ,)picall)/ the!e 'a&a Bea%! #ill (e of differe%t co ple* t)pe!+
1o0er !upport! !i ple propert) co ple* t)pe (i-directio%al i plicit-e*plicit appi%./ appi%./ appi%./ appi%./ appi%.+
A! #ell a! recur!i&e
1o0er %ot o%l) !upport! appi%. (et#ee% attri(ute %a e!/ (ut al!o auto aticall) co%&ert! (et#ee% t)pe! !tri%. to i%te.er co%&er!io%/ date to !tri%. co%&er!io% etc
$here !o we re&uire $*%U( +an!ler Mapping,Ja-aBeans to Ja-aBeans mapping) ,here could (e a re5uire e%t #here )ou %eed to cop) the &alue! of o%e 'a&a Bea% to other/ #here the propert) %a e! are differe%t+ 4or e*a ple i% o%e 'a&a Bea% )ou ha&e a propert) called 9la!tNa e: a%d i% other 'a&a Bea% )ou ha&e a propert) called 9!ur%a e:+ ;e%ce )ou ca%%ot u!e the !a e 'a&a Bea% a%d %eed to cop) &alue! fro o%e 'a&a Bea% to other+ ,hi! ca% (e do%e a%uall) () u!i%. .etter !etter hu%dred! of !uch propertie! to ap<<<< ethod!+ But #hat if )ou ha&e
I% !uch !ce%ario a% Ope% Source 4ra e#or- li-e 1o0er co e! to )our re!cue+ 4or e*a ple i% ) project P=2-I%te.ratio%/ #e #ere to u!e the re!po%!e #e .ot fro the #e(!er&ice! a%d populate the ja&a o(ject #ith tho!e &alue! #hich the "I tea re5uired+ 2e #ere the li%- (et#ee% the !er&ice! tea a%d the "I tea + But the pro(le #a! the re!po%!e fro !er&ice! #a! %ot the !a e #hat "I #a! e*pecti%.+ A%d !i ilarl) to a-e a #e(!er&ice call to .et the re!po%!e #e had to pa!! &alue! #hich the !er&ice! re5uired+ But here too the pro(le #a! !a e+ So #e had t#o optio%! either a%uall) !et the % %u (er of field! () u!i%. .etter! a%d !etter! or u!e a fra e#or- to do thi!+ A%d for o(&iou! rea!o%! #e cho!e the latter+
.ther such 'rameworks an! Bene'its o' Dozer: 'Bea%Mapper i! a%other !uch fra e#or- a&aila(le for doi%. 'a&aBea%! to 'a&aBea%! appi%.+ ,he follo#i%. are the (e%efit! of 1o0er that 1o0er i! li.ht#ei.ht ade u! u!e it o&er other fra e#or-+
$a!) to
ai%tai% the
It! (idirectio%al+ o So o%ce )ou ha&e do%e the appi%. for 'a&a Bea% 9A: to 'a&a Bea% 9B: )ou %eed %ot ha&e to do the appi%. for 'a&a Bea% 9B: to 'a&a Bea% 9A:
Auto atic t)pe co%&er!io%+ o Suppo!e )ou ha&e apped a field 1ate of Bea% A to field Stri%. of Bea% B+ 1o0er auto aticall) doe! the co%&er!io% for 1ate to Stri%.+ Si ilar i! the ca!e #ith all the co pati(le t)pe co%&er!io% li-e Stri%. to I%te.er etc+
*ample Dozer Application with 'ew e/amples o' Mapping: I ha&e co&ered the follo#i%. !ce%ario!+ I ha&e u!ed M)3O+ja&a to M)1,O+ja&a: appi%. of &alue! fro
2he% #e ha&e the !a e propert) %a e! i% (oth the 'a&a Bea%+ o I% thi! ca!e #e do%At ha&e to put a%) appi%. i% the do0erBea%Mappi%.+* l+ 4or e*a ple the propertie! %a e B !ur%a e i% M)3O B M)1,O are !a e+ So there i! %o %eed for it! appi%.+ 1o0er #ill do it auto aticall)+
,)pe Co%&er!io%+ o Stri%. to I%te.er co%&er!io%+ 4or e*a ple the propertie! Stri%. !tri%.Nu (er a%d i%t i%te.erNu (er are apped+ It i! auto aticall) co%&erted () 1o0er fro !tri%. to i%t+
Stri%. to 1ate co%&er!io%+ 4or e*a ple the propert) date i! apped to dateStri%.+ ;ere 1o0er auto aticall) co%&ert! 1ate to Stri%.+ Si ilarl) ja&a+util+date ca% (e co%&erted to Cale%dar date a%d &ice &er!a+
Li!t to Set co%&er!io%+ ;ere the propert) li!t #hich i! a Li!t i! apped to !et #hich i! a Set+ I% the appi%. * l )ou ca% e&e% .i&e a hi%t that #hat t)pe of !et it #ould (e+ Applica(le to ja&a 1+> a%d a(o&e a! .e%eric! i! allo#ed o%l) i% thi! ja&a &er!io%+ ;i%t i! %ot co pul!or)+
Stri%. to MAP co%&er!io%+ ;ere the propertie! 9 apKe)1: a%d 9 apKe)2: #hich are of t)pe Stri%. i% M)3O cla!! are apped to 9do0erMap: i% M)1,O cla!! #hich i! a Map+ 1o0er #ill auto aticall) ap thi! &alue! i% Map a! 9-e) C !tri%. %a e: a%d 9&alue C !tri%. &alue:+ I% our ca!e the -e) &alue pair i% Map #ould (e apKe)1 6-e)7 a%d the corre!po%di%. &alue thi! !tri%. ha! that #e !et i% code+
1eep Mappi%.+ o Suppo!e )ou ha&e a% o(ject refere%ce i% )our 'a&a Bea% a%d )ou %eed to .et &alue! fro that a%d !et it i% the other ja&a (ea% tha% thi! ca% (e do%e throu.h deep appi%.+ ;ere M)3O co%tai%! a% o(ject refere%ce o% A%other+ja&a+ ,he propert) co pa%) fro A%other+ja&a i! apped to propert) 9co pa%): i% M)1,O+ Co%&erter+ 1o0er al!o pro&ide! u! the capa(ilit) to #rite cu!to co%&erter!+ I ha&e e*plai%ed thi! () #riti%. a cu!to co%&erter for co%&erti%. Boolea% to Stri%. &alue+ 4or #riti%. cu!to co%&erter! #e ha&e to e*te%d 1o0erCo%&erter cla!! pro&ided () the 1o0er 4ra e#or-+ I ha&e #ritte% the cla!! Stri%.,oBoolea%1o0erCo%&erter+ja&a #hich e*te%d! the 1o0erCo%&erter cla!!+
Cu!to o
;ere #e ha&e to o&erride t#o ethod! of the fra e#or-/ %a el) 9co%&ert4ro : a%d 9co%&ert,o: a%d #rite our lo.ic for co%&er!io%+ ;ere i% the e*a ple/ if the Boolea% &alue i! true I ha&e !et the &alue of Stri%. to 9Succe!!: el!e 94ailure:+
*ample $orking 0o!e: I ha&e attached a !a ple #or-i%. code+ I port thi! a! a !ta%dalo%e #or-i%. project i% )our I1$ a%d ru% the ai% cla!!+ I ha&e put the co e%t! i% li%e i% the file! for (etter u%der!ta%di%.+
DozerTest.zip
e'erences: http:88do0er+!ourcefor.e+%et8