Model and Event

151 views
Skip to first unread message

Kanat Mergenbayev

unread,
Dec 8, 2021, 12:00:54 AM12/8/21
to iDempiere
Hi.

Can I combine Model and Event?

On change fire event and put custom business logic into model. 

I found that using model generator I can generate X_C_Bank with all new methods/fields I added in "Bank / Cash" window. 

Now on 
registerTableEvent(IEventTopics.PO_AFTER_NEW, MBank.Table_Name);
registerTableEvent(IEventTopics.PO_AFTER_CHANGE, MBank.Table_Name);
registerTableEvent(IEventTopics.PO_AFTER_DELETE, MBank.Table_Name);
I want to work with my custom "fields/methods" get/set them.

Is there an example how I can combine event and model?

Heng Sin Low

unread,
Dec 8, 2021, 12:05:17 AM12/8/21
to idem...@googlegroups.com
If you are using code from master branch, you can follow the example at https://wiki.idempiere.org/en/NF9.1_OSGi_New_Event_Handling_Annotation
 
On earlier version (8.2 and below), use the getPO(event) method and cast it to the model class that should be working with, for e.g MBank bank = (MBank)getPO(event);


--
You received this message because you are subscribed to the Google Groups "iDempiere" group.
To unsubscribe from this group and stop receiving emails from it, send an email to idempiere+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/idempiere/b33d00b6-efb9-42be-9e0d-4ce813c88e1cn%40googlegroups.com.

Saulo Gil

unread,
Dec 8, 2021, 7:37:26 AM12/8/21
to idem...@googlegroups.com

Hello Kanat,

When working with model classes that already exist in the core, it is best to leave the iDempiere core without modifications and handle everything from a plugin. If all you need is to add get/setters to core model classes, you could take only the generated interface files, then use them within your plugin via POWrapper.

Best regards,

Saulo Gil | Orbital Software  | +54 911 3049 4237

Kanat Mergenbayev

unread,
Dec 8, 2021, 12:35:03 PM12/8/21
to idem...@googlegroups.com
Can you please share an example of using Powrapper with me.

Saulo Gil

unread,
Dec 8, 2021, 1:29:03 PM12/8/21
to idem...@googlegroups.com

Sure, it'd go like this:

MBank bank = (MBank) getPO(event); // taking Heng Sin's example

your.package.I_C_Bank customBank = POWrapper.create(bank, your.package.I_C_Bank.class);

customBank.setFoo(bar); // invoke your own setter/getter

bank.save();

On 8/12/21 14:34, Kanat Mergenbayev wrote:
Can you please share an example of using Powrapper with me.
On Wed, 8 Dec 2021 at 18:37 Saulo Gil <saul...@orbital.com.ar> wrote:

Hello Kanat,

When working with model classes that already exist in the core, it is best to leave the iDempiere core without modifications and handle everything from a plugin. If all you need is to add get/setters to core model classes, you could take only the generated interface files, then use them within your plugin via POWrapper.

Best regards,

On 8/12/21 02:00, Kanat Mergenbayev wrote:
Hi.

Can I combine Model and Event?

On change fire event and put custom business logic into model. 

I found that using model generator I can generate X_C_Bank with all new methods/fields I added in "Bank / Cash" window. 

Now on 
registerTableEvent(IEventTopics.PO_AFTER_NEW, MBank.Table_Name);
registerTableEvent(IEventTopics.PO_AFTER_CHANGE, MBank.Table_Name);
registerTableEvent(IEventTopics.PO_AFTER_DELETE, MBank.Table_Name);
I want to work with my custom "fields/methods" get/set them.

Is there an example how I can combine event and model?

--
You received this message because you are subscribed to the Google Groups "iDempiere" group.
To unsubscribe from this group and stop receiving emails from it, send an email to idempiere+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/idempiere/b33d00b6-efb9-42be-9e0d-4ce813c88e1cn%40googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "iDempiere" group.
To unsubscribe from this group and stop receiving emails from it, send an email to idempiere+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/idempiere/0100017d9a0c653d-d4555d66-8f67-4ca4-8e2f-730d5c742de2-000000%40email.amazonses.com.
--
You received this message because you are subscribed to the Google Groups "iDempiere" group.
To unsubscribe from this group and stop receiving emails from it, send an email to idempiere+...@googlegroups.com.

Kanat Mergenbayev

unread,
Dec 8, 2021, 11:35:02 PM12/8/21
to iDempiere
Thank you. works, but with some exceptions...

public class BankEventHandler extends AbstractEventHandler {

        CLogger log = CLogger.getCLogger(BankEventHandler.class);
       
        @Override
        protected void doHandleEvent(Event event) {
                // TODO Auto-generated method stub
                PO po = getPO(event);

                if (po instanceof MBank) {
                       
                        Properties context = Env.getCtx();
                       
                        MBank bank = (MBank) po;
                       
                        Integer tableId = bank.get_Table_ID();
                       
                        Integer recordId = bank.get_ID();

                        kz.greenCrossEco.bank.events.I_C_Bank customBank = POWrapper.create(bank, kz.greenCrossEco.bank.events.I_C_Bank.class);

                        customBank.setC_Bank_P_Rem_Acc_System_Key("parent key");
                       
                        customBank.setC_Bank_B_Rem_Acc_System_key("branch key");

                        bank.save();
               
                }
        }

        @Override
        protected void initialize() {
                // TODO Auto-generated method stub

                registerTableEvent(IEventTopics.PO_AFTER_NEW, MBank.Table_Name);
                registerTableEvent(IEventTopics.PO_AFTER_CHANGE, MBank.Table_Name);
                registerTableEvent(IEventTopics.PO_AFTER_DELETE, MBank.Table_Name);
        }
}
 saves data in database, but getting error in concole

!ENTRY org.eclipse.equinox.event 4 0 2021-12-09 10:30:44.178
!MESSAGE Exception while dispatching event org.osgi.service.event.Event [topic=adempiere/po/afterChange] {event.data=MBank[1000013-My bank 3], event.errorMessages=[java.lang.StackOverflowError], tableName=C_Bank} to handler kz.greenCrossEco.bank.events.BankEventHandler@a58b7d7
!STACK 0
java.lang.StackOverflowError
        at java.base/java.lang.StackStreamFactory$AbstractStackWalker.callStackWalk(Native Method)
        at java.base/java.lang.StackStreamFactory$AbstractStackWalker.beginStackWalk(StackStreamFactory.java:370)
        at java.base/java.lang.StackStreamFactory$AbstractStackWalker.walk(StackStreamFactory.java:243)
        at java.base/java.lang.StackWalker.walk(StackWalker.java:498)
        at java.logging/java.util.logging.LogRecord$CallerFinder.get(LogRecord.java:709)
        at java.logging/java.util.logging.LogRecord.inferCaller(LogRecord.java:683)
        at java.logging/java.util.logging.LogRecord.getSourceMethodName(LogRecord.java:383)
        at org.compiere.util.CLogFormatter.getClassMethod(CLogFormatter.java:220)
        at org.compiere.util.CLogFormatter.format(CLogFormatter.java:125)
        at java.logging/java.util.logging.StreamHandler.publish(StreamHandler.java:199)
        at java.logging/java.util.logging.ConsoleHandler.publish(ConsoleHandler.java:95)
        at java.logging/java.util.logging.Logger.log(Logger.java:979)
        at java.logging/java.util.logging.Logger.doLog(Logger.java:1006)
        at java.logging/java.util.logging.Logger.log(Logger.java:1029)
        at java.logging/java.util.logging.Logger.severe(Logger.java:1776)
        at org.compiere.util.CLogger.saveError(CLogger.java:180)
        at org.compiere.util.CLogger.saveError(CLogger.java:139)
        at org.compiere.model.PO.saveFinish(PO.java:2410)
        at org.compiere.model.PO.saveUpdate(PO.java:2537)
        at org.compiere.model.PO.save(PO.java:2283)
        at kz.greenCrossEco.bank.events.BankEventHandler.doHandleEvent(BankEventHandler.java:46)

четверг, 9 декабря 2021 г. в 00:29:03 UTC+6, saul...@orbital.com.ar:

Heng Sin Low

unread,
Dec 8, 2021, 11:44:42 PM12/8/21
to idem...@googlegroups.com
you can not call bank.save in your event handler, that causes infinite loop.

Saulo Gil

unread,
Dec 9, 2021, 6:57:16 AM12/9/21
to idem...@googlegroups.com

It looks like you could subscribe to events that ocurr before the record is saved, such as PO_BEFORE_NEW. If that is possible, you wouldn't need to invoke the save method as the record would be already in the process of being saved.

Reply all
Reply to author
Forward
0 new messages