/**
   * Insert patient and set primary key
   *
   * @param patient
   * @return number of patients inserted
   * @throws Exception
   */
  public void insert(PatientSummaryVO patientSummaryVO) throws Exception {
    if (patientSummaryVO == null) return;

    Patient patient = this.toPatient(patientSummaryVO);

    // submissions are never inserted via patients, just select newest
    // by date
    // TODO: Do we still need that???
    // List<Submission> submissions =
    // this.db.query(Submission.class).equals(Submission.DATE_,
    // patientSummaryVO.getSubmissionDate()).sortDESC("id").find();
    //
    // if (submissions.size() > 0)
    // {
    // patientSummaryVO.getPatient().setSubmission(submissions.get(0));
    // }

    // mutations are never inserted via patients, just select
    for (MutationSummaryVO mutationSummaryVO : patientSummaryVO.getVariantSummaryVOList()) {
      List<Mutation> mutations =
          this.db
              .query(Mutation.class)
              .equals(Mutation.CDNA_NOTATION, mutationSummaryVO.getCdnaNotation())
              .find();

      // System.out.println(">>> Queried: " +
      // mutationSummaryVO.getCdnaNotation() + ", found: " +
      // mutations.size());
      if (mutations.size() != 1)
        throw new Exception("ERROR: No mutation found for " + mutationSummaryVO.getCdnaNotation());

      patient.getMutations_Id().add(mutations.get(0).getId());
    }

    // phenotypes are never inserted via patients, just select
    // if (patientSummaryVO.getPhenotypeId() == null)
    // {
    // // System.out.println(">>> Query: majortype==" +
    // patientSummaryVO.getPhenotypeMajor() + ", subtype==" +
    // patientSummaryVO.getPhenotypeSub());
    // List<MutationPhenotype> phenotypes =
    // this.db.query(MutationPhenotype.class).equals(MutationPhenotype.MAJORTYPE,
    // patientSummaryVO.getPhenotypeMajor()).equals(MutationPhenotype.SUBTYPE,
    // patientSummaryVO.getPhenotypeSub()).find();
    //
    // if (phenotypes.size() != 1)
    // throw new Exception("No phenotype found for " +
    // patientSummaryVO.getPhenotypeMajor() + ", " +
    // patientSummaryVO.getPhenotypeSub());
    //
    // patientSummaryVO.setPhenotypeId(phenotypes.get(0).getId());
    // }
    // patient.setPhenotype_Id(patientSummaryVO.getPhenotypeId());

    // Publications are never inserted via patients, just select
    if (CollectionUtils.isNotEmpty(patientSummaryVO.getPublicationVOList())) {
      List<Integer> publicationIds = new ArrayList<Integer>();

      for (PublicationVO publicationVO : patientSummaryVO.getPublicationVOList()) {
        if (publicationVO.getPubmedId() == null) continue;

        List<OntologyTerm> pubmedTerms =
            this.db
                .query(OntologyTerm.class)
                .equals(OntologyTerm.NAME, publicationVO.getPubmedId())
                .find();

        if (pubmedTerms.size() != 1) continue;

        List<Publication> publications =
            this.db
                .query(Publication.class)
                .equals(Publication.PUBMEDID, pubmedTerms.get(0).getId())
                .find();

        if (publications.size() != 1)
          throw new Exception("No publication found for Pubmed ID " + publicationVO.getPubmedId());

        publicationIds.add(publications.get(0).getId());
      }

      patient.setPatientreferences_Id(publicationIds);
    }
    patient.setSubmission_Id(1);
    // Insert patient
    this.db.add(patient);

    // Insert ObservedValues

    for (ObservedValueVO observedValueVO : patientSummaryVO.getObservedValueVOList()) {
      if (observedValueVO == null) return;

      List<ObservableFeature> features =
          this.db
              .query(ObservableFeature.class)
              .equals(ObservableFeature.NAME, observedValueVO.getFeatureName())
              .find();

      if (features.size() != 1) return;

      List<Patient> patients =
          this.db
              .query(Patient.class)
              .equals(Patient.IDENTIFIER, observedValueVO.getTargetName())
              .find();

      if (patients.size() != 1) return;

      ObservedValue observedValue = new ObservedValue();
      observedValue.setFeature(features.get(0));
      observedValue.setInvestigation(1);
      observedValue.setTarget(patient.getId());
      observedValue.setValue(observedValueVO.getValue());

      this.db.add(observedValue);
    }
  }
  private Patient toPatient(PatientSummaryVO patientSummaryVO) {
    Patient patient = new Patient();

    patient.setIdentifier(patientSummaryVO.getPatientIdentifier());
    patient.setName(patientSummaryVO.getPatientName());
    patient.setNumber(patientSummaryVO.getPatientNumber());
    patient.setConsent(patientSummaryVO.getPatientConsent());
    patient.setAge(patientSummaryVO.getPatientAge());
    patient.setGender(patientSummaryVO.getPatientGender());
    patient.setEthnicity(patientSummaryVO.getPatientEthnicity());
    patient.setDeceased(patientSummaryVO.getPatientDeceased());
    patient.setDeath_Cause(patientSummaryVO.getPatientDeathCause());
    patient.setMmp1_Allele1(patientSummaryVO.getPatientMmp1Allele1());
    patient.setMmp1_Allele2(patientSummaryVO.getPatientMmp1Allele2());
    if (StringUtils.isNotEmpty(patientSummaryVO.getVariantComment()))
      patient.setMutation2remark(patientSummaryVO.getVariantComment());

    // patientSummaryVO.setPatientMaterialList(patient.getMaterial_Name());
    //
    // Submission submission = this.db.findById(Submission.class,
    // patient.getSubmission_Id());
    // MolgenisUser submitter = this.db.findById(MolgenisUser.class,
    // submission.getSubmitters_Id().get(0));
    // patientSummaryVO.setSubmitterDepartment(submitter.getDepartment());
    // patientSummaryVO.setSubmitterInstitute(submitter.getInstitute());
    // patientSummaryVO.setSubmitterCity(submitter.getCity());
    // patientSummaryVO.setSubmitterCountry(submitter.getCountry());

    return patient;
  }