@RequestMapping(value = "/MGResolution/{pid}/{gid}", method = RequestMethod.GET)
  public String mainGoalResolution(
      @PathVariable("gid") int gid, @PathVariable("pid") int pid, Model model) {
    model.addAttribute("pageTitle", "Lista Grids");
    this.setActiveButton(2, model);
    Grid working = this.gridService.getLatestWorkingGrid(pid);
    Grid current = this.gridService.getGridById(gid);
    List<String> mergedGoalLabels = new ArrayList<String>();
    if (working == null) {
      model.addAttribute("error", "The working Grid is not available");
    } else if (current == null) {
      model.addAttribute("error", "The requested Grid is not available");
    } else if ((!current.isMainGoalsChanged())) {
      model.addAttribute("error", "The requested Grid is not in pending state");
    } else {
      String workingMGList = "['" + pid + "', '" + current.getId() + "', ";
      List<Goal> temp = working.getMainGoals();
      for (int i = 0; i < temp.size(); i++) {
        if (!mergedGoalLabels.contains(temp.get(i).getLabel())) {
          mergedGoalLabels.add(temp.get(i).getLabel());
        }
        if (i < temp.size() - 1) {
          workingMGList = workingMGList + "'" + temp.get(i).getLabel() + "',";
        } else {
          workingMGList = workingMGList + "'" + temp.get(i).getLabel() + "'";
        }
      }
      workingMGList = workingMGList + "]";

      String currentMGList = "['" + pid + "', '" + current.getId() + "', ";
      temp = current.getMainGoals();
      for (int i = 0; i < temp.size(); i++) {
        if (!mergedGoalLabels.contains(temp.get(i).getLabel())) {
          mergedGoalLabels.add(temp.get(i).getLabel());
        }
        if (i < temp.size() - 1) {
          currentMGList = currentMGList + "'" + temp.get(i).getLabel() + "',";
        } else {
          currentMGList = currentMGList + "'" + temp.get(i).getLabel() + "'";
        }
      }
      currentMGList = currentMGList + "]";
      model.addAttribute("mergedGoalLabels", mergedGoalLabels);
      model.addAttribute("workingGrid", working);
      model.addAttribute("currentGrid", current);
      model.addAttribute("workingMGList", workingMGList);
      model.addAttribute("currentMGList", currentMGList);
      model.addAttribute("GEService", this.gridElementService);
    }
    return "MGResolution";
  }
  @RequestMapping(value = "/projects/{id}")
  public String getProject(@PathVariable("id") int id, Model model) {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    String email = auth.getName(); // get logged in username
    Practitioner p = this.practitionerService.getPractitionerByEmail(email);
    Project temp = null;
    try {
      temp = this.projectService.getProjectById(id);
      List<Practitioner> practs = this.gridService.getInvolvedPractitioners(id, true);
      if (practs.contains(p)) {
        model.addAttribute("reqproject", temp);
        List<Grid> templist = this.gridService.getGridLog(id);
        model.addAttribute("nProjectGrids", templist.size());
        model.addAttribute("listProjectGrids", templist);
        Map<String, String> status = new HashMap<String, String>();
        for (Grid g : templist) {
          Grid tempWorking = this.gridService.getLatestWorkingGrid(g.getProject().getId());
          String state = "";
          if (g.isMainGoalsChanged()) {
            state = state + "MGC";
            if (g.obtainGridState() == Grid.GridState.UPDATING) {
              state = state + "-UPDATING";
            }
          } else {
            if (g.obtainGridState() == Grid.GridState.WORKING) {
              if (g.getVersion() < tempWorking.getVersion()) {
                state = state + "ARCHIVED";
              } else {
                state = state + g.obtainGridState().name();
              }
            } else {
              state = state + g.obtainGridState().name();
            }
          }
          status.put(g.getId() + "", state);
        }
        model.addAttribute("status", status);
      } else {
        model.addAttribute("error", "You cannot access to this project");
      }
    } catch (Exception e) {
      model.addAttribute("error", "The requested project is not available");
    }

    return "projects";
  }
  @RequestMapping(value = "/grids", method = RequestMethod.GET)
  public String listAllGrids(Model model) {
    model.addAttribute("pageTitle", "Lista Grids");
    this.setActiveButton(1, model);
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    String email = auth.getName(); // get logged in username
    Practitioner p = this.practitionerService.getPractitionerByEmail(email);
    List<Project> projects = this.practitionerService.getProjectsForPractitioner(p);
    List<Grid> temp = new ArrayList<Grid>();
    Map<String, String> status = new HashMap<String, String>();
    for (Project current : projects) {
      List<Grid> temp2 = this.gridService.getGridLog(current.getId());
      for (Grid g : temp2) {
        Grid tempWorking = this.gridService.getLatestWorkingGrid(g.getProject().getId());
        String state = "";
        if (g.isMainGoalsChanged()) {
          state = state + "MGC";
          if (g.obtainGridState() == Grid.GridState.UPDATING) {
            state = state + "-UPDATING";
          }
        } else {
          if (g.obtainGridState() == Grid.GridState.WORKING) {
            if (g.getVersion() < tempWorking.getVersion()) {
              state = state + "ARCHIVED";
            } else {
              state = state + g.obtainGridState().name();
            }
          } else {
            state = state + g.obtainGridState().name();
          }
        }

        status.put(g.getId() + "", state);
      }
      temp.addAll(temp2);
    }
    if (temp.size() > 0) {
      model.addAttribute("listGrids", temp);
      model.addAttribute("status", status);

    } else {
      model.addAttribute("error", "No grids available");
    }
    return "grids";
  }
  @RequestMapping(
      value = "/resolutionDashBoard",
      method =
          RequestMethod
              .GET) // stati WORKING, MAJOR_UPDATING,MAJOR_CONFLICTING, MINOR_CONFLICTING, FINAL_KO
  public String resolutionDashBoardView(Model model) {
    model.addAttribute("pageTitle", "Grids Versioning System");
    this.setActiveButton(2, model);
    List<String> pendingLabels = new ArrayList<String>();
    int nPendingChanges = 0;
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    String email = auth.getName(); // get logged in username
    Practitioner p = this.practitionerService.getPractitionerByEmail(email);

    List<Project> projList = this.practitionerService.getProjectsForPractitioner(p);

    List<Project> projPending = new ArrayList<Project>();
    Map<String, List<Grid>> projectPendingGrids = new HashMap<>(); // map projid, list pending grids
    Map<String, List<GridElement>> projectGridsMajorPendingElements = new HashMap<>();
    Map<String, List<GridElement>> projectGridsMajorConflictElements = new HashMap<>();
    Map<String, List<GridElement>> projectGridsMinorConflictElements = new HashMap<>();
    Map<String, String> projectGridsMainGoalListChanged = new HashMap<>();
    if (projList.size() > 0) {
      for (int i = 0; i < projList.size(); i++) {
        List<Grid> projGrids = this.gridService.getGridLog(projList.get(i).getId());
        List<Grid> gridPending = new ArrayList<Grid>();
        boolean addedGrid = false;
        for (Grid g : projGrids) {
          if (g.isMainGoalsChanged()) {
            addedGrid = true;
            String temp = projList.get(i).getId() + "-" + g.getId();
            projectGridsMainGoalListChanged.put(temp, "changed");
            gridPending.add(g);
          }
          if (g.obtainGridState() == Grid.GridState.UPDATING) {
            addedGrid = true;
            HashMap<String, GridElement> elements = g.obtainAllEmbeddedElements();
            Set<String> keySet = elements.keySet();
            Iterator<String> anIterator = keySet.iterator();
            boolean addedMPElement = false;
            boolean addedMCElement = false;
            boolean addedmCElement = false;
            List<GridElement> majpendingElements = new ArrayList<GridElement>();
            List<GridElement> majconflElements = new ArrayList<GridElement>();
            List<GridElement> minconflElements = new ArrayList<GridElement>();
            while (anIterator.hasNext()) {
              String key = anIterator.next();
              State aState = elements.get(key).getState();
              if (aState == GridElement.State.MAJOR_CONFLICTING) {
                majconflElements.add(elements.get(key));
                addedMCElement = true;
              }
              if (aState == GridElement.State.MAJOR_UPDATING) {
                majpendingElements.add(elements.get(key));
                addedMPElement = true;
              }
              if (aState == GridElement.State.MINOR_CONFLICTING) {
                minconflElements.add(elements.get(key));
                addedmCElement = true;
              }
            }
            String temp = projList.get(i).getId() + "-" + g.getId();
            if (addedMCElement || addedMPElement || addedmCElement) {
              addedGrid = true;
              if (!g.isMainGoalsChanged()) gridPending.add(g);
            }
            if (addedMCElement) {
              projectGridsMajorConflictElements.put(temp, majconflElements);
            }
            if (addedMPElement) {
              projectGridsMajorPendingElements.put(temp, majpendingElements);
            }
            if (addedmCElement) {
              projectGridsMinorConflictElements.put(temp, minconflElements);
            }
          }
        }
        if (addedGrid) {
          projPending.add(projList.get(i));
          projectPendingGrids.put(projList.get(i).getId() + "", gridPending);
        }
      }
    }

    System.out.println(projectPendingGrids.toString());

    model.addAttribute("PendingProjects", projPending);
    model.addAttribute("PendingProjectsGrids", projectPendingGrids);
    model.addAttribute("MajorPendingProjectsGridsElements", projectGridsMajorPendingElements);
    model.addAttribute("MajorConflictProjectsGridsElements", projectGridsMajorConflictElements);
    model.addAttribute("MinorConflictProjectsGridsElements", projectGridsMinorConflictElements);
    model.addAttribute("GridsMainGoalListChanged", projectGridsMainGoalListChanged);
    model.addAttribute("GridModificationServiceInstance", this.gridModificationService);
    model.addAttribute("GridElementServiceInstance", this.gridElementService);
    model.addAttribute("DefaultResponsibleServiceInstance", this.defaultResponsibleService);
    model.addAttribute("Practitioner", p);
    return "resolutionDashBoard";
  }