Пример #1
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();
  }
  @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);
    }
  }