Search in sources :

Example 71 with EntityCondition

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

the class ContentServices method deactivateAssocs.

/**
 * Deactivates any active ContentAssoc (except the current one) that is associated with the passed in template/layout contentId and mapKey.
 */
public static Map<String, Object> deactivateAssocs(DispatchContext dctx, Map<String, ? extends Object> context) {
    Delegator delegator = dctx.getDelegator();
    String contentIdTo = (String) context.get("contentIdTo");
    String mapKey = (String) context.get("mapKey");
    String contentAssocTypeId = (String) context.get("contentAssocTypeId");
    String activeContentId = (String) context.get("activeContentId");
    String contentId = (String) context.get("contentId");
    Timestamp fromDate = (Timestamp) context.get("fromDate");
    Locale locale = (Locale) context.get("locale");
    Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
    String sequenceNum = null;
    Map<String, Object> results = new HashMap<String, Object>();
    try {
        GenericValue activeAssoc = null;
        if (fromDate != null) {
            activeAssoc = EntityQuery.use(delegator).from("ContentAssoc").where("contentId", activeContentId, "contentIdTo", contentIdTo, "fromDate", fromDate, "contentAssocTypeId", contentAssocTypeId).queryOne();
            if (activeAssoc == null) {
                return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ContentAssocNotFound", UtilMisc.toMap("activeContentId", activeContentId, "contentIdTo", contentIdTo, "contentAssocTypeId", contentAssocTypeId, "fromDate", fromDate), locale));
            }
            sequenceNum = (String) activeAssoc.get("sequenceNum");
        }
        List<EntityCondition> exprList = new LinkedList<EntityCondition>();
        exprList.add(EntityCondition.makeCondition("mapKey", EntityOperator.EQUALS, mapKey));
        if (sequenceNum != null) {
            exprList.add(EntityCondition.makeCondition("sequenceNum", EntityOperator.EQUALS, sequenceNum));
        }
        exprList.add(EntityCondition.makeCondition("mapKey", EntityOperator.EQUALS, mapKey));
        exprList.add(EntityCondition.makeCondition("thruDate", EntityOperator.EQUALS, null));
        exprList.add(EntityCondition.makeCondition("contentIdTo", EntityOperator.EQUALS, contentIdTo));
        exprList.add(EntityCondition.makeCondition("contentAssocTypeId", EntityOperator.EQUALS, contentAssocTypeId));
        if (UtilValidate.isNotEmpty(activeContentId)) {
            exprList.add(EntityCondition.makeCondition("contentId", EntityOperator.NOT_EQUAL, activeContentId));
        }
        if (UtilValidate.isNotEmpty(contentId)) {
            exprList.add(EntityCondition.makeCondition("contentId", EntityOperator.EQUALS, contentId));
        }
        EntityConditionList<EntityCondition> assocExprList = EntityCondition.makeCondition(exprList, EntityOperator.AND);
        List<GenericValue> relatedAssocs = EntityQuery.use(delegator).from("ContentAssoc").where(assocExprList).orderBy("fromDate").filterByDate().queryList();
        for (GenericValue val : relatedAssocs) {
            val.set("thruDate", nowTimestamp);
            val.store();
        }
        results.put("deactivatedList", relatedAssocs);
    } catch (GenericEntityException e) {
        return ServiceUtil.returnError(e.getMessage());
    }
    return results;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) HashMap(java.util.HashMap) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition) Timestamp(java.sql.Timestamp) LinkedList(java.util.LinkedList) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException)

Example 72 with EntityCondition

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

the class CommonWorkers method getStateList.

public static List<GenericValue> getStateList(Delegator delegator) {
    List<GenericValue> geoList = new LinkedList<>();
    EntityCondition condition = EntityCondition.makeCondition(EntityOperator.OR, EntityCondition.makeCondition("geoTypeId", "STATE"), EntityCondition.makeCondition("geoTypeId", "PROVINCE"), EntityCondition.makeCondition("geoTypeId", "TERRITORY"), EntityCondition.makeCondition("geoTypeId", "MUNICIPALITY"));
    try {
        geoList = EntityQuery.use(delegator).from("Geo").where(condition).orderBy("geoName").cache(true).queryList();
    } catch (GenericEntityException e) {
        Debug.logError(e, "Cannot lookup State Geos: " + e.toString(), module);
    }
    return geoList;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition) LinkedList(java.util.LinkedList)

Example 73 with EntityCondition

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

the class CommonWorkers method getAssociatedStateList.

/**
 * Returns a list of regional geo associations.
 */
public static List<GenericValue> getAssociatedStateList(Delegator delegator, String country, String listOrderBy) {
    if (UtilValidate.isEmpty(country)) {
        // Load the system default country
        country = EntityUtilProperties.getPropertyValue("general", "country.geo.id.default", delegator);
    }
    if (UtilValidate.isEmpty(listOrderBy)) {
        listOrderBy = "geoId";
    }
    List<String> sortList = UtilMisc.toList(listOrderBy);
    List<GenericValue> geoList = new LinkedList<>();
    try {
        // Check if the country is a country group and get recursively the
        // states
        List<GenericValue> regionList = EntityQuery.use(delegator).from("GeoAssocAndGeoToWithState").where("geoIdFrom", country, "geoAssocTypeId", "GROUP_MEMBER", "geoTypeId", "GROUP").orderBy(sortList).cache(true).queryList();
        if (regionList.size() == 1) {
            for (GenericValue region : regionList) {
                List<GenericValue> tmpState = EntityQuery.use(delegator).from("GeoAssocAndGeoTo").where("geoId", region.getString("geoIdFrom")).orderBy(sortList).cache(true).queryList();
                for (GenericValue state : tmpState) {
                    geoList.addAll(getAssociatedStateList(delegator, state.getString("geoIdFrom"), listOrderBy));
                }
            }
        }
        // get all related states
        EntityCondition stateProvinceFindCond = EntityCondition.makeCondition(EntityCondition.makeCondition("geoIdFrom", country), EntityCondition.makeCondition("geoAssocTypeId", "REGIONS"), EntityCondition.makeCondition(EntityOperator.OR, EntityCondition.makeCondition("geoTypeId", "STATE"), EntityCondition.makeCondition("geoTypeId", "PROVINCE"), EntityCondition.makeCondition("geoTypeId", "MUNICIPALITY"), EntityCondition.makeCondition("geoTypeId", "TERRITORY"), EntityCondition.makeCondition("geoTypeId", "COUNTY")));
        geoList.addAll(EntityQuery.use(delegator).from("GeoAssocAndGeoToWithState").where(stateProvinceFindCond).orderBy(sortList).cache(true).queryList());
    } catch (GenericEntityException e) {
        Debug.logError(e, "Cannot lookup Geo", module);
    }
    return geoList;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition) LinkedList(java.util.LinkedList)

Example 74 with EntityCondition

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

the class FindServices method createConditionList.

/**
 * Parses input parameters and returns an <code>EntityCondition</code> list.
 *
 * @param parameters
 * @param fieldList
 * @param queryStringMap
 * @param delegator
 * @param context
 * @return returns an EntityCondition list
 */
public static List<EntityCondition> createConditionList(Map<String, ? extends Object> parameters, List<ModelField> fieldList, Map<String, Object> queryStringMap, Delegator delegator, Map<String, ?> context) {
    Set<String> processed = new LinkedHashSet<>();
    Set<String> keys = new LinkedHashSet<>();
    Map<String, ModelField> fieldMap = new LinkedHashMap<>();
    /**
     * When inputFields contains several xxxx_grp, yyyy_grp ... values,
     * Corresponding conditions will grouped by an {@link EntityOperator.AND} then all added to final
     * condition grouped by an {@link EntityOperator.OR}
     * That will allow union of search criteria, instead of default intersection.
     */
    Map<String, List<EntityCondition>> savedGroups = new LinkedHashMap();
    for (ModelField modelField : fieldList) {
        fieldMap.put(modelField.getName(), modelField);
    }
    List<EntityCondition> result = new LinkedList<>();
    for (Map.Entry<String, ? extends Object> entry : parameters.entrySet()) {
        String currentGroup = null;
        String parameterName = entry.getKey();
        if (processed.contains(parameterName)) {
            continue;
        }
        keys.clear();
        String fieldName = parameterName;
        Object fieldValue = null;
        String operation = null;
        boolean ignoreCase = false;
        if (parameterName.endsWith("_ic") || parameterName.endsWith("_op")) {
            fieldName = parameterName.substring(0, parameterName.length() - 3);
        } else if (parameterName.endsWith("_value")) {
            fieldName = parameterName.substring(0, parameterName.length() - 6);
        }
        String key = fieldName.concat("_grp");
        if (parameters.containsKey(key)) {
            if (parameters.containsKey(key)) {
                keys.add(key);
            }
            currentGroup = (String) parameters.get(key);
        }
        key = fieldName.concat("_ic");
        if (parameters.containsKey(key)) {
            keys.add(key);
            ignoreCase = "Y".equals(parameters.get(key));
        }
        key = fieldName.concat("_op");
        if (parameters.containsKey(key)) {
            keys.add(key);
            operation = (String) parameters.get(key);
        }
        key = fieldName.concat("_value");
        if (parameters.containsKey(key)) {
            keys.add(key);
            fieldValue = parameters.get(key);
        }
        if (fieldName.endsWith("_fld0") || fieldName.endsWith("_fld1")) {
            if (parameters.containsKey(fieldName)) {
                keys.add(fieldName);
            }
            fieldName = fieldName.substring(0, fieldName.length() - 5);
        }
        if (parameters.containsKey(fieldName)) {
            keys.add(fieldName);
        }
        processed.addAll(keys);
        ModelField modelField = fieldMap.get(fieldName);
        if (modelField == null) {
            continue;
        }
        if (fieldValue == null) {
            fieldValue = parameters.get(fieldName);
        }
        if (ObjectType.isEmpty(fieldValue) && !"empty".equals(operation)) {
            continue;
        }
        if (UtilValidate.isNotEmpty(currentGroup)) {
            List<EntityCondition> groupedConditions = new LinkedList<>();
            if (savedGroups.get(currentGroup) != null) {
                groupedConditions.addAll(savedGroups.get(currentGroup));
            }
            groupedConditions.add(createSingleCondition(modelField, operation, fieldValue, ignoreCase, delegator, context));
            savedGroups.put(currentGroup, groupedConditions);
        } else {
            result.add(createSingleCondition(modelField, operation, fieldValue, ignoreCase, delegator, context));
        }
        for (String mapKey : keys) {
            queryStringMap.put(mapKey, parameters.get(mapKey));
        }
    }
    // Add OR-grouped conditions
    List<EntityCondition> orConditions = new LinkedList<>();
    for (String groupedConditions : savedGroups.keySet()) {
        orConditions.add(EntityCondition.makeCondition(savedGroups.get(groupedConditions)));
    }
    if (orConditions.size() > 0)
        result.add(EntityCondition.makeCondition(orConditions, EntityOperator.OR));
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition) LinkedList(java.util.LinkedList) LinkedHashMap(java.util.LinkedHashMap) ModelField(org.apache.ofbiz.entity.model.ModelField) EntityConditionList(org.apache.ofbiz.entity.condition.EntityConditionList) UtilGenerics.checkList(org.apache.ofbiz.base.util.UtilGenerics.checkList) LinkedList(java.util.LinkedList) List(java.util.List) UtilGenerics.checkMap(org.apache.ofbiz.base.util.UtilGenerics.checkMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 75 with EntityCondition

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

the class FindServices method createSingleCondition.

/**
 * Creates a single <code>EntityCondition</code> based on a set of parameters.
 *
 * @param modelField
 * @param operation
 * @param fieldValue
 * @param ignoreCase
 * @param delegator
 * @param context
 * @return return an EntityCondition
 */
public static EntityCondition createSingleCondition(ModelField modelField, String operation, Object fieldValue, boolean ignoreCase, Delegator delegator, Map<String, ?> context) {
    EntityCondition cond = null;
    String fieldName = modelField.getName();
    Locale locale = (Locale) context.get("locale");
    TimeZone timeZone = (TimeZone) context.get("timeZone");
    EntityComparisonOperator<?, ?> fieldOp = null;
    if (operation != null) {
        if (operation.equals("contains")) {
            fieldOp = EntityOperator.LIKE;
            fieldValue = "%" + fieldValue + "%";
        } else if ("not-contains".equals(operation) || "notContains".equals(operation)) {
            fieldOp = EntityOperator.NOT_LIKE;
            fieldValue = "%" + fieldValue + "%";
        } else if (operation.equals("empty")) {
            return EntityCondition.makeCondition(fieldName, EntityOperator.EQUALS, null);
        } else if (operation.equals("like")) {
            fieldOp = EntityOperator.LIKE;
            fieldValue = fieldValue + "%";
        } else if ("not-like".equals(operation) || "notLike".equals(operation)) {
            fieldOp = EntityOperator.NOT_LIKE;
            fieldValue = fieldValue + "%";
        } else if ("opLessThan".equals(operation)) {
            fieldOp = EntityOperator.LESS_THAN;
        } else if ("upToDay".equals(operation)) {
            fieldOp = EntityOperator.LESS_THAN;
        } else if ("upThruDay".equals(operation)) {
            fieldOp = EntityOperator.LESS_THAN_EQUAL_TO;
        } else if (operation.equals("greaterThanFromDayStart")) {
            String timeStampString = (String) fieldValue;
            Object startValue = modelField.getModelEntity().convertFieldValue(modelField, dayStart(timeStampString, 0, timeZone, locale), delegator, context);
            return EntityCondition.makeCondition(fieldName, EntityOperator.GREATER_THAN_EQUAL_TO, startValue);
        } else if (operation.equals("sameDay")) {
            String timeStampString = (String) fieldValue;
            Object startValue = modelField.getModelEntity().convertFieldValue(modelField, dayStart(timeStampString, 0, timeZone, locale), delegator, context);
            EntityCondition startCond = EntityCondition.makeCondition(fieldName, EntityOperator.GREATER_THAN_EQUAL_TO, startValue);
            Object endValue = modelField.getModelEntity().convertFieldValue(modelField, dayStart(timeStampString, 1, timeZone, locale), delegator, context);
            EntityCondition endCond = EntityCondition.makeCondition(fieldName, EntityOperator.LESS_THAN, endValue);
            return EntityCondition.makeCondition(startCond, endCond);
        } else {
            fieldOp = entityOperators.get(operation);
        }
    } else {
        if (UtilValidate.isNotEmpty(UtilGenerics.toList(fieldValue))) {
            fieldOp = EntityOperator.IN;
        } else {
            fieldOp = EntityOperator.EQUALS;
        }
    }
    Object fieldObject = fieldValue;
    if ((fieldOp != EntityOperator.IN && fieldOp != EntityOperator.NOT_IN) || !(fieldValue instanceof Collection<?>)) {
        fieldObject = modelField.getModelEntity().convertFieldValue(modelField, fieldValue, delegator, context);
    }
    if (ignoreCase && fieldObject instanceof String) {
        cond = EntityCondition.makeCondition(EntityFunction.UPPER_FIELD(fieldName), fieldOp, EntityFunction.UPPER(((String) fieldValue).toUpperCase(Locale.getDefault())));
    } else {
        if (fieldObject.equals(GenericEntity.NULL_FIELD.toString())) {
            fieldObject = null;
        }
        cond = EntityCondition.makeCondition(fieldName, fieldOp, fieldObject);
    }
    if (EntityOperator.NOT_EQUAL.equals(fieldOp) && fieldObject != null) {
        cond = EntityCondition.makeCondition(UtilMisc.toList(cond, EntityCondition.makeCondition(fieldName, null)), EntityOperator.OR);
    }
    return cond;
}
Also used : Locale(java.util.Locale) TimeZone(java.util.TimeZone) 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