Search in sources :

Example 1 with CategoryOfInstrument

use of org.jaffa.persistence.domainobjects.CategoryOfInstrument 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();
    }
}
Also used : Item(org.jaffa.persistence.domainobjects.Item) CategoryOfInstrument(org.jaffa.persistence.domainobjects.CategoryOfInstrument) StringWriter(java.io.StringWriter) Part(org.jaffa.persistence.domainobjects.Part) StringReader(java.io.StringReader) Iterator(java.util.Iterator) UOW(org.jaffa.persistence.UOW)

Example 2 with CategoryOfInstrument

use of org.jaffa.persistence.domainobjects.CategoryOfInstrument in project jaffa-framework by jaffa-projects.

the class Part method findCategoryOfInstrumentObject.

/**
 * Finds the related foreign CategoryOfInstrument 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 findCategoryOfInstrumentObject(boolean checkExistenceOnly) throws ValidationException, FrameworkException {
    UOW uow = getUOW();
    boolean localUow = false;
    try {
        if (m_categoryOfInstrumentObject == null && getCategoryInstrument() != null) {
            Criteria criteria = new Criteria();
            criteria.setTable(CategoryOfInstrumentMeta.getName());
            criteria.addCriteria(CategoryOfInstrumentMeta.CATEGORY_INSTRUMENT, getCategoryInstrument());
            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_categoryOfInstrumentObject = (CategoryOfInstrument) itr.next();
            }
            if (m_categoryOfInstrumentObject == null && (count == null || count.intValue() <= 0))
                throw new InvalidForeignKeyException(PartMeta.META_CATEGORY_INSTRUMENT.getLabelToken(), new Object[] { CategoryOfInstrumentMeta.getLabelToken(), CategoryOfInstrumentMeta.META_CATEGORY_INSTRUMENT.getLabelToken() });
        }
    } finally {
        if (localUow && uow != null)
            uow.rollback();
    }
}
Also used : CategoryOfInstrument(org.jaffa.persistence.domainobjects.CategoryOfInstrument) InvalidForeignKeyException(org.jaffa.datatypes.exceptions.InvalidForeignKeyException)

Aggregations

CategoryOfInstrument (org.jaffa.persistence.domainobjects.CategoryOfInstrument)2 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 Iterator (java.util.Iterator)1 InvalidForeignKeyException (org.jaffa.datatypes.exceptions.InvalidForeignKeyException)1 UOW (org.jaffa.persistence.UOW)1 Item (org.jaffa.persistence.domainobjects.Item)1 Part (org.jaffa.persistence.domainobjects.Part)1