com.cws.us.pws.controllers.AboutController.java Source code

Java tutorial

Introduction

Here is the source code for com.cws.us.pws.controllers.AboutController.java

Source

/*
 * Copyright (c) 2009 - 2013 By: CWS, Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.cws.us.pws.controllers;

import org.slf4j.Logger;
import java.util.Enumeration;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.StringUtils;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.cws.us.pws.Constants;
import com.cws.us.pws.processors.dto.Career;
import com.cws.us.pws.ApplicationServiceBean;
import com.cws.us.pws.processors.dto.CareerRequest;
import com.cws.us.pws.processors.dto.CareerResponse;
import com.cws.us.pws.processors.impl.CareerReferenceImpl;
import com.cws.us.pws.processors.exception.CareerRequestException;
import com.cws.esolutions.core.processors.enums.CoreServicesStatus;

/*
 * Project: cws_java_source
 * Package: com.cws.us.pws.controllers
 * File: AboutController.java
 *
 * History
 * ----------------------------------------------------------------------------
 * kh05451 @ Jan 4, 2013 3:36:54 PM
 *     Created.
 */
@Controller
@RequestMapping("/corp")
public class AboutController {
    private String aboutUsPage = null;
    private String careersPage = null;
    private String messageNoCareerLocated = null;
    private ApplicationServiceBean appConfig = null;
    private CareerReferenceImpl careerRefSvc = null;

    private static final String CNAME = AboutController.class.getName();

    private static final Logger DEBUGGER = LoggerFactory.getLogger(Constants.DEBUGGER);
    private static final boolean DEBUG = DEBUGGER.isDebugEnabled();
    private static final Logger ERROR_RECORDER = LoggerFactory.getLogger(Constants.ERROR_LOGGER + CNAME);

    public final void setAppConfig(final ApplicationServiceBean value) {
        final String methodName = AboutController.CNAME + "#setAppConfig(final ApplicationServiceBean value)";

        if (DEBUG) {
            DEBUGGER.debug(methodName);
            DEBUGGER.debug("Value: {}", value);
        }

        this.appConfig = value;
    }

    public final void setCareerRefSvc(final CareerReferenceImpl value) {
        final String methodName = AboutController.CNAME + "#setCareerRefSvc(final CareersReferenceImpl value)";

        if (DEBUG) {
            DEBUGGER.debug(methodName);
            DEBUGGER.debug("Value: {}", value);
        }

        this.careerRefSvc = value;
    }

    public final void setMessageNoCareerLocated(final String value) {
        final String methodName = AboutController.CNAME + "#setMessageNoCareerLocated(final String value)";

        if (DEBUG) {
            DEBUGGER.debug(methodName);
            DEBUGGER.debug("Value: {}", value);
        }

        this.messageNoCareerLocated = value;
    }

    public final void setAboutUsPage(final String value) {
        final String methodName = AboutController.CNAME + "#setAboutUsPage(final String value)";

        if (DEBUG) {
            DEBUGGER.debug(methodName);
            DEBUGGER.debug("Value: {}", value);
        }

        this.aboutUsPage = value;
    }

    public final void setCareersPage(final String value) {
        final String methodName = AboutController.CNAME + "#setCareersPage(final String value)";

        if (DEBUG) {
            DEBUGGER.debug(methodName);
            DEBUGGER.debug("Value: {}", value);
        }

        this.careersPage = value;
    }

    @RequestMapping(value = "/default", method = RequestMethod.GET)
    public final ModelAndView showDefaultPage() {
        final String methodName = AboutController.CNAME + "#showDefaultPage()";

        if (DEBUG) {
            DEBUGGER.debug(methodName);
        }

        ModelAndView mView = new ModelAndView();

        final ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
                .currentRequestAttributes();
        final HttpServletRequest hRequest = requestAttributes.getRequest();
        final HttpSession hSession = hRequest.getSession();

        if (DEBUG) {
            DEBUGGER.debug("ServletRequestAttributes: {}", requestAttributes);
            DEBUGGER.debug("HttpServletRequest: {}", hRequest);
            DEBUGGER.debug("HttpSession: {}", hSession);

            DEBUGGER.debug("Dumping session content:");
            @SuppressWarnings("unchecked")
            Enumeration<String> sessionEnumeration = hSession.getAttributeNames();

            while (sessionEnumeration.hasMoreElements()) {
                String sessionElement = sessionEnumeration.nextElement();
                Object sessionValue = hSession.getAttribute(sessionElement);

                DEBUGGER.debug("Attribute: " + sessionElement + "; Value: " + sessionValue);
            }

            DEBUGGER.debug("Dumping request content:");
            @SuppressWarnings("unchecked")
            Enumeration<String> requestEnumeration = hRequest.getAttributeNames();

            while (requestEnumeration.hasMoreElements()) {
                String requestElement = requestEnumeration.nextElement();
                Object requestValue = hRequest.getAttribute(requestElement);

                DEBUGGER.debug("Attribute: " + requestElement + "; Value: " + requestValue);
            }

            DEBUGGER.debug("Dumping request parameters:");
            @SuppressWarnings("unchecked")
            Enumeration<String> paramsEnumeration = hRequest.getParameterNames();

            while (paramsEnumeration.hasMoreElements()) {
                String requestElement = paramsEnumeration.nextElement();
                Object requestValue = hRequest.getParameter(requestElement);

                DEBUGGER.debug("Parameter: " + requestElement + "; Value: " + requestValue);
            }
        }

        try {
            CareerRequest request = new CareerRequest();
            request.setLang(
                    (StringUtils.isBlank(hRequest.getParameter("lang"))) ? "en" : hRequest.getParameter("lang"));

            if (DEBUG) {
                DEBUGGER.debug("CareerRequest: {}", request);
            }

            CareerResponse response = this.careerRefSvc.getCareerList(request);

            if (DEBUG) {
                DEBUGGER.debug("CareerResponse: {}", response);
            }

            if (response.getRequestStatus() == CoreServicesStatus.SUCCESS) {
                mView.addObject("doCareersExist", Boolean.TRUE);
            }
        } catch (CareerRequestException crx) {
            ERROR_RECORDER.error(crx.getMessage(), crx);
        }

        mView.setViewName(this.aboutUsPage);

        if (DEBUG) {
            DEBUGGER.debug("ModelAndView: {}", mView);
        }

        return mView;
    }

    @RequestMapping(value = "/careers", method = RequestMethod.GET)
    public final ModelAndView showCareersPage() {
        final String methodName = AboutController.CNAME + "#showCareersPage()";

        if (DEBUG) {
            DEBUGGER.debug(methodName);
        }

        ModelAndView mView = new ModelAndView();

        final ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
                .currentRequestAttributes();
        final HttpServletRequest hRequest = requestAttributes.getRequest();
        final HttpSession hSession = hRequest.getSession();

        if (DEBUG) {
            DEBUGGER.debug("ServletRequestAttributes: {}", requestAttributes);
            DEBUGGER.debug("HttpServletRequest: {}", hRequest);
            DEBUGGER.debug("HttpSession: {}", hSession);

            DEBUGGER.debug("Dumping session content:");
            @SuppressWarnings("unchecked")
            Enumeration<String> sessionEnumeration = hSession.getAttributeNames();

            while (sessionEnumeration.hasMoreElements()) {
                String sessionElement = sessionEnumeration.nextElement();
                Object sessionValue = hSession.getAttribute(sessionElement);

                DEBUGGER.debug("Attribute: " + sessionElement + "; Value: " + sessionValue);
            }

            DEBUGGER.debug("Dumping request content:");
            @SuppressWarnings("unchecked")
            Enumeration<String> requestEnumeration = hRequest.getAttributeNames();

            while (requestEnumeration.hasMoreElements()) {
                String requestElement = requestEnumeration.nextElement();
                Object requestValue = hRequest.getAttribute(requestElement);

                DEBUGGER.debug("Attribute: " + requestElement + "; Value: " + requestValue);
            }

            DEBUGGER.debug("Dumping request parameters:");
            @SuppressWarnings("unchecked")
            Enumeration<String> paramsEnumeration = hRequest.getParameterNames();

            while (paramsEnumeration.hasMoreElements()) {
                String requestElement = paramsEnumeration.nextElement();
                Object requestValue = hRequest.getParameter(requestElement);

                DEBUGGER.debug("Parameter: " + requestElement + "; Value: " + requestValue);
            }
        }

        try {
            CareerRequest request = new CareerRequest();
            request.setLang(
                    (StringUtils.isBlank(hRequest.getParameter("lang"))) ? "en" : hRequest.getParameter("lang"));

            if (DEBUG) {
                DEBUGGER.debug("CareerRequest: {}", request);
            }

            CareerResponse response = this.careerRefSvc.getCareerList(request);

            if (DEBUG) {
                DEBUGGER.debug("CareerResponse: {}", response);
            }

            if (response.getRequestStatus() == CoreServicesStatus.SUCCESS) {
                mView.addObject("careerList", response.getCareerList());
            }
        } catch (CareerRequestException crx) {
            ERROR_RECORDER.error(crx.getMessage(), crx);
        }

        mView.setViewName(this.careersPage);

        if (DEBUG) {
            DEBUGGER.debug("ModelAndView: {}", mView);
        }

        return mView;
    }

    @RequestMapping(value = "/careers/career/{reqId}", method = RequestMethod.GET)
    public final ModelAndView showCareer(@PathVariable("reqId") final String reqId) {
        final String methodName = AboutController.CNAME
                + "#showCareer(@PathVariable(\"reqId\") final String reqId)";

        if (DEBUG) {
            DEBUGGER.debug(methodName);
        }

        ModelAndView mView = new ModelAndView();

        final ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
                .currentRequestAttributes();
        final HttpServletRequest hRequest = requestAttributes.getRequest();
        final HttpSession hSession = hRequest.getSession();

        if (DEBUG) {
            DEBUGGER.debug("ServletRequestAttributes: {}", requestAttributes);
            DEBUGGER.debug("HttpServletRequest: {}", hRequest);
            DEBUGGER.debug("HttpSession: {}", hSession);

            DEBUGGER.debug("Dumping session content:");
            @SuppressWarnings("unchecked")
            Enumeration<String> sessionEnumeration = hSession.getAttributeNames();

            while (sessionEnumeration.hasMoreElements()) {
                String sessionElement = sessionEnumeration.nextElement();
                Object sessionValue = hSession.getAttribute(sessionElement);

                DEBUGGER.debug("Attribute: " + sessionElement + "; Value: " + sessionValue);
            }

            DEBUGGER.debug("Dumping request content:");
            @SuppressWarnings("unchecked")
            Enumeration<String> requestEnumeration = hRequest.getAttributeNames();

            while (requestEnumeration.hasMoreElements()) {
                String requestElement = requestEnumeration.nextElement();
                Object requestValue = hRequest.getAttribute(requestElement);

                DEBUGGER.debug("Attribute: " + requestElement + "; Value: " + requestValue);
            }

            DEBUGGER.debug("Dumping request parameters:");
            @SuppressWarnings("unchecked")
            Enumeration<String> paramsEnumeration = hRequest.getParameterNames();

            while (paramsEnumeration.hasMoreElements()) {
                String requestElement = paramsEnumeration.nextElement();
                Object requestValue = hRequest.getParameter(requestElement);

                DEBUGGER.debug("Parameter: " + requestElement + "; Value: " + requestValue);
            }
        }

        try {
            Career career = new Career();
            career.setJobReqId(reqId);

            if (DEBUG) {
                DEBUGGER.debug("Career: {}", career);
            }

            CareerRequest request = new CareerRequest();
            request.setCareer(career);
            request.setLang(
                    (StringUtils.isBlank(hRequest.getParameter("lang"))) ? "en" : hRequest.getParameter("lang"));

            if (DEBUG) {
                DEBUGGER.debug("CareerRequest: {}", request);
            }

            CareerResponse response = this.careerRefSvc.getCareerData(request);

            if (DEBUG) {
                DEBUGGER.debug("CareerResponse: {}", response);
            }

            if (response.getRequestStatus() == CoreServicesStatus.SUCCESS) {
                mView.addObject("career", response.getCareer());
            } else {
                mView.addObject(Constants.ERROR_MESSAGE, this.messageNoCareerLocated);
            }
        } catch (CareerRequestException crx) {
            ERROR_RECORDER.error(crx.getMessage(), crx);

            mView = new ModelAndView(new RedirectView());
            mView.setViewName(this.appConfig.getErrorResponsePage());
        }

        mView.setViewName(this.careersPage);

        if (DEBUG) {
            DEBUGGER.debug("ModelAndView: {}", mView);
        }

        return mView;
    }
}