@Override
  @PlusTransactional
  public ResponseEvent<DistributionProtocolDetail> updateDistributionProtocol(
      RequestEvent<DistributionProtocolDetail> req) {
    try {
      AccessCtrlMgr.getInstance().ensureUserIsAdmin();

      Long protocolId = req.getPayload().getId();
      String title = req.getPayload().getTitle();

      DistributionProtocol existing = null;
      if (protocolId != null) {
        existing = daoFactory.getDistributionProtocolDao().getById(protocolId);
      } else {
        existing = daoFactory.getDistributionProtocolDao().getDistributionProtocol(title);
      }

      if (existing == null) {
        return ResponseEvent.userError(DistributionProtocolErrorCode.NOT_FOUND);
      }

      DistributionProtocol distributionProtocol =
          distributionProtocolFactory.createDistributionProtocol(req.getPayload());
      ensureUniqueConstraints(distributionProtocol, existing);

      existing.update(distributionProtocol);
      daoFactory.getDistributionProtocolDao().saveOrUpdate(existing);
      return ResponseEvent.response(DistributionProtocolDetail.from(existing));
    } catch (OpenSpecimenException ose) {
      return ResponseEvent.error(ose);
    } catch (Exception ex) {
      return ResponseEvent.serverError(ex);
    }
  }
  @Override
  public SpecimenRequirement createDerived(DerivedSpecimenRequirement req) {
    OpenSpecimenException ose = new OpenSpecimenException(ErrorType.USER_ERROR);

    Long parentId = req.getParentSrId();
    if (parentId == null) {
      ose.addError(PARENT_REQ_REQUIRED);
      throw ose;
    }

    SpecimenRequirement parent = daoFactory.getSpecimenRequirementDao().getById(parentId);
    if (parent == null) {
      ose.addError(SrErrorCode.PARENT_NOT_FOUND);
      throw ose;
    }

    SpecimenRequirement derived = parent.copy();
    derived.setLabelFormat(null);
    derived.setLineage(Specimen.DERIVED);
    setSpecimenClass(req.getSpecimenClass(), derived, ose);
    setSpecimenType(req.getSpecimenClass(), req.getType(), derived, ose);
    setInitialQty(req.getQuantity(), derived, ose);
    setStorageType(req.getStorageType(), derived, ose);
    setConcentration(req.getConcentration(), derived, ose);
    setPathologyStatus(req.getPathology(), derived, ose);
    setLabelFormat(req.getLabelFmt(), derived, ose);
    setLabelAutoPrintMode(req.getLabelAutoPrintMode(), derived, ose);
    setCode(req.getCode(), derived, ose);
    derived.setName(req.getName());

    ose.checkAndThrow();
    derived.setParentSpecimenRequirement(parent);
    return derived;
  }
  @Override
  @PlusTransactional
  public ResponseEvent<List<DpRequirementDetail>> getRequirements(RequestEvent<Long> req) {
    try {
      AccessCtrlMgr.getInstance().ensureUserIsAdmin();
      Long dpId = req.getPayload();
      DistributionProtocol dp = daoFactory.getDistributionProtocolDao().getById(dpId);
      if (dp == null) {
        return ResponseEvent.userError(DistributionProtocolErrorCode.NOT_FOUND);
      }

      List<DpRequirementDetail> reqDetails = DpRequirementDetail.from(dp.getRequirements());
      Map<Long, BigDecimal> distributedQty = getDprDao().getDistributedQtyByDp(dpId);
      for (DpRequirementDetail reqDetail : reqDetails) {
        BigDecimal qty = distributedQty.get(reqDetail.getId());
        reqDetail.setDistributedQty(qty == null ? BigDecimal.ZERO : qty);
      }

      return ResponseEvent.response(reqDetails);
    } catch (OpenSpecimenException ose) {
      return ResponseEvent.error(ose);
    } catch (Exception e) {
      return ResponseEvent.serverError(e);
    }
  }
  @Override
  public List<SpecimenRequirement> createSpecimenPoolReqs(SpecimenPoolRequirements req) {
    OpenSpecimenException ose = new OpenSpecimenException(ErrorType.USER_ERROR);

    Long pooledSpmnReqId = req.getPooledSpecimenReqId();
    if (pooledSpmnReqId == null) {
      ose.addError(POOLED_SPMN_REQ);
      throw ose;
    }

    SpecimenRequirement pooledSpecimenReq =
        daoFactory.getSpecimenRequirementDao().getById(pooledSpmnReqId);
    if (pooledSpecimenReq == null) {
      ose.addError(POOLED_SPMN_REQ_NOT_FOUND, pooledSpmnReqId);
      throw ose;
    }

    if (pooledSpecimenReq.getParentSpecimenRequirement() != null
        || pooledSpecimenReq.getPooledSpecimenRequirement() != null) {
      ose.addError(INVALID_POOLED_SPMN, pooledSpmnReqId);
      throw ose;
    }

    List<SpecimenRequirement> specimenPoolReqs = new ArrayList<SpecimenRequirement>();
    for (SpecimenRequirementDetail detail : req.getSpecimenPoolReqs()) {
      specimenPoolReqs.add(createSpecimenPoolReq(pooledSpecimenReq, detail, ose));
      ose.checkAndThrow();
    }

    return specimenPoolReqs;
  }
  @Override
  @PlusTransactional
  public ResponseEvent<List<DistributionProtocolDetail>> getDistributionProtocols(
      RequestEvent<DpListCriteria> req) {
    try {
      DpListCriteria crit = req.getPayload();
      Set<Long> siteIds = AccessCtrlMgr.getInstance().getReadAccessDistributionOrderSites();
      if (siteIds != null && CollectionUtils.isEmpty(siteIds)) {
        return ResponseEvent.userError(RbacErrorCode.ACCESS_DENIED);
      }

      if (siteIds != null) {
        crit.siteIds(siteIds);
      }

      List<DistributionProtocol> dps =
          daoFactory.getDistributionProtocolDao().getDistributionProtocols(crit);
      List<DistributionProtocolDetail> result = DistributionProtocolDetail.from(dps);

      if (crit.includeStat()) {
        addDpStats(result);
      }

      return ResponseEvent.response(result);
    } catch (OpenSpecimenException ose) {
      return ResponseEvent.error(ose);
    } catch (Exception e) {
      return ResponseEvent.serverError(e);
    }
  }
 @Override
 @PlusTransactional
 public ResponseEvent<List<AuthDomainSummary>> getDomains(
     RequestEvent<ListAuthDomainCriteria> req) {
   List<AuthDomain> authDomains =
       daoFactory.getAuthDao().getAuthDomains(req.getPayload().maxResults());
   return ResponseEvent.response(AuthDomainSummary.from(authDomains));
 }
  private User ensureValidUser(
      UserSummary userSummary, ErrorCode notFound, OpenSpecimenException ose) {
    if (userSummary == null) {
      return null;
    }

    User user = null;
    if (userSummary.getId() != null) {
      user = daoFactory.getUserDao().getById(userSummary.getId());
    } else if (userSummary.getLoginName() != null && userSummary.getDomain() != null) {
      user = daoFactory.getUserDao().getUser(userSummary.getLoginName(), userSummary.getDomain());
    }

    if (user == null) {
      ose.addError(notFound);
    }

    return user;
  }
  private List<DistributionOrderStat> getOrderStats(DistributionOrderStatListCriteria crit) {
    if (crit.dpId() != null) {
      DistributionProtocol dp = daoFactory.getDistributionProtocolDao().getById(crit.dpId());
      if (dp == null) {
        throw OpenSpecimenException.userError(DistributionProtocolErrorCode.NOT_FOUND);
      }

      AccessCtrlMgr.getInstance().ensureReadDPRights(dp);
    } else {
      Set<Long> siteIds = AccessCtrlMgr.getInstance().getCreateUpdateAccessDistributionOrderSites();
      if (siteIds != null && CollectionUtils.isEmpty(siteIds)) {
        throw OpenSpecimenException.userError(RbacErrorCode.ACCESS_DENIED);
      }

      if (siteIds != null) {
        crit.siteIds(siteIds);
      }
    }

    return daoFactory.getDistributionProtocolDao().getOrderStats(crit);
  }
  @Override
  @PlusTransactional
  public ResponseEvent<DistributionProtocolDetail> deleteDistributionProtocol(
      RequestEvent<Long> req) {
    try {
      AccessCtrlMgr.getInstance().ensureUserIsAdmin();

      DistributionProtocol existing =
          daoFactory.getDistributionProtocolDao().getById(req.getPayload());
      if (existing == null) {
        return ResponseEvent.userError(DistributionProtocolErrorCode.NOT_FOUND);
      }

      existing.delete();
      daoFactory.getDistributionProtocolDao().saveOrUpdate(existing);
      return ResponseEvent.response(DistributionProtocolDetail.from(existing));
    } catch (OpenSpecimenException ose) {
      return ResponseEvent.error(ose);
    } catch (Exception e) {
      return ResponseEvent.serverError(e);
    }
  }
  private boolean isUniqueShortTitle(DistributionProtocol newDp, DistributionProtocol existingDp) {
    if (existingDp != null && newDp.getShortTitle().equals(existingDp.getShortTitle())) {
      return true;
    }

    DistributionProtocol existing =
        daoFactory.getDistributionProtocolDao().getByShortTitle(newDp.getShortTitle());
    if (existing != null) {
      return false;
    }

    return true;
  }
  private void setCpe(
      SpecimenRequirementDetail detail, SpecimenRequirement sr, OpenSpecimenException ose) {
    Long eventId = detail.getEventId();
    if (eventId == null) {
      ose.addError(CPE_REQUIRED);
      return;
    }

    CollectionProtocolEvent cpe = daoFactory.getCollectionProtocolDao().getCpe(eventId);
    if (cpe == null) {
      ose.addError(CpeErrorCode.NOT_FOUND);
    }

    sr.setCollectionProtocolEvent(cpe);
  }
  @Override
  @PlusTransactional
  public ResponseEvent<DistributionProtocolDetail> updateActivityStatus(
      RequestEvent<DistributionProtocolDetail> req) {
    try {
      AccessCtrlMgr.getInstance().ensureUserIsAdmin();

      Long dpId = req.getPayload().getId();
      String status = req.getPayload().getActivityStatus();
      if (StringUtils.isBlank(status) || !Status.isValidActivityStatus(status)) {
        return ResponseEvent.userError(ActivityStatusErrorCode.INVALID);
      }

      DistributionProtocol existing = daoFactory.getDistributionProtocolDao().getById(dpId);
      if (existing == null) {
        return ResponseEvent.userError(DistributionProtocolErrorCode.NOT_FOUND);
      }

      if (existing.getActivityStatus().equals(status)) {
        return ResponseEvent.response(DistributionProtocolDetail.from(existing));
      }

      if (status.equals(Status.ACTIVITY_STATUS_DISABLED.getStatus())) {
        existing.delete();
      } else {
        existing.setActivityStatus(status);
      }

      daoFactory.getDistributionProtocolDao().saveOrUpdate(existing);
      return ResponseEvent.response(DistributionProtocolDetail.from(existing));
    } catch (OpenSpecimenException ose) {
      return ResponseEvent.error(ose);
    } catch (Exception e) {
      return ResponseEvent.serverError(e);
    }
  }
  @Override
  @PlusTransactional
  public ResponseEvent<List<DependentEntityDetail>> getDependentEntities(RequestEvent<Long> req) {
    try {
      DistributionProtocol existing =
          daoFactory.getDistributionProtocolDao().getById(req.getPayload());
      if (existing == null) {
        return ResponseEvent.userError(DistributionProtocolErrorCode.NOT_FOUND);
      }

      return ResponseEvent.response(existing.getDependentEntities());
    } catch (Exception e) {
      return ResponseEvent.serverError(e);
    }
  }
  private void addDpStats(List<DistributionProtocolDetail> dps) {
    if (CollectionUtils.isEmpty(dps)) {
      return;
    }

    Map<Long, DistributionProtocolDetail> dpMap = new HashMap<Long, DistributionProtocolDetail>();
    for (DistributionProtocolDetail dp : dps) {
      dpMap.put(dp.getId(), dp);
    }

    Map<Long, Integer> countMap =
        daoFactory.getDistributionProtocolDao().getSpecimensCountByDpIds(dpMap.keySet());
    for (Map.Entry<Long, Integer> count : countMap.entrySet()) {
      dpMap.get(count.getKey()).setDistributedSpecimensCount(count.getValue());
    }
  }
  @Override
  @PlusTransactional
  public ResponseEvent<AuthDomainDetail> registerDomain(RequestEvent<AuthDomainDetail> req) {
    try {
      AuthDomainDetail detail = req.getPayload();
      AuthDomain authDomain = domainRegFactory.createDomain(detail);

      ensureUniqueDomainName(authDomain.getName());
      daoFactory.getAuthDao().saveOrUpdate(authDomain);
      return ResponseEvent.response(AuthDomainDetail.from(authDomain));
    } catch (OpenSpecimenException ose) {
      return ResponseEvent.error(ose);
    } catch (Exception e) {
      return ResponseEvent.serverError(e);
    }
  }
  @Override
  public List<SpecimenRequirement> createAliquots(AliquotSpecimensRequirement req) {
    OpenSpecimenException ose = new OpenSpecimenException(ErrorType.USER_ERROR);

    Long parentSrId = req.getParentSrId();
    if (parentSrId == null) {
      ose.addError(PARENT_REQ_REQUIRED);
      throw ose;
    }

    SpecimenRequirement parent = daoFactory.getSpecimenRequirementDao().getById(parentSrId);
    if (parent == null) {
      ose.addError(SrErrorCode.PARENT_NOT_FOUND);
      throw ose;
    }

    if (req.getNoOfAliquots() == null || req.getNoOfAliquots() < 1L) {
      ose.addError(SrErrorCode.INVALID_ALIQUOT_CNT);
      throw ose;
    }

    if (req.getQtyPerAliquot() == null || NumUtil.lessThanEqualsZero(req.getQtyPerAliquot())) {
      ose.addError(SrErrorCode.INVALID_QTY);
    }

    BigDecimal total = NumUtil.multiply(req.getQtyPerAliquot(), req.getNoOfAliquots());
    if (NumUtil.greaterThan(total, parent.getQtyAfterAliquotsUse())) {
      ose.addError(SrErrorCode.INSUFFICIENT_QTY);
    }

    List<SpecimenRequirement> aliquots = new ArrayList<SpecimenRequirement>();
    for (int i = 0; i < req.getNoOfAliquots(); ++i) {
      SpecimenRequirement aliquot = parent.copy();
      aliquot.setLabelFormat(null);
      aliquot.setLineage(Specimen.ALIQUOT);
      setStorageType(req.getStorageType(), aliquot, ose);
      setLabelFormat(req.getLabelFmt(), aliquot, ose);
      setLabelAutoPrintMode(req.getLabelAutoPrintMode(), aliquot, ose);
      aliquot.setInitialQuantity(req.getQtyPerAliquot());
      aliquot.setParentSpecimenRequirement(parent);
      aliquots.add(aliquot);

      ose.checkAndThrow();
    }

    return aliquots;
  }
  @Override
  @PlusTransactional
  public ResponseEvent<DistributionProtocolDetail> getDistributionProtocol(RequestEvent<Long> req) {
    try {
      Long protocolId = req.getPayload();
      DistributionProtocol existing = daoFactory.getDistributionProtocolDao().getById(protocolId);
      if (existing == null) {
        return ResponseEvent.userError(DistributionProtocolErrorCode.NOT_FOUND);
      }

      AccessCtrlMgr.getInstance().ensureReadDPRights(existing);
      return ResponseEvent.response(DistributionProtocolDetail.from(existing));
    } catch (OpenSpecimenException ose) {
      return ResponseEvent.error(ose);
    } catch (Exception e) {
      return ResponseEvent.serverError(e);
    }
  }
  @Override
  @PlusTransactional
  public ResponseEvent<DistributionProtocolDetail> createDistributionProtocol(
      RequestEvent<DistributionProtocolDetail> req) {
    try {
      AccessCtrlMgr.getInstance().ensureUserIsAdmin();

      DistributionProtocol dp =
          distributionProtocolFactory.createDistributionProtocol(req.getPayload());
      ensureUniqueConstraints(dp, null);

      daoFactory.getDistributionProtocolDao().saveOrUpdate(dp);
      return ResponseEvent.response(DistributionProtocolDetail.from(dp));
    } catch (OpenSpecimenException ose) {
      return ResponseEvent.error(ose);
    } catch (Exception e) {
      return ResponseEvent.serverError(e);
    }
  }
  @PlusTransactional
  public ResponseEvent<AuthDomainDetail> updateDomain(RequestEvent<AuthDomainDetail> req) {
    try {
      AuthDomainDetail detail = req.getPayload();
      AuthDomain existingDomain = daoFactory.getAuthDao().getAuthDomainByName(detail.getName());
      if (existingDomain == null) {
        throw OpenSpecimenException.userError(AuthProviderErrorCode.DOMAIN_NOT_FOUND);
      }

      AuthDomain authDomain = domainRegFactory.createDomain(detail);
      existingDomain.update(authDomain);

      return ResponseEvent.response(AuthDomainDetail.from(existingDomain));
    } catch (OpenSpecimenException ose) {
      return ResponseEvent.error(ose);
    } catch (Exception e) {
      return ResponseEvent.serverError(e);
    }
  }
Esempio n. 20
0
  @Override
  public String getLabel(Specimen specimen) {
    String ppid = specimen.getVisit().getRegistration().getPpid();

    while (specimen.isAliquot() || specimen.isDerivative()) {
      specimen = specimen.getParentSpecimen();
    }

    Calendar cal = Calendar.getInstance();
    SpecimenCollectionEvent collEvent = specimen.getCollectionEvent();
    if (collEvent != null) {
      cal.setTime(collEvent.getTime());
    } else if (specimen.getCreatedOn() != null) {
      cal.setTime(specimen.getCreatedOn());
    }

    int yoc = cal.get(Calendar.YEAR);
    String key = ppid + "_" + yoc;
    Long uniqueId = daoFactory.getUniqueIdGenerator().getUniqueId(name, key);
    return uniqueId.toString();
  }
Esempio n. 21
0
  private String deIdentifyParticipantName(String report, Map<String, Object> contextMap) {
    Long visitId = (Long) contextMap.get("visitId");
    Visit visit = daoFactory.getVisitsDao().getById(visitId);
    Participant participant = visit.getRegistration().getParticipant();

    StringBuilder regex = new StringBuilder();
    String lastName = participant.getLastName();
    if (StringUtils.isNotBlank(lastName)) {
      regex.append(" ").append(lastName).append(" ");
    }

    String firstName = participant.getFirstName();
    if (StringUtils.isNotBlank(firstName)) {
      addOr(regex);
      regex.append(" ").append(firstName).append(" ");
    }

    if (regex.length() > 0) {
      regex.insert(0, "(?i)\\b(");
      regex.append(")\\b");
      report = report.replaceAll(regex.toString(), REPLACEMENT_STRING);
    }
    return report;
  }
 @Override
 public String getNextTrid() {
   return TRID_PREFIX + daoFactory.getUniqueIdGenerator().getUniqueId("TRID", "SGH");
 }
  @SuppressWarnings("unchecked")
  @Override
  public ResponseEvent<Map<String, Object>> importObject(
      RequestEvent<ImportObjectDetail<Map<String, Object>>> req) {
    try {
      ImportObjectDetail<Map<String, Object>> importDetail = req.getPayload();
      Map<String, Object> params = importDetail.getParams();

      Map<String, Object> extnObj = importDetail.getObject();
      String recordId = (String) extnObj.get("recordId");
      if (importDetail.isCreate() && StringUtils.isNotBlank(recordId)) {
        return ResponseEvent.userError(FormErrorCode.REC_ID_SPECIFIED_FOR_CREATE);
      } else if (!importDetail.isCreate() && StringUtils.isBlank(recordId)) {
        return ResponseEvent.userError(FormErrorCode.REC_ID_REQUIRED);
      }

      String entityType = (String) params.get("entityType");
      Long objectId = null;
      CollectionProtocol cp = null;

      if (entityType.equals("Participant")) {
        String ppid = (String) extnObj.get("ppid");
        String cpShortTitle = (String) extnObj.get("cpShortTitle");
        CollectionProtocolRegistration cpr =
            daoFactory.getCprDao().getCprByCpShortTitleAndPpid(cpShortTitle, ppid);
        if (cpr == null) {
          return ResponseEvent.userError(CprErrorCode.NOT_FOUND);
        }

        objectId = cpr.getId();
        cp = cpr.getCollectionProtocol();
      } else if (entityType.equals("SpecimenCollectionGroup")) {
        String visitName = (String) extnObj.get("visitName");
        Visit visit = daoFactory.getVisitsDao().getByName(visitName);
        if (visit == null) {
          return ResponseEvent.userError(VisitErrorCode.NOT_FOUND);
        }

        objectId = visit.getId();
        cp = visit.getCollectionProtocol();
      } else if (entityType.equals("Specimen") || entityType.equals("SpecimenEvent")) {
        String label = (String) extnObj.get("specimenLabel");
        Specimen specimen = daoFactory.getSpecimenDao().getByLabel(label);
        if (specimen == null) {
          return ResponseEvent.userError(SpecimenErrorCode.NOT_FOUND, label);
        }

        objectId = specimen.getId();
        cp = specimen.getCollectionProtocol();
      }

      String formName = (String) params.get("formName");
      Container form = Container.getContainer(formName);
      if (form == null) {
        return ResponseEvent.userError(FormErrorCode.NOT_FOUND);
      }

      Long formCtxId = formDao.getFormCtxtId(form.getId(), entityType, cp.getId());
      if (formCtxId == null) {
        return ResponseEvent.userError(
            FormErrorCode.NO_ASSOCIATION, cp.getShortTitle(), form.getCaption());
      }

      Map<String, Object> appData = new HashMap<String, Object>();
      appData.put("formCtxtId", formCtxId);
      appData.put("objectId", objectId);

      Map<String, Object> formValueMap = (Map<String, Object>) extnObj.get("formValueMap");
      formValueMap.put("appData", appData);

      if (StringUtils.isNotBlank(recordId)) {
        formValueMap.put("id", Long.parseLong(recordId));
      }

      FormData formData = FormData.getFormData(form, formValueMap, true, null);

      FormDataDetail formDataDetail = new FormDataDetail();
      formDataDetail.setFormId(form.getId());
      formDataDetail.setRecordId(formData.getRecordId());
      formDataDetail.setFormData(formData);
      ResponseEvent<FormDataDetail> resp =
          formSvc.saveFormData(new RequestEvent<FormDataDetail>(formDataDetail));
      resp.throwErrorIfUnsuccessful();

      return ResponseEvent.response(resp.getPayload().getFormData().getFieldNameValueMap(true));
    } catch (OpenSpecimenException ose) {
      return ResponseEvent.error(ose);
    } catch (Exception e) {
      return ResponseEvent.serverError(e);
    }
  }
 private void ensureUniqueDomainName(String domainName) {
   if (!daoFactory.getAuthDao().isUniqueAuthDomainName(domainName)) {
     throw OpenSpecimenException.userError(AuthProviderErrorCode.DUP_DOMAIN_NAME);
   }
 }
 private DpRequirementDao getDprDao() {
   return daoFactory.getDistributionProtocolRequirementDao();
 }