Search in sources :

Example 71 with Criteria

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

the class BeanMoulder method deleteChildBean.

/**
 *  Take a source object and try and mold it back it its domain object.
 *  This is the same as updateParent, except from the way it retrieved the
 *  record, and the way it creates a new record.
 */
private static void deleteChildBean(String path, DomainDAO source, UOW uow, MouldHandler handler, IPersistent parentDomain, GraphMapping parentMapping, String parentField) throws ApplicationExceptions, FrameworkException {
    log.debug("Delete Child Bean " + path);
    // Call custom validation code in the DAO
    source.validate();
    ApplicationExceptions aes = new ApplicationExceptions();
    if (uow == null) {
        String err = "UOW Required";
        log.error(err);
        throw new RuntimeException(err);
    }
    String relationshipName = parentMapping.getDomainFieldName(parentField);
    if (relationshipName.endsWith("Array"))
        relationshipName = relationshipName.substring(0, relationshipName.length() - 5);
    if (relationshipName.endsWith("Object"))
        relationshipName = relationshipName.substring(0, relationshipName.length() - 6);
    try {
        IPersistent domainObject = null;
        GraphMapping mapping = MappingFactory.getInstance(source);
        Map keys = new LinkedHashMap();
        Class doClass = mapping.getDomainClass();
        boolean gotKeys = false;
        if (mapping.getKeyFields() == null || mapping.getKeyFields().size() == 0) {
            // No keys, must be one-to-one
            log.debug("Find 'one-to-one' object - " + path);
            // Just use the getXxxObject method to get the related domain object,
            // if there is one...
            domainObject = (IPersistent) getProperty(parentMapping.getDomainFieldDescriptor(parentField), parentDomain);
            if (domainObject == null)
                log.debug("Not Found - " + path);
        } else {
            // Get the key fields used in the domain object. Use the findXxxxxCriteria() method,
            // then add the extra fields to the criteria object, to get the unique record.
            gotKeys = fillInKeys(path, source, mapping, keys);
            // read DO based on key
            if (gotKeys) {
                // get the method to get the PK criteria (i.e. public Criteria findVendorSiteCriteria(); )
                Method findCriteria = null;
                String methodName = "find" + StringHelper.getUpper1(relationshipName) + "Criteria";
                try {
                    findCriteria = parentDomain.getClass().getMethod(methodName, new Class[] {});
                } catch (NoSuchMethodException e) {
                    log.error("Find method '" + methodName + "' not found!");
                }
                if (findCriteria == null)
                    throw new MouldException(MouldException.METHOD_NOT_FOUND, path, methodName);
                // Find Criteria For Related Object
                Criteria criteria = (Criteria) findCriteria.invoke(parentDomain, new Object[] {});
                // Add extra key info...
                for (Iterator it = keys.keySet().iterator(); it.hasNext(); ) {
                    String keyField = (String) it.next();
                    Object value = keys.get(keyField);
                    keyField = StringHelper.getUpper1(mapping.getDomainFieldName(keyField));
                    criteria.addCriteria(keyField, value);
                    log.debug(path + "- Add to criteria:" + keyField + "=" + value);
                }
                // See if we get an object :-)
                Iterator itr = uow.query(criteria).iterator();
                if (itr.hasNext())
                    domainObject = (IPersistent) itr.next();
                if (itr.hasNext()) {
                    // Error, multiple objects found
                    MultipleDomainObjectsFoundException m = new MultipleDomainObjectsFoundException(criteria.getTable() + " @ " + path);
                    aes.add(m);
                    throw aes;
                }
            }
        }
        // Error if DO not found
        if (domainObject == null) {
            aes.add(new DomainObjectNotFoundException(doClass.getName() + " @ " + path));
            throw aes;
        }
        // Process the delete, either on this DO, or a related DO if there is one
        deleteBeanData(path, source, uow, handler, mapping, domainObject);
    } catch (IllegalAccessException e) {
        MouldException me = new MouldException(MouldException.ACCESS_ERROR, path, e.getMessage());
        log.error(me.getLocalizedMessage(), e);
        throw me;
    } catch (InvocationTargetException e) {
        if (e.getCause() != null) {
            if (e.getCause() instanceof FrameworkException)
                throw (FrameworkException) e.getCause();
            if (e.getCause() instanceof ApplicationExceptions)
                throw (ApplicationExceptions) e.getCause();
            if (e.getCause() instanceof ApplicationException) {
                aes.add((ApplicationException) e.getCause());
                throw aes;
            }
        }
        MouldException me = new MouldException(MouldException.INVOCATION_ERROR, path, e);
        log.error(me.getLocalizedMessage(), me.getCause());
        throw me;
    } catch (InstantiationException e) {
        MouldException me = new MouldException(MouldException.INSTANTICATION_ERROR, path, e.getMessage());
        log.error(me.getLocalizedMessage(), e);
        throw me;
    }
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) FrameworkException(org.jaffa.exceptions.FrameworkException) Method(java.lang.reflect.Method) Criteria(org.jaffa.persistence.Criteria) InvocationTargetException(java.lang.reflect.InvocationTargetException) LinkedHashMap(java.util.LinkedHashMap) ApplicationException(org.jaffa.exceptions.ApplicationException) IPersistent(org.jaffa.persistence.IPersistent) DomainObjectNotFoundException(org.jaffa.exceptions.DomainObjectNotFoundException) MultipleDomainObjectsFoundException(org.jaffa.exceptions.MultipleDomainObjectsFoundException) Iterator(java.util.Iterator) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) GraphMapping(org.jaffa.beans.moulding.mapping.GraphMapping)

Example 72 with Criteria

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

the class CodeHelperTx method processInputElement.

private CodeHelperOutElementDto processInputElement(UOW uow, CodeHelperInElementDto inputElement) throws ApplicationExceptions, FrameworkException {
    Criteria c = new Criteria();
    c.setTable(inputElement.getDomainClassName());
    if (inputElement.getCriteriaFieldCount() > 0) {
        CriteriaElementDto[] criteriaFields = inputElement.getCriteriaFields();
        for (int i = 0; i < criteriaFields.length; i++) {
            CriteriaElementDto criteriaField = criteriaFields[i];
            addCriteria(criteriaField, c);
        }
    }
    c.addOrderBy(inputElement.getCodeFieldName(), Criteria.ORDER_BY_ASC);
    Iterator i = uow.query(c).iterator();
    CodeHelperOutElementDto outputElement = new CodeHelperOutElementDto();
    outputElement.setCode(inputElement.getCode());
    outputElement.setDomainClassName(inputElement.getDomainClassName());
    try {
        while (i.hasNext()) {
            Object domainObject = i.next();
            Object code = BeanHelper.getField(domainObject, inputElement.getCodeFieldName());
            Object description = BeanHelper.getField(domainObject, inputElement.getDescriptionFieldName());
            CodeHelperOutCodeDto codeDto = new CodeHelperOutCodeDto();
            codeDto.setCode(code);
            codeDto.setDescription(formatDescription(inputElement, code, description));
            outputElement.addCodeHelperOutCodeDto(codeDto);
        }
    } catch (NoSuchMethodException e) {
        String str = "The CodeHelperTx could not introspect the Domain Class " + inputElement.getDomainClassName() + " for the code/description field";
        log.warn(str, e);
    }
    return outputElement;
}
Also used : CriteriaElementDto(org.jaffa.components.codehelper.dto.CriteriaElementDto) CodeHelperOutCodeDto(org.jaffa.components.codehelper.dto.CodeHelperOutCodeDto) Iterator(java.util.Iterator) CodeHelperOutElementDto(org.jaffa.components.codehelper.dto.CodeHelperOutElementDto) AtomicCriteria(org.jaffa.persistence.AtomicCriteria) Criteria(org.jaffa.persistence.Criteria)

Example 73 with Criteria

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

the class FinderTx method isCriteria.

/**
 * @param field - The CriteriaField to be checked
 * @return true if anything would be added to the criteria block when the addCriteria is used for this same criteria field
 */
public static boolean isCriteria(CriteriaField field) {
    if (field != null) {
        // Create a dummy criteria to check if this field would be added
        Criteria c = new Criteria();
        addCriteria(field, TEST_CRITERIA_NAME, c);
        if (c.getCriteriaEntries() != null && c.getCriteriaEntries().size() > 0)
            return true;
    }
    return false;
}
Also used : AtomicCriteria(org.jaffa.persistence.AtomicCriteria) Criteria(org.jaffa.persistence.Criteria)

Example 74 with Criteria

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

the class SimpleVoucherGenerator method generate.

/**
 * The client will invoke this method to generate a voucher.
 * @throws FrameworkException if any framework error occurs.
 * @throws ApplicationExceptions if any application error occurs.
 * @return a unique voucher.
 */
public String generate() throws FrameworkException, ApplicationExceptions {
    String voucher = null;
    if (getDomainClassName() == null || getFieldName() == null) {
        voucher = new VMID().toString();
    } else {
        String numericPortion = null;
        UOW uow = getUow();
        boolean localUow = (uow == null);
        try {
            if (localUow)
                uow = new UOW();
            Criteria c = new Criteria();
            c.setTable(getDomainClassName());
            c.addFunction(Criteria.FUNCTION_MAX, StringHelper.getUpper1(getFieldName()), MAX_VALUE);
            Iterator itr = uow.query(c).iterator();
            if (itr.hasNext()) {
                Map row = (Map) itr.next();
                Object maxValue = row.get(MAX_VALUE);
                if (maxValue != null) {
                    numericPortion = getPrefix() != null ? maxValue.toString().substring(getPrefix().length()) : maxValue.toString();
                    numericPortion = getLength() != null ? StringHelper.pad(Integer.parseInt(numericPortion) + 1, getLength()) : "" + (Integer.parseInt(numericPortion) + 1);
                }
            }
            if (numericPortion == null)
                numericPortion = getLength() != null ? StringHelper.pad(0, getLength()) : "0";
            voucher = getPrefix() != null ? getPrefix() + numericPortion : numericPortion;
        } finally {
            if (localUow && uow != null)
                uow.rollback();
        }
    }
    return voucher;
}
Also used : Iterator(java.util.Iterator) VMID(java.rmi.dgc.VMID) Criteria(org.jaffa.persistence.Criteria) UOW(org.jaffa.persistence.UOW) Map(java.util.Map)

Example 75 with Criteria

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

the class BeanMoulder method updateChildBean.

/**
 *  Take a source object and try and mold it back it its domain object.
 *  This is the same as updateParent, except from the way it retrieved the
 *  record, and the way it creates a new record.
 */
private static void updateChildBean(String path, DomainDAO source, UOW uow, MouldHandler handler, IPersistent parentDomain, GraphMapping parentMapping, String parentField) throws ApplicationExceptions, FrameworkException {
    log.debug("Update Child Bean " + path);
    // Call custom validation code in the DAO
    source.validate();
    ApplicationExceptions aes = new ApplicationExceptions();
    if (uow == null) {
        String err = "UOW Required";
        log.error(err);
        throw new RuntimeException(err);
    }
    String relationshipName = parentMapping.getDomainFieldName(parentField);
    if (relationshipName.endsWith("Array"))
        relationshipName = relationshipName.substring(0, relationshipName.length() - 5);
    if (relationshipName.endsWith("Object"))
        relationshipName = relationshipName.substring(0, relationshipName.length() - 6);
    try {
        IPersistent domainObject = null;
        GraphMapping mapping = MappingFactory.getInstance(source);
        Map keys = new LinkedHashMap();
        Class doClass = mapping.getDomainClass();
        boolean gotKeys = false;
        if (mapping.getKeyFields() == null || mapping.getKeyFields().size() == 0) {
            // No keys, must be one-to-one
            log.debug("Find 'one-to-one' object - " + path);
            // Just use the getXxxObject method to get the related domain object,
            // if there is one...
            domainObject = (IPersistent) getProperty(parentMapping.getDomainFieldDescriptor(parentField), parentDomain);
            if (domainObject == null)
                log.debug("Not Found - " + path);
        } else {
            // Get the key fields used in the domain object. Use the findXxxxxCriteria() method,
            // then add the extra fields to the criteria object, to get the unique record.
            gotKeys = fillInKeys(path, source, mapping, keys);
            // read DO based on key
            if (gotKeys) {
                // get the method to get the PK criteria (i.e. public Criteria findVendorSiteCriteria(); )
                Method findCriteria = null;
                String methodName = "find" + StringHelper.getUpper1(relationshipName) + "Criteria";
                try {
                    findCriteria = parentDomain.getClass().getMethod(methodName, new Class[] {});
                } catch (NoSuchMethodException e) {
                    log.error("Find method '" + methodName + "' not found!");
                }
                if (findCriteria == null) {
                    throw new MouldException(MouldException.METHOD_NOT_FOUND, path, methodName);
                }
                // Find Criteria For Related Object
                Criteria criteria = (Criteria) findCriteria.invoke(parentDomain, new Object[] {});
                // Add extra key info...
                for (Iterator it = keys.keySet().iterator(); it.hasNext(); ) {
                    String keyField = (String) it.next();
                    Object value = keys.get(keyField);
                    keyField = StringHelper.getUpper1(mapping.getDomainFieldName(keyField));
                    criteria.addCriteria(keyField, value);
                    log.debug(path + "- Add to criteria:" + keyField + "=" + value);
                }
                // See if we get an object :-)
                Iterator itr = uow.query(criteria).iterator();
                if (itr.hasNext())
                    domainObject = (IPersistent) itr.next();
                if (itr.hasNext()) {
                    // Error, multiple objects found
                    MultipleDomainObjectsFoundException m = new MultipleDomainObjectsFoundException(criteria.getTable() + " @ " + path);
                    aes.add(m);
                    throw aes;
                }
            } else {
                log.debug("Object " + path + " has either missing or null key values - Assume Create is needed");
            }
        }
        // Create object if not found
        if (domainObject == null) {
            // NEW OBJECT, create and reflect keys
            log.debug("DO '" + mapping.getDomainClassShortName() + "' not found with key, create a new one...");
            // find method on parent used to create object
            Method newObject = null;
            String methodName = "new" + StringHelper.getUpper1(relationshipName) + "Object";
            try {
                newObject = parentDomain.getClass().getMethod(methodName, new Class[] {});
            } catch (NoSuchMethodException e) {
                log.error("Method '" + methodName + "()' not found!");
            }
            if (newObject == null)
                throw new MouldException(MouldException.METHOD_NOT_FOUND, path, methodName);
            // Call method to create object
            domainObject = (IPersistent) newObject.invoke(parentDomain, new Object[] {});
            // Set the key fields
            for (Iterator it = keys.keySet().iterator(); it.hasNext(); ) {
                String keyField = (String) it.next();
                Object value = keys.get(keyField);
                updateProperty(mapping.getDomainFieldDescriptor(keyField), value, domainObject);
            }
        } else {
            log.debug("Found DO '" + mapping.getDomainClassShortName() + "' with key,");
        }
        // Now update all domain fields
        updateBeanData(path, source, uow, handler, mapping, domainObject);
    } catch (IllegalAccessException e) {
        MouldException me = new MouldException(MouldException.ACCESS_ERROR, path, e.getMessage());
        log.error(me.getLocalizedMessage(), e);
        throw me;
    } catch (InvocationTargetException e) {
        if (e.getCause() != null) {
            if (e.getCause() instanceof FrameworkException)
                throw (FrameworkException) e.getCause();
            if (e.getCause() instanceof ApplicationExceptions)
                throw (ApplicationExceptions) e.getCause();
            if (e.getCause() instanceof ApplicationException) {
                aes.add((ApplicationException) e.getCause());
                throw aes;
            }
        }
        MouldException me = new MouldException(MouldException.INVOCATION_ERROR, path, e);
        log.error(me.getLocalizedMessage(), me.getCause());
        throw me;
    } catch (InstantiationException e) {
        MouldException me = new MouldException(MouldException.INSTANTICATION_ERROR, path, e.getMessage());
        log.error(me.getLocalizedMessage(), e);
        throw me;
    }
}
Also used : ApplicationExceptions(org.jaffa.exceptions.ApplicationExceptions) FrameworkException(org.jaffa.exceptions.FrameworkException) Method(java.lang.reflect.Method) Criteria(org.jaffa.persistence.Criteria) InvocationTargetException(java.lang.reflect.InvocationTargetException) LinkedHashMap(java.util.LinkedHashMap) ApplicationException(org.jaffa.exceptions.ApplicationException) IPersistent(org.jaffa.persistence.IPersistent) MultipleDomainObjectsFoundException(org.jaffa.exceptions.MultipleDomainObjectsFoundException) Iterator(java.util.Iterator) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) GraphMapping(org.jaffa.beans.moulding.mapping.GraphMapping)

Aggregations

Criteria (org.jaffa.persistence.Criteria)300 UOW (org.jaffa.persistence.UOW)136 AtomicCriteria (org.jaffa.persistence.AtomicCriteria)124 ApplicationExceptions (org.jaffa.exceptions.ApplicationExceptions)66 Iterator (java.util.Iterator)53 DomainObjectNotFoundException (org.jaffa.exceptions.DomainObjectNotFoundException)39 TransactionCriteria (org.jaffa.transaction.apis.data.TransactionCriteria)32 TransactionFieldCriteria (org.jaffa.transaction.apis.data.TransactionFieldCriteria)32 OrderByField (org.jaffa.components.finder.OrderByField)28 Map (java.util.Map)23 ArrayList (java.util.ArrayList)17 DateTime (org.jaffa.datatypes.DateTime)16 FrameworkException (org.jaffa.exceptions.FrameworkException)14 ApplicationException (org.jaffa.exceptions.ApplicationException)13 DuplicateKeyException (org.jaffa.exceptions.DuplicateKeyException)13 Transaction (org.jaffa.transaction.domain.Transaction)13 Collection (java.util.Collection)12 LinkedHashMap (java.util.LinkedHashMap)11 IPersistent (org.jaffa.persistence.IPersistent)11 HashMap (java.util.HashMap)9