Search in sources :

Example 61 with EntityCondition

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

the class PartyRelationshipHelper method getActivePartyRelationships.

/**
 * Return A List of the active Party Relationships (ie with valid from and thru dates)
 *@param delegator needed Delegator
 *@param partyRelationshipValues Map containing the input parameters (primaries keys + partyRelationshipTypeId)
 *@return List of the active Party Relationships
 */
public static List<GenericValue> getActivePartyRelationships(Delegator delegator, Map<String, ?> partyRelationshipValues) {
    String partyIdFrom = (String) partyRelationshipValues.get("partyIdFrom");
    String partyIdTo = (String) partyRelationshipValues.get("partyIdTo");
    String roleTypeIdFrom = (String) partyRelationshipValues.get("roleTypeIdFrom");
    String roleTypeIdTo = (String) partyRelationshipValues.get("roleTypeIdTo");
    String partyRelationshipTypeId = (String) partyRelationshipValues.get("partyRelationshipTypeId");
    Timestamp fromDate = UtilDateTime.nowTimestamp();
    List<EntityCondition> condList = new LinkedList<>();
    condList.add(EntityCondition.makeCondition("partyIdFrom", partyIdFrom));
    condList.add(EntityCondition.makeCondition("partyIdTo", partyIdTo));
    condList.add(EntityCondition.makeCondition("roleTypeIdFrom", roleTypeIdFrom));
    condList.add(EntityCondition.makeCondition("roleTypeIdTo", roleTypeIdTo));
    condList.add(EntityCondition.makeCondition("partyRelationshipTypeId", partyRelationshipTypeId));
    condList.add(EntityCondition.makeCondition("fromDate", EntityOperator.LESS_THAN_EQUAL_TO, fromDate));
    EntityCondition thruCond = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("thruDate", null), EntityCondition.makeCondition("thruDate", EntityOperator.GREATER_THAN, fromDate)), EntityOperator.OR);
    condList.add(thruCond);
    EntityCondition condition = EntityCondition.makeCondition(condList);
    List<GenericValue> partyRelationships = null;
    try {
        partyRelationships = EntityQuery.use(delegator).from("PartyRelationship").where(condition).queryList();
    } catch (GenericEntityException e) {
        Debug.logError(e, "Problem finding PartyRelationships. ", module);
        return null;
    }
    if (UtilValidate.isNotEmpty(partyRelationships)) {
        return partyRelationships;
    }
    return null;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition) Timestamp(java.sql.Timestamp) LinkedList(java.util.LinkedList)

Example 62 with EntityCondition

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

the class ContactMechWorker method getFacilityContactMechByPurpose.

/**
 * Returns the first valid FacilityContactMech found based on the given facilityId and a prioritized list of purposes
 * @param delegator the delegator
 * @param facilityId the facility id
 * @param purposeTypes a List of ContactMechPurposeType ids which will be checked one at a time until a valid contact mech is found
 * @return returns the first valid FacilityContactMech found based on the given facilityId and a prioritized list of purposes
 */
public static GenericValue getFacilityContactMechByPurpose(Delegator delegator, String facilityId, List<String> purposeTypes) {
    if (UtilValidate.isEmpty(facilityId)) {
        return null;
    }
    if (UtilValidate.isEmpty(purposeTypes)) {
        return null;
    }
    for (String purposeType : purposeTypes) {
        List<GenericValue> facilityContactMechPurposes = null;
        List<EntityCondition> conditionList = new LinkedList<>();
        conditionList.add(EntityCondition.makeCondition("facilityId", facilityId));
        conditionList.add(EntityCondition.makeCondition("contactMechPurposeTypeId", purposeType));
        EntityCondition entityCondition = EntityCondition.makeCondition(conditionList);
        try {
            facilityContactMechPurposes = EntityQuery.use(delegator).from("FacilityContactMechPurpose").where(entityCondition).orderBy("-fromDate").cache(true).filterByDate().queryList();
        } catch (GenericEntityException e) {
            Debug.logWarning(e, module);
            continue;
        }
        for (GenericValue facilityContactMechPurpose : facilityContactMechPurposes) {
            String contactMechId = facilityContactMechPurpose.getString("contactMechId");
            List<GenericValue> facilityContactMechs = null;
            conditionList = new LinkedList<>();
            conditionList.add(EntityCondition.makeCondition("facilityId", facilityId));
            conditionList.add(EntityCondition.makeCondition("contactMechId", contactMechId));
            entityCondition = EntityCondition.makeCondition(conditionList);
            try {
                facilityContactMechs = EntityQuery.use(delegator).from("FacilityContactMech").where(entityCondition).orderBy("-fromDate").cache(true).filterByDate().queryList();
            } catch (GenericEntityException e) {
                Debug.logWarning(e, module);
            }
            if (UtilValidate.isNotEmpty(facilityContactMechs)) {
                return EntityUtil.getFirst(facilityContactMechs);
            }
        }
    }
    return null;
}
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 63 with EntityCondition

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

the class PartyServices method performFindParty.

public static Map<String, Object> performFindParty(DispatchContext dctx, Map<String, ? extends Object> context) {
    Map<String, Object> result = ServiceUtil.returnSuccess();
    Delegator delegator = dctx.getDelegator();
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    Locale locale = (Locale) context.get("locale");
    String extInfo = (String) context.get("extInfo");
    EntityCondition extCond = (EntityCondition) context.get("extCond");
    EntityListIterator listIt = null;
    // get the lookup flag
    String noConditionFind = (String) context.get("noConditionFind");
    // create the dynamic view entity
    DynamicViewEntity dynamicView = new DynamicViewEntity();
    // default view settings
    dynamicView.addMemberEntity("PT", "Party");
    dynamicView.addAlias("PT", "partyId");
    dynamicView.addAlias("PT", "statusId");
    dynamicView.addAlias("PT", "partyTypeId");
    dynamicView.addAlias("PT", "externalId");
    dynamicView.addAlias("PT", "createdDate");
    dynamicView.addAlias("PT", "lastModifiedDate");
    dynamicView.addRelation("one-nofk", "", "PartyType", ModelKeyMap.makeKeyMapList("partyTypeId"));
    dynamicView.addRelation("many", "", "UserLogin", ModelKeyMap.makeKeyMapList("partyId"));
    // define the main condition & expression list
    List<EntityCondition> andExprs = new ArrayList<>();
    EntityCondition mainCond = null;
    List<String> orderBy = new ArrayList<>();
    String sortField = (String) context.get("sortField");
    if (UtilValidate.isNotEmpty(sortField)) {
        orderBy.add(sortField);
    }
    List<String> fieldsToSelect = new ArrayList<>();
    // fields we need to select; will be used to set distinct
    fieldsToSelect.add("partyId");
    fieldsToSelect.add("statusId");
    fieldsToSelect.add("partyTypeId");
    fieldsToSelect.add("externalId");
    fieldsToSelect.add("createdDate");
    fieldsToSelect.add("lastModifiedDate");
    // filter on parties that have relationship with logged in user
    String partyRelationshipTypeId = (String) context.get("partyRelationshipTypeId");
    if (UtilValidate.isNotEmpty(partyRelationshipTypeId)) {
        // add relation to view
        dynamicView.addMemberEntity("PRSHP", "PartyRelationship");
        dynamicView.addAlias("PRSHP", "partyIdTo");
        dynamicView.addAlias("PRSHP", "partyRelationshipTypeId");
        dynamicView.addViewLink("PT", "PRSHP", Boolean.FALSE, ModelKeyMap.makeKeyMapList("partyId", "partyIdTo"));
        List<String> ownerPartyIds = UtilGenerics.cast(context.get("ownerPartyIds"));
        EntityCondition relationshipCond = null;
        if (UtilValidate.isEmpty(ownerPartyIds)) {
            String partyIdFrom = userLogin.getString("partyId");
            relationshipCond = EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("partyIdFrom"), EntityOperator.EQUALS, EntityFunction.UPPER(partyIdFrom));
        } else {
            relationshipCond = EntityCondition.makeCondition("partyIdFrom", EntityOperator.IN, ownerPartyIds);
        }
        dynamicView.addAlias("PRSHP", "partyIdFrom");
        // add the expr
        andExprs.add(EntityCondition.makeCondition(relationshipCond, EntityOperator.AND, EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("partyRelationshipTypeId"), EntityOperator.EQUALS, EntityFunction.UPPER(partyRelationshipTypeId))));
        fieldsToSelect.add("partyIdTo");
    }
    // get the params
    String partyId = (String) context.get("partyId");
    String partyTypeId = (String) context.get("partyTypeId");
    String roleTypeId = (String) context.get("roleTypeId");
    String statusId = (String) context.get("statusId");
    String userLoginId = (String) context.get("userLoginId");
    String externalId = (String) context.get("externalId");
    String firstName = (String) context.get("firstName");
    String lastName = (String) context.get("lastName");
    String groupName = (String) context.get("groupName");
    // check for a partyId
    if (UtilValidate.isNotEmpty(partyId)) {
        andExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("partyId"), EntityOperator.LIKE, EntityFunction.UPPER("%" + partyId + "%")));
    }
    // now the statusId - send ANY for all statuses; leave null for just enabled; or pass a specific status
    if (UtilValidate.isNotEmpty(statusId)) {
        andExprs.add(EntityCondition.makeCondition("statusId", statusId));
    } else {
        // NOTE: _must_ explicitly allow null as it is not included in a not equal in many databases... odd but true
        andExprs.add(EntityCondition.makeCondition(EntityCondition.makeCondition("statusId", GenericEntity.NULL_FIELD), EntityOperator.OR, EntityCondition.makeCondition("statusId", EntityOperator.NOT_EQUAL, "PARTY_DISABLED")));
    }
    // check for partyTypeId
    if (UtilValidate.isNotEmpty(partyTypeId)) {
        andExprs.add(EntityCondition.makeCondition("partyTypeId", partyTypeId));
    }
    if (UtilValidate.isNotEmpty(externalId)) {
        andExprs.add(EntityCondition.makeCondition("externalId", externalId));
    }
    // filter on user login
    if (UtilValidate.isNotEmpty(userLoginId)) {
        // modify the dynamic view
        dynamicView.addMemberEntity("UL", "UserLogin");
        dynamicView.addAlias("UL", "userLoginId");
        dynamicView.addViewLink("PT", "UL", Boolean.FALSE, ModelKeyMap.makeKeyMapList("partyId"));
        // add the expr
        andExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("userLoginId"), EntityOperator.LIKE, EntityFunction.UPPER("%" + userLoginId + "%")));
        fieldsToSelect.add("userLoginId");
    }
    // filter on groupName
    if (UtilValidate.isNotEmpty(groupName)) {
        // modify the dynamic view
        dynamicView.addMemberEntity("PG", "PartyGroup");
        dynamicView.addAlias("PG", "groupName");
        dynamicView.addViewLink("PT", "PG", Boolean.FALSE, ModelKeyMap.makeKeyMapList("partyId"));
        // add the expr
        andExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("groupName"), EntityOperator.LIKE, EntityFunction.UPPER("%" + groupName + "%")));
        fieldsToSelect.add("groupName");
    }
    // modify the dynamic view
    if (UtilValidate.isNotEmpty(firstName) || UtilValidate.isNotEmpty(lastName)) {
        dynamicView.addMemberEntity("PE", "Person");
        dynamicView.addAlias("PE", "firstName");
        dynamicView.addAlias("PE", "lastName");
        dynamicView.addViewLink("PT", "PE", Boolean.FALSE, ModelKeyMap.makeKeyMapList("partyId"));
        fieldsToSelect.add("firstName");
        fieldsToSelect.add("lastName");
        orderBy.add("lastName");
        orderBy.add("firstName");
    }
    // filter on firstName
    if (UtilValidate.isNotEmpty(firstName)) {
        andExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("firstName"), EntityOperator.LIKE, EntityFunction.UPPER("%" + firstName + "%")));
    }
    // filter on lastName
    if (UtilValidate.isNotEmpty(lastName)) {
        andExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("lastName"), EntityOperator.LIKE, EntityFunction.UPPER("%" + lastName + "%")));
    }
    // filter on role member
    if (UtilValidate.isNotEmpty(roleTypeId)) {
        // add role to view
        dynamicView.addMemberEntity("PR", "PartyRole");
        dynamicView.addAlias("PR", "roleTypeId");
        dynamicView.addViewLink("PT", "PR", Boolean.FALSE, ModelKeyMap.makeKeyMapList("partyId"));
        // add the expr
        andExprs.add(EntityCondition.makeCondition("roleTypeId", roleTypeId));
        fieldsToSelect.add("roleTypeId");
    }
    // ----
    // PartyIdentification Fields
    // ----
    String idValue = (String) context.get("idValue");
    String partyIdentificationTypeId = (String) context.get("partyIdentificationTypeId");
    if ("I".equals(extInfo) || UtilValidate.isNotEmpty(idValue) || UtilValidate.isNotEmpty(partyIdentificationTypeId)) {
        // add role to view
        dynamicView.addMemberEntity("PAI", "PartyIdentification");
        dynamicView.addAlias("PAI", "idValue");
        dynamicView.addAlias("PAI", "partyIdentificationTypeId");
        dynamicView.addViewLink("PT", "PAI", Boolean.FALSE, ModelKeyMap.makeKeyMapList("partyId"));
        fieldsToSelect.add("idValue");
        fieldsToSelect.add("partyIdentificationTypeId");
        if (UtilValidate.isNotEmpty(idValue)) {
            andExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("idValue"), EntityOperator.LIKE, EntityFunction.UPPER("%".concat(idValue).concat("%"))));
        }
        if (UtilValidate.isNotEmpty(partyIdentificationTypeId)) {
            andExprs.add(EntityCondition.makeCondition("partyIdentificationTypeId", partyIdentificationTypeId));
        }
    }
    // ----
    // InventoryItem Fields
    // ----
    // filter on inventory item's fields
    String inventoryItemId = (String) context.get("inventoryItemId");
    String serialNumber = (String) context.get("serialNumber");
    String softIdentifier = (String) context.get("softIdentifier");
    if (UtilValidate.isNotEmpty(inventoryItemId) || UtilValidate.isNotEmpty(serialNumber) || UtilValidate.isNotEmpty(softIdentifier)) {
        // add role to view
        dynamicView.addMemberEntity("II", "InventoryItem");
        dynamicView.addAlias("II", "ownerPartyId");
        dynamicView.addViewLink("PT", "II", Boolean.FALSE, ModelKeyMap.makeKeyMapList("partyId", "ownerPartyId"));
    }
    if (UtilValidate.isNotEmpty(inventoryItemId)) {
        dynamicView.addAlias("II", "inventoryItemId");
        // add the expr
        andExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("inventoryItemId"), EntityOperator.LIKE, EntityFunction.UPPER("%" + inventoryItemId + "%")));
        fieldsToSelect.add("inventoryItemId");
    }
    if (UtilValidate.isNotEmpty(serialNumber)) {
        dynamicView.addAlias("II", "serialNumber");
        // add the expr
        andExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("serialNumber"), EntityOperator.LIKE, EntityFunction.UPPER("%" + serialNumber + "%")));
        fieldsToSelect.add("serialNumber");
    }
    if (UtilValidate.isNotEmpty(softIdentifier)) {
        dynamicView.addAlias("II", "softIdentifier");
        // add the expr
        andExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("softIdentifier"), EntityOperator.LIKE, EntityFunction.UPPER("%" + softIdentifier + "%")));
        fieldsToSelect.add("softIdentifier");
    }
    // ----
    // PostalAddress fields
    // ----
    String stateProvinceGeoId = (String) context.get("stateProvinceGeoId");
    if ("P".equals(extInfo) || UtilValidate.isNotEmpty(context.get("address1")) || UtilValidate.isNotEmpty(context.get("address2")) || UtilValidate.isNotEmpty(context.get("city")) || UtilValidate.isNotEmpty(context.get("postalCode")) || UtilValidate.isNotEmpty(context.get("countryGeoId")) || (UtilValidate.isNotEmpty(stateProvinceGeoId))) {
        // add address to dynamic view
        dynamicView.addMemberEntity("PC", "PartyContactMech");
        dynamicView.addMemberEntity("PA", "PostalAddress");
        dynamicView.addAlias("PC", "contactMechId");
        dynamicView.addAlias("PA", "address1");
        dynamicView.addAlias("PA", "address2");
        dynamicView.addAlias("PA", "city");
        dynamicView.addAlias("PA", "stateProvinceGeoId");
        dynamicView.addAlias("PA", "countryGeoId");
        dynamicView.addAlias("PA", "postalCode");
        dynamicView.addViewLink("PT", "PC", Boolean.FALSE, ModelKeyMap.makeKeyMapList("partyId"));
        dynamicView.addViewLink("PC", "PA", Boolean.FALSE, ModelKeyMap.makeKeyMapList("contactMechId"));
        // filter on address1
        String address1 = (String) context.get("address1");
        if (UtilValidate.isNotEmpty(address1)) {
            andExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("address1"), EntityOperator.LIKE, EntityFunction.UPPER("%" + address1 + "%")));
        }
        // filter on address2
        String address2 = (String) context.get("address2");
        if (UtilValidate.isNotEmpty(address2)) {
            andExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("address2"), EntityOperator.LIKE, EntityFunction.UPPER("%" + address2 + "%")));
        }
        // filter on city
        String city = (String) context.get("city");
        if (UtilValidate.isNotEmpty(city)) {
            andExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("city"), EntityOperator.LIKE, EntityFunction.UPPER("%" + city + "%")));
        }
        // filter on state geo
        if (UtilValidate.isNotEmpty(stateProvinceGeoId)) {
            andExprs.add(EntityCondition.makeCondition("stateProvinceGeoId", stateProvinceGeoId));
        }
        // filter on postal code
        String postalCode = (String) context.get("postalCode");
        if (UtilValidate.isNotEmpty(postalCode)) {
            andExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("postalCode"), EntityOperator.LIKE, EntityFunction.UPPER("%" + postalCode + "%")));
        }
        fieldsToSelect.add("postalCode");
        fieldsToSelect.add("city");
        fieldsToSelect.add("stateProvinceGeoId");
    }
    // ----
    if ("O".equals(extInfo) || UtilValidate.isNotEmpty(context.get("infoString"))) {
        // add info to dynamic view
        dynamicView.addMemberEntity("PC", "PartyContactMech");
        dynamicView.addMemberEntity("CM", "ContactMech");
        dynamicView.addAlias("PC", "contactMechId");
        dynamicView.addAlias("CM", "infoString");
        dynamicView.addViewLink("PT", "PC", Boolean.FALSE, ModelKeyMap.makeKeyMapList("partyId"));
        dynamicView.addViewLink("PC", "CM", Boolean.FALSE, ModelKeyMap.makeKeyMapList("contactMechId"));
        // filter on infoString
        String infoString = (String) context.get("infoString");
        if (UtilValidate.isNotEmpty(infoString)) {
            andExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("infoString"), EntityOperator.LIKE, EntityFunction.UPPER("%" + infoString + "%")));
            fieldsToSelect.add("infoString");
        }
    }
    // ----
    if ("T".equals(extInfo) || UtilValidate.isNotEmpty(context.get("countryCode")) || UtilValidate.isNotEmpty(context.get("areaCode")) || UtilValidate.isNotEmpty(context.get("contactNumber"))) {
        // add telecom to dynamic view
        dynamicView.addMemberEntity("PC", "PartyContactMech");
        dynamicView.addMemberEntity("TM", "TelecomNumber");
        dynamicView.addAlias("PC", "contactMechId");
        dynamicView.addAlias("TM", "countryCode");
        dynamicView.addAlias("TM", "areaCode");
        dynamicView.addAlias("TM", "contactNumber");
        dynamicView.addViewLink("PT", "PC", Boolean.FALSE, ModelKeyMap.makeKeyMapList("partyId"));
        dynamicView.addViewLink("PC", "TM", Boolean.FALSE, ModelKeyMap.makeKeyMapList("contactMechId"));
        // filter on countryCode
        String countryCode = (String) context.get("countryCode");
        if (UtilValidate.isNotEmpty(countryCode)) {
            andExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("countryCode"), EntityOperator.EQUALS, EntityFunction.UPPER(countryCode)));
        }
        // filter on areaCode
        String areaCode = (String) context.get("areaCode");
        if (UtilValidate.isNotEmpty(areaCode)) {
            andExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("areaCode"), EntityOperator.EQUALS, EntityFunction.UPPER(areaCode)));
        }
        // filter on contact number
        String contactNumber = (String) context.get("contactNumber");
        if (UtilValidate.isNotEmpty(contactNumber)) {
            andExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("contactNumber"), EntityOperator.EQUALS, EntityFunction.UPPER(contactNumber)));
        }
        fieldsToSelect.add("contactNumber");
        fieldsToSelect.add("areaCode");
    }
    // build the main condition, add the extend condition is it present
    if (UtilValidate.isNotEmpty(extCond)) {
        andExprs.add(extCond);
    }
    if (UtilValidate.isNotEmpty(andExprs)) {
        mainCond = EntityCondition.makeCondition(andExprs, EntityOperator.AND);
    }
    if (Debug.infoOn()) {
        Debug.logInfo("In findParty mainCond=" + mainCond, module);
    }
    // do the lookup
    if (UtilValidate.isNotEmpty(noConditionFind) && ("Y".equals(noConditionFind) || andExprs.size() > 1)) {
        // exclude on condition the status expr
        try {
            // set distinct on so we only get one row per party
            // using list iterator
            listIt = EntityQuery.use(delegator).select(UtilMisc.toSet(fieldsToSelect)).from(dynamicView).where(mainCond).orderBy(orderBy).cursorScrollInsensitive().distinct().queryIterator();
        } catch (GenericEntityException e) {
            String errMsg = "Failure in party find operation, rolling back transaction: " + e.toString();
            Debug.logError(e, errMsg, module);
            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "PartyLookupPartyError", UtilMisc.toMap("errMessage", e.toString()), locale));
        }
    }
    result.put("listIt", listIt);
    return result;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition) ArrayList(java.util.ArrayList) DynamicViewEntity(org.apache.ofbiz.entity.model.DynamicViewEntity) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator)

Example 64 with EntityCondition

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

the class PartyServices method findParty.

// migration from ftl to widget in process.
@Deprecated
public static Map<String, Object> findParty(DispatchContext dctx, Map<String, ? extends Object> context) {
    Map<String, Object> result = ServiceUtil.returnSuccess();
    Delegator delegator = dctx.getDelegator();
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    Locale locale = (Locale) context.get("locale");
    String extInfo = (String) context.get("extInfo");
    // get the role types
    try {
        List<GenericValue> roleTypes = EntityQuery.use(delegator).from("RoleType").orderBy("description").queryList();
        result.put("roleTypes", roleTypes);
    } catch (GenericEntityException e) {
        String errMsg = "Error looking up RoleTypes: " + e.toString();
        Debug.logError(e, errMsg, module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "PartyLookupRoleTypeError", UtilMisc.toMap("errMessage", e.toString()), locale));
    }
    // current role type
    String roleTypeId;
    try {
        roleTypeId = (String) context.get("roleTypeId");
        if (UtilValidate.isNotEmpty(roleTypeId)) {
            GenericValue currentRole = EntityQuery.use(delegator).from("RoleType").where("roleTypeId", roleTypeId).cache().queryOne();
            result.put("currentRole", currentRole);
        }
    } catch (GenericEntityException e) {
        String errMsg = "Error looking up current RoleType: " + e.toString();
        Debug.logError(e, errMsg, module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "PartyLookupRoleTypeError", UtilMisc.toMap("errMessage", e.toString()), locale));
    }
    // get party types
    try {
        List<GenericValue> partyTypes = EntityQuery.use(delegator).from("PartyType").orderBy("description").queryList();
        result.put("partyTypes", partyTypes);
    } catch (GenericEntityException e) {
        String errMsg = "Error looking up PartyTypes: " + e.toString();
        Debug.logError(e, errMsg, module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "PartyLookupPartyTypeError", UtilMisc.toMap("errMessage", e.toString()), locale));
    }
    // current party type
    String partyTypeId;
    try {
        partyTypeId = (String) context.get("partyTypeId");
        if (UtilValidate.isNotEmpty(partyTypeId)) {
            GenericValue currentPartyType = EntityQuery.use(delegator).from("PartyType").where("partyTypeId", partyTypeId).cache().queryOne();
            result.put("currentPartyType", currentPartyType);
        }
    } catch (GenericEntityException e) {
        String errMsg = "Error looking up current PartyType: " + e.toString();
        Debug.logError(e, errMsg, module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "PartyLookupPartyTypeError", UtilMisc.toMap("errMessage", e.toString()), locale));
    }
    // current state
    String stateProvinceGeoId;
    try {
        stateProvinceGeoId = (String) context.get("stateProvinceGeoId");
        if (UtilValidate.isNotEmpty(stateProvinceGeoId)) {
            GenericValue currentStateGeo = EntityQuery.use(delegator).from("Geo").where("geoId", stateProvinceGeoId).cache().queryOne();
            result.put("currentStateGeo", currentStateGeo);
        }
    } catch (GenericEntityException e) {
        String errMsg = "Error looking up current stateProvinceGeo: " + e.toString();
        Debug.logError(e, errMsg, module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "PartyLookupStateProvinceGeoError", UtilMisc.toMap("errMessage", e.toString()), locale));
    }
    // set the page parameters
    int viewIndex = 0;
    try {
        viewIndex = Integer.parseInt((String) context.get("VIEW_INDEX"));
    } catch (Exception e) {
        viewIndex = 0;
    }
    result.put("viewIndex", Integer.valueOf(viewIndex));
    int viewSize = 20;
    try {
        viewSize = Integer.parseInt((String) context.get("VIEW_SIZE"));
    } catch (Exception e) {
        viewSize = 20;
    }
    result.put("viewSize", Integer.valueOf(viewSize));
    // get the lookup flag
    String lookupFlag = (String) context.get("lookupFlag");
    // blank param list
    String paramList = "";
    List<GenericValue> partyList = null;
    int partyListSize = 0;
    int lowIndex = 0;
    int highIndex = 0;
    if ("Y".equals(lookupFlag)) {
        String showAll = (context.get("showAll") != null ? (String) context.get("showAll") : "N");
        paramList = paramList + "&lookupFlag=" + lookupFlag + "&showAll=" + showAll + "&extInfo=" + extInfo;
        // create the dynamic view entity
        DynamicViewEntity dynamicView = new DynamicViewEntity();
        // default view settings
        dynamicView.addMemberEntity("PT", "Party");
        dynamicView.addAlias("PT", "partyId");
        dynamicView.addAlias("PT", "statusId");
        dynamicView.addAlias("PT", "partyTypeId");
        dynamicView.addAlias("PT", "createdDate");
        dynamicView.addAlias("PT", "lastModifiedDate");
        dynamicView.addRelation("one-nofk", "", "PartyType", ModelKeyMap.makeKeyMapList("partyTypeId"));
        dynamicView.addRelation("many", "", "UserLogin", ModelKeyMap.makeKeyMapList("partyId"));
        // define the main condition & expression list
        List<EntityCondition> andExprs = new LinkedList<>();
        EntityCondition mainCond = null;
        List<String> orderBy = new LinkedList<>();
        List<String> fieldsToSelect = new LinkedList<>();
        // fields we need to select; will be used to set distinct
        fieldsToSelect.add("partyId");
        fieldsToSelect.add("statusId");
        fieldsToSelect.add("partyTypeId");
        fieldsToSelect.add("createdDate");
        fieldsToSelect.add("lastModifiedDate");
        // filter on parties that have relationship with logged in user
        String partyRelationshipTypeId = (String) context.get("partyRelationshipTypeId");
        if (UtilValidate.isNotEmpty(partyRelationshipTypeId)) {
            // add relation to view
            dynamicView.addMemberEntity("PRSHP", "PartyRelationship");
            dynamicView.addAlias("PRSHP", "partyIdTo");
            dynamicView.addAlias("PRSHP", "partyRelationshipTypeId");
            dynamicView.addViewLink("PT", "PRSHP", Boolean.FALSE, ModelKeyMap.makeKeyMapList("partyId", "partyIdTo"));
            List<String> ownerPartyIds = UtilGenerics.cast(context.get("ownerPartyIds"));
            EntityCondition relationshipCond = null;
            if (UtilValidate.isEmpty(ownerPartyIds)) {
                String partyIdFrom = userLogin.getString("partyId");
                paramList = paramList + "&partyIdFrom=" + partyIdFrom;
                relationshipCond = EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("partyIdFrom"), EntityOperator.EQUALS, EntityFunction.UPPER(partyIdFrom));
            } else {
                relationshipCond = EntityCondition.makeCondition("partyIdFrom", EntityOperator.IN, ownerPartyIds);
            }
            dynamicView.addAlias("PRSHP", "partyIdFrom");
            // add the expr
            andExprs.add(EntityCondition.makeCondition(relationshipCond, EntityOperator.AND, EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("partyRelationshipTypeId"), EntityOperator.EQUALS, EntityFunction.UPPER(partyRelationshipTypeId))));
            fieldsToSelect.add("partyIdTo");
        }
        // get the params
        String partyId = (String) context.get("partyId");
        String statusId = (String) context.get("statusId");
        String userLoginId = (String) context.get("userLoginId");
        String firstName = (String) context.get("firstName");
        String lastName = (String) context.get("lastName");
        String groupName = (String) context.get("groupName");
        if (!"Y".equals(showAll)) {
            // check for a partyId
            if (UtilValidate.isNotEmpty(partyId)) {
                paramList = paramList + "&partyId=" + partyId;
                andExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("partyId"), EntityOperator.LIKE, EntityFunction.UPPER("%" + partyId + "%")));
            }
            // now the statusId - send ANY for all statuses; leave null for just enabled; or pass a specific status
            if (statusId != null) {
                paramList = paramList + "&statusId=" + statusId;
                if (!"ANY".equalsIgnoreCase(statusId)) {
                    andExprs.add(EntityCondition.makeCondition("statusId", EntityOperator.EQUALS, statusId));
                }
            } else {
                // NOTE: _must_ explicitly allow null as it is not included in a not equal in many databases... odd but true
                andExprs.add(EntityCondition.makeCondition(EntityCondition.makeCondition("statusId", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("statusId", EntityOperator.NOT_EQUAL, "PARTY_DISABLED")));
            }
            // check for partyTypeId
            if (partyTypeId != null && !"ANY".equals(partyTypeId)) {
                paramList = paramList + "&partyTypeId=" + partyTypeId;
                andExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("partyTypeId"), EntityOperator.LIKE, EntityFunction.UPPER("%" + partyTypeId + "%")));
            }
            // filter on user login
            if (UtilValidate.isNotEmpty(userLoginId)) {
                paramList = paramList + "&userLoginId=" + userLoginId;
                // modify the dynamic view
                dynamicView.addMemberEntity("UL", "UserLogin");
                dynamicView.addAlias("UL", "userLoginId");
                dynamicView.addViewLink("PT", "UL", Boolean.FALSE, ModelKeyMap.makeKeyMapList("partyId"));
                // add the expr
                andExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("userLoginId"), EntityOperator.LIKE, EntityFunction.UPPER("%" + userLoginId + "%")));
                fieldsToSelect.add("userLoginId");
            }
            // filter on groupName
            if (UtilValidate.isNotEmpty(groupName)) {
                paramList = paramList + "&groupName=" + groupName;
                // modify the dynamic view
                dynamicView.addMemberEntity("PG", "PartyGroup");
                dynamicView.addAlias("PG", "groupName");
                dynamicView.addViewLink("PT", "PG", Boolean.FALSE, ModelKeyMap.makeKeyMapList("partyId"));
                // add the expr
                andExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("groupName"), EntityOperator.LIKE, EntityFunction.UPPER("%" + groupName + "%")));
                fieldsToSelect.add("groupName");
            }
            // modify the dynamic view
            if (UtilValidate.isNotEmpty(firstName) || UtilValidate.isNotEmpty(lastName)) {
                dynamicView.addMemberEntity("PE", "Person");
                dynamicView.addAlias("PE", "firstName");
                dynamicView.addAlias("PE", "lastName");
                dynamicView.addViewLink("PT", "PE", Boolean.FALSE, ModelKeyMap.makeKeyMapList("partyId"));
                fieldsToSelect.add("firstName");
                fieldsToSelect.add("lastName");
                orderBy.add("lastName");
                orderBy.add("firstName");
            }
            // filter on firstName
            if (UtilValidate.isNotEmpty(firstName)) {
                paramList = paramList + "&firstName=" + firstName;
                andExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("firstName"), EntityOperator.LIKE, EntityFunction.UPPER("%" + firstName + "%")));
            }
            // filter on lastName
            if (UtilValidate.isNotEmpty(lastName)) {
                paramList = paramList + "&lastName=" + lastName;
                andExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("lastName"), EntityOperator.LIKE, EntityFunction.UPPER("%" + lastName + "%")));
            }
            // filter on role member
            if (roleTypeId != null && !"ANY".equals(roleTypeId)) {
                paramList = paramList + "&roleTypeId=" + roleTypeId;
                // add role to view
                dynamicView.addMemberEntity("PR", "PartyRole");
                dynamicView.addAlias("PR", "roleTypeId");
                dynamicView.addViewLink("PT", "PR", Boolean.FALSE, ModelKeyMap.makeKeyMapList("partyId"));
                // add the expr
                andExprs.add(EntityCondition.makeCondition("roleTypeId", EntityOperator.EQUALS, roleTypeId));
                fieldsToSelect.add("roleTypeId");
            }
            // ----
            // InventoryItem Fields
            // ----
            // filter on inventory item's fields
            String inventoryItemId = (String) context.get("inventoryItemId");
            String serialNumber = (String) context.get("serialNumber");
            String softIdentifier = (String) context.get("softIdentifier");
            if (UtilValidate.isNotEmpty(inventoryItemId) || UtilValidate.isNotEmpty(serialNumber) || UtilValidate.isNotEmpty(softIdentifier)) {
                // add role to view
                dynamicView.addMemberEntity("II", "InventoryItem");
                dynamicView.addAlias("II", "ownerPartyId");
                dynamicView.addViewLink("PT", "II", Boolean.FALSE, ModelKeyMap.makeKeyMapList("partyId", "ownerPartyId"));
            }
            if (UtilValidate.isNotEmpty(inventoryItemId)) {
                paramList = paramList + "&inventoryItemId=" + inventoryItemId;
                dynamicView.addAlias("II", "inventoryItemId");
                // add the expr
                andExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("inventoryItemId"), EntityOperator.LIKE, EntityFunction.UPPER("%" + inventoryItemId + "%")));
                fieldsToSelect.add("inventoryItemId");
            }
            if (UtilValidate.isNotEmpty(serialNumber)) {
                paramList = paramList + "&serialNumber=" + serialNumber;
                dynamicView.addAlias("II", "serialNumber");
                // add the expr
                andExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("serialNumber"), EntityOperator.LIKE, EntityFunction.UPPER("%" + serialNumber + "%")));
                fieldsToSelect.add("serialNumber");
            }
            if (UtilValidate.isNotEmpty(softIdentifier)) {
                paramList = paramList + "&softIdentifier=" + softIdentifier;
                dynamicView.addAlias("II", "softIdentifier");
                // add the expr
                andExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("softIdentifier"), EntityOperator.LIKE, EntityFunction.UPPER("%" + softIdentifier + "%")));
                fieldsToSelect.add("softIdentifier");
            }
            // ----
            if ("P".equals(extInfo)) {
                // add address to dynamic view
                dynamicView.addMemberEntity("PC", "PartyContactMech");
                dynamicView.addMemberEntity("PA", "PostalAddress");
                dynamicView.addAlias("PC", "contactMechId");
                dynamicView.addAlias("PA", "address1");
                dynamicView.addAlias("PA", "address2");
                dynamicView.addAlias("PA", "city");
                dynamicView.addAlias("PA", "stateProvinceGeoId");
                dynamicView.addAlias("PA", "countryGeoId");
                dynamicView.addAlias("PA", "postalCode");
                dynamicView.addViewLink("PT", "PC", Boolean.FALSE, ModelKeyMap.makeKeyMapList("partyId"));
                dynamicView.addViewLink("PC", "PA", Boolean.FALSE, ModelKeyMap.makeKeyMapList("contactMechId"));
                // filter on address1
                String address1 = (String) context.get("address1");
                if (UtilValidate.isNotEmpty(address1)) {
                    paramList = paramList + "&address1=" + address1;
                    andExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("address1"), EntityOperator.LIKE, EntityFunction.UPPER("%" + address1 + "%")));
                }
                // filter on address2
                String address2 = (String) context.get("address2");
                if (UtilValidate.isNotEmpty(address2)) {
                    paramList = paramList + "&address2=" + address2;
                    andExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("address2"), EntityOperator.LIKE, EntityFunction.UPPER("%" + address2 + "%")));
                }
                // filter on city
                String city = (String) context.get("city");
                if (UtilValidate.isNotEmpty(city)) {
                    paramList = paramList + "&city=" + city;
                    andExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("city"), EntityOperator.LIKE, EntityFunction.UPPER("%" + city + "%")));
                }
                // filter on state geo
                if (stateProvinceGeoId != null && !"ANY".equals(stateProvinceGeoId)) {
                    paramList = paramList + "&stateProvinceGeoId=" + stateProvinceGeoId;
                    andExprs.add(EntityCondition.makeCondition("stateProvinceGeoId", EntityOperator.EQUALS, stateProvinceGeoId));
                }
                // filter on postal code
                String postalCode = (String) context.get("postalCode");
                if (UtilValidate.isNotEmpty(postalCode)) {
                    paramList = paramList + "&postalCode=" + postalCode;
                    andExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("postalCode"), EntityOperator.LIKE, EntityFunction.UPPER("%" + postalCode + "%")));
                }
                fieldsToSelect.add("postalCode");
                fieldsToSelect.add("city");
                fieldsToSelect.add("stateProvinceGeoId");
            }
            // ----
            if ("O".equals(extInfo)) {
                // add info to dynamic view
                dynamicView.addMemberEntity("PC", "PartyContactMech");
                dynamicView.addMemberEntity("CM", "ContactMech");
                dynamicView.addAlias("PC", "contactMechId");
                dynamicView.addAlias("CM", "infoString");
                dynamicView.addViewLink("PT", "PC", Boolean.FALSE, ModelKeyMap.makeKeyMapList("partyId"));
                dynamicView.addViewLink("PC", "CM", Boolean.FALSE, ModelKeyMap.makeKeyMapList("contactMechId"));
                // filter on infoString
                String infoString = (String) context.get("infoString");
                if (UtilValidate.isNotEmpty(infoString)) {
                    paramList = paramList + "&infoString=" + infoString;
                    andExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("infoString"), EntityOperator.LIKE, EntityFunction.UPPER("%" + infoString + "%")));
                    fieldsToSelect.add("infoString");
                }
            }
            // ----
            if ("T".equals(extInfo)) {
                // add telecom to dynamic view
                dynamicView.addMemberEntity("PC", "PartyContactMech");
                dynamicView.addMemberEntity("TM", "TelecomNumber");
                dynamicView.addAlias("PC", "contactMechId");
                dynamicView.addAlias("TM", "countryCode");
                dynamicView.addAlias("TM", "areaCode");
                dynamicView.addAlias("TM", "contactNumber");
                dynamicView.addViewLink("PT", "PC", Boolean.FALSE, ModelKeyMap.makeKeyMapList("partyId"));
                dynamicView.addViewLink("PC", "TM", Boolean.FALSE, ModelKeyMap.makeKeyMapList("contactMechId"));
                // filter on countryCode
                String countryCode = (String) context.get("countryCode");
                if (UtilValidate.isNotEmpty(countryCode)) {
                    paramList = paramList + "&countryCode=" + countryCode;
                    andExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("countryCode"), EntityOperator.EQUALS, EntityFunction.UPPER(countryCode)));
                }
                // filter on areaCode
                String areaCode = (String) context.get("areaCode");
                if (UtilValidate.isNotEmpty(areaCode)) {
                    paramList = paramList + "&areaCode=" + areaCode;
                    andExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("areaCode"), EntityOperator.EQUALS, EntityFunction.UPPER(areaCode)));
                }
                // filter on contact number
                String contactNumber = (String) context.get("contactNumber");
                if (UtilValidate.isNotEmpty(contactNumber)) {
                    paramList = paramList + "&contactNumber=" + contactNumber;
                    andExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("contactNumber"), EntityOperator.EQUALS, EntityFunction.UPPER(contactNumber)));
                }
                fieldsToSelect.add("contactNumber");
                fieldsToSelect.add("areaCode");
            }
            // build the main condition
            if (andExprs.size() > 0) {
                mainCond = EntityCondition.makeCondition(andExprs, EntityOperator.AND);
            }
        }
        Debug.logInfo("In findParty mainCond=" + mainCond, module);
        String sortField = (String) context.get("sortField");
        if (UtilValidate.isNotEmpty(sortField)) {
            orderBy.add(sortField);
        }
        // do the lookup
        if (mainCond != null || "Y".equals(showAll)) {
            lowIndex = viewIndex * viewSize + 1;
            highIndex = (viewIndex + 1) * viewSize;
            // set distinct on so we only get one row per order
            // using list iterator
            EntityQuery eq = EntityQuery.use(delegator).select(UtilMisc.toSet(fieldsToSelect)).from(dynamicView).where(mainCond).orderBy(orderBy).cursorScrollInsensitive().fetchSize(highIndex).distinct();
            try (EntityListIterator pli = eq.queryIterator()) {
                // get the partial list for this page
                partyList = pli.getPartialList(lowIndex, viewSize);
                // attempt to get the full size
                partyListSize = pli.getResultsSizeAfterPartialList();
                if (highIndex > partyListSize) {
                    highIndex = partyListSize;
                }
            } catch (GenericEntityException e) {
                String errMsg = "Failure in party find operation, rolling back transaction: " + e.toString();
                Debug.logError(e, errMsg, module);
                return ServiceUtil.returnError(UtilProperties.getMessage(resource, "PartyLookupPartyError", UtilMisc.toMap("errMessage", e.toString()), locale));
            }
        } else {
            partyListSize = 0;
        }
    }
    if (partyList == null) {
        partyList = new LinkedList<>();
    }
    result.put("partyList", partyList);
    result.put("partyListSize", Integer.valueOf(partyListSize));
    result.put("paramList", paramList);
    result.put("highIndex", Integer.valueOf(highIndex));
    result.put("lowIndex", Integer.valueOf(lowIndex));
    return result;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition) EntityQuery(org.apache.ofbiz.entity.util.EntityQuery) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) IOException(java.io.IOException) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) LinkedList(java.util.LinkedList) DynamicViewEntity(org.apache.ofbiz.entity.model.DynamicViewEntity) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator)

Example 65 with EntityCondition

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

the class PartyWorker method findMatchingPartyPostalAddress.

/**
 * Finds all matching parties based on the values provided.  Excludes party records with a statusId of PARTY_DISABLED.  Results are ordered by descending PartyContactMech.fromDate.
 * 1. Candidate addresses are found by querying PartyAndPostalAddress using the supplied city and if provided, stateProvinceGeoId, postalCode, postalCodeExt and countryGeoId
 * 2. In-memory address line comparisons are then performed against the supplied address1 and if provided, address2.  Address lines are compared after the strings have been converted using {@link #makeMatchingString(Delegator, String)}.
 *
 * @param delegator             Delegator instance
 * @param address1              PostalAddress.address1 to match against (Required).
 * @param address2              Optional PostalAddress.address2 to match against.
 * @param city                  PostalAddress.city value to match against (Required).
 * @param stateProvinceGeoId    Optional PostalAddress.stateProvinceGeoId value to match against.  If null or "**" is passed then the value will be ignored during matching.  "NA" can be passed in place of "_NA_".
 * @param postalCode            PostalAddress.postalCode value to match against.  Cannot be null but can be skipped by passing a value starting with an "*".  If the length of the supplied string is 10 characters and the string contains a "-" then the postal code will be split at the "-" and the second half will be used as the postalCodeExt.
 * @param postalCodeExt         Optional PostalAddress.postalCodeExt value to match against.  Will be overridden if a postalCodeExt value is retrieved from postalCode as described above.
 * @param countryGeoId          Optional PostalAddress.countryGeoId value to match against.
 * @param partyTypeId           Optional Party.partyTypeId to match against.
 * @return List of PartyAndPostalAddress GenericValue objects that match the supplied criteria.
 * @throws GenericEntityException
 */
public static List<GenericValue> findMatchingPartyPostalAddress(Delegator delegator, String address1, String address2, String city, String stateProvinceGeoId, String postalCode, String postalCodeExt, String countryGeoId, String partyTypeId) throws GenericEntityException {
    if (address1 == null || city == null || postalCode == null) {
        throw new IllegalArgumentException();
    }
    List<EntityCondition> addrExprs = new LinkedList<>();
    if (stateProvinceGeoId != null) {
        if ("**".equals(stateProvinceGeoId)) {
            Debug.logWarning("Illegal state code passed!", module);
        } else if ("NA".equals(stateProvinceGeoId)) {
            addrExprs.add(EntityCondition.makeCondition("stateProvinceGeoId", EntityOperator.EQUALS, "_NA_"));
        } else {
            addrExprs.add(EntityCondition.makeCondition("stateProvinceGeoId", EntityOperator.EQUALS, stateProvinceGeoId.toUpperCase(Locale.getDefault())));
        }
    }
    if (!postalCode.startsWith("*")) {
        if (postalCode.length() == 10 && postalCode.indexOf('-') != -1) {
            String[] zipSplit = postalCode.split("-", 2);
            postalCode = zipSplit[0];
            postalCodeExt = zipSplit[1];
        }
        addrExprs.add(EntityCondition.makeCondition("postalCode", EntityOperator.EQUALS, postalCode));
    }
    if (postalCodeExt != null) {
        addrExprs.add(EntityCondition.makeCondition("postalCodeExt", EntityOperator.EQUALS, postalCodeExt));
    }
    addrExprs.add(EntityCondition.makeCondition(EntityFunction.UPPER_FIELD("city"), EntityOperator.EQUALS, EntityFunction.UPPER(city)));
    if (countryGeoId != null) {
        addrExprs.add(EntityCondition.makeCondition("countryGeoId", EntityOperator.EQUALS, countryGeoId.toUpperCase(Locale.getDefault())));
    }
    // limit to only non-disabled status
    addrExprs.add(EntityCondition.makeCondition(EntityCondition.makeCondition("statusId", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("statusId", EntityOperator.NOT_EQUAL, "PARTY_DISABLED")));
    if (partyTypeId != null) {
        addrExprs.add(EntityCondition.makeCondition("partyTypeId", EntityOperator.EQUALS, partyTypeId));
    }
    List<GenericValue> addresses = EntityQuery.use(delegator).from("PartyAndPostalAddress").where(EntityCondition.makeCondition(addrExprs, EntityOperator.AND)).orderBy("-fromDate").filterByDate().queryList();
    if (UtilValidate.isEmpty(addresses)) {
        // No address matches, return an empty list
        return addresses;
    }
    List<GenericValue> validFound = new LinkedList<>();
    // check the address line
    for (GenericValue address : addresses) {
        // address 1 field
        String addr1Source = PartyWorker.makeMatchingString(delegator, address1);
        String addr1Target = PartyWorker.makeMatchingString(delegator, address.getString("address1"));
        if (addr1Target != null) {
            Debug.logInfo("Comparing address1 : " + addr1Source + " / " + addr1Target, module);
            if (addr1Target.equals(addr1Source)) {
                // address 2 field
                if (address2 != null) {
                    String addr2Source = PartyWorker.makeMatchingString(delegator, address2);
                    String addr2Target = PartyWorker.makeMatchingString(delegator, address.getString("address2"));
                    if (addr2Target != null) {
                        Debug.logInfo("Comparing address2 : " + addr2Source + " / " + addr2Target, module);
                        if (addr2Source.equals(addr2Target)) {
                            Debug.logInfo("Matching address2; adding valid address", module);
                            validFound.add(address);
                        }
                    }
                } else {
                    if (address.get("address2") == null) {
                        Debug.logInfo("No address2; adding valid address", module);
                        validFound.add(address);
                    }
                }
            }
        }
    }
    return validFound;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition) LinkedList(java.util.LinkedList)

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