Package com.vst.webapp.gateway.xml

Source Code of com.vst.webapp.gateway.xml.XmlGatewayController

package com.vst.webapp.gateway.xml;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.vst.dto.CustomersNavigationListDto;
import com.vst.service.impl.XmlGatewayManager;
import com.vst.webapp.gateway.xml.cmd.AjaxCmd;
import com.vst.webapp.gateway.xml.cmd.AjaxCmdUtils;
import com.vst.webapp.gateway.xml.cmd.AjaxCmdUtils.AjaxForm;
import com.vst.webapp.gateway.xml.cmd.AjaxCmdUtils.AjaxList;
import com.vst.webapp.gateway.xml.cmd.AjaxCmdUtils.AjaxTree;
import com.vst.webapp.gateway.xml.cmd.AjaxCmdUtils.PgDescr;
import com.vst.webapp.gateway.xml.cmd.CmdXmlParser;

public class XmlGatewayController extends AbstractController {
  private static final String REQUEST_PARAM_CMD_BODY = "action";

  private static final String REQUEST_ATTR_PAGE_NAME = "pagename";
  private static final String REQUEST_ATTR_FORM_DATA = "formdata";
  private static final String REQUEST_ATTR_DATA_LIST = "datalist";
  private static final String REQUEST_ATTR_DATA_EXT = "extdata";
  private static final String REQUEST_ATTR_TREE_ROOT = "rootitem";
  private static final String REQUEST_ATTR_AJAX_CMD = "ajax_cmd";
  private static final String REQUEST_ATTR_TOTAL_ROWS = "total_rows";
  private static final String REQUEST_ATTR_TOTAL_PAGES = "total_pages";
  private static final String REQUEST_ATTR_CUR_PAGE = "current_page";

  private String answerText;

  private XmlGatewayManager gatewayManager;

  public XmlGatewayManager getGatewayManager() {
    return gatewayManager;
  }

  public void setGatewayManager(XmlGatewayManager gatewayManager) {
    this.gatewayManager = gatewayManager;
  }

  public String getAnswerText() {
    return answerText;
  }

  public void setAnswerText(String answerText) {
    this.answerText = answerText;
  }

  private AjaxCmd extractAjaxCommand(HttpServletRequest request)
      throws Exception {
    String xmlCmd = request.getParameter(REQUEST_PARAM_CMD_BODY);
    CmdXmlParser parser = new CmdXmlParser();
    return parser.parse(xmlCmd);
  }

  private void processAjaxCommand(AjaxCmd cmd, HttpServletRequest request,
      Map<String, Object> model) throws Exception {
    if (AjaxCmdUtils.CMD_NAME_GET_ROWS.equalsIgnoreCase(cmd.getName())) {
      prepareDataForTableView(cmd, request, model);
    } else if (AjaxCmdUtils.CMD_NAME_GET_TREE.equalsIgnoreCase(cmd.getName())) {
      prepareDataForTreeView(cmd, request, model);
    } else if (AjaxCmdUtils.CMD_NAME_GET_FORM.equalsIgnoreCase(cmd.getName())) {
      prepareDataForFormView(cmd, request, model);
    } else {
      throw new Exception("Unsuppoted command : " + cmd.getName());
    }
  }



  private void prepareDataForFormView(AjaxCmd cmd,
      HttpServletRequest request, Map<String, Object> model) {
    String formName = cmd.getParamValues(
        AjaxCmdUtils.PARAM_FORM_NAME).get(0);
    String resultPage = AjaxCmdUtils.convertFormNameToPageName(formName);
    model.put(REQUEST_ATTR_PAGE_NAME, "forms/" + resultPage);

    AjaxForm formType  = AjaxCmdUtils.convertFormNameToFormEnumItem(formName);
    Object data = null;
    switch (formType) {
    case CUSTOMER_FORM:
      // extract  id from params
      String customerId = cmd.getParamValues(
          AjaxCmdUtils.PARAM_FORM_OBJECT_ID).get(0);
      // fill form model by manager method
      data = gatewayManager.getCustomerForForm(new Integer(customerId));
      break;
    case BUILDING_FORM:
      // extract  id from params
      String buildingId = cmd.getParamValues(
          AjaxCmdUtils.PARAM_FORM_OBJECT_ID).get(0);
      // fill form model by manager method
      data = gatewayManager.getBuildingForForm(new Integer(buildingId));
      break;
    case BUILDING_CONSTRUCTION:
      // extract  id from params
      // fill form model by manager method
      break;
    case CONSTRUCTION_DEFECT:
      // extract  id from params
      // fill form model by manager method
      break;
    case MEASUREMENT_POINT:
      // extract  id from params
      // fill form model by manager method
      break;
    default:
      break;
    }
    model.put(REQUEST_ATTR_FORM_DATA, data);

  }

  private void prepareDataForTreeView(AjaxCmd cmd, HttpServletRequest request,
      Map<String, Object> model) throws Exception {
    String treeName = cmd.getParamValues(
        AjaxCmdUtils.PARAM_TREE_NAME).get(0);
    String resultPage = AjaxCmdUtils.convertTreeNameToPageName(treeName);
    model.put(REQUEST_ATTR_PAGE_NAME, "trees/" + resultPage);

    AjaxTree treeType  = AjaxCmdUtils.convertTreeNameToTreeEnumItem(treeName);
    Object root = null;
    switch (treeType) {
    case GEOTREE:
      root = gatewayManager.getGeoTree();
      break;
    default:
      break;
    }
    model.put(REQUEST_ATTR_TREE_ROOT, root);
  }

  private void prepareDataForTableView(AjaxCmd cmd, HttpServletRequest request,
      Map<String, Object> model) throws Exception {
    String listName = cmd.getParamValues(
        AjaxCmdUtils.PARAM_LIST_NAME).get(0);
    String resultPage = AjaxCmdUtils.convertListNameToPageName(listName);
    model.put(REQUEST_ATTR_PAGE_NAME, "tables/" + resultPage);

    AjaxList listType  = AjaxCmdUtils.convertListNameToListEnumItem(listName);
    List<?> tblList = null;
    Object extData = null;
    switch (listType) {
    case REGIONS:
      tblList = gatewayManager.getDataForRegionsView();
      break;
//TODO not implemented yet
//    case HINTS:
//      tblList = gatewayManager.getDataForHintsView();
//      break;
    case MATERIALS:
      tblList = gatewayManager.getDataForMaterialsView();
      break;
    case DANGER_CATEGORIES:
      tblList = gatewayManager.getDataForDangerCategoriesView();
      break;
    case DEFECT_ZONES:
      tblList = gatewayManager.getDataForDefectZonesView();
      break;
    case CUSTOMERS_ORG:
      extData = gatewayManager.getCustomersForNavigationList();
      tblList = ((CustomersNavigationListDto) extData).getCustomers();
      break;
    case CONSTRUCTION_TYPES:
      tblList = gatewayManager.getDataForConstructionTypesView();
      break;
    default:
      break;
    }

    if (tblList != null) {
      model.put(REQUEST_ATTR_TOTAL_ROWS, tblList.size());
    } else {
      model.put(REQUEST_ATTR_TOTAL_ROWS, 0);
    }

    if (listType.pagingMode == AjaxCmdUtils.PAGING_ON) {
      PgDescr<?> dscr = AjaxCmdUtils.extractPage(tblList, cmd.getPage(), cmd.getRecordsPerPage());
      model.put(REQUEST_ATTR_DATA_LIST, dscr.getPageData());
      model.put(REQUEST_ATTR_DATA_EXT, extData);
      model.put(REQUEST_ATTR_TOTAL_PAGES, dscr.getTotalPages());
      model.put(REQUEST_ATTR_CUR_PAGE, dscr.getCurrentPage());

    } else {
      model.put(REQUEST_ATTR_DATA_LIST, tblList);
      model.put(REQUEST_ATTR_DATA_EXT, extData);
      model.put(REQUEST_ATTR_TOTAL_PAGES, 1);
      model.put(REQUEST_ATTR_CUR_PAGE, 1);
    }


  }

  @Override
  protected ModelAndView handleRequestInternal(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
    AjaxCmd cmd = null;
    Map<String, Object> model = new HashMap<String, Object>();
    try {
      cmd = extractAjaxCommand(request);
      model.put(REQUEST_ATTR_AJAX_CMD, cmd);
      processAjaxCommand(cmd, request, model);
    } catch (Throwable e) {
      cmd = null;
      //TODO use logger
      e.printStackTrace();
      return new ModelAndView("xmlgateway/error_answer", "text", e.getMessage());
    }

    String resultPage = (String) model.get(REQUEST_ATTR_PAGE_NAME);
    if (resultPage == null) {
      String msg = "Not find pagename in command processor context.";
      return new ModelAndView("xmlgateway/error_answer", "text", msg);
    }

    return new ModelAndView("xmlgateway/" + resultPage, model);
  }

}
TOP

Related Classes of com.vst.webapp.gateway.xml.XmlGatewayController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.