Arcehtype Validation

Hi Guys (well Tim)

I am creating a few test to work through functions I want to test with my export patch

Party customer = (Party) create(ExportArchetypes.CUSTOMER);
Party importer = (Party) create(ExportArchetypes.IMPORTER);
bean.addParticipation(ExportArchetypes.EXPORTER_PARTICIPATION,customer);
bean.addParticipation(ExportArchetypes.IMPORTER_PARTICIPATION,importer);
IMObjectBean customerbean = new IMObjectBean(customer);
IMObjectBean importerbean = new IMObjectBean(importer);
Lookup mr = TestHelper.getLookup("lookup.personTitle", "MR");
customerbean.setValue("title", mr.getCode());
customerbean.setValue("firstName", "Foo");
customerbean.setValue("lastName", "Bar");
customerbean.save();
Lookup mrs = TestHelper.getLookup("lookup.personTitle", "MRS");
importerbean.setValue("title", mrs.getCode());
importerbean.setValue("firstName", "Jane");
importerbean.setValue("lastName", "Doe");
importerbean.save();
bean.save();

that final line throws an error.   The exporter is just a party.customer and the importer is a modified party.customer (no patient nodes)

The exporter participation is a participation.customer and the import participation is a 

org.openvpms.component.business.dao.im.common.IMObjectDAOException: Object not found: party.importer:-1:61bffb41-d37a-11e4-b31d-c3dcc4e3ece2
    at org.openvpms.component.business.dao.hibernate.im.IMObjectDAOHibernate.saveDeferred(IMObjectDAOHibernate.java:949)
    at org.openvpms.component.business.dao.hibernate.im.IMObjectDAOHibernate.preCommit(IMObjectDAOHibernate.java:527)
    at org.openvpms.component.business.dao.hibernate.im.common.Context$ContextSynchronization.beforeCommit(Context.java:530)
    at org.springframework.transaction.support.TransactionSynchronizationUtils.triggerBeforeCommit(TransactionSynchronizationUtils.java:95)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.triggerBeforeCommit(AbstractPlatformTransactionManager.java:927)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:737)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
    at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:147)
    at org.openvpms.component.business.service.archetype.rule.ArchetypeRuleService.execute(ArchetypeRuleService.java:171)
    at org.openvpms.component.business.service.archetype.rule.ArchetypeRuleService.save(ArchetypeRuleService.java:115)
    at org.openvpms.component.business.service.archetype.DelegatingArchetypeService.save(DelegatingArchetypeService.java:208)
    at org.openvpms.component.business.service.archetype.helper.IMObjectBean.save(IMObjectBean.java:1643)
    at org.openvpms.archetype.rules.export.ExportRulesTestCase.testGetFullName(ExportRulesTestCase.java:55)

I initially thought it was because I was derefencing the importer object when i created the bean reference, but running bean.save() prior to declaring the customer and importerbeans gives the same error.

 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Re: Arcehtype Validation

Try adding the participation references after you've saved the customer and importer. The way you've done it will only work if you save everything in the one transaction.

Re: Arcehtype Validation

When I changed to using the Testhelper.createCustomer() method this worked fine.  

So I have my tests working , but I still dont understand why when I the importer and exporter objectrefs seem to be dereferenced from the underlying objects when I try and save the export act bean. 

Act export = (Act) create(ExportArchetypes.EXPORT);
ActBean bean = new ActBean(export);
Party customer = TestHelper.createCustomer("Foo", "Bar", true);
Party importer = createImporter(true);
bean.addParticipation(ExportArchetypes.EXPORTER_PARTICIPATION,customer);
bean.addParticipation(ExportArchetypes.IMPORTER_PARTICIPATION,importer);
IMObjectBean customerbean = new IMObjectBean(customer);
Lookup mr = TestHelper.getLookup("lookup.personTitle", "MR");
customerbean.setValue("title", mr.getCode());
customerbean.save();
assertEquals("Mr Foo Bar", rules.getFullName(export));
bean.setValue("exporterAgent", "Jenny Smith");
assertEquals("Jenny Smith", rules.getFullName(export));

So this test passes and I am still saving the customerbean after I have added the participation...and I never need to call bean.save();

I suspect I may be missing a basic understanding of object dereferencing here from "Java 101" and if so just tell me to go read a textbox on Java objects...

I dislike not having a complete grasp as to why one method works when another fails.

 

Regards
 
Ben 
OpenVPMS Installer and Helper 
Ph: +61423044823 
Email: info[at]charltonit.com[dot]au

Re: Arcehtype Validation

To uniquely identify IMObject instances, OpenVPMS uses:

  • an id - long, assigned by the database;  -1 until the object is saved
  • a linkId - a UUID generated when the object is created
  • an archetype identifier

When you save an object for the first time, its id is updated with the actual database id. Until that time, its linkId is used to identify it.

If you reference an unsaved object outside of a transaction, the ArchetypeService has no way of saving that reference in the database, as it needs a valid id.

Within a transaction, the ArchetypeService can use the linkId to resolve the appropriate IMObject. So if you have multiple unsaved objects that reference each other, you can either:

  • create a transaction and save them individually within the context of the transaction
  • add them all to a list, and save them all at once. This creates a transaction for you

When you used the customer returned by TestHelper.createCustomer(), it had already been saved, thus had a valid database id.

 

 

Syndicate content