All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.causecode.contact.ContactService.groovy Maven / Gradle / Ivy

package com.causecode.contact

import grails.plugin.springsecurity.SpringSecurityService

import javax.servlet.http.HttpServletRequest

import com.causecode.geo.location.City
import com.causecode.geo.location.Country

/**
 * ContactService resolves location and contact information parameters and validates instances.
 * @author Shashank Agrawal
 *
 */
class ContactService {

    /**
     * Dependency injection for the springSecurityService.
     */
    SpringSecurityService springSecurityService

    /**
     * Resolves location and contact information and save user instance with resolved parameter.
     * @param args list of location and contact parameters
     * @param request HttpServletRequest object used to get Locale for country parameter.
     * @return Boolean field specifying successfully saved current user instance with given parameters.
     */
    boolean resolveParameters(Map args, HttpServletRequest request, String fieldName = 'contact') {
        Country countryInstance
        String city = args['city']
        String state = args['state']
        String country = args['country']
        PhoneCountryCode countryCodeInstance

        Map locationMap = getCityStateCountry(args)
        city = locationMap.containsKey('city') ? locationMap['city'] : city
        state = locationMap.containsKey('state') ? locationMap['state'] : state
        country = locationMap.containsKey('country') ? locationMap['country'] : country
        countryInstance = retrieveCountryInstance(args, request, country)

        log.info "Resolved [city: $city, state: $state, country: $country]"

        City cityInstance = City.findOrSaveWhere(city: city, state: state, country: countryInstance)
        if (cityInstance.hasErrors()) {
            log.warn "Error saving city instance: $cityInstance.errors"
        }
        if (args['mobileCountryCode']) {
            countryCodeInstance = PhoneCountryCode.findOrSaveWhere(code: args['mobileCountryCode'],
                    country: countryInstance)
            args["${fieldName}.phone.countryCode.id"] = countryCodeInstance.id
        }
        if (args['phoneNumber']) {
            args["${fieldName}.phone.number"] = args['phoneNumber']
        }
        args["${fieldName}.email"] = args['email']
        args["${fieldName}.altEmail"] = args['altEmail']
        args["${fieldName}.facebook"] = args['facebook']
        args["${fieldName}.linkedIn"] = args['linkedIn']
        args["${fieldName}.twitter"] = args['twitter']
        args["${fieldName}.address.zip"] = args['zip']
        args["${fieldName}.address.latitude"] = args['latitude'] ?: 0
        args["${fieldName}.address.longitude"] = args['longitude'] ?: 0
        args["${fieldName}.address.city.id"] = cityInstance.id

        return true
    }

    /**
     * Retrieves city, state and country values from args map.
     * @param args list of location and contact parameters
     * @return Map containing corresponding values of city, state and country
     */
    private Map getCityStateCountry(Map args) {
        Map cityStateCountryMap = [:]
        int two = 2
        if (args['cityState']) {
            List cityState = args['cityState'].tokenize(',')
            if (cityState != null && cityState.size() >= two) {
                cityStateCountryMap['city'] = cityState[0]?.trim()
                cityStateCountryMap['state'] = cityState[1]?.trim()
            }
        }

        if (args['cityStateCountry']) {
            List cityStateCountry = args['cityStateCountry'].tokenize(',')
            if (cityStateCountry != null && cityStateCountry.size() > 0) {
                cityStateCountryMap['city'] = cityStateCountry[0]?.trim()
                if (cityStateCountry.size() < 3) {
                    cityStateCountryMap['state'] = ''
                    cityStateCountryMap['country'] = cityStateCountry[1]?.trim()
                } else {
                    cityStateCountryMap['state'] = cityStateCountry[1]?.trim()
                    cityStateCountryMap['country'] = cityStateCountry[two]?.trim()
                }
            }
        }

        return cityStateCountryMap
    }

    /**
     * Returns the country instance based on the parameters provided.
     * @param args list of location and contact parameters
     * @param request HttpServletRequest object used to get Locale for country parameter.
     * @param country country name
     * @return countryInstance
     */
    private Country retrieveCountryInstance(Map args, HttpServletRequest request, String country) {
        Country countryInstance
        String tempCountry = country
        if (!args['countryId'] && !country) {
            log.warn "User [${springSecurityService.currentUser?.email ?: 'Anonymous'}] no country found in params." +
                    "Setting [${request.locale.displayCountry}]"
            tempCountry = request.locale.displayCountry
        }
        if (tempCountry) {
            countryInstance = Country.findOrSaveByName(tempCountry)
        } else {
            if (args['countryId']) {
                countryInstance = Country.get(args['countryId'].toLong()) // Useful in token auto-complete
            }
        }

        return countryInstance
    }

    /**
     * Checks whether instance validated without any errors or not for given field name.
     * @param instance Object to be validated
     * @param fieldName Name of embedded field.
     * @return True when instance for given field validated with no errors. Returns False if it throws errors in
     * validation.
     */
    boolean hasErrors(Object instance, String fieldName = 'contact') {
        List validationResult = []
        validationResult << instance[fieldName]?.validate()
        validationResult << instance[fieldName]?.phone?.validate()
        validationResult << instance[fieldName]?.address?.validate()
        validationResult << instance[fieldName]?.address?.city?.validate()
        validationResult << instance[fieldName]?.address?.city?.country?.validate()

        def currentUserInstance = springSecurityService.currentUser
        log.info "User [${currentUserInstance?.email ?: 'Anonymous'}]"

        return validateResults(instance, validationResult, fieldName)
    }

    /**
     * Method simply checks whether there are any errors in data,
     * @param validationResult - List containing result of performed validations
     * @return boolean if there are errors, it returns true, otherwise false
     */
    boolean validateResults(Object instance, List validationResult, String fieldName) {
        if (validationResult.contains(false)) {
            log.warn "Instance with field name $fieldName has errors"
            log.warn "${instance[fieldName]?.errors}"
            log.warn "${instance[fieldName]?.phone?.errors}"
            log.warn "${instance[fieldName]?.address?.errors}"
            log.warn "${instance[fieldName]?.address?.city?.errors}"

            return true
        }

        return false
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy