Search in sources :

Example 1 with Predicate

use of org.kuali.rice.core.api.criteria.Predicate in project cu-kfs by CU-CommunityApps.

the class CuVendorRuleBase method validateParentVendorTaxNumber.

/**
 * Overridden so that when an existing vendor tax number is found when trying to add a new parent vendor,
 * the error message will include the existing vendor detail's ID (in payee ID format).
 *
 * @see org.kuali.kfs.vnd.document.validation.impl.VendorRule#validateParentVendorTaxNumber(org.kuali.kfs.vnd.businessobject.VendorDetail)
 */
@SuppressWarnings("deprecation")
@Override
protected boolean validateParentVendorTaxNumber(VendorDetail vendorDetail) {
    boolean valid = true;
    boolean isParent = vendorDetail.isVendorParentIndicator();
    // ==== CU Customization: Use criteria API instead, due to limited BO service methods with negative criteria. ====
    List<Predicate> criteria = new ArrayList<Predicate>();
    if (ObjectUtils.isNotNull(vendorDetail.getVendorHeader().getVendorTaxTypeCode()) && ObjectUtils.isNotNull(vendorDetail.getVendorHeader().getVendorTaxNumber())) {
        criteria.add(PredicateFactory.equal(VendorPropertyConstants.VENDOR_TAX_TYPE_CODE, vendorDetail.getVendorHeader().getVendorTaxTypeCode()));
        criteria.add(PredicateFactory.equal(VendorPropertyConstants.VENDOR_TAX_NUMBER, vendorDetail.getVendorHeader().getVendorTaxNumber()));
    } else {
        return valid;
    }
    // ==== CU Customization: Use the actual vendor details, not just the count. ====
    List<VendorDetail> existingVendors;
    // excluded from the search
    if (ObjectUtils.isNotNull(vendorDetail.getVendorHeaderGeneratedIdentifier())) {
        // ==== CU Customization: Use CriteriaLookupService instead, since BO service doesn't allow negative criteria in non-count methods. ====
        criteria.add(PredicateFactory.notEqual(VendorPropertyConstants.VENDOR_HEADER_GENERATED_ID, vendorDetail.getVendorHeaderGeneratedIdentifier()));
        existingVendors = SpringContext.getBean(CriteriaLookupService.class).lookup(VendorDetail.class, QueryByCriteria.Builder.fromPredicates(criteria.toArray(new Predicate[criteria.size()]))).getResults();
    } else {
        // If this is creating a new vendor, we can't include the header generated id
        // in the negative criteria because it's null, so we'll only look for existing
        // vendors with the same tax # and tax type regardless of the vendor header generated id.
        // ==== CU Customization: Use CriteriaLookupService instead, since BO service doesn't allow negative criteria in non-count methods. ====
        existingVendors = SpringContext.getBean(CriteriaLookupService.class).lookup(VendorDetail.class, QueryByCriteria.Builder.fromPredicates(criteria.toArray(new Predicate[criteria.size()]))).getResults();
    }
    if (!existingVendors.isEmpty()) {
        if (isParent) {
            // ==== CU Customization: Print a different error message that also includes the existing vendor's ID. ====
            if (KFSConstants.SYSTEM_USER.equals(GlobalVariables.getUserSession().getActualPerson().getPrincipalName())) {
                // Only print new message when the actual KFS system user is in place (and not someone backdoored), to keep it specific to batch runs.
                VendorDetail existingVendor = existingVendors.get(0);
                // Parent vendor takes precedence when printing the error message.
                for (int i = 1; !existingVendor.isVendorParentIndicator() && i < existingVendors.size(); i++) {
                    existingVendor = existingVendors.get(i);
                }
                putFieldError(VendorPropertyConstants.VENDOR_TAX_NUMBER, CUVendorKeyConstants.ERROR_VENDOR_TAX_TYPE_AND_NUMBER_COMBO_EXISTS_AND_PRINT_EXISTING, new String[] { existingVendor.getVendorHeaderGeneratedIdentifier().toString(), existingVendor.getVendorDetailAssignedIdentifier().toString() });
            } else {
                // Just use standard KFS-delivered message for general users who are trying to submit a regular vendor maintenance document.
                putFieldError(VendorPropertyConstants.VENDOR_TAX_NUMBER, VendorKeyConstants.ERROR_VENDOR_TAX_TYPE_AND_NUMBER_COMBO_EXISTS);
            }
        } else {
            putFieldError(VendorPropertyConstants.VENDOR_TAX_NUMBER, VendorKeyConstants.ERROR_VENDOR_PARENT_NEEDS_CHANGED);
        }
        valid &= false;
    }
    return valid;
}
Also used : VendorDetail(org.kuali.kfs.vnd.businessobject.VendorDetail) ArrayList(java.util.ArrayList) CriteriaLookupService(org.kuali.rice.core.api.criteria.CriteriaLookupService) Predicate(org.kuali.rice.core.api.criteria.Predicate)

Example 2 with Predicate

use of org.kuali.rice.core.api.criteria.Predicate in project cu-kfs by CU-CommunityApps.

the class CuPayeeACHAccountLookupableHelperServiceImpl method getSearchResultsHelper.

/**
 * Overridden to perform custom searching when a principal name is specified on the search screen.
 *
 * @see org.kuali.kfs.kns.lookup.KualiLookupableHelperServiceImpl#getSearchResultsHelper(java.util.Map, boolean)
 */
@SuppressWarnings("unchecked")
@Override
protected List<? extends BusinessObject> getSearchResultsHelper(Map<String, String> fieldValues, boolean unbounded) {
    if (StringUtils.isNotBlank(fieldValues.get(CUPdpPropertyConstants.PAYEE_PRINCIPAL_NAME))) {
        List<PayeeACHAccount> results = null;
        // Search for people with the given principal name(s), in a manner that respects lookup criteria Strings.
        List<Person> people = personService.findPeople(Collections.singletonMap(KIMPropertyConstants.Principal.PRINCIPAL_NAME, fieldValues.get(CUPdpPropertyConstants.PAYEE_PRINCIPAL_NAME)));
        if (!people.isEmpty()) {
            // Get the users' entity IDs and employee IDs for searching.
            List<String> entityIds = new ArrayList<String>();
            List<String> employeeIds = new ArrayList<String>();
            for (Person person : people) {
                entityIds.add(person.getEntityId());
                if (StringUtils.isNotBlank(person.getEmployeeId())) {
                    employeeIds.add(person.getEmployeeId());
                }
            }
            // Create a map without blank values and with all encrypted values decrypted, similar to the ancestor class's logic.
            Map<String, String> finalFieldValues = new HashMap<String, String>();
            for (Map.Entry<String, String> entry : fieldValues.entrySet()) {
                // Only add non-blank values.
                if (StringUtils.isBlank(entry.getValue())) {
                // Do nothing.
                } else if (entry.getValue().endsWith(EncryptionService.ENCRYPTION_POST_PREFIX)) {
                    // Decrypt encrypted values accordingly, as in the ancestor class.
                    String newValue = StringUtils.removeEnd(entry.getValue(), EncryptionService.ENCRYPTION_POST_PREFIX);
                    if (getEncryptionService().isEnabled()) {
                        try {
                            newValue = getEncryptionService().decrypt(newValue);
                        } catch (GeneralSecurityException e) {
                            throw new RuntimeException("Error decrypting Payee ACH Account attribute value", e);
                        }
                    }
                    finalFieldValues.put(entry.getKey(), newValue);
                } else {
                    finalFieldValues.put(entry.getKey(), entry.getValue());
                }
            }
            // Remove "payeePrincipalName" from the map, along with any hidden or non-BO-property-related entries (like back location).
            LookupUtils.removeHiddenCriteriaFields(getBusinessObjectClass(), finalFieldValues);
            finalFieldValues.remove(CUPdpPropertyConstants.PAYEE_PRINCIPAL_NAME);
            finalFieldValues.remove(KRADConstants.BACK_LOCATION);
            finalFieldValues.remove(KRADConstants.DOC_FORM_KEY);
            finalFieldValues.remove(KRADConstants.REFERENCES_TO_REFRESH);
            // Build the sub-predicate to limit by the entity or employee IDs for the given principal names.
            Predicate principalNameEquivalentPredicate;
            if (employeeIds.isEmpty()) {
                principalNameEquivalentPredicate = PredicateFactory.and(PredicateFactory.equal(PdpPropertyConstants.PAYEE_IDENTIFIER_TYPE_CODE, PayeeIdTypeCodes.ENTITY), PredicateFactory.in(PdpPropertyConstants.PAYEE_ID_NUMBER, entityIds.toArray(new String[entityIds.size()])));
            } else {
                principalNameEquivalentPredicate = PredicateFactory.or(PredicateFactory.and(PredicateFactory.equal(PdpPropertyConstants.PAYEE_IDENTIFIER_TYPE_CODE, PayeeIdTypeCodes.ENTITY), PredicateFactory.in(PdpPropertyConstants.PAYEE_ID_NUMBER, entityIds.toArray(new String[entityIds.size()]))), PredicateFactory.and(PredicateFactory.equal(PdpPropertyConstants.PAYEE_IDENTIFIER_TYPE_CODE, PayeeIdTypeCodes.EMPLOYEE), PredicateFactory.in(PdpPropertyConstants.PAYEE_ID_NUMBER, employeeIds.toArray(new String[employeeIds.size()]))));
            }
            // Build the criteria and run the search.
            QueryByCriteria.Builder crit = QueryByCriteria.Builder.create();
            if (!unbounded) {
                crit.setMaxResults(LookupUtils.getSearchResultsLimit(getBusinessObjectClass()));
            }
            if (!finalFieldValues.isEmpty()) {
                crit.setPredicates(PredicateUtils.convertMapToPredicate(finalFieldValues), principalNameEquivalentPredicate);
            } else {
                crit.setPredicates(principalNameEquivalentPredicate);
            }
            results = criteriaLookupService.lookup(getBusinessObjectClass(), crit.build()).getResults();
            // Move results to a mutable list, since the result list from CriteriaLookupService is immutable.
            results = new ArrayList<PayeeACHAccount>(results);
            // Sort results accordingly using code from the ancestor class's version of the method.
            List<String> defaultSortColumns = getDefaultSortColumns();
            if (defaultSortColumns.size() > 0) {
                Collections.sort(results, new BeanPropertyComparator(defaultSortColumns, true));
            }
        }
        // If no people were found with the given principal names, then return an empty list accordingly; otherwise, return the results.
        return (results != null) ? results : new ArrayList<PayeeACHAccount>();
    } else {
        // If principal name is not specified, then do the normal superclass processing.
        return super.getSearchResultsHelper(fieldValues, unbounded);
    }
}
Also used : HashMap(java.util.HashMap) BeanPropertyComparator(org.kuali.kfs.krad.util.BeanPropertyComparator) PayeeACHAccount(org.kuali.kfs.pdp.businessobject.PayeeACHAccount) GeneralSecurityException(java.security.GeneralSecurityException) ArrayList(java.util.ArrayList) Predicate(org.kuali.rice.core.api.criteria.Predicate) QueryByCriteria(org.kuali.rice.core.api.criteria.QueryByCriteria) Person(org.kuali.rice.kim.api.identity.Person) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ArrayList (java.util.ArrayList)2 Predicate (org.kuali.rice.core.api.criteria.Predicate)2 GeneralSecurityException (java.security.GeneralSecurityException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 BeanPropertyComparator (org.kuali.kfs.krad.util.BeanPropertyComparator)1 PayeeACHAccount (org.kuali.kfs.pdp.businessobject.PayeeACHAccount)1 VendorDetail (org.kuali.kfs.vnd.businessobject.VendorDetail)1 CriteriaLookupService (org.kuali.rice.core.api.criteria.CriteriaLookupService)1 QueryByCriteria (org.kuali.rice.core.api.criteria.QueryByCriteria)1 Person (org.kuali.rice.kim.api.identity.Person)1