Java tutorial
package com.stoxx.portlet.preregcustomeradmin.controller; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; import javax.portlet.PortletRequest; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.portlet.bind.annotation.ActionMapping; import org.springframework.web.portlet.bind.annotation.RenderMapping; import com.liferay.portal.NoSuchLayoutException; 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.portlet.LiferayPortletURL; import com.liferay.portal.kernel.servlet.SessionErrors; import com.liferay.portal.kernel.servlet.SessionMessages; import com.liferay.portal.kernel.util.StringPool; import com.liferay.portal.kernel.util.WebKeys; import com.liferay.portal.model.Layout; import com.liferay.portal.service.LayoutLocalServiceUtil; import com.liferay.portal.theme.ThemeDisplay; import com.liferay.portal.util.PortalUtil; import com.liferay.portlet.PortletURLFactoryUtil; import com.stoxx.portlet.model.PreRegAdminVO; import com.stoxx.portlet.model.SalesEntryEditVO; import com.stoxx.portlet.preregcustomeradmin.delegate.CustomerAdminDelegate; import com.stoxx.service.exception.STOXXException; @Controller @RequestMapping("view") public class CustomerAdminController { private static final String NUMBER_OF_LICENSE_REGISTERED_WITH_THIS_GROUP_CAN_T_EXCEED_NUMBER_OF_ALLOWED_USERS_SET = "Number of license registered with this group can't exceed number of allowed users set."; private static final String REQUEST_PROCESSED = "requestProcessed"; private static final String FRIENDLYURL_MANAGE_STOXX_USER = "/manage-stoxx-user"; private static final String SHOWHOMEPAGE = "/preRegAdminDisplay"; private CustomerAdminDelegate customerAdminDelegate; public void setCustomerAdminDelegate(CustomerAdminDelegate customerAdminDelegate) { this.customerAdminDelegate = customerAdminDelegate; } public CustomerAdminDelegate getCustomerAdminDelegate() { return customerAdminDelegate; } @RenderMapping public String showHomePage(RenderRequest request, RenderResponse response, Model model) throws SystemException { model.addAttribute("countries", customerAdminDelegate.getAllCountries()); model.addAttribute("allPackages", customerAdminDelegate.getAllPackages()); return SHOWHOMEPAGE; } @ModelAttribute("preRegAdminVO") public PreRegAdminVO getCommandObject() { return new PreRegAdminVO(); } @RenderMapping(params = "action=changeUserType") public String showChangeUserTypeForm(@RequestParam(required = true) String emailAddress, RenderRequest request, RenderResponse response, Model model) throws SystemException { model.addAttribute("countries", customerAdminDelegate.getAllCountries()); model.addAttribute("allPackages", customerAdminDelegate.getAllPackages()); PreRegAdminVO preRegAdminVO = new PreRegAdminVO(); preRegAdminVO.setKeyAccountHolderEmailAddress(emailAddress); preRegAdminVO.setUsersAllowed(1); preRegAdminVO.setChangeUserTypeFlag(Boolean.TRUE); model.addAttribute("preRegAdminVO", preRegAdminVO); model.addAttribute("additionalFormCss", "changeUserForm"); return SHOWHOMEPAGE; } @ActionMapping(params = "action=saveSalesEntry") public void addSalesEntry(@ModelAttribute("preRegAdminVO") PreRegAdminVO preRegAdminVO, ActionRequest request, ActionResponse response, Model model) throws SystemException, IOException, PortalException { ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY); try { if (customerAdminDelegate.hasSalesEntryByGroupName(preRegAdminVO.getGroupName())) { SessionErrors.add(request, "SEGroupNameUsed"); log.info("Sales entry already exist with group name :" + preRegAdminVO.getGroupName()); } else { if (validateSalesEntryLicense(preRegAdminVO, "Add", null)) { log.info("Adding sales entry: " + preRegAdminVO); try { Map<String, List<String>> result = customerAdminDelegate.addSalesEntry(preRegAdminVO, themeDisplay); log.info("Sales entry added with result: " + result); SessionMessages.add(request, REQUEST_PROCESSED, LanguageUtil.get(themeDisplay.getLocale(), "stoxx-prereg-sales-entry-create-success")); processSessionMessages(request, result); } catch (STOXXException e) { SessionErrors.add(request, e.getExpCode(), e.getExpMsg()); log.error(e.getExpCode() + e.getExpMsg()); } } else { log.info(NUMBER_OF_LICENSE_REGISTERED_WITH_THIS_GROUP_CAN_T_EXCEED_NUMBER_OF_ALLOWED_USERS_SET); SessionErrors.add(request, "SalesEntryAccountExceed", NUMBER_OF_LICENSE_REGISTERED_WITH_THIS_GROUP_CAN_T_EXCEED_NUMBER_OF_ALLOWED_USERS_SET); } } } catch (STOXXException e) { log.error("STOXXException", e); } response.sendRedirect(getResultPageUrl(request, themeDisplay)); } private String getResultPageUrl(ActionRequest request, ThemeDisplay themeDisplay) { LiferayPortletURL redirectUrl = PortletURLFactoryUtil.create(request, PortalUtil.getPortletId(request), themeDisplay.getPlid(), PortletRequest.RENDER_PHASE); redirectUrl.setParameter("page", "result"); return redirectUrl.toString(); } @RenderMapping(params = "page=result") public String showResult(RenderRequest request, RenderResponse response, Model model) { return "result"; } @RenderMapping(params = "action=editSalesEntry") public String editSalesEntryForm(@RequestParam(required = true) long salesEntryId, RenderRequest request, RenderResponse response, Model model) throws PortalException, SystemException, STOXXException { log.info("Opening form to edit sales entry with id " + salesEntryId); SalesEntryEditVO salesEntryEditVO = customerAdminDelegate.getSalesEntry(salesEntryId); model.addAttribute("salesEntryEditVO", salesEntryEditVO); model.addAttribute("allCountries", customerAdminDelegate.getAllCountries()); model.addAttribute("allPackages", customerAdminDelegate.getAllPackages()); ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY); try { Layout layout = LayoutLocalServiceUtil.getFriendlyURLLayout(themeDisplay.getScopeGroupId(), true, FRIENDLYURL_MANAGE_STOXX_USER); LiferayPortletURL displayUsersPortletURL = PortletURLFactoryUtil.create(request, "STOXXManageUsersportlet_WAR_STOXXManageUsersportlet", layout.getPlid(), PortletRequest.ACTION_PHASE); displayUsersPortletURL.setParameter("action", "COMPANY"); displayUsersPortletURL.setParameter("companyID", String.valueOf(salesEntryEditVO.getSalesEntryId())); displayUsersPortletURL.setParameter("p_p_state", "normal"); displayUsersPortletURL.setParameter("p_p_mode", "view"); model.addAttribute("displayUsersUrl", displayUsersPortletURL.toString()); } catch (NoSuchLayoutException ex) { log.error("Users listing layout not found with friendly url " + FRIENDLYURL_MANAGE_STOXX_USER); } return "editSalesEntryForm"; } @ActionMapping(params = "action=updateSalesEntry") public void updateSalesEntry(@ModelAttribute("editSalesEntryVO") SalesEntryEditVO salesEntryEditVO, ActionRequest request, ActionResponse response) throws IOException, PortalException, SystemException { log.info("Updating sales entry with information: " + salesEntryEditVO); try { ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY); if (validateSalesEntryLicense(null, "Edit", salesEntryEditVO)) { try { Map<String, List<String>> result = customerAdminDelegate.updateSalesEntry(salesEntryEditVO, themeDisplay); log.info("Sales entry updated with result: " + result); SessionMessages.add(request, REQUEST_PROCESSED, LanguageUtil.get(themeDisplay.getLocale(), "stoxx-prereg-sales-entry-update-success")); processSessionMessages(request, result); } catch (STOXXException e) { SessionErrors.add(request, e.getExpCode(), e.getExpMsg()); log.error(e.getExpCode() + e.getExpMsg()); } } else { log.info(NUMBER_OF_LICENSE_REGISTERED_WITH_THIS_GROUP_CAN_T_EXCEED_NUMBER_OF_ALLOWED_USERS_SET); SessionErrors.add(request, "SalesEntryAccountExceed", NUMBER_OF_LICENSE_REGISTERED_WITH_THIS_GROUP_CAN_T_EXCEED_NUMBER_OF_ALLOWED_USERS_SET); } String returnUrl = getResultPageUrl(request, themeDisplay); response.sendRedirect(returnUrl); } catch (STOXXException e) { log.error("STOXXException", e); } } private void processSessionMessages(ActionRequest request, Map<String, List<String>> result) { for (Iterator<Entry<String, List<String>>> iter = result.entrySet().iterator(); iter.hasNext();) { Entry<String, List<String>> entry = iter.next(); List<String> list = entry.getValue(); if (!list.isEmpty()) { SessionMessages.add(request, entry.getKey(), entry.getValue().toString()); } } } @ModelAttribute("editSalesEntryVO") public SalesEntryEditVO getEditSalesEntryCommandObject() { return new SalesEntryEditVO(); } private boolean validateSalesEntryLicense(PreRegAdminVO preRegAdminVO, String addOrEdit, SalesEntryEditVO salesEntryEditVO) throws STOXXException { // number of allowed user is greater than already used licenses, then set to true. if ("Add".equalsIgnoreCase(addOrEdit)) { int alreadyUsedLicenses = 0; if (preRegAdminVO.getUsersAllowed() > 0) { if (preRegAdminVO.getKeyAccountHolderEmailAddress() != null && !preRegAdminVO.getKeyAccountHolderEmailAddress().isEmpty()) { alreadyUsedLicenses = alreadyUsedLicenses + 1; } if (preRegAdminVO.getEmailAddresses() != null && preRegAdminVO.getEmailAddresses().size() > 0) { alreadyUsedLicenses = alreadyUsedLicenses + preRegAdminVO.getEmailAddresses().size(); } log.info("ALready used licesnses--> " + alreadyUsedLicenses + ": allowed users--> " + preRegAdminVO.getUsersAllowed()); if (preRegAdminVO.getUsersAllowed() < alreadyUsedLicenses) { return false; } } } if ("Edit".equalsIgnoreCase(addOrEdit)) { int alreadyUsedLicenses = 0; if (salesEntryEditVO.getUsersAllowed() > 0) { if (salesEntryEditVO.getKeyAccountHolderEmailAddress() != null && !salesEntryEditVO.getKeyAccountHolderEmailAddress().isEmpty() && !isAlreadyExistKAH(salesEntryEditVO.getSalesEntryId())) { List<String> keyAccountHolderEmailAddressList = new ArrayList<String>(); keyAccountHolderEmailAddressList.add(salesEntryEditVO.getKeyAccountHolderEmailAddress()); int licenseCount = customerAdminDelegate .checkUserExistsForComapny(keyAccountHolderEmailAddressList); log.info("licenseCount for keyaccountholder block is " + licenseCount); if (licenseCount > 0) { alreadyUsedLicenses = alreadyUsedLicenses + 1; log.info("ALready used licesnses in 1st If--> " + alreadyUsedLicenses); } } if (salesEntryEditVO.getUsedUserLicenses() > 0) { alreadyUsedLicenses = alreadyUsedLicenses + salesEntryEditVO.getUsedUserLicenses(); log.info("ALready used licesnses in 2nd If--> " + alreadyUsedLicenses); } if (salesEntryEditVO.getNewEmailAddresses() != null && salesEntryEditVO.getNewEmailAddresses().size() > 0) { salesEntryEditVO.getNewEmailAddresses().removeAll(Collections.singleton(StringPool.BLANK)); int licenseCount = customerAdminDelegate .checkUserExistsForComapny(salesEntryEditVO.getNewEmailAddresses()); log.info("licenseCount for email addresses list block is " + licenseCount); if (licenseCount > 0) { alreadyUsedLicenses = alreadyUsedLicenses + licenseCount; log.info("ALready used licesnses in 3rd If--> " + alreadyUsedLicenses); } } log.info("ALready used licesnses--> " + alreadyUsedLicenses + ": allowed users--> " + salesEntryEditVO.getUsersAllowed()); if (salesEntryEditVO.getUsersAllowed() < alreadyUsedLicenses) { return false; } } } return true; } private boolean isAlreadyExistKAH(long salesEntryId) { try { SalesEntryEditVO salesEntryEditVO = customerAdminDelegate.getSalesEntry(salesEntryId); if (salesEntryEditVO != null && salesEntryEditVO.getKeyAccountHolderEmailAddress() != null && !salesEntryEditVO.getKeyAccountHolderEmailAddress().isEmpty()) { return true; } } catch (Exception e) { log.error("Error in isAlreadyExistKAH to retrieve existing SalesEntry record" + e.getMessage()); } return false; } private static Log log = LogFactoryUtil.getLog(CustomerAdminController.class); }