Menu

Map Element at specific index in ArrayList

Help
kiran
2005-12-14
2019-07-25
1 2 > >> (Page 1 of 2)
  • kiran

    kiran - 2005-12-14

    My requirement is some thing like this...

    I have a source bean which has a property whose datatype is an ArrayList and destination bean with a string property.

    My requirement is to map the element at Nth index of the source property to destination string property.

    Is it possible to configure this using dozer?

    Also, I have an another similar kind of requirement where in the element at the middle of deep-field mapping is a array list and I want to map the element at a specific index.

     
    • Franz Garsombke

      Franz Garsombke - 2005-12-14

      Both of those requirements are not possible in dozer. I do not know how we would map an ArrayList to a String? How would you suggest?

      Sorry.

      Franz

       
      • Wojtek K

        Wojtek K - 2006-02-13

        Hi, I am very new to Dozer and at very begining I found similar mapping problem. Let say I have two classes:

        class ClassWithList
          private List forenames;

        class ClassWithProperties
          private String forename1;
          private String forename2;
          private String forename3;

        I would like to populate ClassWithProperties with element 0, 1 & 2 from the property forenames of ClassWithTheList.

        This feature would be very useful as we are mapping quite complicated Axis class to our simple domain objects.

        Is it any chance that you would add such feature, please?

         
    • Ben Sullins

      Ben Sullins - 2005-12-17

      Do you mean you know that an ArrayList contains a certain type of element (String) and you want to map a particular index to a destination object?

      -ben

       
    • kiran

      kiran - 2005-12-20

      Yes. I know the datatype & index of the arraylist from which I have to take the source information.

      I could do this as part of my business processing. But thought it could be better to configure it as part of deep-field mapping instead of extracting the source object in my business logic.

       
      • Franz Garsombke

        Franz Garsombke - 2005-12-21

        I don't think this is possible. Ben?

        Thanks.

        Franz

         
        • Chetan

          Chetan - 2006-02-15

          Just going through the post... I would also be intrested in such a feature. The way I would like it to be ...Say I have a class

          public class ExampleA{
                   List col;
                   ...
          }

          col contains object of type ExampleB

          public class ExampleB{
                   String prop1;
                   String prop2;
                   String prop3;
                   ...
          }

          Now ExapmleA has to be converted to

          Usecase - 1
          public class DestExampleA1{
                 List coll;
                 ...
          }
          The coll in DestExampleA contains only the prop1 property of ExampleB. SO in my mapping I would like to do that as (instead of custome convertor)

          <mapping>
               <class-a>ExampleA</class-a>
               <class-a>DestExampleA1</class-a>
               <field>
                  <a>col[].prop1</a>
                  <b>col</b>
                  ...

          Usecase - 2

          public class DestExampleA2{
                 String prop1;
                 ...
          }

          <mapping>
               <class-a>ExampleA</class-a>
               <class-a>DestExampleA1</class-a>
               <field>
                  <a>col[0].prop1</a>
                  <b>prop1</b>
                  ...

          If I have a way to map like above it would be useful to us.

           
    • Ben Sullins

      Ben Sullins - 2006-02-13

      Can you tell us how you know what value will be in each index?  If its not a guaranteed order, then the mapping would be invalid.  Is it better to refactor the source class?  Can you explain the Axis example that you are referring to?

       
    • Wojtek K

      Wojtek K - 2006-02-14

      Hi,

      Source classes are generated by the wsdl2java tool, and describe some of the govtalk schemas (http://www.govtalk.gov.uk/gdsc/html/frames/). The schema for user details covers all possible variations of user details - for example an individual may have infinite number of names - so the genereted class has a field String[] userNames. The aim our system is not to store all possible variations of name but maximum 2 or 3 so we want to keep userNames[0] as userName1, userNames[1] as userName2 etc. This is the reason of my feature request and I am aware of order issue.

      We can not change govtalk schemas.

      Regards
      w.

       
    • Ben Sullins

      Ben Sullins - 2006-02-14

      Well, thats a pretty good use case.  I think for now, what you need to do is make use of a custom converter. http://dozer.sourceforge.net/documentation/customconverter.html

      I will discuss your situation more with the team and see what they think as well.

      -ben

       
    • Matt Tierney

      Matt Tierney - 2006-02-15

      One other thing I like to do in an application that has a good amount of mapping is to create "Assembler" objects that encapsulate all of the mapping logic.  They hide the underlying mapping implementation.  It gives you the flexibility to modify mapping implementation without having to change much code.  You can use Dozer for most of mappings and if you have a real corner case, you have the flexibility to manually map any fields.  You can also run your mapping through Dozer to 99% of your fields and then have a manual mapping for some odd ball field.  This would happen all within the Assembler without the client having any knowledge of the underlying implementation.  One other important benefit of using Assemblers is that it makes writing unit tests specific to the mapping a lot easier and more focused.  If you ever need to determine if a particular bug is due to mapping of objects, it is simple to write an Assembler unit test that reproduces the use case.  Hope this helps.

       
    • Matt Tierney

      Matt Tierney - 2006-02-15

      To expand on the previous post, here is an overly simplified example of using an assembler type of class.  Almost all of the mappings would just use dozer. 

      public class SomeAssembler {

        public DestObject assembleA(SrcObject src) {
          return dozerMapper.map(src, DestObject.class);
        }

      }

       
    • Wojtek K

      Wojtek K - 2006-02-15

      Hi,

      I sent private message to bgsesr yesterday, but not sure if he received it.

      I did some changes to the code (classes Field, FieldMap, XMLParser and StringConstructorConverter) and now I am able to convert arrays, lists and sets into single fields.

      If you are interested I can put my code somewhere.
      Regards
      w.

       
      • Franz Garsombke

        Franz Garsombke - 2006-02-15

        Can you please add this to the 'Patches' area in SourceForge. I will review, make some unit tests, and potentially add it to our core code base.

        Much appreciated.

        thanks.

        Franz

         
    • Wojtek K

      Wojtek K - 2006-02-16

      Done

      cheers
      w.

       
    • Wojtek K

      Wojtek K - 2006-02-20

      We've made some more changes to the dozer source as we found some bugs in our code.

      Is it possible to create a branch that we could commit to rather than just sending you patches when we think we have something?

      Thanks

      W

       
    • Franz Garsombke

      Franz Garsombke - 2006-02-21

      I am still trying sell this one...if I can sell it a branch will be no problem.

      So, how do you envision mapping back or is this a one-way mapping?

      Thanks for contributing.

      Franz

       
    • Wojtek K

      Wojtek K - 2006-02-21

      This is a two way mapping, let say you have:

      class User {
        private String title;
      }

      class UserStructure {
      UserDetailsStructure details;
      }

      class UserDetailsStructure {
        String[] title;
        String[] firstName;
        String secondName;
      }

      and the mapping:
      <mapping>
        <class-a>User</class>
        <class-b>UserStructure</class-b>
        <field>title</field>
        <field>details.title[0]</field>
      </mapping>

      Additionally we added support for custom converters for indexed fields.

      Currently it's impossible to do mapping like this:
      myfield1.myfield1_1[1].myfield1_1_1 - (index in the middle)

      Also we added support for custom converters or nasted fields - we weren't sure if such feature was available in original version (it didn't want to work for us) - anyway the xml definition is the same.

      In the last 2 day we changed quite a lot of our code - so it's not point for you to use one I published as a patch. If you 'sell' this feature I will try to manage some time to commit our changes as a diffrent branch so you will be able to see the changes in more convenience way - and than you can decide what to do with the code.

      The test result is the same - our code pass all tests expect one in the MapperTest - testCustomConverterMapping

      Regards
      w.

       
      • Franz Garsombke

        Franz Garsombke - 2006-02-22

        That sounds good. I have made a branch off of the v20Branch called 'v20IndexBranch'.

        Please let me know if you have any questions.

        Thanks for your contribution.

        Franz

         
    • Wojtek K

      Wojtek K - 2006-02-22

      I am only able to connect as an annonymous, any advice?

      w.

       
      • Franz Garsombke

        Franz Garsombke - 2006-02-23

        Sorry about that. I need to figure out how to give you branch access. I will let you know when I figure the innards of SF out.

        Thanks,

        Franz

         
        • Franz Garsombke

          Franz Garsombke - 2006-02-23

          That took about 1.5 hours longer than it should have. Try now on the 'v20IndexBranch'. I was able to successfully use that branch w/ a test SF user I created.

          Franz

           
    • Wojtek K

      Wojtek K - 2006-02-27

      Hi,

      I merged our changes into branch: v20IndexBranch also I put an info file describing what has been done. I will look at this more as we expereinced 3 failures during running tests. From the indexBranch.txt file:

      Purpose:
      The purpose of our change was to make translation between Axis generated objects
      and Hibernate objects more smoothly.
      The project we were working on generated classes using Axis wsdl2java tool according to WSDL/XSD structure.
      Relation zero to many was coverted as an array of objects. We were working with govtalk schemas
      http://www.govtalk.gov.uk/gdsc/html/frames/ that describe all possible variations of users details.
      For instance they are able to keep infinite amount of individual's firstnames. It is rather rare to keep more
      than two firstnames for instance in our project we had a domain object that had two String fields: firstName1
      and firstName2. The desired feature we wanted to have was an ability to convert indexed fields into single
      field without writting a single line of code.

      Features:
      1. Converts single indexed field into single field (bi-directional)
      2. Converts indexed field using custom converters

      Limitations:
      1. It is not able to convert field like (without using custom converters):
        a. prop1[1].prop2 (nested indexed property, albeit it's possible to convert prop1.prop2[2])
        b. prop1[1][2] (multi dimmension)

      Examples:
      Example of usage can be found in the test class:
      net.sf.dozer.util.mapping.IndividualMappingTest

      Example mapping can be found in
      mappings/IndividualMapping.xml

       
      • Franz Garsombke

        Franz Garsombke - 2006-02-28

        Thank you for adding this. Please let me know when you consider this 'stable' and I will merge back into v20Branch and form an official release.

        Thanks,

        franz

         
      • sireesh

        sireesh - 2006-03-20

        Hi,
        I need to map two fields, which may be indexed.
        e.g: ResultObject._Result_List[i]._addressList[0]._city --> CustomerSearchResultList.customerSearchResultList[i].city

        As dozer doesn't support indexing, we have thought of implementing it by automatically generating the custom converter code for mapping these fields.
        But as Wojtek has worked on this and implemented partial indexing with some limitations, I would like to know from him or someone who has gone through the code if the mapping specified by me can be acheived. If so, can please specify roughly the effort needed.

        Thanks,
        Sireesh

         
1 2 > >> (Page 1 of 2)

Log in to post a comment.