Changeset 1286


Ignore:
Timestamp:
Feb 8, 2011, 2:51:55 PM (11 years ago)
Author:
Nicklas Nordborg
Message:

References #291: Personal information registration

If one is careful it is now possible to register new patients and cases. Error handling is not fully working and may result in corrupted data. Several use cases are still not supported.

Location:
extensions/net.sf.basedb.reggie/trunk
Files:
2 edited
1 moved

Legend:

Unmodified
Added
Removed
  • extensions/net.sf.basedb.reggie/trunk/META-INF/servlets.xml

    r1284 r1286  
    22<servlets xmlns="http://base.thep.lu.se/servlets.xsd">
    33  <servlet>
    4     <servlet-name>Ajax</servlet-name>
    5     <servlet-class>net.sf.basedb.reggie.servlet.AjaxServlet</servlet-class>
     4    <servlet-name>PersonalRegistration</servlet-name>
     5    <servlet-class>net.sf.basedb.reggie.servlet.PersonalRegistrationServlet</servlet-class>
    66  </servlet>
    77  <servlet>
  • extensions/net.sf.basedb.reggie/trunk/resources/persinfo.jsp

    r1283 r1286  
    3030var caseIsValid = false;
    3131
     32var patientInfo = null;
     33var caseInfo = null;
     34
    3235function goNext()
    3336{
     
    3538  {
    3639    if (pnrIsValid && caseIsValid) gotoStep2();
     40  }
     41  else if (currentStep == 2)
     42  {
     43    gotoStep3();
    3744  }
    3845}
     
    103110 
    104111  var pnr = frm.personalNumber.value;
     112  var caseName = frm.caseName.value;
    105113  var request = Ajax.getXmlHttpRequest();
    106   var url = 'Ajax.servlet?ID=<%=ID%>&cmd=FindPatientByPersonalNumber&personalNumber=' + pnr;
     114  var url = 'PersonalRegistration.servlet?ID=<%=ID%>&cmd=CheckPersonalNumberAndCaseName';
     115  url += '&personalNumber=' + pnr;
     116  url += '&caseName=' + caseName;
    107117  request.open("GET", url, false);
    108118  request.send(null);
     
    118128 
    119129  // Get biosource information from the AJAX response
    120   var patientInfo = response.patient;
    121  
    122   if (!patientInfo)
     130  patientInfo = response.patientInfo;
     131  caseInfo = response.caseInfo;
     132 
     133  if (!patientInfo.id)
    123134  {
    124135    Main.show('newPatientSection');
    125     frm.patientCode.value=response.nextPatientCode;
    126     setInnerHTML('new.dateOfBirth', pnr.substr(0,4) + '-' + pnr.substr(4,2)+'-'+pnr.substr(6,2));
    127     setInnerHTML('new.gender', parseInt(pnr.substr(10,1)) % 2 == 0 ? 'F' : 'M');
     136    frm.patientCode.value=patientInfo.name;
     137    setInnerHTML('new.dateOfBirth', patientInfo.dateOfBirth);
     138    setInnerHTML('new.gender', patientInfo.gender);
    128139    frm.familyName.focus();
    129140  }
     
    139150  }
    140151}
     152
     153function gotoStep3()
     154{
     155  // Check entered case and pnr with AJAX
     156  var frm = document.forms['reggie'];
     157  if (frm.familyName)
     158  {
     159    // Check form
     160  }
     161 
     162  currentStep = 3;
     163  if (!patientInfo.id)
     164  {
     165    frm.patientCode.disabled = true;
     166    frm.familyName.disabled = true;
     167    frm.allFirstNames.disabled = true;
     168    patientInfo.familyName = frm.familyName.value;
     169    patientInfo.allFirstNames = frm.allFirstNames.value;
     170  }
     171 
     172  if (!patientInfo.cases || patientInfo.cases.length == 0)
     173  {
     174    Main.show('newCaseSection'); 
     175  }
     176 
     177  if (!caseInfo.specimen || caseInfo.specimen.length == 0)
     178  {
     179    Main.show('reasonIfNoSpecimenSection');
     180  }
     181 
     182  Main.hide('gonext');
     183  Main.show('gocreate');
     184}
     185
     186function goCreate()
     187{
     188  var frm = document.forms['reggie'];
     189
     190  caseInfo.laterality = Forms.getCheckedRadio(frm.laterality).value;
     191  caseInfo.reasonIfNoSpecimen = frm.reasonIfNoSpecimen.value;
     192 
     193  var submitInfo = new Object();
     194  submitInfo.patientInfo = patientInfo;
     195  submitInfo.caseInfo = caseInfo;
     196  alert(JSON.stringify(submitInfo));
     197
     198  var request = Ajax.getXmlHttpRequest();
     199  var url = 'PersonalRegistration.servlet?ID=<%=ID%>&cmd=Create';
     200  request.open("POST", url, false);
     201  request.setRequestHeader("Content-Type", "application/json");
     202  request.send(JSON.stringify(submitInfo));
     203
     204  setInnerHTML('debug', request.responseText);
     205}
     206
    141207
    142208function setInnerHTML(id, html)
     
    302368      <table border="0" cellspacing="0" cellpadding="0" width="100%">
    303369      <tr>
    304         <td class="prompt">Patient cumber</td>
     370        <td class="prompt">Patient code</td>
    305371        <td class="input"><input type="text" name="patientCode" value="" size="12" maxlength="12"></td>
    306372        <td class="status"></td>
     
    386452  </div>
    387453
     454
     455  <!-- 3. New case registration -->
     456  <div id="newCaseSection" style="display: none;">
     457  <p>
     458  <table border="0" cellspacing="0" cellpadding="0" class="stepform">
     459  <tr>
     460    <td rowspan="2" class="stepno">3</td>
     461    <td class="steptitle">New case: Enter information</td>
     462  </tr>
     463  <tr>
     464    <td class="stepfields">
     465      <table border="0" cellspacing="0" cellpadding="0" width="100%">
     466      <tr>
     467        <td class="prompt">Laterality</td>
     468        <td class="input">
     469          <input type="radio" name="laterality" value="LEFT">LEFT
     470          <input type="radio" name="laterality" value="RIGHT">RIGHT
     471        </td>
     472        <td class="status"></td>
     473        <td class="help"></td>
     474      </tr>
     475      <tr id="reasonIfNoSpecimenSection" style="display: none;">
     476        <td class="prompt">Reason if no specimen</td>
     477        <td class="input"><input type="text" name="reasonIfNoSpecimen" value="" size="30" maxlength="200"></td>
     478        <td class="status"></td>
     479        <td class="help"></td>
     480      </tr>
     481      </table>
     482    </td>
     483  </tr>
     484  </table>
     485  </div>
     486
    388487  <div class="error" id="errorMessage" style="display: none; width: 800px;">
    389488  </div>
     
    391490  <table>
    392491    <tr><td><base:button id="gonext" title="Next" image="gonext.gif" onclick="goNext()"/></td></tr>
    393   </table> 
    394 
     492    <tr><td><base:button id="gocreate" title="Create" image="gonext.gif" onclick="goCreate()" style="display: none;"/></td></tr>
     493  </table>
    395494  </form>
    396495
  • extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/servlet/PersonalRegistrationServlet.java

    r1284 r1286  
    22
    33import java.io.IOException;
     4import java.util.ArrayList;
    45import java.util.Date;
    56import java.util.List;
     
    1213import org.json.simple.JSONArray;
    1314import org.json.simple.JSONObject;
     15import org.json.simple.parser.JSONParser;
    1416
    1517import net.sf.basedb.clients.web.Base;
     18import net.sf.basedb.core.Annotation;
     19import net.sf.basedb.core.AnnotationSet;
    1620import net.sf.basedb.core.AnnotationType;
    1721import net.sf.basedb.core.Application;
     
    2428import net.sf.basedb.core.Sample;
    2529import net.sf.basedb.core.SessionControl;
     30import net.sf.basedb.core.Type;
    2631import net.sf.basedb.core.query.Annotations;
    2732import net.sf.basedb.core.query.Expressions;
     
    3742
    3843
    39 public class AjaxServlet
     44public class PersonalRegistrationServlet
    4045  extends HttpServlet
    4146{
    4247
    43   public AjaxServlet()
     48  public PersonalRegistrationServlet()
    4449  {}
    4550
     
    6267    try
    6368    {
    64       if ("FindPatientByPersonalNumber".equals(cmd))
     69      if ("CheckPersonalNumberAndCaseName".equals(cmd))
    6570      {
    6671        dc = sc.newDbControl();
     
    6873       
    6974        String pnr = req.getParameter("personalNumber");
    70        
    71         ItemQuery<BioSource> query = BioSource.getQuery();
    72         query.restrict(Annotations.eq(null, pnrType, pnr, false));
    73         List<BioSource> result = query.list(dc);
    74        
    75         if (result.size() > 1)
     75        String caseName = req.getParameter("caseName");
     76       
     77        // Find patient information given the personal number
     78        ItemQuery<BioSource> patientQuery = BioSource.getQuery();
     79        patientQuery.restrict(Annotations.eq(null, pnrType, pnr, false));
     80        List<BioSource> patients = patientQuery.list(dc);
     81        JSONObject jsonPat = new JSONObject();
     82        jsonPat.put("personalNumber", pnr);
     83       
     84        if (patients.size() > 1)
    7685        {
    7786          throw new InvalidDataException(
     
    8190        }
    8291       
    83         if (result.size() == 1)
     92        if (patients.size() == 1)
    8493        {
    8594          AnnotationType familyNameType = Reggie.findAnnotationType(dc, Item.BIOSOURCE, Reggie.ANNOTATION_FAMILY_NAME, true);
     
    8998          AnnotationType lateralityType = Reggie.findAnnotationType(dc, Item.SAMPLE, Reggie.ANNOTATION_LATERALITY, true);
    9099          DateFormatter df = new DateFormatter("yyyy-MM-dd");
    91           BioSource bs = result.get(0);
    92           JSONObject jsonPat = new JSONObject();
     100          BioSource bs = patients.get(0);
    93101          jsonPat.put("id", bs.getId());
    94102          jsonPat.put("name", bs.getName());
     
    105113          {
    106114            JSONObject jsonCase = new JSONObject();
     115            jsonCase.put("id", s.getId());
    107116            jsonCase.put("name", s.getName());
    108117            jsonCase.put("laterality",  Reggie.getAnnotationValue(s, lateralityType));
     
    110119          }
    111120          jsonPat.put("cases", jsonCases);
    112 
    113           json.put("patient", jsonPat);
    114121        }
    115122        else
    116123        {
    117124          // Try to find the highest existing patient number
    118           query = BioSource.getQuery();
    119           query.restrict(Restrictions.like(Hql.property("name"), Expressions.string("PAT%")));
    120           query.order(Orders.desc(Hql.property("name")));
    121           query.setMaxResults(1);
    122           query.include(Include.ALL);
    123           result = query.list(dc);
    124          
    125           if (result.size() == 0)
     125          patientQuery = BioSource.getQuery();
     126          patientQuery.restrict(Restrictions.like(Hql.property("name"), Expressions.string("PAT%")));
     127          patientQuery.order(Orders.desc(Hql.property("name")));
     128          patientQuery.setMaxResults(1);
     129          patientQuery.include(Include.ALL);
     130          patients = patientQuery.list(dc);
     131         
     132          if (patients.size() == 0)
    126133          {
    127             json.put("nextPatientNumber", 1);
     134            jsonPat.put("name", "PAT000001");
    128135          }
    129136          else
    130137          {
    131             String patientName = result.get(0).getName().replace("PAT", "");
     138            String patientName = patients.get(0).getName().replace("PAT", "");
    132139            int nextPatientNumber = Integer.parseInt(patientName) + 1;
    133             json.put("nextPatientCode", "PAT" + MD5.leftPad(Integer.toString(nextPatientNumber), '0', 6));
     140            jsonPat.put("name", "PAT" + MD5.leftPad(Integer.toString(nextPatientNumber), '0', 6));
    134141          }
    135         }
     142          jsonPat.put("dateOfBirth", pnr.substring(0,4) + '-' + pnr.substring(4,6)+'-'+pnr.substring(6,8));
     143          jsonPat.put("gender", Integer.parseInt(pnr.substring(10,11)) % 2 == 0 ? "F" : "M");
     144        }
     145       
     146        // Check for existing case
     147        ItemQuery<Sample> caseQuery = Sample.getQuery();
     148        caseQuery.restrict(Restrictions.eq(Hql.property("name"), Expressions.parameter("name", caseName, Type.STRING)));
     149        List<Sample> cases = caseQuery.list(dc);
     150        JSONObject jsonCase = new JSONObject();
     151        if (cases.size() >= 1)
     152        {
     153          throw new InvalidDataException(
     154            "At least one case with the given case name already exists. " +
     155            "This wizard can't be used for registration until that is corrected.");
     156        }
     157        jsonCase.put("name", caseName);
     158       
     159        // Find specimen information
     160        ItemQuery<Sample> specimenQuery = Sample.getQuery();
     161        specimenQuery.restrict(Restrictions.like(Hql.property("name"), Expressions.parameter("name", caseName + ".%", Type.STRING)));
     162
     163        JSONArray jsonSpecimens = new JSONArray();
     164        for (Sample specimen : specimenQuery.list(dc))
     165        {
     166          JSONObject jsonSpecimen = new JSONObject();
     167          jsonSpecimen.put("id", specimen.getId());
     168          jsonSpecimen.put("name", specimen.getName());
     169          jsonSpecimens.add(jsonSpecimen);
     170        }
     171        jsonCase.put("specimen", jsonSpecimens);
     172       
     173        json.put("patientInfo", jsonPat);
     174        json.put("caseInfo", jsonCase);
    136175      }
    137      
    138      
    139176    }
    140177    catch (Throwable t)
     
    154191   
    155192  }
     193
     194  @SuppressWarnings("unchecked")
     195  @Override
     196  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
     197    throws ServletException, IOException
     198  {
     199    String ID = req.getParameter("ID");
     200    String cmd = req.getParameter("cmd");
     201    resp.setContentType("application/json");
     202    resp.setCharacterEncoding("UTF-8");
     203   
     204    JSONObject json = new JSONObject();
     205    json.put("status", "ok");
     206 
     207    final SessionControl sc = Application.getSessionControl(ID, req.getRemoteAddr());
     208    DbControl dc = null;
     209    try
     210    {
     211      if ("Create".equals(cmd))
     212      {
     213        JSONObject jsonReq = (JSONObject)new JSONParser().parse(req.getReader());
     214       
     215        JSONObject jsonPat = (JSONObject)jsonReq.get("patientInfo");
     216        JSONObject jsonCase = (JSONObject)jsonReq.get("caseInfo");
     217       
     218        dc = sc.newDbControl();
     219        Number patientId = (Number)jsonPat.get("id");
     220        BioSource patient = null;
     221       
     222        if (patientId != null)
     223        {
     224          patient = BioSource.getById(dc, patientId.intValue());
     225        }
     226        else
     227        {
     228          // Create new patient
     229          patient = BioSource.getNew(dc);
     230          patient.setName((String)jsonPat.get("name"));
     231          String pnr = (String)jsonPat.get("personalNumber");
     232         
     233          AnnotationType personalNumberType = Reggie.findAnnotationType(dc, Item.BIOSOURCE, Reggie.ANNOTATION_PERSONAL_NUMBER, true);
     234          AnnotationType familyNameType = Reggie.findAnnotationType(dc, Item.BIOSOURCE, Reggie.ANNOTATION_FAMILY_NAME, true);
     235          AnnotationType allFirstNamesType = Reggie.findAnnotationType(dc, Item.BIOSOURCE, Reggie.ANNOTATION_ALL_FIRST_NAMES, true);
     236          AnnotationType genderType = Reggie.findAnnotationType(dc, Item.BIOSOURCE, Reggie.ANNOTATION_GENDER, true);
     237          AnnotationType dateOfBirthType = Reggie.findAnnotationType(dc, Item.BIOSOURCE, Reggie.ANNOTATION_DATE_OF_BIRTH, true);
     238         
     239          AnnotationSet as = patient.getAnnotationSet();
     240          as.getAnnotation(personalNumberType).setValue(pnr);
     241          as.getAnnotation(familyNameType).setValue((String)jsonPat.get("familyName"));
     242          as.getAnnotation(allFirstNamesType).setValue((String)jsonPat.get("allFirstNames"));
     243          as.getAnnotation(genderType).setValue(Integer.parseInt(pnr.substring(10,11)) % 2 == 0 ? "F" : "M");
     244          DateFormatter df = new DateFormatter("yyyyMMdd");
     245          as.getAnnotation(dateOfBirthType).setValue(df.parseString(pnr.substring(0, 8)));
     246          dc.saveItem(patient);
     247        }
     248       
     249        // Create new case
     250        Sample cse = Sample.getNew(dc);
     251        cse.setName((String)jsonCase.get("name"));
     252        AnnotationType lateralityType = Reggie.findAnnotationType(dc, Item.SAMPLE, Reggie.ANNOTATION_LATERALITY, true);
     253        AnnotationSet as = cse.getAnnotationSet();
     254        as.getAnnotation(lateralityType).setValue((String)jsonCase.get("laterality"));
     255        cse.setBioSource(patient);
     256        dc.saveItem(cse);
     257       
     258        // Link the case with specimen
     259        JSONArray specimen = (JSONArray)jsonCase.get("specimen");
     260        if (specimen != null && specimen.size() > 0)
     261        {
     262          for (int i = 0; i < specimen.size(); ++i)
     263          {
     264            JSONObject jsonSpec = (JSONObject)specimen.get(i);
     265            Sample s = Sample.getById(dc, ((Long)jsonSpec.get("id")).intValue());
     266            s.setPooled(true);
     267            s.getCreationEvent().addSource(cse, null);
     268          }
     269        }
     270        else
     271        {
     272          AnnotationType reasonIfNoSpecimenType = Reggie.findAnnotationType(dc, Item.SAMPLE, Reggie.ANNOTATION_REASON_IF_NO_SPECIMEN, true);
     273          Annotation reasonIfNoSpecimen = as.getAnnotation(reasonIfNoSpecimenType);
     274          List values = new ArrayList(reasonIfNoSpecimen.getValues());
     275          values.add("[" + cse.getName() + "] " + jsonCase.get("reasonIfNoSpecimen"));
     276          reasonIfNoSpecimen.setValues(values);
     277        }
     278       
     279        dc.commit();
     280       
     281      }
     282    }
     283    catch (Throwable t)
     284    {
     285      t.printStackTrace();
     286      json.clear();
     287      json.put("status", "error");
     288      json.put("message", t.getMessage());
     289      json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
     290    }
     291    finally
     292    {
     293      if (dc != null) dc.close();
     294      json.writeJSONString(resp.getWriter());
     295    }
     296   
     297  }
    156298 
    157299 
Note: See TracChangeset for help on using the changeset viewer.