Search in sources :

Example 1 with GetAll

use of org.apache.ofbiz.entity.finder.EntityFinderUtil.GetAll in project ofbiz-framework by apache.

the class ListFinder method runFind.

@Override
public void runFind(Map<String, Object> context, Delegator delegator) throws GeneralException {
    String entityName = this.entityNameExdr.expandString(context);
    String useCacheStr = this.useCacheStrExdr.expandString(context);
    String filterByDateStr = this.filterByDateStrExdr.expandString(context);
    String distinctStr = this.distinctStrExdr.expandString(context);
    String delegatorName = this.delegatorNameExdr.expandString(context);
    ModelEntity modelEntity = delegator.getModelEntity(entityName);
    String resultSetTypeString = this.resultSetTypeExdr.expandString(context);
    if (modelEntity == null) {
        throw new IllegalArgumentException("In find entity by " + label + " could not find definition for entity with name [" + entityName + "].");
    }
    boolean useCache = "true".equals(useCacheStr);
    boolean filterByDate = "true".equals(filterByDateStr);
    boolean distinct = "true".equals(distinctStr);
    int resultSetType = ResultSet.TYPE_SCROLL_INSENSITIVE;
    if ("forward".equals(resultSetTypeString)) {
        resultSetType = ResultSet.TYPE_FORWARD_ONLY;
    }
    if (UtilValidate.isNotEmpty(delegatorName)) {
        delegator = DelegatorFactory.getDelegator(delegatorName);
    }
    EntityCondition whereEntityCondition = getWhereEntityCondition(context, modelEntity, delegator.getModelFieldTypeReader(modelEntity));
    EntityCondition havingEntityCondition = getHavingEntityCondition(context, modelEntity, delegator.getModelFieldTypeReader(modelEntity));
    if (useCache) {
        // if useCache == true && outputHandler instanceof UseIterator, throw exception; not a valid combination
        if (outputHandler instanceof UseIterator) {
            Debug.logWarning("In find entity by " + label + " cannot have use-cache set to true " + label + " select use-iterator for the output type. Using cache and ignoring use-iterator setting.", module);
            outputHandler = new GetAll();
        }
        if (distinct) {
            throw new IllegalArgumentException("In find entity by " + label + " cannot have use-cache set to true " + label + " set distinct to true.");
        }
        if (havingEntityCondition != null) {
            throw new IllegalArgumentException("In find entity by " + label + " cannot have use-cache set to true and specify a having-condition-list (can only use a where condition with condition-expr or condition-list).");
        }
    }
    // get the list of fieldsToSelect from selectFieldExpanderList
    Set<String> fieldsToSelect = EntityFinderUtil.makeFieldsToSelect(selectFieldExpanderList, context);
    // if fieldsToSelect != null and useCacheBool is true, throw an error
    if (fieldsToSelect != null && useCache) {
        throw new IllegalArgumentException("Error in entity query by " + label + " definition, cannot specify select-field elements when use-cache is set to true");
    }
    // get the list of orderByFields from orderByExpanderList
    List<String> orderByFields = EntityFinderUtil.makeOrderByFieldList(this.orderByExpanderList, context);
    try {
        // if filterByDate, do a date filter on the results based on the now-timestamp
        if (filterByDate && !useCache) {
            EntityCondition filterByDateCondition = EntityUtil.getFilterByDateExpr();
            if (whereEntityCondition != null) {
                whereEntityCondition = EntityCondition.makeCondition(UtilMisc.toList(whereEntityCondition, filterByDateCondition));
            } else {
                whereEntityCondition = filterByDateCondition;
            }
        }
        if (useCache) {
            List<GenericValue> results = delegator.findList(entityName, whereEntityCondition, fieldsToSelect, orderByFields, null, true);
            if (filterByDate) {
                results = EntityUtil.filterByDate(results);
            }
            this.outputHandler.handleOutput(results, context, listAcsr);
        } else {
            boolean useTransaction = true;
            if (this.outputHandler instanceof UseIterator && !TransactionUtil.isTransactionInPlace()) {
                Exception newE = new Exception("Stack Trace");
                Debug.logError(newE, "ERROR: Cannot do a by " + label + " find that returns an EntityListIterator with no transaction in place. Wrap this call in a transaction.", module);
                useTransaction = false;
            }
            EntityFindOptions options = new EntityFindOptions();
            options.setDistinct(distinct);
            options.setResultSetType(resultSetType);
            if (outputHandler instanceof LimitRange) {
                LimitRange limitRange = (LimitRange) outputHandler;
                int start = limitRange.getStart(context);
                int size = limitRange.getSize(context);
                options.setMaxRows(start + size);
            } else if (outputHandler instanceof LimitView) {
                LimitView limitView = (LimitView) outputHandler;
                int index = limitView.getIndex(context);
                int size = limitView.getSize(context);
                options.setMaxRows(size * (index + 1));
            }
            boolean beganTransaction = false;
            try {
                if (useTransaction) {
                    beganTransaction = TransactionUtil.begin();
                }
                EntityListIterator eli = delegator.find(entityName, whereEntityCondition, havingEntityCondition, fieldsToSelect, orderByFields, options);
                this.outputHandler.handleOutput(eli, context, listAcsr);
            // NOTE: the eli EntityListIterator is not closed here. It SHOULD be closed later after the returned list will be used (eg see EntityAnd.getChildren() in ModelTree.java)
            } catch (GenericEntityException e) {
                String errMsg = "Failure in by " + label + " find operation, rolling back transaction";
                Debug.logError(e, errMsg, module);
                try {
                    // only rollback the transaction if we started one...
                    TransactionUtil.rollback(beganTransaction, errMsg, e);
                } catch (GenericEntityException e2) {
                    Debug.logError(e2, "Could not rollback transaction: " + e2.toString(), module);
                }
                // after rolling back, rethrow the exception
                throw e;
            } finally {
                // only commit the transaction if we started one... this will throw an exception if it fails
                TransactionUtil.commit(beganTransaction);
            }
        }
    } catch (GenericEntityException e) {
        String errMsg = "Error doing find by " + label + ": " + e.toString();
        Debug.logError(e, module);
        throw new GeneralException(errMsg, e);
    }
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GeneralException(org.apache.ofbiz.base.util.GeneralException) GetAll(org.apache.ofbiz.entity.finder.EntityFinderUtil.GetAll) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition) LimitView(org.apache.ofbiz.entity.finder.EntityFinderUtil.LimitView) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GeneralException(org.apache.ofbiz.base.util.GeneralException) LimitRange(org.apache.ofbiz.entity.finder.EntityFinderUtil.LimitRange) UseIterator(org.apache.ofbiz.entity.finder.EntityFinderUtil.UseIterator) EntityFindOptions(org.apache.ofbiz.entity.util.EntityFindOptions) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator)

Aggregations

GeneralException (org.apache.ofbiz.base.util.GeneralException)1 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)1 GenericValue (org.apache.ofbiz.entity.GenericValue)1 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)1 GetAll (org.apache.ofbiz.entity.finder.EntityFinderUtil.GetAll)1 LimitRange (org.apache.ofbiz.entity.finder.EntityFinderUtil.LimitRange)1 LimitView (org.apache.ofbiz.entity.finder.EntityFinderUtil.LimitView)1 UseIterator (org.apache.ofbiz.entity.finder.EntityFinderUtil.UseIterator)1 ModelEntity (org.apache.ofbiz.entity.model.ModelEntity)1 EntityFindOptions (org.apache.ofbiz.entity.util.EntityFindOptions)1 EntityListIterator (org.apache.ofbiz.entity.util.EntityListIterator)1