Announcement Announcement Module
Collapse
No announcement yet.
How to assign domain objects to each other inside a flow (select box, converter) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to assign domain objects to each other inside a flow (select box, converter)

    Hi all,

    I am using Spring 2.5.6, SWF 2.0.6 and Hibernate 3 and am pretty new to all three of them.

    I got a problem which I guess is pretty common (found several related threads here). While populating one domain object in a form/flow (let's say a shop), I want the user to select an instance of another object (employee) in a select box (the shop has a field of type Employee). All employees are already in the database, so they do not need to be created and come with a database ID.

    In my flow definition, I get all employees from the database, put them in a flowScope variable and render the view-state with the model shop, using the employees' database IDs as <option> value like this:

    Code:
    <form:select path="employee">
     <form:options items="${employees}" itemValue="id" itemLabel="name" />
    </form:select>
    So what I am getting on the next transition is the database ID of the selected employee in form of a string. Right then I get a ConversionExecutorNotFoundException, so I created the custom converter StringToEmployee and added it. So far, this converter just returns a new employee to get everything working. It seems to me, that in order to convert the ID to an employee I would have to get it again from the database from inside the converter. Before getting to that, I thought that what I am doing might not be the best way.

    All this Employee to String to Employee conversion seems unnecessary (but that might just be me not getting flows).

    How would one usually do this kind of thing? I have domain objects associated to each other and want to assign instances inside a flow by letting the user select. Do I really need to create converters for each type including database read operations to get a selected object even when I already fetched all objects before?

    I skimmed through the documentation, looked at the booking application, searched this forum, but am still looking for help. I hope my problem is clear, all input is appreciated. Thanks in advance.

    Wolfram

  • #2
    I've run into the same kind of problem with JSF and select elements.

    You'll probably need to maintain a map of employee id's to employees, and use that map to get the already-created employee when the user selects the choice with the employee ID value.

    For my solution (which may or may not help you out here), I created a SelectionControl class, which can be populated with a collection (usually list) of items. I chose to use indexes for the value selection on my page, and bound the value of my select element to the selected index property of the SelectionControl. On transition, I would call the getSelection() method on my SelectionControl to get the item at the selected index, and update the value whereever it's required. I made SelectionControl a prototype-scoped Spring bean, so I could get a new one whenever I wanted and place it in whichever scope worked best.

    Comment


    • #3
      Originally posted by InverseFalcon View Post
      I've run into the same kind of problem with JSF and select elements.

      You'll probably need to maintain a map of employee id's to employees, and use that map to get the already-created employee when the user selects the choice with the employee ID value.

      For my solution (which may or may not help you out here), I created a SelectionControl class, which can be populated with a collection (usually list) of items. I chose to use indexes for the value selection on my page, and bound the value of my select element to the selected index property of the SelectionControl. On transition, I would call the getSelection() method on my SelectionControl to get the item at the selected index, and update the value whereever it's required. I made SelectionControl a prototype-scoped Spring bean, so I could get a new one whenever I wanted and place it in whichever scope worked best.
      Could you please post example code?

      Comment


      • #4
        Originally posted by digz6666 View Post
        Could you please post example code?
        I think he's refering to this post introducing his SelectionControl.

        @InverseFalcon: Thanks, I tried that briefly before posting myself. I didn't work for me right away, but I guess, I will look into it again.

        Did you have any issues with a "ConversionExecutorNotFoundException" using your solution?

        Comment


        • #5
          Originally posted by Wolfram View Post
          I think he's refering to this post introducing his SelectionControl.

          @InverseFalcon: Thanks, I tried that briefly before posting myself. I didn't work for me right away, but I guess, I will look into it again.

          Did you have any issues with a "ConversionExecutorNotFoundException" using your solution?
          I've fixed ConversionExecutorNotFoundException by adding custom data converters.

          Here's the custom type converter. Maybe return null is the issue for me.

          Code:
          package com.eon.ibuy.forms;
          
          import org.springframework.binding.convert.converters.StringToObject;
          import com.eon.ibuy.persistence.Country;
          
          public class CountryToString extends StringToObject {
          	public CountryToString(Class country) {
          		super(country);
          		System.out.println(">>>>countryBLA");
          	}
          
          	@Override
          	protected Object toObject(String countryId, Class country) throws Exception {
          		System.out.println(">>>>countryBLA1: " + countryId);
          		
          		return null;
          	}
          
          	@Override
          	protected String toString(Object country) throws Exception {
          		System.out.println(">>>>countryBLA2: " + ((Country)country).getCountryId());
          		return ((Country)country).getCountryName();
          	}
          }
          How can I access hibernate dao from above code? (from toObject function)?

          Comment


          • #6
            Originally posted by digz6666 View Post
            I've fixed ConversionExecutorNotFoundException by adding custom data converters.

            Here's the custom type converter. Maybe return null is the issue for me.

            Code:
            package com.eon.ibuy.forms;
            
            import org.springframework.binding.convert.converters.StringToObject;
            import com.eon.ibuy.persistence.Country;
            
            public class CountryToString extends StringToObject {
            	public CountryToString(Class country) {
            		super(country);
            		System.out.println(">>>>countryBLA");
            	}
            
            	@Override
            	protected Object toObject(String countryId, Class country) throws Exception {
            		System.out.println(">>>>countryBLA1: " + countryId);
            		
            		return null;
            	}
            
            	@Override
            	protected String toString(Object country) throws Exception {
            		System.out.println(">>>>countryBLA2: " + ((Country)country).getCountryId());
            		return ((Country)country).getCountryName();
            	}
            }
            How can I access hibernate dao from above code? (from toObject function)?
            I've accessed applicationContext then get the CountryDAO from applicationContext, by mentioned from there:

            Access spring applicationContext from everywhere in your application.

            But it didn't fixed my PropertyNotFoundException mentioned here.

            Comment

            Working...
            X