grails application design for multiple checkbox scenario - grails

I have a Domain lookup table of Ethnicities. A person can have multiple ethnicities - caucasian, indian, latino, etc. There are around 10 total.
In one part of my application a user will select their own ethnicity, so they will check one or more of the checkboxes. In another part of the application a different type of user can specify to be matched with someone by ethnicity. This user can also specify one ore more, but the can also specify 'no preference'.
So the first user would see:
caucasian
latino
asian
indian
...
The second user sees:
No preference
caucasian
latino
asian
indian
My question is how to implement this in the lookup table/domain user object. The 'no preference' is throwing me off. Is it a boolean in the user object, or another value in the ethnicity lookup table that gets filtered out when the view for first user is displayed?
There is also the option of having the no preference triggering a check all by jquery behind the scenes so that second user would have every ethnicity as part of their their domain object. But then I have to do kludgy stuff like 'does the second user have every ethnicity stored in their domain, if yes then check 'no preference' in the view'.
EDIT:
It's probably better to show the UI. This is what I need to implement. The ethnicities are stored in a look up table. My domain will look something like this:
class Profile P
static hasMany = [ethnicity:Ethnicity]
...
}
And here is what the other use will see:

if ANY == [caucasian, latino, asian, indian......] then additional property is not required.(if user select ANY, you add all ethnicities)
ELSE you can use a list [ANY, caucasian, latino, asian, indian...]. although 'ANY' is not ethnicity.

Related

How to know which checkbox (es) is/are selected

I have this result from the DataBase. The number of result can be one, more than one, or none at all. For every register returned, a table is created, and inside one column a checkbox is displayed. In the view, I need to know which one of the checkboxes are checked, so I can pass the content of another column of the table to the controller, to be processed. It's like "Database returns me a list of people, and I want to send an email to some of them, not all of them. I select the checkbox, look for the "email" column and if the checkbox of that person is checked, the email is sent".
Is there any (preferably, easy) way to achieve that?
Thank you.
If you give all your checkboxes the same name, but distinct values:
<g:checkBox name="sendTo" value="${firstPerson.id}" checked="${false}"/>
....
<g:checkBox name="sendTo" value="${secondPerson.id}" checked="${false}"/>
then in the controller you can use
params.list("sendTo").each { personId ->
def person = Person.get(personId)
// send email to this person
}
params.list() is a handy tool to know about when dealing with potentially multi-valued parameters. A standard params.sendTo would give you null if no checkboxes were selected, a String if one is selected, and a List if two or more are selected. Using params.list("sendTo") will always give you a List (with zero, one or more than one element as appropriate).

Spring: Object stored and used in Sitemesh decorator

This is probably a newbie question.
I have a table USER which contains info about login, pass and authorities. Depending on authority or role, detail information about each user can be found in one of following: Teacher, Student, Parent. When the User logs in, the information stored in USER table can be easly taken from security context.
I want to display first name and last name all the time in the header after log in - these can be fetched from the other tables.
My question is this: how do I handle storing one of these objects in session all the time? Or is it okay just to store User (its stored by spring) and then fetch particular table every time I need detail information?
I use spring security 3, hibernate, jsp, sitemash.
For more clarification:
I know how to deal with logged user and to restrict some content. Login details (id, pass, role) are stored in USER table and this is ok - I can fetch it and show whereever I want. The problem is that the details about a particular user (address, name, email, etc) are stored in in another table (STUDENT, TEACHER, PARENT - depending on the role in USER table). This is what I want to know on every page - for example to show his/her name.
TO cut it short -
1. you need to extend spring User to provide additional fields.
2. you need to implement UserDetailsService interface and reference it in the security context.
3. Now you can fetch your object in a controller like this: authentication.getPrincipal() - rememebr to cast to your type.
Additionaly - personally i always have AbstracController which is a base for every controller in my project. There, among others, I have method which returns current principal.

How can I check if it's sfguarduser s first logon in symfony?

I want someone to enter their logon details and then the action to check if they are an existing user. If not, then I want them to have to select their department from a drop down, otherwise proceed as normal.
I don't know whereabouts the logon is processed though to check if they are an existing user. Can anyone help?
In Symfony sfGuard, if someone isn't an existing user, they cannot login at all. This means that you should separate these into login and registration:
If a user, go login.
If not, go register and select department.
However, if you'd like custom behaviour to occur on a user's first time logging in, you could create a flag for this yourself, possibly by creating an associated profile table for the sfGuardUser. The sfGuardUser model doesn't have custom fields you could use for something like this but... it does have "Timestampable" behaviour, so you could maybe do (pseudo-code):
if (created_at == updated_at) {
// go to select department and update updated_at
} else {
// login as normal
}
I believe those two fields are set to equal when a new sfGuardUser is made.
hope that helps.
IMHO it might be useful to store their department information in a table (either extend sf_guard_user, or make another for such "extended userdata", and check if it's set there.
If not, they haven't answered this yet, and thus are here the first time; consider giving the user an option "don't want to choose", different from "not chosen yet" - depending on your business logic, you may want this or not.

add user define properties to a domain class

i have a requirement to allow the user to define some custom field in one of the system entities. do you have any suggestion/pattern/plugin that will help me add this feature to my application.
thanks,
Meni
You can add a Map property to your domain class and store arbitrary data there. It's rather limited though. It will generate a table with varchar(255) keys and values, so you need to manage any type conversions yourself, e.g.
class Thing {
String name
Map extraProperties = [:]
}
int age = 123
def thing = new Thing(name: 'whatever')
thing.extraProperties.age = age.toString()
thing.save()
...
def thing = Thing.get(thingId)
int age = thing.extraProperties.age.toInteger()
See section "5.2.4 Sets, Lists and Maps" at http://grails.org/doc/latest/ for the brief online docs.
Sounds like you want your application to be an infinitely adjustable wrench that users can modify at will. Is that fair?
I don't think it's possible or desirable. Think about what happens when you add an attribute to an existing domain object in Grails. The attribute is added to the ORM mapping, which means the tables have to be modified. The UI has another text box added for data entry; the list page has another column added to its table.
There's a lot going on when you add an attribute. How will you manage multiple users modifying the app all at the same time? What happens when one user is modifying a table while another is accessing the old version?
You ask too much. I don't think it's a reasonable requirement. Grails' sweet spot is rapid development of web-based CRUD applications. I don't think that includes modification by users at runtime.

Complex model binding

I have a Client dto that contains a bunch of fields and also contains a List.
Now, I can bind to it quite easy, and it will display the Client with all of his addresses. The thing is that the user can delete and add addresses dynamically.
I thought about adding forms surrounding each address but then I end up with inner forms and I know browsers don't play well with that.
Then I thought about using javascript but if an address is removed, I have to go over all addresses and change their indexes (addresses[0].City )because I noticed that if the indexes are not in order, and the action takes a ClientForm as a parameter, then only the addresses that have consecutive indexes and they start at 0 - will get in the ClientForm.Addresses list.
Any other solutions that are easy to implement ? Am I missing something ?
If you place a submit button with a different name on each of the addresses but no form tags, your outer form can check for the existence of a specific button and redirect to the right action (e.g edit address number 1, delete address 3 etc)
If you are using jquery validation one caveat is that the type of all the "child" submit buttons must be set to "cancel" so on pressing them validation does not occur.
HTH,
Dan
For each address on your page provide a hidden form field called addresses.Index, this will take an integer value. The ASP.NET MVC model binder (in version 2 and above) will receive the multiple values of the addresses.Index form field, and use the integer values to determine which addresses[index].property field values logically belong together.
E.g.
addresses.Index = 0
addresses.Index = 3
Would prompt the model binder to go looking for...
addresses[0].City
addresses[0].Street
addresses[3].City
addresses[3].Street
...and populate an ICollection<Address> in your controller action with two elements.
Of course if you can delete and insert these records on your page, your Javascript will need to track the next index to use in a global variable, to avoid re-using the same index for multiple rows (i.e. don't just rely on the length of a table that holds the address elements, etc).
This solution described in further detail by Phil Haack here.

Resources