/**
   * API to display entry-form for adding a new UtilityAttribute time Usage :
   * /UtilityAttribute/create
   *
   * @param
   * @return : name of jsp file
   */
  @RequestMapping(value = "/utilityattribute/create/{uid}/{slno}", method = RequestMethod.GET)
  public String showNewUtilityAttribute(
      ModelMap model, @PathVariable("uid") int utilityid, @PathVariable("slno") int slno) {

    int stat = -1;
    if (slno != -1) {
      boolean bln = mastersservice.deleteUtilityAttr(slno);
      if (bln) {
        stat = 1;
      } else {
        stat = -1;
      }
    }

    logger.info(" going to create new UtilityAttribute");
    UtilityAttributes uattr = new UtilityAttributes();
    uattr.setUtilityid(utilityid);
    model.addAttribute("utilityattribute", uattr);
    List<Attributes> attrList = mastersservice.getAttributeList();
    model.addAttribute("attrList", attrList);
    model.addAttribute("utilityid", utilityid);
    List<UtilityAttributes> utlyattr = mastersservice.getUtilityAttr(utilityid);
    model.addAttribute("utlyattr", utlyattr);
    model.addAttribute("stat", stat);
    return "utilityattributes";
  }
  /**
   * API to display an existing Utility Usage : /utility/update/{id}
   *
   * @param : id of Utility
   * @return : name of jsp file to which control is to be returned
   */
  @RequestMapping(value = "/utilityattribute/update/{utilityid}/{slno}", method = RequestMethod.GET)
  public String showExistingUtilityAttribute(
      @PathVariable("utilityid") int utilityid, @PathVariable("slno") int slno, Model model) {
    logger.info("inside UtilityAttributeController.utilityattribute: id= " + slno);
    UtilityAttributes utilityattribute = mastersservice.getSelUtilityAttr(slno);
    utilityattribute.setUtilityid(utilityid);
    model.addAttribute("utilityattribute", utilityattribute);
    List<UtilityAttributes> utlyattr = mastersservice.getUtilityAttr(utilityid);
    model.addAttribute("utlyattr", utlyattr);

    List<Attributes> attrList = mastersservice.getAttributeList();
    model.addAttribute("attrList", attrList);
    model.addAttribute("stat", -1);
    return "utilityattributes";
  }
  /**
   * API to update a UtilityAttribute Usage : /UtilityAttribute/update/{utilityid}/{slno}
   *
   * @param ID of attribute
   * @return name of jsp file to which control is to be returned
   */
  @RequestMapping(
      value = "/utilityattribute/update/{utilityid}/{slno}",
      method = RequestMethod.POST)
  protected String updateUtilityAttribute(
      @PathVariable("utilityid") int utilityid,
      @PathVariable("slno") int slno,
      @ModelAttribute("utilityattribute") UtilityAttributes utilityattr) {

    logger.info(
        "*** in utilityattr update id=** " + slno + " :utilityattr=" + utilityattr.getUtilityid());
    int stat = 0;
    try {
      utilityattr.setUtilityid(utilityid);
      utilityattr.setN_slno(slno);
      if (mastersservice.updateUtilityAttr(utilityattr)) {
        stat = 1;
      }
    } catch (Exception e) {
      logger.info("Exception in update utilityattr  " + e.getMessage());
    }
    // Redirect to list page via main page
    String saveoper = "/API/utilityattribute/create/" + utilityid + "/-1?savestat=" + stat;
    SecurityContext sec = SecurityContextHolder.getContext();
    AbstractAuthenticationToken auth = (AbstractAuthenticationToken) sec.getAuthentication();
    @SuppressWarnings("unchecked")
    Map<String, Object> info = (Map<String, Object>) auth.getDetails();
    info.put("saveoper", saveoper);
    auth.setDetails(info);
    return "redirect:/start.htm";
  }
  /**
   * API to save a new UtilityAttribute Usage : /UtilityAttribute/create
   *
   * @param
   * @return name of jsp file to which control is to be returned
   */
  @RequestMapping(value = "/utilityattribute/create/{uid}/{slno}", method = RequestMethod.POST)
  protected String createUtilityAttribute(
      @ModelAttribute("utilityarrtibute") UtilityAttributes utlyattribute,
      @PathVariable("uid") int utilityid,
      @PathVariable("slno") int slno) {
    int stat = 0;
    try {

      logger.info(" in utility create POST. utility=  " + utlyattribute.getUtilityid());

      int primarykey = mastersservice.insertUtilityAttr(utlyattribute);
      if (primarykey != -1) {
        stat = 1;
      }

    } catch (Exception e) {
      logger.info("Exception in create UtilityAttribute " + e.getMessage());
    }
    //  Redirect control to list page via main page
    String saveoper = "/API/utilityattribute/create/" + utilityid + "/-1?savestat=" + stat;
    SecurityContext sec = SecurityContextHolder.getContext();
    AbstractAuthenticationToken auth = (AbstractAuthenticationToken) sec.getAuthentication();
    @SuppressWarnings("unchecked")
    Map<String, Object> info = (Map<String, Object>) auth.getDetails();

    info.put("saveoper", saveoper);
    auth.setDetails(info);
    return "redirect:/start.htm";
  }
  /**
   * Handler
   *
   * @param
   * @return
   */
  @RequestMapping(value = "/AssetDtlsMon", method = RequestMethod.GET)
  public ModelAndView getAsseDetails(HttpServletRequest request, HttpServletResponse response) {
    String oper = request.getParameter("oper");
    logger.info(" In getAssetDetails. Oper=" + oper);

    String url = request.getHeader("host");
    String[] urlarr = url.split(":");
    List<RoleDsp> rle = mastersservice.getLogUserRole();
    is_sslport = rle.get(0).getNwcportsecure() + "";
    is_port = rle.get(0).getNwcport() + "";
    if (request.isSecure()) {
      is_url = "https://" + urlarr[0] + ":" + is_sslport;
    } else {
      is_url = "http://" + urlarr[0] + ":" + is_port;
    }
    Map<String, Object> myModel = new HashMap<String, Object>();
    myModel.put("is_url", is_url);
    myModel.put("precision", precision);
    if (oper.contains("mon")) {
      String frm = request.getParameter("frm");
      if (frm == null || frm == "") {
        frm = "main";
      }
      String parid_s = request.getParameter("parid");
      logger.info(" In getAssetDetails. oper=mon. ParentId=" + parid_s);
      int parid = Integer.valueOf(parid_s);

      MeterDsp mtr = metertagmngr.getSecMeterList(parid).get(0);
      // MeterDsp mtr = metertagmngr.getAssetMeterList(parid, "asset").get(0);
      myModel.put("assetdtls", mtr);
      myModel.put("secid", parid_s);
      myModel.put("displaytype", "monitordtls");
      myModel.put("frm", frm);
      ModelAndView model = new ModelAndView("assetmtrdtls", "model", myModel);
      return model;
    } else if (oper.contains("trend")) {
      ObjectMapper mapper = new ObjectMapper();
      String stagid = request.getParameter("tagid");
      String chartno = request.getParameter("chartno");
      String pright = request.getParameter("pright");
      String pbottom = request.getParameter("pbottom");
      String mname = request.getParameter("mname");
      String prevnames = request.getParameter("prevnames");
      logger.info(" In getAssetDetails. chartno=" + chartno);

      logger.info("Oper = trend, tagid=" + request.getParameter("tagid"));
      myModel.put("tagid", request.getParameter("tagid"));
      myModel.put("typ", "log");
      myModel.put("chartno", chartno);
      myModel.put("pright", pright);
      myModel.put("pbottom", pbottom);
      myModel.put("mname", mname);
      myModel.put("prevnames", prevnames);
      String[] strarr = metertagmngr.getTagTrend(Integer.parseInt(stagid));
      //			logger.info("strarr "+strarr.length);

      String json = "";
      try {
        json = mapper.writeValueAsString(strarr);
      } catch (Exception e) {
        e.printStackTrace();
      }

      myModel.put("meterlog", json);
      return new ModelAndView("chart", "model", myModel);
    } else {
      logger.info(
          "**ERROR condition. AssetDtlsMon called with undefined operation string :" + oper);
      return null;
    }
  }