Validating nested loops with helper classes

View: New views
8 Messages — Rating Filter:   Alert me  

Validating nested loops with helper classes

by Stephan Windmüller-8 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello!

I am trying to create a simple use-case with Tapestry components, but I
am unsure how to handle this correctly. Perhaps someone knows a solution.

The site is used to assign users to jobs. Each job has a java.util.List
of users assigned to it. Of course a user should not be assigned twice
to a job. So basically my class should look like

public class Job {
        private String title;
        private List<User> assignments;

        ... getter/setter ...
}

Unfortunately the List is not part of the class, I have to get it
directly from the database. So I wrote a helper bean class DisplayJob
which contains the assignments.

Now I want to create a page that displays all jobs and for each job the
assigned users. My first try is a table with

-----

<tr t:type="loop" t:source="jobs" t:encoder="jobEncoder" t:value="job">
 <td>${job.title}</td>

 <td t:type="loop" t:source="job.assignments" t:value="user"
t:encoder="jobEncoder">

 <select t:type="select" t:id="job" t:model="userModel" t:value="user"
t:encoder="userEncoder" blankLabel="Unassigned" />

 </td>
</tr>

-----

All jobs and select boxes are drawn as expected, but now I am stuck.
After submitting the form the values reset, job.assignments is never
changed. Also I am unable to validate if a user has been assigned twice
or more to a job.

JobEncoder creates a new DisplayJob, reading the assignments from the
database, so the data is of course not changed. But where is the place
to change the database?

Does anyone know how I can accomplish this task? Or is it even possible
to display the data in a Grid component?

TIA
 Stephan

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


Re: Validating nested loops with helper classes

by xtien :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Stephan Windmüller wrote:
> All jobs and select boxes are drawn as expected, but now I am stuck.
> After submitting the form
How do you submit the form?

> the values reset, job.assignments is never
> changed. Also I am unable to validate if a user has been assigned twice
> or more to a job.
>
> JobEncoder creates a new DisplayJob, reading the assignments from the
> database, so the data is of course not changed. But where is the place
> to change the database?
>
> Does anyone know how I can accomplish this task? Or is it even possible
> to display the data in a Grid component?
>
> TIA
>  Stephan
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@...
> For additional commands, e-mail: users-help@...
>
>  


--
dagdag is just a two-character rotation of byebye.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


Re: Validating nested loops with helper classes

by Stephan Windmüller-8 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Christine schrieb:

>> All jobs and select boxes are drawn as expected, but now I am stuck.
>> After submitting the form
> How do you submit the form?

With a standard submit component:

 <input type="submit" t:type="submit" value="${message:assign}"/>

I even tried the GenericSelectModel described on [0] for displaying the
user data, but it seems that my main problem is the storing of values in
a Java List. :(

Is it even possible to iterate over such a list, storing values there?

Regards
 Stephan

[0] http://wiki.apache.org/tapestry/Tapestry5HowtoSelectWithObjects

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


Parent Message unknown Re: Validating nested loops with helper classes

by nille hammer :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Stephan,
> Of course a user should not be assigned twice
> to a job.
This sounds like a valid use case for a Set rather than a List. To achieve this implement a reasonable equals-Method for User and rewrite your Job-class/your helper Bean DisplayJob to return a Set. As implementation of Set use a LinkedHashSet. This keeps the order of elements on each iteration. If you could even write a reasonable compareTo()-Method for User or alternatively a Comparator you would be able to use SortedSet and as implementation a TreeSet.
...
>  <td t:type="loop" t:source="job.assignments" t:value="user"
> t:encoder="jobEncoder">
>
>  <select t:type="select" t:id="job" t:model="userModel" t:value="user"
> t:encoder="userEncoder" blankLabel="Unassigned" />
>
>  </td>
...
This looks to me as if you were displaying all users that are allready assigned to a Job in a select box. You´d need a select box with the UN-assigned users to make it possible to add new ones to the Job. This select box should be a multi select box. I have never done this myself so all I can do for you is point  you to the wiki article: http://wiki.apache.org/tapestry/Tapestry5MultipleSelectOnObjects

Hope this helps, nillehammer

==
http://www.winfonet.eu

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


Re: Validating nested loops with helper classes

by Stephan Windmüller-8 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

nille hammer schrieb:

> This looks to me as if you were displaying all users that are allready assigned to a Job in a select box.

Correct, one user per SelectBox.

> You´d need a select box with the UN-assigned users to make it possible to add new ones to the Job.

The number of users and the number of jobs may grow quite large, but the
number of users per job is always small. So I decided to display one
SelectBox per assignment.

> This select box should be a multi select box.

A multi select box would be too large because of the large number of
users and jobs.

Regards
 Stephan

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


Re: Validating nested loops with helper classes

by Stephan Windmüller-8 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Mon, 29. Jun 2009, Stephan Windmüller wrote:

> The site is used to assign users to jobs. Each job has a java.util.List
> of users assigned to it. Of course a user should not be assigned twice
> to a job.

To clarify this I modified the example from
http://wiki.apache.org/tapestry/Tapestry5HowtoSelectWithObjects

-----

SelectTest.java

package de.ls5.ocs.webclient.tapestry.pages;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.services.PropertyAccess;

import de.ls5.ocs.backend.ConferenceController;
import de.ls5.ocs.backend.User;
import de.ls5.ocs.webclient.tapestry.services.GenericSelectModel;
import de.ls5.ocs.webclient.tapestry.util.ASO;

public class SelectTest {
    @Persist
    private List<User> assignments;
    @Inject
    private PropertyAccess _access;
   
    @Persist
    @Property
    private User selectedUser;
     
    @Inject
    private ConferenceController conferenceController;
       
    private GenericSelectModel<User> selectableUsers;
   
    void onActivate() {
    System.out.println("OnActivate");
    if (assignments == null) {
    User emptyUSer = new User("None", "Undecided", "none");
    System.out.println("Filling List");
    assignments = new ArrayList<User>();
    assignments.add(emptyUSer);
    assignments.add(emptyUSer);
    assignments.add(emptyUSer);
    } else {
    System.out.println("List was already filled with " + assignments.size() + " elements.");
    }
   
    // Getting NullPointerException if this is missing. Why?
    if (selectedUser == null) {
    System.out.println("Setting selectedUser");
    selectedUser = new User("bla");
    }
    }

    public SelectTest(){
        selectableUsers = new GenericSelectModel<User>(DAO.getAllUsers(),User.class,"fullName","id",_access);
    }
   
    public List<User> getAssignments(){
       return assignments;
    }

    public void setAssignments(List<User> assignments){
       this.assignments = assignments;
    }
   
    public GenericSelectModel<User> getSelectableUsers(){
       return selectableUsers;
    }
}




SelectTest.tml

<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
    <head>
            <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
        <title>Select test</title>
    </head>
    <body style="font-family:Courier new">
   
        <form t:type="Form">
        <table>
        <tr t:type="loop" t:source="assignments" t:encoder="selectableUsers" t:value="selectedUser">
        <td>
                <t:select model="selectableUsers" encoder="selectableUsers" value="selectedUser"/>
                </td>
            </tr>
        </table>
                <t:submit/>
        </form>
value: ${assignments}
    </body>
</html>

-----

As long as I do not use a List to store the objects, all works fine. But
when I submit the form above, it always resets and the list consists of
"undecided" users.

Regards
 Stephan


smime.p7s (3K) Download Attachment

Re: Validating nested loops with helper classes

by Stephan Windmüller-8 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Stephan Windmüller wrote:

> As long as I do not use a List to store the objects, all works fine. But
> when I submit the form above, it always resets and the list consists of
> "undecided" users.

The next thing I found out: This has nothing to do with my helper
classes. Even when I use a list of Strings for the assignments and
construct the select component like

 <t:select model="literal:User1,User2,User3" value="selectedUser"/>

the users are not stored in the list. I tried different values for the
formState-Parameter, nothing helped. Even when I print all values from
the sychronizeValues-Event, all are empty.

Please, is there anyone who can tell me how to iterate over a list and
store data in it?

Regards
 Stephan

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...


Re: Validating nested loops with helper classes

by Stephan Windmüller-8 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Stephan Windmüller wrote:

> Please, is there anyone who can tell me how to iterate over a list and
> store data in it?

I found the solution. It was a mixture of three different things:

1. Using the formState="literal:VALUES" in loops.
2. The two value-elements from select and loop must not be the same
   value, otherwise the object references do not match.
3. Losing the session after reloading the tomcat application or even
   restarting tomcat is not the same as manually logging out.

Another solution I found on this list is using the index-parameter of
the loop, removing the Property-Annotation and using the index in
getAssignedUser.

Hope that helps others.

- Stephan

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...