@RequestMapping(value = "/grids/{id}")
  public String getGrid(@PathVariable("id") int id, 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);
    Grid tempGrid = null;
    String chart = "";
    try {
      tempGrid = this.gridService.getGridById(id);
      if (projects.contains(tempGrid.getProject())) {
        chart = createChart(tempGrid);
        model.addAttribute("grid", tempGrid);
        model.addAttribute("gridTreeString", chart);

      } else {
        model.addAttribute("error", "You cannot acces to this grid");
      }
    } catch (Exception e) {
      model.addAttribute("error", "Grid not found");
    }
    return "grids";
  }
 private String createChart(Grid g) {
   if (g.getMainGoals().size() != 0) {
     List<Object> stack = new ArrayList<Object>();
     stack.addAll(g.getMainGoals());
     String chart =
         "chart_config = {chart: { connectors: {type: \"bCurve\",style: {\"stroke-width\": 2}}, container: \"#gridChart\", siblingSeparation:70, rootOrientation:'WEST',  subTeeSeparation:70, animateOnInit: true,node: {collapsable: true, HTMLclass: 'nodeExample1'},animation: {nodeAnimation: \"easeOutBounce\",nodeSpeed: 500,connectorsAnimation: \"bounce\",connectorsSpeed: 700}},";
     chart =
         chart
             + "nodeStructure: {HTMLclass: 'project',innerHTML:\"<div class='nodeTxt'><div class='txtProjectTitle'>"
             + g.getProject().getProjectId()
             + "</div><div class='txtElement'>"
             + g.getProject().getDescription()
             + "</div></div>\",children: [";
     chart = chart + updateChart(stack) + "]}};";
     return chart;
   } else {
     JSONObject jsonObject = new JSONObject();
     jsonObject.put("msg", "error");
     jsonObject.put("resp", "grid main goal error");
     return jsonObject.toString();
   }
 }
  @RequestMapping(value = "/grids/add", method = RequestMethod.POST)
  public @ResponseBody String addGrid(@RequestBody String jsonData) {
    Grid temp;
    try {
      temp = aFactory.loadFromJson(jsonData, this.projectService);
      if (temp == null) {
        logger.info("in blocco null");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", "error");
        jsonObject.put("msg", "Wrong format");
        return jsonObject.toString();
      }
      Grid latest = this.gridService.getLatestGrid(temp.getProject().getId());
      if (latest == null) {
        this.gridService.addGrid(temp);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", "success");
        jsonObject.put("msg", "Grid successfully uploaded");
        Firebase myFirebaseRef = new Firebase("https://fiery-torch-6050.firebaseio.com/");
        myFirebaseRef.child("ISSSR/" + temp.getProject().getProjectId()).setValue("timestamp");

        return jsonObject.toString();
      } else {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", "error");
        jsonObject.put("msg", "Already exists a gird for this project");
        return jsonObject.toString();
      }
    } catch (Exception e) {
      e.printStackTrace();
      JSONObject jsonObject = new JSONObject();
      jsonObject.put("type", "error");
      jsonObject.put("msg", "Generic exception");
      return jsonObject.toString();
    }
  }
  @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";
  }