// Get details of Task/Card/Story
  @RequestMapping(value = "/{userId}/edit/{type}/{typeId}", method = RequestMethod.GET)
  public String getTypeDetails(
      @ModelAttribute("project") Project project,
      @PathVariable Long userId,
      @PathVariable String type,
      @PathVariable Long typeId,
      ModelMap model) {

    if (type.contentEquals("story")) {
      List ls = userDao.getStoryById(typeId);
      Story s = (Story) ls.get(0);
      model.addAttribute("story", s);
    } else if (type.contentEquals("cards")) {
      List lc = userDao.getCardById(typeId);
      Card c = (Card) lc.get(0);
      model.addAttribute("cards", c);
    } else if (type.contentEquals("tasks")) {
      List lt = userDao.getTaskById(typeId);
      Task t = (Task) lt.get(0);
      model.addAttribute("tasks", t);
    }

    List p = userDao.getProjectById(userId);
    Project ps = (Project) p.get(0);
    model.addAttribute("project", ps.getProjectname());
    model.addAttribute("type", type);
    model.addAttribute("userid", userId);
    return "Details";
  }
  public String jsonRestProjectList(Long projectId) throws IOException {
    StringBuilder sb = new StringBuilder();

    Project p = requestManager.getProjectById(projectId);
    sb.append("'id':'" + projectId + "'");
    sb.append(",");
    sb.append("'name':'" + p.getName() + "'");
    sb.append(",");
    sb.append("'alias':'" + p.getAlias() + "'");

    return "{" + sb.toString() + "}";
  }
  // Redirect to Add page to create new Task/Card/Story
  @RequestMapping(value = "/{userId}/add/{type}", method = RequestMethod.GET)
  public String getAdd(
      @ModelAttribute("project") Project project,
      @PathVariable Long userId,
      @PathVariable String type,
      ModelMap model) {

    List p = userDao.getProjectById(userId);
    Project ps = (Project) p.get(0);
    model.addAttribute("project", ps.getProjectname());
    model.addAttribute("type", type);
    model.addAttribute("userid", userId);
    return "Add";
  }
 @RequestMapping(value = "projects", method = RequestMethod.GET)
 public @ResponseBody String jsonRest() throws IOException {
   StringBuilder sb = new StringBuilder();
   Collection<Project> lp = requestManager.listAllProjects();
   int pi = 0;
   for (Project p : lp) {
     pi++;
     sb.append(jsonRestProjectList(p.getProjectId()));
     if (pi < lp.size()) {
       sb.append(",");
     }
   }
   return "[" + sb.toString() + "]";
 }
  // Get project status
  @RequestMapping(value = "/{userId}/status", method = RequestMethod.GET)
  public String showStatus(@PathVariable Long userId, ModelMap model) throws SQLException {
    User u = (User) userDao.getUserById(userId).get(0);
    Project p = (Project) userDao.getProjectById(userId).get(0);

    if (u.getTenanttype().contentEquals("kanban")) {
      List l = userDao.cardCount(userId);
      List<Object[]> resultList = l;
      Map<String, Integer> resultMap = new HashMap<>(resultList.size());
      int a[] = new int[4];
      int i = 0;

      for (Object[] result : resultList) {
        String name = (String) result[0];
        int count = ((Number) result[1]).intValue();
        a[i] = count;
        i++;
        resultMap.put(name, count);
      }
      model.addAttribute("a", a);
    } else if (u.getTenanttype().contentEquals("scrum")) {

      List<Long> t = userDao.tCount(userId);
      List<Long> r = userDao.rCount(userId);

      Long tr = t.get(0);
      Long rr = r.get(0);

      int tHours = Long.valueOf(tr).intValue();
      int rHours = Long.valueOf(rr).intValue();

      List<Integer> actual = new ArrayList<>();
      List<Integer> initial = new ArrayList<>();

      int burnDown = 2 * 5;

      while (rHours > 0) {
        actual.add(rHours);
        rHours = rHours - burnDown;
      }

      while (tHours > 0) {
        initial.add(tHours);
        tHours = tHours - burnDown;
      }
      int k = 0;
      int l = 1;

      List<Integer[]> dataArray = new ArrayList<>();
      for (int item : actual) {
        Integer[] o = {k, item, initial.get(k)};
        dataArray.add(o);
        k++;
      }

      if (initial.size() > actual.size()) {
        for (int item : initial) {
          if (l > actual.size()) {
            Integer[] o = {l, 0, item};
            dataArray.add(o);
          }
          l++;
        }
        Integer[] o = {l, 0, 0};
        dataArray.add(o);
      } else if (initial.size() < actual.size()) {
        for (int item : initial) {
          if (l > initial.size()) {
            Integer[] o = {l, item, 0};
            dataArray.add(o);
          }
          l++;
        }
        Integer[] o = {l, 0, 0};
        dataArray.add(o);
      } else if (initial.size() == actual.size()) {
        Integer[] o = {k, 0, 0};
        dataArray.add(o);
      }
      model.addAttribute("dataArray", dataArray);
    } else if (u.getTenanttype().contentEquals("waterfall")) {

      List l = userDao.taskCount(userId);
      List<Object[]> resultList = l;
      Map<String, Integer> resultMap = new HashMap<>(resultList.size());
      int a[] = {0, 0, 0};

      for (Object[] result : resultList) {
        String name = (String) result[0];
        int count = ((Number) result[1]).intValue();
        if (name.contentEquals("Requested")) {
          a[0] = count;
        } else if (name.contentEquals("In Progress")) {
          a[1] = count;
        } else if (name.contentEquals("Completed")) {
          a[2] = count;
        }
        resultMap.put(name, count);
      }
      model.addAttribute("a", a);
    }

    model.addAttribute("tenant", u.getTenanttype());
    model.addAttribute("project", p.getProjectname());
    model.addAttribute("userid", userId);

    return "Status";
  }
  @RequestMapping(value = "project/{projectId}", method = RequestMethod.GET)
  public @ResponseBody String jsonRestProject(@PathVariable Long projectId, ModelMap model)
      throws IOException {
    StringBuilder sb = new StringBuilder();

    Project p = requestManager.getProjectById(projectId);
    sb.append("'id':'" + projectId + "'");
    sb.append(",");
    sb.append("'name':'" + p.getName() + "'");
    sb.append(",");
    sb.append("'alias':'" + p.getAlias() + "'");
    sb.append(",");
    sb.append("'progress':'" + p.getProgress().name() + "'");
    sb.append(",");
    sb.append("'description':'" + p.getDescription() + "'");
    sb.append(",");

    sb.append("'overviews':[");
    if (p.getOverviews().size() > 0) {
      int oi = 0;
      for (ProjectOverview overview : p.getOverviews()) {
        oi++;
        sb.append("{");
        sb.append("'allSampleQcPassed':" + overview.getAllSampleQcPassed());
        sb.append(",");
        sb.append("'libraryPreparationComplete':" + overview.getLibraryPreparationComplete());
        sb.append(",");
        sb.append("'allLibrariesQcPassed':" + overview.getAllLibrariesQcPassed());
        sb.append(",");
        sb.append("'allPoolsConstructed':" + overview.getAllPoolsConstructed());
        sb.append(",");
        sb.append("'allRunsCompleted':" + overview.getAllRunsCompleted());
        sb.append(",");
        sb.append("'primaryAnalysisCompleted':" + overview.getPrimaryAnalysisCompleted());
        sb.append("}");
        if (oi < p.getOverviews().size()) {
          sb.append(",");
        }
      }
    }
    sb.append("]");
    sb.append(",");

    sb.append("'samples':[");
    Collection<Sample> samples = requestManager.listAllSamplesByProjectId(projectId);
    if (samples.size() > 0) {
      int si = 0;
      for (Sample sample : samples) {
        si++;
        String sampleQubit = "not available";
        if (requestManager.listAllSampleQCsBySampleId(sample.getId()).size() > 0) {
          ArrayList<SampleQC> sampleQcList =
              new ArrayList(requestManager.listAllSampleQCsBySampleId(sample.getId()));
          SampleQC lastQc = sampleQcList.get(sampleQcList.size() - 1);
          sampleQubit = (lastQc.getResults() != null ? lastQc.getResults().toString() : "");
        }
        sb.append("{");
        sb.append("'alias':'" + sample.getAlias() + "'");
        sb.append(",");
        sb.append(
            "'qcPassed':'"
                + (sample.getQcPassed() != null ? sample.getQcPassed().toString() : "")
                + "'");
        sb.append(",");

        sb.append(
            "'receivedDate':'"
                + (sample.getReceivedDate() != null
                    ? LimsUtils.getDateAsString(sample.getReceivedDate())
                    : "not available")
                + "'");
        sb.append(",");
        sb.append(
            "'sampleType':'"
                + (sample.getSampleType() != null ? sample.getSampleType() : "")
                + "'");
        sb.append(",");
        sb.append("'sampleQubit':'" + sampleQubit + "'");
        sb.append("}");

        if (si < samples.size()) {
          sb.append(",");
        }
      }
    }
    sb.append("]");
    sb.append(",");

    sb.append("'runs':[");
    Collection<Run> runs = requestManager.listAllRunsByProjectId(projectId);
    if (runs.size() > 0) {
      int ri = 0;
      for (Run run : runs) {
        ri++;
        if (!run.getStatus().getHealth().getKey().equals("Failed")) {
          ArrayList<String> runSamples = new ArrayList();
          Collection<SequencerPartitionContainer<SequencerPoolPartition>> spcs =
              requestManager.listSequencerPartitionContainersByRunId(run.getId());
          if (spcs.size() > 0) {
            for (SequencerPartitionContainer<SequencerPoolPartition> spc : spcs) {

              if (spc.getPartitions().size() > 0) {
                for (SequencerPoolPartition spp : spc.getPartitions()) {
                  if (spp.getPool() != null) {
                    if (spp.getPool().getDilutions().size() > 0) {
                      for (Dilution dilution : spp.getPool().getDilutions()) {
                        Sample sample = dilution.getLibrary().getSample();
                        if (sample.getProject().equals(p)) {
                          runSamples.add(sample.getAlias());
                        }
                      }
                    }
                  }
                }
              }
            }
          }

          sb.append("{");
          sb.append("'name':'" + run.getName() + "'");
          sb.append(",");
          sb.append(
              "'status':'"
                  + (run.getStatus() != null && run.getStatus().getHealth() != null
                      ? run.getStatus().getHealth().getKey()
                      : "")
                  + "'");
          sb.append(",");
          sb.append(
              "'startDate':'"
                  + (run.getStatus() != null && run.getStatus().getStartDate() != null
                      ? run.getStatus().getStartDate().toString()
                      : "")
                  + "'");
          sb.append(",");
          sb.append(
              "'completionDate':'"
                  + (run.getStatus() != null && run.getStatus().getCompletionDate() != null
                      ? run.getStatus().getCompletionDate().toString()
                      : "")
                  + "'");
          sb.append(",");
          sb.append(
              "'platformType':'"
                  + (run.getPlatformType() != null ? run.getPlatformType().getKey() : "")
                  + "'");
          sb.append(",");
          sb.append("'samples':[");
          if (runSamples.size() > 0) {
            int rsi = 0;
            for (String alias : runSamples) {
              rsi++;
              sb.append("{'sampleAlias':'" + alias + "'}");
              if (rsi < runSamples.size()) {
                sb.append(",");
              }
            }
          }
          sb.append("]");
          sb.append("}");
          if (ri < runs.size()) {
            sb.append(",");
          }
        }
      }
    }
    sb.append("]");

    return "{" + sb.toString() + "}";
  }