use of org.jaffa.persistence.IPersistent in project jaffa-framework by jaffa-projects.
the class DataSourceCursor method fetchData.
/**
* This will fetch additional data. Each row will be molded into a Persistent object
* and added to the collection. It will invoke the PostLoad trigger of each Persistent object.
* If all the data has been fetched, then the ResultSet will be closed.
*/
private void fetchData() throws SQLException, PostLoadFailedException, DataSourceCursorRuntimeException, IOException {
if (!m_allRead) {
for (int i = 0; i < m_dataSource.getHitlistSize().intValue(); i++) {
if (m_pagingPlugin != null ? m_pagingPlugin.next(m_resultSet) : m_resultSet.next()) {
try {
if (m_criteria.getGroupBys() != null || m_criteria.getFunctionEntries() != null) {
Map m = MoldingService.getFunctionQueryMap(m_criteria, m_classMetaData, m_resultSet, m_dataSource.getEngineType());
if (log.isDebugEnabled()) {
StringBuffer buf = new StringBuffer();
for (Iterator itr = m.entrySet().iterator(); itr.hasNext(); ) {
if (buf.length() > 0)
buf.append(',');
Map.Entry me = (Map.Entry) itr.next();
buf.append(me.getKey() + "=" + me.getValue());
}
log.debug("Results from SQL functions:" + buf.toString());
}
m_collection.add(m);
} else {
IPersistent object = MoldingService.getObject(m_classMetaData, m_resultSet, m_dataSource.getEngineType());
if (log.isDebugEnabled()) {
log.debug("Fetched the Persistent object: " + object.toString());
log.debug("Invoking the PostLoad trigger of the Persistent object");
}
JdbcBridge.updatePersistentFlagsOnQuery(object, m_criteria);
object.postLoad();
m_collection.add(object);
m_dataSource.cacheObject(object);
}
if (log.isDebugEnabled())
log.debug("Current size of the retrieved record set is " + m_collection.size());
} catch (ClassNotFoundException e) {
String str = "Error while molding a ResultSet into a Persistent object";
log.error(str, e);
throw new DataSourceCursorRuntimeException(str, e);
} catch (InstantiationException e) {
String str = "Error while molding a ResultSet into a Persistent object";
log.error(str, e);
throw new DataSourceCursorRuntimeException(str, e);
} catch (IllegalAccessException e) {
String str = "Error while molding a ResultSet into a Persistent object";
log.error(str, e);
throw new DataSourceCursorRuntimeException(str, e);
} catch (InvocationTargetException e) {
String str = "Error while molding a ResultSet into a Persistent object";
log.error(str, e);
throw new DataSourceCursorRuntimeException(str, e);
}
} else {
if (log.isDebugEnabled())
log.debug("Fetched in the complete result set. Invoking the closeStatement() method of the DataSource.");
m_dataSource.closeStatement(m_statement);
m_allRead = true;
break;
}
}
}
}
use of org.jaffa.persistence.IPersistent in project jaffa-framework by jaffa-projects.
the class HitlistTest method testHitlist.
/**
* This creates 123 Item records. It then retrieves them. After reading each record, the size of the Collection will be checked.
* The size will increment in steps of hitlist size (which is assumed to be 10). The size will be negative, until all the records have been fetched.
*/
public void testHitlist() {
try {
int rows = 123;
int hitlistSize = 10;
String itemIdPrefix = "ZZ-HIT-LIST-";
DateTime createdDateTime = new DateTime();
// create items
for (int i = 1; i <= rows; i++) {
Item item = (Item) m_uow.newPersistentInstance(Item.class);
item.updateItemId(itemIdPrefix + i);
item.updateQty(new Long(i));
item.updateCreatedDatetime(createdDateTime);
m_uow.add(item);
}
m_uow.commit();
// now retrieve the items
m_uow = new UOW();
Criteria c = new Criteria();
c.setLocking(Criteria.LOCKING_PARANOID);
c.setTable(ItemMeta.getName());
c.addCriteria(ItemMeta.ITEM_ID, Criteria.RELATIONAL_BEGINS_WITH, itemIdPrefix);
c.addOrderBy(ItemMeta.QTY, Criteria.ORDER_BY_ASC);
Collection items = m_uow.query(c);
assertEquals(-hitlistSize, items.size());
int i = 0;
for (Iterator itr = items.iterator(); itr.hasNext(); ) {
++i;
Item item = (Item) itr.next();
assertEquals(itemIdPrefix + i, item.getItemId());
assertEquals(new Long(i), item.getQty());
assertEquals(createdDateTime.toString(), item.getCreatedDatetime().toString());
// hitlist checks
if (i == rows) {
assertEquals(rows, items.size());
} else if (i % hitlistSize == 0) {
assertEquals(-i, items.size());
} else {
int expectedSize = i - (i % hitlistSize) + hitlistSize;
if (expectedSize >= rows)
assertEquals(rows, items.size());
else
assertEquals(-expectedSize, items.size());
}
}
// one final check.. to ensure that all the records were retrieved
assertEquals(rows, items.size());
// now delete the records
for (Iterator itr = items.iterator(); itr.hasNext(); ) m_uow.delete((IPersistent) itr.next());
m_uow.commit();
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
use of org.jaffa.persistence.IPersistent in project jaffa-framework by jaffa-projects.
the class HitlistTest method testHitlistUsingProxy.
/**
* This creates 123 Item records. It then retrieves them. After reading each record, the size of the Collection will be checked.
* The size will increment in steps of hitlist size (which is assumed to be 10). The size will be negative, until all the records have been fetched.
*/
public void testHitlistUsingProxy() {
try {
int rows = 123;
int hitlistSize = 10;
String itemIdPrefix = "ZZ-HIT-LIST-";
DateTime createdDateTime = new DateTime();
// create items
for (int i = 1; i <= rows; i++) {
IItem item = (IItem) m_uow.newPersistentInstance(IItem.class);
item.setItemId(itemIdPrefix + i);
item.setQty(new Long(i));
item.setCreatedDatetime(createdDateTime);
m_uow.add(item);
}
m_uow.commit();
// now retrieve the items
m_uow = new UOW();
Criteria c = new Criteria();
c.setLocking(Criteria.LOCKING_PARANOID);
c.setTable(IItem.class.getName());
c.addCriteria(IItem.ITEM_ID, Criteria.RELATIONAL_BEGINS_WITH, itemIdPrefix);
c.addOrderBy(IItem.QTY, Criteria.ORDER_BY_ASC);
Collection items = m_uow.query(c);
assertEquals(-hitlistSize, items.size());
int i = 0;
for (Iterator itr = items.iterator(); itr.hasNext(); ) {
++i;
IItem item = (IItem) itr.next();
assertEquals(itemIdPrefix + i, item.getItemId());
assertEquals(new Long(i), item.getQty());
assertEquals(createdDateTime.toString(), item.getCreatedDatetime().toString());
// hitlist checks
if (i == rows) {
assertEquals(rows, items.size());
} else if (i % hitlistSize == 0) {
assertEquals(-i, items.size());
} else {
int expectedSize = i - (i % hitlistSize) + hitlistSize;
if (expectedSize >= rows)
assertEquals(rows, items.size());
else
assertEquals(-expectedSize, items.size());
}
}
// one final check.. to ensure that all the records were retrieved
assertEquals(rows, items.size());
// now delete the records
for (Iterator itr = items.iterator(); itr.hasNext(); ) m_uow.delete((IPersistent) itr.next());
m_uow.commit();
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
use of org.jaffa.persistence.IPersistent in project jaffa-framework by jaffa-projects.
the class DomainXmlWriter method write.
/**
* Generate XML based on the added objects.
* @param w The writer to which the XML will be written to.
* @param escapeDomainXml if true, the domain XML will be escaped before being inserted into the main
* @throws IOException if any IO error occurs.
*/
public void write(Writer w, boolean escapeDomainXml) throws IOException {
w.write("<?xml version='1.0'?>\n");
w.write("<rows>\n");
for (IPersistent obj : m_objects) {
w.write(" <row>\n");
w.write(" <bean-class>");
w.write(obj.getClass().getName());
w.write("</bean-class>\n");
w.write(" <bean-data>\n");
try {
StringWriter tempWriter = new StringWriter();
JAXBContext jc = JAXBContext.newInstance(new Class[] { obj.getClass() });
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty("jaxb.fragment", Boolean.TRUE);
marshaller.marshal(obj, tempWriter);
w.write(escapeDomainXml ? StringHelper.convertToHTML(tempWriter.toString()) : tempWriter.toString());
} catch (Exception e) {
throw new RuntimeException("Error in marshalling domain object to XML", e);
}
w.write("\n </bean-data>\n");
w.write(" </row>\n");
}
w.write("</rows>\n");
}
use of org.jaffa.persistence.IPersistent in project jaffa-framework by jaffa-projects.
the class PrimaryKeyValidator method validateProperty.
@Override
protected void validateProperty(T targetObject, String targetPropertyName, Object targetPropertyValue, List<RuleMetaData> rules, UOW uow) throws ApplicationException, FrameworkException {
// No need to perform validations for an IPersistent instance that exists in the database
if (targetObject instanceof IPersistent && ((IPersistent) targetObject).isDatabaseOccurence()) {
if (log.isDebugEnabled()) {
log.debug(getName() + " check not performed since the target object already exists in the database");
}
return;
}
RuleMetaData rule = rules.get(0);
if (log.isDebugEnabled()) {
log.debug("Applying " + rule + " on " + targetObject);
}
Criteria criteria = null;
String pk = rule.getParameter(RuleMetaData.PARAMETER_VALUE);
String[] pkFields = pk.split(",");
for (String pkField : pkFields) {
Object pkValue = null;
try {
pkValue = BeanHelper.getField(targetObject, pkField);
} catch (NoSuchMethodException e) {
if (log.isDebugEnabled()) {
log.debug("No Such Method Exception: " + targetObject.getClass().getName() + "." + pkField, e);
}
return;
}
if (pkValue == null) {
if (log.isDebugEnabled()) {
log.debug(getName() + " check not performed since the key field " + pkField + " is null");
}
return;
} else {
// Add to criteria
if (criteria == null) {
criteria = new Criteria();
}
criteria.addCriteria(pkField, pkValue);
}
}
if (criteria != null) {
criteria.setTable(getActualClassName(targetObject.getClass()));
if (uow.query(criteria).iterator().hasNext()) {
if (log.isDebugEnabled()) {
log.debug("Primary key '" + pk + "' is not unique for the object " + targetObject);
}
String objectLabel = getObjectLabel(getActualClassName(targetObject.getClass()), targetObject);
throw wrapException(new DuplicateKeyException(objectLabel), targetObject, rule);
}
}
}
Aggregations