philroc (4) [Avatar] Offline
#1
Hi,

I am currently developing an application which lets users create clients and associate one or more emails with them.

Here’s the roo script which I wrote to generate the application:

# Email Table
entity jpa --class ~.model.Email --testAutomatically
field string --fieldName email --notNull --sizeMin 1 --sizeMax 30

# Note Table
entity jpa --class ~.model.Note --testAutomatically
field string --fieldName note

# Client Table
entity jpa --class ~.model.Client --testAutomatically
field string --fieldName name --notNull --sizeMin 1 --sizeMax 30
field set --fieldName emails --type ~.model.Email --cardinality ONE_TO_MANY --mappedBy client
field set --fieldName notes --type ~.model.Note --cardinality ONE_TO_MANY --mappedBy client

# Email Table
focus --class ~.model.Email
field reference --fieldName client --type ~.model.Client --cardinality MANY_TO_ONE

# Note Table
focus --class ~.model.Note
field reference --fieldName client --type ~.model.Client --cardinality MANY_TO_ONE

# MVC
# change project type to web application and output war artifact
web mvc setup
# Generate Controller, etc.
web mvc all --package ~.web


Whenever I attempt to create new e-mails in my running application, I get a Status 400 error stating that “the request sent by the client was syntactically incorrect”.

The data sent over the network by my browser to the application are the following:

POST /myapplication/emails

and

Email=email1&&client=client1

In the POST’s BODY.

My conclusion is that the 400 error is due to the presence of two parameters in the POST’s body instead of one.
[The Client Controller processes queries with one parameter - client name - without issues].

How can I modify the Email Controller to handle those two parameters?

Since .aj files shouldn't be modified in Roo, how do I override the Controller?

Is there a better place to process the two parameters than in the Email Controller?

Many thanks.

Philroc
ken.rimple (246) [Avatar] Offline
#2
Re: Issue with multiple parameters
> Hi,
>
> I am currently developing an application which lets
> users create clients and associate one or more emails
> with them.
...
> Whenever I attempt to create new e-mails in my
> running application, I get a Status 400 error stating
> that “the request sent by the client was
> syntactically incorrect”.

Huh. Can you give us more information? A stack trace?

> The data sent over the network by my browser to the
> application are the following:
>
> POST /myapplication/emails
>
> and
>
> Email=email1&&client=client1
Ok, the post will need you to submit the data in the format:

modelAttributeName.fieldName

So, if you are adding a new email, and your model attribute is email, you'd have:

email.emailAddress
email.userName

in the model.

If you're associating a key to a person, such as Person -> Email (one to many) you'd have to a) make sure you cascade your changes in the @OneToMany settings (see cascade=ALL for a start), and b) you'd nest form elements. Let's say the person id is 45:

email.person.id = 45

That would then potentially cause it to associate the key. Or you could do it in code:

email.personId (a synthetic form field)
long personId = email.getPersonId();
Person person = // load person from JPA
then
email.setPerson(person);

That should cause it to update. Perhaps. Depending on how your mappings work.

>
> My conclusion is that the 400 error is due to the
> presence of two parameters in the POST’s body instead
> of one.
If you're saving the two objects as fields within the outer object, and not entity references, I'd agree that there is a bug. But those things are references to other entity instances, and for that you may need to fetch and populate them based on keys you've passed back in.

> [The Client Controller processes queries with one
> parameter - client name - without issues].

Right, an elemental field for an entity - therefore saveable by itself.
>
> How can I modify the Email Controller to handle those
> two parameters?

Again, your parameters need to be attributes of the email model, and then when they are nested, you probably need to associate the keys properly. Take a look at the .aj controller to see what's going on.

>
> Since .aj files shouldn't be modified in Roo, how do
> I override the Controller?

Just use STS/Eclipse to 'push-in refactor' the aspect code into the java class. Or, just cut and paste the method, remove the static ClassName. part, and you can get going. Run the Roo shell and it will detect that the method is in the main class, and remove it from your .aj file automatically.
>
> Is there a better place to process the two parameters
> than in the Email Controller?

Again, I don't understand the case. Roo does not have problem with multi-field forms. But relations are more tricky, and I'd suggest you debug one of the roo samples and take a look at the posts, to get an idea of how they relate keys together.

>
> Many thanks.
>
> Philroc

Ken