AbstractForm newly created ID [message #1075797] |
Tue, 30 July 2013 00:54  |
Eclipse User |
|
|
|
Hi,
Is there any function in AbstractForm to get the 'id' of a newly created item?
I want to use it in an AbstractSmartField, I add a 'menu' to it to create a new Item and I want to get the freshly created item to show it in the smartfield.
The execAction of the Menu into AbstractForm (client side)
protected void execAction() throws ProcessingException {
ProduitForm form = new ProduitForm();
form.startNew();
form.waitFor();
if (form.isFormStored()) {
setValue(form.getProduitNr());
}
The create function (server side)
public ProduitFormData create(ProduitFormData formData) throws ProcessingException {
if (!ACCESS.check(new CreateProduitPermission())) {
throw new VetoException(TEXTS.get("AuthorizationFailed"));
}
SQL.insert("INSERT prod(nomProd, Cat_idCat, descProd) VALUES (:nomDuProduit, :catgorie, :descProd) INTO :produitNr", formData);
return formData;
}
Regards Ranto
|
|
|
Re: AbstractForm newly created ID [message #1076119 is a reply to message #1075797] |
Tue, 30 July 2013 14:41   |
Eclipse User |
|
|
|
For the server part, I think it really depends on your database engine.
With Derby, I have implemented it like this:
@Override
public QuestionFormData create(QuestionFormData formData) throws ProcessingException {
if (!ACCESS.check(new CreateQuestionPermission())) {
throw new VetoException(TEXTS.get("AuthorizationFailed"));
}
SQL.insert(" insert into questions (question_text, multiple_choices) " +
" values (:QuestionText, :MultipleChoices) ", formData);
SQL.selectInto(" values IDENTITY_VAL_LOCAL() " +
" into :QuestionNr", formData);
if (formData.getQuestionNr() == null) {
throw new ProcessingException("QuestionNr can not be null");
}
return formData;
}
With an oracle DB, we often select from a sequence before inserting.
The important point is to store the ID you want to access (QuestionNr in my case) in the FormData. If you are not sure, you can debug it or use System.out.println to test it (in the formData you return in the create function)
On the client, you will have something like this in your form handler:
public class NewHandler extends AbstractFormHandler {
@Override
public void execLoad() throws ProcessingException {
IQuestionProcessService service = SERVICES.getService(IQuestionProcessService.class);
QuestionFormData formData = new QuestionFormData();
exportFormData(formData);
formData = service.prepareCreate(formData);
importFormData(formData);
}
@Override
public void execStore() throws ProcessingException {
IQuestionProcessService service = SERVICES.getService(IQuestionProcessService.class);
QuestionFormData formData = new QuestionFormData();
exportFormData(formData);
formData = service.create(formData);
}
}
This is the code that is generated when you create a new form, so I assume you have something like this.
In the FormData that you get with "service.create(formData)" at the end of execStore() you should have the value that you are looking for.
A possibility: (but at this point you can do what you want with the value)
It is possible to import the values contained in the formData in the Form:
@Override
public void execStore() throws ProcessingException {
IQuestionProcessService service = SERVICES.getService(IQuestionProcessService.class);
QuestionFormData formData = new QuestionFormData();
exportFormData(formData);
formData = service.store(formData);
importFormData(formData); //<-- New line
}
This way, you will be able to get the value from the form:
@Override
protected void execAction() throws ProcessingException {
QuestionForm form = new QuestionForm();
form.startNew();
form.waitFor();
if (form.isFormStored()) {
System.out.println(form.getQuestionNr());//<-- The value is here [you could do setValue(..)]
reloadPage();
}
I hope this helps you to understand how it works.
Do not hesitate to ask if you need more informations.
[Updated on: Tue, 30 July 2013 14:41] by Moderator
|
|
|
|
Powered by
FUDForum. Page generated in 0.48814 seconds