Search in sources :

Example 36 with EntityCondition

use of org.apache.ofbiz.entity.condition.EntityCondition in project ofbiz-framework by apache.

the class ContentServicesComplex method getAssocAndContentAndDataResourceMethod.

public static Map<String, Object> getAssocAndContentAndDataResourceMethod(Delegator delegator, String contentId, String mapKey, String direction, Timestamp fromDate, Timestamp thruDate, String fromDateStr, String thruDateStr, List<String> assocTypes, List<String> contentTypes) {
    List<EntityCondition> exprList = new LinkedList<EntityCondition>();
    EntityExpr joinExpr = null;
    String viewName = null;
    if (mapKey != null) {
        EntityExpr mapKeyExpr = EntityCondition.makeCondition("caMapKey", EntityOperator.EQUALS, mapKey);
        exprList.add(mapKeyExpr);
    }
    if (direction != null && "From".equalsIgnoreCase(direction)) {
        joinExpr = EntityCondition.makeCondition("caContentIdTo", EntityOperator.EQUALS, contentId);
        viewName = "ContentAssocDataResourceViewFrom";
    } else {
        joinExpr = EntityCondition.makeCondition("caContentId", EntityOperator.EQUALS, contentId);
        viewName = "ContentAssocDataResourceViewTo";
    }
    exprList.add(joinExpr);
    if (UtilValidate.isNotEmpty(assocTypes)) {
        exprList.add(EntityCondition.makeCondition("caContentAssocTypeId", EntityOperator.IN, assocTypes));
    }
    if (UtilValidate.isNotEmpty(contentTypes)) {
        exprList.add(EntityCondition.makeCondition("contentTypeId", EntityOperator.IN, contentTypes));
    }
    if (fromDate == null && fromDateStr != null) {
        fromDate = UtilDateTime.toTimestamp(fromDateStr);
    }
    if (thruDate == null && thruDateStr != null) {
        thruDate = UtilDateTime.toTimestamp(thruDateStr);
    }
    if (fromDate != null) {
        EntityExpr fromExpr = EntityCondition.makeCondition("caFromDate", EntityOperator.LESS_THAN, fromDate);
        exprList.add(fromExpr);
    }
    if (thruDate != null) {
        List<EntityExpr> thruList = new LinkedList<EntityExpr>();
        EntityExpr thruExpr = EntityCondition.makeCondition("caThruDate", EntityOperator.LESS_THAN, thruDate);
        thruList.add(thruExpr);
        EntityExpr thruExpr2 = EntityCondition.makeCondition("caThruDate", EntityOperator.EQUALS, null);
        thruList.add(thruExpr2);
        EntityConditionList<EntityExpr> thruExprList = EntityCondition.makeCondition(thruList, EntityOperator.OR);
        exprList.add(thruExprList);
    } else if (fromDate != null) {
        List<EntityExpr> thruList = new LinkedList<EntityExpr>();
        EntityExpr thruExpr = EntityCondition.makeCondition("caThruDate", EntityOperator.GREATER_THAN, fromDate);
        thruList.add(thruExpr);
        EntityExpr thruExpr2 = EntityCondition.makeCondition("caThruDate", EntityOperator.EQUALS, null);
        thruList.add(thruExpr2);
        EntityConditionList<EntityExpr> thruExprList = EntityCondition.makeCondition(thruList, EntityOperator.OR);
        exprList.add(thruExprList);
    }
    EntityConditionList<EntityCondition> assocExprList = EntityCondition.makeCondition(exprList, EntityOperator.AND);
    List<GenericValue> relatedAssocs = null;
    try {
        relatedAssocs = EntityQuery.use(delegator).from(viewName).where(assocExprList).orderBy("caFromDate").queryList();
    } catch (GenericEntityException e) {
        return ServiceUtil.returnError(e.getMessage());
    }
    for (int i = 0; i < relatedAssocs.size(); i++) {
        GenericValue a = relatedAssocs.get(i);
        if (Debug.verboseOn())
            Debug.logVerbose(" contentId:" + a.get("contentId") + " To:" + a.get("caContentIdTo") + " fromDate:" + a.get("caFromDate") + " thruDate:" + a.get("caThruDate") + " AssocTypeId:" + a.get("caContentAssocTypeId"), null);
    }
    Map<String, Object> results = new HashMap<String, Object>();
    results.put("entityList", relatedAssocs);
    return results;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) EntityConditionList(org.apache.ofbiz.entity.condition.EntityConditionList) HashMap(java.util.HashMap) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition) LinkedList(java.util.LinkedList) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) ArrayList(java.util.ArrayList) EntityConditionList(org.apache.ofbiz.entity.condition.EntityConditionList) LinkedList(java.util.LinkedList) List(java.util.List) EntityExpr(org.apache.ofbiz.entity.condition.EntityExpr)

Example 37 with EntityCondition

use of org.apache.ofbiz.entity.condition.EntityCondition in project ofbiz-framework by apache.

the class FindServices method performFind.

/**
 * performFind
 *
 * This is a generic method that expects entity data affixed with special suffixes
 * to indicate their purpose in formulating an SQL query statement.
 */
public static Map<String, Object> performFind(DispatchContext dctx, Map<String, ?> context) {
    String entityName = (String) context.get("entityName");
    String orderBy = (String) context.get("orderBy");
    // Input
    Map<String, ?> inputFields = checkMap(context.get("inputFields"), String.class, Object.class);
    String noConditionFind = (String) context.get("noConditionFind");
    String distinct = (String) context.get("distinct");
    List<String> fieldList = UtilGenerics.<String>checkList(context.get("fieldList"));
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    Locale locale = (Locale) context.get("locale");
    Delegator delegator = dctx.getDelegator();
    if (UtilValidate.isEmpty(noConditionFind)) {
        // try finding in inputFields Map
        noConditionFind = (String) inputFields.get("noConditionFind");
    }
    if (UtilValidate.isEmpty(noConditionFind)) {
        // Use configured default
        noConditionFind = EntityUtilProperties.getPropertyValue("widget", "widget.defaultNoConditionFind", delegator);
    }
    String filterByDate = (String) context.get("filterByDate");
    if (UtilValidate.isEmpty(filterByDate)) {
        // try finding in inputFields Map
        filterByDate = (String) inputFields.get("filterByDate");
    }
    Timestamp filterByDateValue = (Timestamp) context.get("filterByDateValue");
    String fromDateName = (String) context.get("fromDateName");
    if (UtilValidate.isEmpty(fromDateName)) {
        // try finding in inputFields Map
        fromDateName = (String) inputFields.get("fromDateName");
    }
    String thruDateName = (String) context.get("thruDateName");
    if (UtilValidate.isEmpty(thruDateName)) {
        // try finding in inputFields Map
        thruDateName = (String) inputFields.get("thruDateName");
    }
    Integer viewSize = (Integer) context.get("viewSize");
    Integer viewIndex = (Integer) context.get("viewIndex");
    Integer maxRows = null;
    if (viewSize != null && viewIndex != null) {
        maxRows = viewSize * (viewIndex + 1);
    }
    LocalDispatcher dispatcher = dctx.getDispatcher();
    Map<String, Object> prepareResult = null;
    try {
        prepareResult = dispatcher.runSync("prepareFind", UtilMisc.toMap("entityName", entityName, "orderBy", orderBy, "inputFields", inputFields, "filterByDate", filterByDate, "noConditionFind", noConditionFind, "filterByDateValue", filterByDateValue, "userLogin", userLogin, "fromDateName", fromDateName, "thruDateName", thruDateName, "locale", context.get("locale"), "timeZone", context.get("timeZone")));
    } catch (GenericServiceException gse) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "CommonFindErrorPreparingConditions", UtilMisc.toMap("errorString", gse.getMessage()), locale));
    }
    EntityConditionList<EntityCondition> exprList = UtilGenerics.cast(prepareResult.get("entityConditionList"));
    List<String> orderByList = checkList(prepareResult.get("orderByList"), String.class);
    Map<String, Object> executeResult = null;
    try {
        executeResult = dispatcher.runSync("executeFind", UtilMisc.toMap("entityName", entityName, "orderByList", orderByList, "fieldList", fieldList, "entityConditionList", exprList, "noConditionFind", noConditionFind, "distinct", distinct, "locale", context.get("locale"), "timeZone", context.get("timeZone"), "maxRows", maxRows));
    } catch (GenericServiceException gse) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "CommonFindErrorRetrieveIterator", UtilMisc.toMap("errorString", gse.getMessage()), locale));
    }
    if (executeResult.get("listIt") == null) {
        if (Debug.verboseOn()) {
            Debug.logVerbose("No list iterator found for query string + [" + prepareResult.get("queryString") + "]", module);
        }
    }
    Map<String, Object> results = ServiceUtil.returnSuccess();
    results.put("listIt", executeResult.get("listIt"));
    results.put("listSize", executeResult.get("listSize"));
    results.put("queryString", prepareResult.get("queryString"));
    results.put("queryStringMap", prepareResult.get("queryStringMap"));
    return results;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition) Timestamp(java.sql.Timestamp) Delegator(org.apache.ofbiz.entity.Delegator) GenericServiceException(org.apache.ofbiz.service.GenericServiceException)

Example 38 with EntityCondition

use of org.apache.ofbiz.entity.condition.EntityCondition in project ofbiz-framework by apache.

the class FindServices method createCondition.

/**
 * createCondition, comparing the normalizedFields with the list of keys, .
 *
 * This is use to the generic method that expects entity data affixed with special suffixes
 * to indicate their purpose in formulating an SQL query statement.
 * @param modelEntity the model entity object
 * @param normalizedFields list of field the user have populated
 * @return a arrayList usable to create an entityCondition
 */
public static List<EntityCondition> createCondition(ModelEntity modelEntity, Map<String, Map<String, Map<String, Object>>> normalizedFields, Map<String, Object> queryStringMap, Map<String, List<Object[]>> origValueMap, Delegator delegator, Map<String, ?> context) {
    Map<String, Map<String, Object>> subMap = null;
    Map<String, Object> subMap2 = null;
    // If it is a "value" field, it will be the value to be used in the query.
    Object fieldValue = null;
    // If it is an "op" field, it will be "equals", "greaterThan", etc.
    EntityCondition cond = null;
    List<EntityCondition> tmpList = new LinkedList<>();
    String opString = null;
    boolean ignoreCase = false;
    List<ModelField> fields = modelEntity.getFieldsUnmodifiable();
    for (ModelField modelField : fields) {
        String fieldName = modelField.getName();
        subMap = normalizedFields.get(fieldName);
        if (subMap == null) {
            continue;
        }
        subMap2 = subMap.get("fld0");
        fieldValue = subMap2.get("value");
        opString = (String) subMap2.get("op");
        // null fieldValue is OK if operator is "empty"
        if (fieldValue == null && !"empty".equals(opString)) {
            continue;
        }
        ignoreCase = "Y".equals(subMap2.get("ic"));
        cond = createSingleCondition(modelField, opString, fieldValue, ignoreCase, delegator, context);
        tmpList.add(cond);
        subMap2 = subMap.get("fld1");
        if (subMap2 == null) {
            continue;
        }
        fieldValue = subMap2.get("value");
        opString = (String) subMap2.get("op");
        if (fieldValue == null && !"empty".equals(opString)) {
            continue;
        }
        ignoreCase = "Y".equals(subMap2.get("ic"));
        cond = createSingleCondition(modelField, opString, fieldValue, ignoreCase, delegator, context);
        tmpList.add(cond);
        // add to queryStringMap
        List<Object[]> origList = origValueMap.get(fieldName);
        if (UtilValidate.isNotEmpty(origList)) {
            for (Object[] arr : origList) {
                queryStringMap.put((String) arr[0], arr[1]);
            }
        }
    }
    return tmpList;
}
Also used : EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition) LinkedList(java.util.LinkedList) ModelField(org.apache.ofbiz.entity.model.ModelField) UtilGenerics.checkMap(org.apache.ofbiz.base.util.UtilGenerics.checkMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 39 with EntityCondition

use of org.apache.ofbiz.entity.condition.EntityCondition 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 40 with EntityCondition

use of org.apache.ofbiz.entity.condition.EntityCondition in project ofbiz-framework by apache.

the class EntityTestSuite method testRemoveByPK.

/*
     * Test the .removeByPrimaryKey by using findByCondition and then retrieving the GenericPk from a GenericValue
     */
public void testRemoveByPK() throws Exception {
    flushAndRecreateTree("remove-by-pk");
    // 
    // Find all the root nodes,
    // delete them their primary key
    // 
    EntityCondition isRoot = EntityCondition.makeCondition(EntityCondition.makeCondition("description", EntityOperator.LIKE, "remove-by-pk:%"), EntityOperator.AND, EntityCondition.makeCondition("primaryParentNodeId", EntityOperator.NOT_EQUAL, GenericEntity.NULL_FIELD));
    List<GenericValue> rootValues = EntityQuery.use(delegator).select("testingNodeId").from("TestingNode").where(isRoot).queryList();
    for (GenericValue value : rootValues) {
        GenericPK pk = value.getPrimaryKey();
        int del = delegator.removeByPrimaryKey(pk);
        assertEquals("Removing Root by primary key", 1, del);
    }
    // no more TestingNode should be in the data base anymore.
    List<GenericValue> testingNodes = EntityQuery.use(delegator).from("TestingNode").where(isRoot).queryList();
    assertEquals("No more TestingNode after removing the roots", 0, testingNodes.size());
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GenericPK(org.apache.ofbiz.entity.GenericPK) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition)

Aggregations

EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)118 GenericValue (org.apache.ofbiz.entity.GenericValue)96 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)78 Delegator (org.apache.ofbiz.entity.Delegator)60 LinkedList (java.util.LinkedList)58 Locale (java.util.Locale)43 Timestamp (java.sql.Timestamp)40 HashMap (java.util.HashMap)32 EntityListIterator (org.apache.ofbiz.entity.util.EntityListIterator)29 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)26 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)24 Map (java.util.Map)22 BigDecimal (java.math.BigDecimal)21 EntityExpr (org.apache.ofbiz.entity.condition.EntityExpr)18 EntityQuery (org.apache.ofbiz.entity.util.EntityQuery)14 GeneralException (org.apache.ofbiz.base.util.GeneralException)12 ArrayList (java.util.ArrayList)11 DynamicViewEntity (org.apache.ofbiz.entity.model.DynamicViewEntity)9 ModelKeyMap (org.apache.ofbiz.entity.model.ModelKeyMap)8 HashSet (java.util.HashSet)7