/*
   * This view displays a list of all ItemTypes available.
   * If the parameter itemTypeName is set a new item type will be created.
   */
  public ModelAndView assoctypes(
      HttpServletRequest request, HttpServletResponse httpServletResponse) {
    ModelAndView mav = new ModelAndView("easyrec/assoctypes");

    Operator signedInOperator = Security.signedInOperator(request);
    RemoteTenant remoteTenant = viewInitializationService.initializeView(request, mav);

    String assocTypeName =
        ServletUtils.getSafeParameter(request, "assocTypeName", "").toUpperCase();

    if (signedInOperator != null) {

      if (!"".equals(assocTypeName)) {
        // create a new item type if the itemTypeName parameter is set

        String error = isValidAssocTypeName(assocTypeName);

        if ("".equals(error)) {
          tenantService.insertAssocTypeForTenant(remoteTenant.getId(), assocTypeName, true);
        } else {
          mav.addObject("error", error);
        }
      }

      mav.addObject("apiKey", signedInOperator.getApiKey());
      mav.addObject("assocTypes", assocTypeDAO.getTypes(remoteTenant.getId(), true));
      mav.addObject(
          "tenants", remoteTenantDAO.getTenantsFromOperator(remoteTenant.getOperatorId()));
    }

    return mav;
  }
  /*
  todo(pm): move function away from this controller
  */
  private RemoteTenant initializeView(HttpServletRequest request, ModelAndView mav) {
    RemoteTenant remoteTenant = null;

    String operatorId = ServletUtils.getSafeParameter(request, "operatorId", "");
    String tenantId = ServletUtils.getSafeParameter(request, "tenantId", "");

    String signedInOperatorId = Security.signedInOperatorId(request);
    Operator signedInOperator = Security.signedInOperator(request);

    if (signedInOperator != null) {
      if (Security.isDeveloper(request) && !Strings.isNullOrEmpty(operatorId))
        remoteTenant = remoteTenantDAO.get(operatorId, tenantId);
      else remoteTenant = remoteTenantDAO.get(signedInOperator.getOperatorId(), tenantId);

      if (remoteTenant != null) mav.addObject("remoteTenant", remoteTenant);
    }

    if (Security.isDeveloper(request) && !Strings.isNullOrEmpty(operatorId))
      mav.addObject("operatorId", operatorId);
    else mav.addObject("operatorId", signedInOperatorId);

    mav.addObject("tenantId", tenantId);
    mav.addObject("tenants", remoteTenantDAO.getTenantsFromOperator(operatorId));

    String apiKey = Operator.DEFAULT_API_KEY;

    if (signedInOperator != null) {
      apiKey = signedInOperator.getApiKey();
    }

    if (remoteTenant != null) {
      Set<String> itemTypes = itemTypeDAO.getTypes(remoteTenant.getId(), true);
      mav.addObject("availableItemTypes", itemTypes);

      Set<String> assocTypes = assocTypeDAO.getTypes(remoteTenant.getId(), true);
      assocTypes.add("");
      mav.addObject("availableAssocTypes", assocTypes);
    }

    mav.addObject("apiKey", apiKey);
    mav.addObject("signedIn", Security.isSignedIn(request));

    return remoteTenant;
  }