com.stoxx.portlet.clientlist.delegate.ClientListInfoDelegator.java Source code

Java tutorial

Introduction

Here is the source code for com.stoxx.portlet.clientlist.delegate.ClientListInfoDelegator.java

Source

package com.stoxx.portlet.clientlist.delegate;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import org.apache.commons.lang.StringUtils;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.language.LanguageUtil;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.model.Address;
import com.liferay.portal.model.Contact;
import com.liferay.portal.model.Country;
import com.liferay.portal.model.Phone;
import com.liferay.portal.model.User;
import com.liferay.portal.security.auth.PrincipalThreadLocal;
import com.liferay.portal.security.permission.PermissionChecker;
import com.liferay.portal.security.permission.PermissionCheckerFactoryUtil;
import com.liferay.portal.security.permission.PermissionThreadLocal;
import com.liferay.portal.service.AddressServiceUtil;
import com.liferay.portal.service.CountryServiceUtil;
import com.liferay.portal.service.PhoneServiceUtil;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.stoxx.digital.platform.model.EmailDomain;
import com.stoxx.digital.platform.model.SalesEntry;
import com.stoxx.digital.platform.service.EmailDomainLocalServiceUtil;
import com.stoxx.digital.platform.service.SalesEntryLocalServiceUtil;
import com.stoxx.portlet.clientlist.model.ClientListVO;
import com.stoxx.portlet.clientlist.model.ClientModel;
import com.stoxx.portlet.clientlist.model.EmployeeVO;
import com.stoxx.service.business.userprofile.UserProfileService;
import com.stoxx.service.constant.STOXXConstants;
import com.stoxx.service.domain.userprofile.UserProfileDetails;
import com.stoxx.service.util.STOXXDateUtil;

public class ClientListInfoDelegator implements Serializable {

    private static final String ALL = "ALL";
    private static final String NA = "NA";
    private static final long serialVersionUID = 1L;
    //private ClientListInfoServiceImpl clientListInfoService;
    private UserProfileService userProfileService;
    private static Log log = LogFactoryUtil.getLog(ClientListInfoDelegator.class);

    /**
     * @return the userProfileService
     */
    public UserProfileService getUserProfileService() {
        return userProfileService;
    }

    /**
     * @param userProfileService the userProfileService to set
     */
    public void setUserProfileService(UserProfileService userProfileService) {
        this.userProfileService = userProfileService;
    }

    @SuppressWarnings("unchecked")
    public ClientModel getClientListInfo(long companyId, Locale locale, String alphabet, ClientModel clientModel,
            long userId) {
        List<SalesEntry> salesEntryList = null;
        try {
            if (alphabet.equalsIgnoreCase(ALL)) {
                salesEntryList = SalesEntryLocalServiceUtil.getSalesEntries(0,
                        SalesEntryLocalServiceUtil.getSalesEntriesCount());
            } else {
                salesEntryList = SalesEntryLocalServiceUtil.getSalesListByStartingAlphabet(alphabet);
            }
            List<ClientListVO> clientListVOList = new ArrayList<ClientListVO>();
            if (Validator.isNotNull(salesEntryList) && salesEntryList.size() > 0) {
                for (SalesEntry salesEntry : salesEntryList) {
                    ClientListVO clientListVO = new ClientListVO();
                    List<EmployeeVO> employeeList = new ArrayList<EmployeeVO>();
                    long salesEntryId = salesEntry.getSalesEntryId();
                    if (0 != salesEntryId) {
                        String companyName = salesEntry.getCompanyName();
                        if (StringUtils.isNotBlank(companyName)) {
                            companyName = companyName.trim();
                            clientListVO.setCompanyName(companyName);
                        }
                        Date endDate = salesEntry.getEndDate();
                        if (Validator.isNotNull(endDate)) {
                            String salesEntryEndDate = STOXXDateUtil
                                    .convertDateToStringInStoxxApplicationDateFormat(salesEntry.getEndDate());
                            clientListVO.setEndDate(salesEntryEndDate);
                        } else {
                            clientListVO.setEndDate(NA);
                        }
                        clientListVO.setAllowedUsers(salesEntry.getUsersAllowed());
                        clientListVO.setPackagesSubscribed(salesEntry.getPackageNames());
                        List<UserProfileDetails> licensedUserDetails = userProfileService
                                .getLicensedUsers(salesEntryId);
                        if (Validator.isNotNull(licensedUserDetails) && licensedUserDetails.size() > 0) {
                            int usedLicenses = licensedUserDetails.size();
                            clientListVO.setUsedLicenses(usedLicenses);
                        }
                        List<EmailDomain> emailDomainList = EmailDomainLocalServiceUtil
                                .getEmailDomains(salesEntryId);
                        String commaSeperatedEmailDomain = new String();
                        for (EmailDomain emailDomain : emailDomainList) {
                            String domainName = emailDomain.getDomain();
                            commaSeperatedEmailDomain += domainName + StringPool.COMMA;

                        }
                        if (commaSeperatedEmailDomain.endsWith(StringPool.COMMA)) {
                            commaSeperatedEmailDomain = commaSeperatedEmailDomain.substring(0,
                                    commaSeperatedEmailDomain.length() - 1);
                        }
                        if (StringUtils.isNotBlank(commaSeperatedEmailDomain)) {
                            clientListVO.setDomainName(commaSeperatedEmailDomain);
                        } else {
                            clientListVO.setDomainName(LanguageUtil.get(locale, "stoxx-client-no-domain-present"));
                        }
                        List<String> emailAddressList = userProfileService
                                .getLicensedUsersEmailAddress(salesEntryId);
                        if (Validator.isNotNull(emailAddressList) && emailAddressList.size() > 0) {
                            for (String emailAddress : emailAddressList) {
                                if (StringUtils.isNotBlank(emailAddress)) {
                                    EmployeeVO employeeVO = new EmployeeVO();
                                    User user = UserLocalServiceUtil.fetchUserByEmailAddress(companyId,
                                            emailAddress.toLowerCase());
                                    UserProfileDetails userProfileDetails = userProfileService
                                            .getUserProfileData(emailAddress.toLowerCase());
                                    if (Validator.isNotNull(userProfileDetails)) {
                                        Integer stoxxUserStatus = userProfileDetails.getStatus();
                                        String statusFlag = null;
                                        if (Validator.isNotNull(user)) {
                                            String fullName = user.getFirstName() + StringPool.SPACE
                                                    + user.getLastName();
                                            employeeVO.setFullName(fullName);
                                            employeeVO.setEmailAddress(emailAddress);
                                            //PermissionChecker permissionChecker = setAnonymousUserPermission(UserLocalServiceUtil.getDefaultUserId(companyId)); 
                                            List<Address> addressList = AddressServiceUtil
                                                    .getAddresses(Contact.class.getName(), user.getContactId());
                                            for (Address address : addressList) {
                                                if (address.getType().getName()
                                                        .equals(STOXXConstants.ADDRESS_TYPE_BUSINESS)) {
                                                    String street = address.getStreet1();
                                                    if (StringUtils.isNotBlank(address.getStreet2())) {
                                                        street = street.concat(
                                                                StringPool.COMMA + address.getStreet2().trim());
                                                    }
                                                    long countryId = address.getCountryId();
                                                    String countryName = null;
                                                    Country country = CountryServiceUtil.getCountry(countryId);
                                                    if (Validator.isNotNull(country)) {
                                                        countryName = country.getName(locale);
                                                    }
                                                    employeeVO.setCountry(countryName);
                                                    employeeVO.setAddress(street);
                                                }
                                            }
                                            int liferayUserstatus = user.getStatus();
                                            if (Validator.isNull(stoxxUserStatus)) {
                                                statusFlag = STOXXConstants.INTIATAED;
                                            } else {
                                                if (liferayUserstatus == 0 && stoxxUserStatus == 1) {
                                                    statusFlag = STOXXConstants.ACTIVE;
                                                } else if (stoxxUserStatus == 3) {
                                                    statusFlag = STOXXConstants.NOTREGISTERED;
                                                } else {
                                                    statusFlag = STOXXConstants.SUSPENDED;
                                                }
                                            }
                                            employeeVO.setStatus(statusFlag);
                                            List<Phone> phoneList = PhoneServiceUtil
                                                    .getPhones(Contact.class.getName(), user.getContactId());
                                            if (Validator.isNotNull(phoneList) && phoneList.size() > 0) {
                                                for (Phone phone : phoneList) {
                                                    if (phone.getType().getName()
                                                            .equalsIgnoreCase(STOXXConstants.PHONE_TYPE_BUSINESS))
                                                        employeeVO.setPhoneNumber(phone.getNumber());
                                                }
                                            }
                                            employeeList.add(employeeVO);
                                        } else {
                                            employeeVO.setFullName(NA);
                                            employeeVO.setEmailAddress(emailAddress);
                                            String location = userProfileDetails.getLocation();
                                            if (StringUtils.isNotBlank(location)) {
                                                employeeVO.setEmailAddress(location);
                                            }
                                            employeeVO.setPhoneNumber(NA);
                                            if (Validator.isNull(stoxxUserStatus)) {
                                                statusFlag = STOXXConstants.INTIATAED;
                                            } else if (stoxxUserStatus.intValue() == 1
                                                    || stoxxUserStatus.intValue() == 0) { // if a user starts 1st step of registration process its staus=1, but its not updated in user_ table, so its status is Initiated.
                                                statusFlag = STOXXConstants.INTIATAED; // if user is created from sales entry page or account detail page by KAH, then status=null, so its Initiated.
                                            } else {
                                                statusFlag = "No Status Found";
                                            }
                                            employeeVO.setStatus(statusFlag);
                                            employeeVO.setAddress(NA);
                                            employeeList.add(employeeVO);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    clientListVO.setEmployeeList(employeeList);
                    clientListVOList.add(clientListVO);

                }
            }
            Collections.sort(clientListVOList);
            clientModel.setClientList(clientListVOList);
        } catch (Exception e) {
            log.error("Exception in getClientListInfo of ClientListInfoDelegator", e);
        }
        return clientModel;
    }

    /**
     * @param userId
     * @return
     * @throws Exception
     * @throws PortalException
     * @throws SystemException
     */
    private PermissionChecker setAnonymousUserPermission(long userId)
            throws Exception, PortalException, SystemException {
        PrincipalThreadLocal.setName(userId);
        PermissionChecker permissionChecker = PermissionCheckerFactoryUtil
                .create(UserLocalServiceUtil.getUser(userId));
        PermissionThreadLocal.setPermissionChecker(permissionChecker);
        return permissionChecker;
    }
}