Search in sources :

Example 16 with EntityListIterator

use of org.apache.ofbiz.entity.util.EntityListIterator in project ofbiz-framework by apache.

the class PaymentGatewayServices method retryFailedAuths.

public static Map<String, Object> retryFailedAuths(DispatchContext dctx, Map<String, ? extends Object> context) {
    Delegator delegator = dctx.getDelegator();
    LocalDispatcher dispatcher = dctx.getDispatcher();
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    EntityQuery eq = EntityQuery.use(delegator).from("OrderPaymentPreference").where(EntityCondition.makeCondition("statusId", EntityOperator.EQUALS, "PAYMENT_NOT_AUTH"), EntityCondition.makeCondition("processAttempt", EntityOperator.GREATER_THAN, Long.valueOf(0))).orderBy("orderId");
    try (EntityListIterator eli = eq.queryIterator()) {
        List<String> processList = new LinkedList<>();
        if (eli != null) {
            Debug.logInfo("Processing failed order re-auth(s)", module);
            GenericValue value = null;
            while (((value = eli.next()) != null)) {
                String orderId = value.getString("orderId");
                if (!processList.contains(orderId)) {
                    // just try each order once
                    try {
                        // each re-try is independent of each other; if one fails it should not effect the others
                        dispatcher.runAsync("retryFailedOrderAuth", UtilMisc.<String, Object>toMap("orderId", orderId, "userLogin", userLogin));
                        processList.add(orderId);
                    } catch (GenericServiceException e) {
                        Debug.logError(e, module);
                    }
                }
            }
        }
    } catch (GenericEntityException e) {
        Debug.logError(e, module);
    }
    return ServiceUtil.returnSuccess();
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator) EntityQuery(org.apache.ofbiz.entity.util.EntityQuery) LinkedList(java.util.LinkedList)

Example 17 with EntityListIterator

use of org.apache.ofbiz.entity.util.EntityListIterator in project ofbiz-framework by apache.

the class FindServices method performFindItem.

/**
 * Returns the first generic item of the service 'performFind'
 * Same parameters as performFind service but returns a single GenericValue
 *
 * @param dctx
 * @param context
 * @return returns the first item
 */
public static Map<String, Object> performFindItem(DispatchContext dctx, Map<String, Object> context) {
    context.put("viewSize", 1);
    context.put("viewIndex", 0);
    Map<String, Object> result = org.apache.ofbiz.common.FindServices.performFind(dctx, context);
    List<GenericValue> list = null;
    GenericValue item = null;
    try (EntityListIterator it = (EntityListIterator) result.get("listIt")) {
        // list starts at '1'
        list = it.getPartialList(1, 1);
        if (UtilValidate.isNotEmpty(list)) {
            item = list.get(0);
        }
    } catch (ClassCastException | NullPointerException | GenericEntityException e) {
        Debug.logInfo("Problem getting list Item" + e, module);
    }
    if (UtilValidate.isNotEmpty(item)) {
        result.put("item", item);
    }
    result.remove("listIt");
    if (result.containsKey("listSize")) {
        result.remove("listSize");
    }
    return result;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator)

Example 18 with EntityListIterator

use of org.apache.ofbiz.entity.util.EntityListIterator in project ofbiz-framework by apache.

the class FindServices method executeFind.

/**
 * executeFind
 *
 * This is a generic method that returns an EntityListIterator.
 */
public static Map<String, Object> executeFind(DispatchContext dctx, Map<String, ?> context) {
    String entityName = (String) context.get("entityName");
    EntityConditionList<EntityCondition> entityConditionList = UtilGenerics.cast(context.get("entityConditionList"));
    List<String> orderByList = checkList(context.get("orderByList"), String.class);
    boolean noConditionFind = "Y".equals(context.get("noConditionFind"));
    boolean distinct = "Y".equals(context.get("distinct"));
    List<String> fieldList = UtilGenerics.checkList(context.get("fieldList"));
    Locale locale = (Locale) context.get("locale");
    Set<String> fieldSet = null;
    if (fieldList != null) {
        fieldSet = UtilMisc.makeSetWritable(fieldList);
    }
    Integer maxRows = (Integer) context.get("maxRows");
    maxRows = maxRows != null ? maxRows : -1;
    Delegator delegator = dctx.getDelegator();
    // Retrieve entities  - an iterator over all the values
    EntityListIterator listIt = null;
    int listSize = 0;
    try {
        if (noConditionFind || (entityConditionList != null && entityConditionList.getConditionListSize() > 0)) {
            listIt = EntityQuery.use(delegator).select(fieldSet).from(entityName).where(entityConditionList).orderBy(orderByList).cursorScrollInsensitive().maxRows(maxRows).distinct(distinct).queryIterator();
            listSize = listIt.getResultsSizeAfterPartialList();
        }
    } catch (GenericEntityException e) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "CommonFindErrorRunning", UtilMisc.toMap("entityName", entityName, "errorString", e.getMessage()), locale));
    }
    Map<String, Object> results = ServiceUtil.returnSuccess();
    results.put("listIt", listIt);
    results.put("listSize", listSize);
    return results;
}
Also used : Locale(java.util.Locale) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator)

Example 19 with EntityListIterator

use of org.apache.ofbiz.entity.util.EntityListIterator in project ofbiz-framework by apache.

the class EntityTestSuite method testEntityListIterator.

/*
     * This test will use the large number of unique items from above and test the EntityListIterator looping through the list
     */
public void testEntityListIterator() throws Exception {
    try {
        List<GenericValue> newValues = new LinkedList<>();
        for (int i = 0; i < TEST_COUNT; i++) {
            newValues.add(delegator.makeValue("Testing", "testingId", getTestId("T3-", i)));
        }
        delegator.storeAll(newValues);
        List<GenericValue> newlyCreatedValues = EntityQuery.use(delegator).from("Testing").where(EntityCondition.makeCondition("testingId", EntityOperator.LIKE, "T3-%")).orderBy("testingId").queryList();
        assertEquals("Test to create " + TEST_COUNT + " and store all at once", TEST_COUNT, newlyCreatedValues.size());
        boolean beganTransaction = false;
        try {
            beganTransaction = TransactionUtil.begin();
            EntityListIterator iterator = EntityQuery.use(delegator).from("Testing").where(EntityCondition.makeCondition("testingId", EntityOperator.LIKE, "T3-%")).orderBy("testingId").queryIterator();
            assertNotNull("Test if EntityListIterator was created: ", iterator);
            int i = 0;
            GenericValue item = iterator.next();
            while (item != null) {
                assertEquals("Testing if iterated data matches test data (row " + i + "): ", getTestId("T3-", i), item.getString("testingId"));
                item = iterator.next();
                i++;
            }
            assertEquals("Test if EntitlyListIterator iterates exactly " + TEST_COUNT + " times: ", TEST_COUNT, i);
            iterator.close();
        } catch (GenericEntityException e) {
            TransactionUtil.rollback(beganTransaction, "GenericEntityException occurred while iterating with EntityListIterator", e);
            assertTrue("GenericEntityException:" + e.toString(), false);
            return;
        } finally {
            TransactionUtil.commit(beganTransaction);
        }
    } finally {
        List<GenericValue> entitiesToRemove = EntityQuery.use(delegator).from("Testing").where(EntityCondition.makeCondition("testingId", EntityOperator.LIKE, "T3-%")).queryList();
        delegator.removeAll(entitiesToRemove);
    }
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator) LinkedList(java.util.LinkedList)

Example 20 with EntityListIterator

use of org.apache.ofbiz.entity.util.EntityListIterator in project ofbiz-framework by apache.

the class EntityQueryTestSuite method testQueryIterator.

/*
     * queryIterator(): This method is used to get iterator object over the entity.
     * assert: Compared first record of both the iterator.
     */
public void testQueryIterator() throws GenericEntityException {
    List<GenericValue> testingTypes = new LinkedList<>();
    testingTypes.add(delegator.makeValue("TestingType", "testingTypeId", "queryIterator-1", "description", "Value One"));
    testingTypes.add(delegator.makeValue("TestingType", "testingTypeId", "queryIterator-2", "description", "Value Two"));
    testingTypes.add(delegator.makeValue("TestingType", "testingTypeId", "queryIterator-3", "description", "Value Three"));
    delegator.storeAll(testingTypes);
    boolean transactionStarted = false;
    try {
        transactionStarted = TransactionUtil.begin();
        EntityListIterator eliByEntityEngine = null;
        EntityListIterator eliByEntityQuery = null;
        eliByEntityEngine = delegator.find("TestingType", null, null, null, null, null);
        eliByEntityQuery = EntityQuery.use(delegator).from("TestingType").queryIterator();
        GenericValue recordByEntityEngine = eliByEntityEngine.next();
        GenericValue recordByEntityQuery = eliByEntityQuery.next();
        assertEquals("queryIterator(): Value of first record pointed by both iterators matched", recordByEntityEngine, recordByEntityQuery);
        eliByEntityEngine.close();
        eliByEntityQuery.close();
        TransactionUtil.commit(transactionStarted);
    } catch (GenericEntityException e) {
        TransactionUtil.rollback(transactionStarted, "Transaction is Rolled Back", e);
    }
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator) LinkedList(java.util.LinkedList)

Aggregations

EntityListIterator (org.apache.ofbiz.entity.util.EntityListIterator)65 GenericValue (org.apache.ofbiz.entity.GenericValue)59 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)57 Delegator (org.apache.ofbiz.entity.Delegator)33 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)29 LinkedList (java.util.LinkedList)25 HashMap (java.util.HashMap)24 Timestamp (java.sql.Timestamp)23 Locale (java.util.Locale)21 EntityQuery (org.apache.ofbiz.entity.util.EntityQuery)19 GenericTransactionException (org.apache.ofbiz.entity.transaction.GenericTransactionException)18 Map (java.util.Map)16 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)13 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)12 DynamicViewEntity (org.apache.ofbiz.entity.model.DynamicViewEntity)8 ModelKeyMap (org.apache.ofbiz.entity.model.ModelKeyMap)8 GeneralException (org.apache.ofbiz.base.util.GeneralException)7 ModelEntity (org.apache.ofbiz.entity.model.ModelEntity)7 ArrayList (java.util.ArrayList)6 EntityExpr (org.apache.ofbiz.entity.condition.EntityExpr)6