Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » scout » AbstractForm newly created ID
AbstractForm newly created ID [message #1075797] Tue, 30 July 2013 04:54 Go to next message
Ranto ANDRIANJAFY is currently offline Ranto ANDRIANJAFY
Messages: 7
Registered: July 2013
Junior Member
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 18:41 Go to previous messageGo to next message
Jeremie Bresson is currently offline Jeremie Bresson
Messages: 699
Registered: October 2011
Senior Member
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 18:41]

Report message to a moderator

Re: AbstractForm newly created ID [message #1078087 is a reply to message #1076119] Fri, 02 August 2013 14:23 Go to previous message
Ranto ANDRIANJAFY is currently offline Ranto ANDRIANJAFY
Messages: 7
Registered: July 2013
Junior Member
Thanks Jeremie, It solves my problem. Smile

For others who use MySQL Database. Use
SQL.select("SELECT LAST_INSERT_ID() INTO :produitNr", formData);

[Updated on: Fri, 02 August 2013 15:01]

Report message to a moderator

Previous Topic:How to use AbstractWizzard
Next Topic:Desktop aplication and web aplication.
Goto Forum:
  


Current Time: Fri Sep 19 04:04:22 GMT 2014

Powered by FUDForum. Page generated in 0.04406 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software