Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » scout » Merge FormDatas(Fill a FormData with another FormData)
Merge FormDatas [message #1404103] Tue, 29 July 2014 15:45 Go to next message
Dennis Geesen is currently offline Dennis GeesenFriend
Messages: 46
Registered: June 2014
Member
Hi,

Since I did not found anything I can (re)use, may be someone has some hints for me:

I want to map one FormData into another one. Or you can also say: Fill one FormData with data of another FormData.

For example, I have a CustomerFormData (with fields like Street, City) and a UserFormData (also with fields Street and City).
Now, I want to set the fields of CustomerFormData using the values from the UserFormData without defining each mapping by hand. Therefore, the mapping of street and city should be automatically done instead of explicitly calling
customerFormData.getStreet().setValue(userFormData.getStreet().getValue())  
customerFormData.getCity().setValue(userFormData.getCity().getValue())  
...



Thus, I want to automatically match and bind values from one formData into another.
I think, it is similar to the binding while using SQL.selectInto...

Although I can iterate through the fields by hand, this does not recognize nested fields etc... So, may be someone has a better approach or already a solution what to use?

PS: and yes, I know that it is possible to use abstract classes/templates for common fields...
Re: Merge FormDatas [message #1404131 is a reply to message #1404103] Tue, 29 July 2014 20:09 Go to previous messageGo to next message
Jeremie Bresson is currently offline Jeremie BressonFriend
Messages: 971
Registered: October 2011
Senior Member
Dennis Geesen wrote on Tue, 29 July 2014 17:45
PS: and yes, I know that it is possible to use abstract classes/templates for common fields...


If you do so, this will also be reflected in your FormData (if you use exernal group box data for your group box definded as template, both FormData will share the same GroupBoxData).


For your problem, if you are sure that your field have the same name "StreetField" in both cases, you might use the FieldId returned by the ValueFieldData...

There is also a method on the form data getAllFieldsRec() where you get all the FormFieldData with their ids. It might be a start for your mapping utility.

You should also ensure the type safety (with AbstractValueFieldData#getHolderType()) otherwise you might have nasty ClassCastException at runtime.


It hope it helps you to start. Feel free to continue the discussion.
Re: Merge FormDatas [message #1404164 is a reply to message #1404131] Wed, 30 July 2014 07:17 Go to previous messageGo to next message
Dennis Geesen is currently offline Dennis GeesenFriend
Messages: 46
Registered: June 2014
Member
Thanks for the HolderType hint!

Now, I have the following solution that works for me:

 public static <T extends AbstractFormData> T mergeInto(T from, T into) {
    for (Entry<Integer, Map<String, AbstractFormFieldData>> fieldsAtLevel : from.getAllFieldsRec().entrySet()) {
      for (Entry<String, AbstractFormFieldData> fieldEntry : fieldsAtLevel.getValue().entrySet()) {
        AbstractFormFieldData fromField = fieldEntry.getValue();
        AbstractFormFieldData toField = into.getFieldById(fromField.getFieldId());
        if (toField != null) {
          if (fromField instanceof AbstractValueFieldData && toField instanceof AbstractValueFieldData) {
            AbstractValueFieldData<Object> fromValueField = (AbstractValueFieldData<Object>) fromField;
            AbstractValueFieldData<Object> toValueField = (AbstractValueFieldData<Object>) toField;
            if (fromValueField.getHolderType().equals(toValueField.getHolderType())) {
              toValueField.setValue(fromValueField.getValue());
            }
          }
        }
      }
    }

    // and properties
    for (Entry<Integer, Map<String, AbstractPropertyData<?>>> propsAtLevel : from.getAllPropertiesRec().entrySet()) {
      for (Entry<String, AbstractPropertyData<?>> propEntry : propsAtLevel.getValue().entrySet()) {
        AbstractPropertyData fromProp = propEntry.getValue();
        AbstractPropertyData toProp = into.getPropertyById(fromProp.getPropertyId());
        if (toProp != null) {
          if (fromProp.getHolderType().equals(toProp.getHolderType())) {
            toProp.setValue(fromProp.getValue());
          }
        }
      }

    }
    return into;
  }
Re: Merge FormDatas [message #1404201 is a reply to message #1404164] Wed, 30 July 2014 11:55 Go to previous message
Jeremie Bresson is currently offline Jeremie BressonFriend
Messages: 971
Registered: October 2011
Senior Member
Thank you for your feedback and for sharing!
Previous Topic:Upgrading 3.9 to 3.10 almost ok
Next Topic:Component IDs
Goto Forum:
  


Current Time: Wed Jul 01 06:39:09 GMT 2015

Powered by FUDForum. Page generated in 0.02864 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software