public String processRequest()

in clns-eTarget/src/main/java/org/digitalecmt/etarget/GetSinglePatient.java [67:167]


  public String processRequest() {
    if(!super.isUserPermittedEndpoint(userID, "GetSinglePatient")) {
      // Stop the request if user doesn't have permission for this API or web component
    	Map<String,String> response = new HashMap<>();
    	response.put("success", "false");
    	response.put("error", "User not permitted to access: GetSinglePatient");
    	return new Gson().toJson(response);
    } else {
      // Process the request
      try {
        String sql = "SELECT * FROM dbo.PATIENTS WHERE person_id = "+personID;
        ResultSet rs = super.getData(sql);
        log.info("after ResutlSet " + Boolean.toString(rs==null));
        while(rs.next()) {
          // Get the data
          targetID = rs.getString("target_id");
          personAge = rs.getInt("age_at_consent");
          conditionName = rs.getString("condition_name");
          subtypeName = rs.getString("subtype_name");
          genderName = rs.getString("gender_name");
          site = rs.getString("care_site_name");
          consentDate = rs.getDate("consent_date");
          conditionStartDate = rs.getDate("condition_start_date");
          consultant = rs.getString("consultant_name");
          // Reformat the date
          DateFormat dfconsentDate = new SimpleDateFormat("dd/MM/yyyy");
          
          String consentDateFormatted ="n/a";
          if(consentDate!=null)
        	  consentDateFormatted = dfconsentDate.format(consentDate);

          DateFormat dfconditionStartDate = new SimpleDateFormat("dd/MM/yyyy");
          String conditionStartDateFormatted="n/a";
          if(conditionStartDate!=null) {
        	  conditionStartDateFormatted = dfconditionStartDate.format(conditionStartDate);
          }

          // Add to a list
          patient.put("personID", personID);
          patient.put("targetID", targetID);
          patient.put("personAge", Integer.toString(personAge));
          patient.put("conditionName", (subtypeName==null)?conditionName:subtypeName);
          patient.put("genderName", genderName);
          patient.put("site", site);
          patient.put("consentDate", consentDateFormatted);
          patient.put("conditionStartDate", conditionStartDateFormatted);
          patient.put("consultant", consultant);
          String trialreport="trial_report_"+targetID+".html";
          log.info("name " + trialreport);
          if(FileAccess.exists(trialreport)) {
        	  patient.put("trialFile",trialreport);
          } else {
        	  patient.put("trialFile","");
          }
          List<String> qciFiles=FileAccess.getAllFiles(targetID+"T", "QCI.pdf");
          Map<String,String> qciMap=new TreeMap<String,String>(new TimepointComparator());
          for(String qciFile : qciFiles) {
        	  String timepoint= qciFile.substring(targetID.length(), qciFile.indexOf("QCI.pdf"));
        	  if(Pattern.matches(targetID+"T\\d*QCI.pdf", qciFile)){
	        	  qciMap.put(timepoint, qciFile);
	        	  log.info(qciFile+" "+ timepoint);
        	  }
          }
          patient.put("qciFiles", qciMap);
          
          String rnareport_prefix=targetID+"T1SM";
          List<String> rnaFiles=FileAccess.getAllFiles(rnareport_prefix, ".pdf");
          patient.put("rnaFiles", rnaFiles);
        }
        
        //check edit permission
        EditLockDAO editLockDao = this.getContext().getBean(EditLockDAO.class);
        if(editLockDao.isEditor(userID)) {
        	try {
	        	if(editLockDao.isLocked(userID, Integer.parseInt(personID))) {
	        		patient.put("editLockMessage", "This patient is currently being edited by " + editLockDao.getLocker(Integer.parseInt(personID)));
	        	} else {
	        		patient.put("editLockMessage", "");
	        	}
        	} catch (NumberFormatException e) {
        		patient.put("editLockMessage", "");
        	}
        	
        } else {
        	patient.put("editLockMessage", "");
        }

        // Return patient data as JSON string
        String json = new Gson().toJson(patient);
        return json;

      } catch(Exception e) {
        e.printStackTrace();
        log.log(Level.SEVERE,e.getMessage(), e);
        Map<String,String> response = new HashMap<>();
    	response.put("success", "false");
    	response.put("error", "Failed to get patient data.");
    	return new Gson().toJson(response);
      }
    }
  }