use of org.jaffa.persistence.domainobjects.Part in project jaffa-framework by jaffa-projects.
the class EqualsTest method testEquals.
public void testEquals() {
try {
// Create 2 Parts with different keys. The comparison should fail
Part p1 = new Part();
p1.setPart("P1");
p1.setNoun("P1Noun");
Part p2 = new Part();
p2.setPart("P2");
p2.setNoun("P2Noun");
assertTrue(!p1.equals(p2));
assertTrue(!p2.equals(p1));
// Now make the keys the same. The comparison should succeed
p2.setPart("P1");
assertTrue(p1.equals(p2));
assertTrue(p2.equals(p1));
// Create another domain object with similar keyfield. The comparison should fail.
PartRemarks pr = new PartRemarks();
pr.setPart("P1");
assertTrue(!p1.equals(pr));
assertTrue(!pr.equals(p1));
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
use of org.jaffa.persistence.domainobjects.Part in project jaffa-framework by jaffa-projects.
the class EqualsTest method testHashCode.
public void testHashCode() {
try {
// Create 2 Parts with different keys. The hashCodes should be different
Part p1 = new Part();
p1.setPart("P1");
p1.setNoun("P1Noun");
Part p2 = new Part();
p2.setPart("P2");
p2.setNoun("P2Noun");
assertTrue(p1.hashCode() != p2.hashCode());
// Now make the keys the same. The hashCodes should be same
p2.setPart("P1");
assertTrue(p1.hashCode() == p2.hashCode());
// Create another domain object with similar keyfield. The hashCodes should be same.
PartRemarks pr = new PartRemarks();
pr.setPart("P1");
assertTrue(p1.hashCode() == pr.hashCode());
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
use of org.jaffa.persistence.domainobjects.Part in project jaffa-framework by jaffa-projects.
the class Asset method findPartObject.
/**
* Finds the related foreign Part object.
* If checkExistenceOnly is false, then the foreign object will be fetched and assigned to the corresponding member variable of this class.
* If checkExistenceOnly is true, then a mere existence check is performed for the foreign object, as oppposed to fetching all the values for that object.
*/
private void findPartObject(boolean checkExistenceOnly) throws ValidationException, FrameworkException {
UOW uow = getUOW();
boolean localUow = false;
try {
if (m_partObject == null && getPart() != null) {
Criteria criteria = new Criteria();
criteria.setTable(PartMeta.getName());
criteria.addCriteria(PartMeta.PART, getPart());
if (checkExistenceOnly)
criteria.addFunction(Criteria.FUNCTION_COUNT, null, Criteria.ID_FUNCTION_COUNT);
Number count = null;
if (uow == null || !uow.isActive()) {
uow = new UOW();
localUow = true;
}
Iterator itr = uow.query(criteria).iterator();
if (itr.hasNext()) {
if (checkExistenceOnly)
count = (Number) ((Map) itr.next()).get(Criteria.ID_FUNCTION_COUNT);
else
m_partObject = (Part) itr.next();
}
if (m_partObject == null && (count == null || count.intValue() <= 0))
throw new InvalidForeignKeyException(AssetMeta.META_PART.getLabelToken(), new Object[] { PartMeta.getLabelToken(), PartMeta.META_PART.getLabelToken() });
}
} finally {
if (localUow && uow != null)
uow.rollback();
}
}
use of org.jaffa.persistence.domainobjects.Part in project jaffa-framework by jaffa-projects.
the class PartRemarks method findPartObject.
/**
* Finds the related Part object.
* If checkExistenceOnly is false, then the related object will be fetched and assigned to the corresponding member variable of this class.
* If checkExistenceOnly is true, then a mere existence check is performed for the related object, as oppposed to fetching all the values for that object.
*/
private void findPartObject(boolean checkExistenceOnly) throws ValidationException, FrameworkException {
UOW uow = getUOW();
boolean localUow = false;
try {
if (m_partObject == null && getPart() != null) {
Criteria criteria = new Criteria();
criteria.setTable(PartMeta.getName());
criteria.addCriteria(PartMeta.PART, getPart());
if (checkExistenceOnly)
criteria.addFunction(Criteria.FUNCTION_COUNT, null, Criteria.ID_FUNCTION_COUNT);
Number count = null;
if (uow == null || !uow.isActive()) {
uow = new UOW();
localUow = true;
}
Iterator itr = uow.query(criteria).iterator();
if (itr.hasNext()) {
if (checkExistenceOnly)
count = (Number) ((Map) itr.next()).get(Criteria.ID_FUNCTION_COUNT);
else
m_partObject = (Part) itr.next();
}
if (m_partObject == null && (count == null || count.intValue() <= 0))
throw new InvalidForeignKeyException(PartRemarksMeta.META_PART.getLabelToken(), new Object[] { PartMeta.getLabelToken(), PartMeta.META_PART.getLabelToken() });
}
} finally {
if (localUow && uow != null)
uow.rollback();
}
}
use of org.jaffa.persistence.domainobjects.Part in project jaffa-framework by jaffa-projects.
the class DomainXmlTest method writeAndReadDomainXml.
/**
* This method serializes a bunch of domain objects to XML.
* The XML is then deserialized back to domain objects.
*/
private void writeAndReadDomainXml(boolean escapeDomainXml) throws Exception {
UOW uow = null;
try {
// Retrieve a few objects and add them to the DomainXmlWriter
uow = new UOW();
DomainXmlWriter dxw = new DomainXmlWriter();
dxw.addObject(CategoryOfInstrument.findByPK(uow, "Z-TESTCI-01"));
dxw.addObject(Part.findByPK(uow, "Z-TESTPART-01"));
dxw.addObject(Part.findByPK(uow, "Z-TESTPART-02"));
dxw.addObject(Item.findByPK(uow, "Z-TESTITEM-01"));
dxw.addObject(Item.findByPK(uow, "Z-TESTITEM-03"));
// write XML
StringWriter w = new StringWriter();
dxw.write(w, escapeDomainXml);
// Now unmarhsal the XML into domain objects
Object obj = null;
String xml = w.toString();
System.out.println("*** Marshalled XML ***\n" + xml);
DomainXmlReader dxr = new DomainXmlReader(new StringReader(xml));
Iterator itr = dxr.iterator();
// Verify the deserialzed objects
obj = itr.next();
assertTrue("CategoryOfInstrument should have been generated", obj instanceof CategoryOfInstrument);
assertEquals("Z-TESTCI-01", ((CategoryOfInstrument) obj).getCategoryInstrument());
assertEquals("Z-TESTCIDESC-01", ((CategoryOfInstrument) obj).getDescription());
obj = itr.next();
assertTrue("Part should have been generated", obj instanceof Part);
assertEquals("Z-TESTPART-01", ((Part) obj).getPart());
assertEquals("Z-TESTNOUN-01", ((Part) obj).getNoun());
obj = itr.next();
assertTrue("Part should have been generated", obj instanceof Part);
assertEquals("Z-TESTPART-02", ((Part) obj).getPart());
assertEquals("Z-TESTNOUN-02", ((Part) obj).getNoun());
obj = itr.next();
assertTrue("Item should have been generated", obj instanceof Item);
assertEquals("Z-TESTITEM-01", ((Item) obj).getItemId());
obj = itr.next();
assertTrue("Item should have been generated", obj instanceof Item);
assertEquals("Z-TESTITEM-03", ((Item) obj).getItemId());
assertTrue("No more records should exist", !itr.hasNext());
} catch (Exception e) {
e.printStackTrace();
fail();
} finally {
if (uow != null)
uow.rollback();
}
}
Aggregations