private void deleteSampleWhenError(String sampleName) throws Exception {
    Sample sample = this.findFullyLoadedSampleByName(sampleName);
    CaNanoLabApplicationService appService =
        (CaNanoLabApplicationService) ApplicationServiceProvider.getApplicationService();
    // delete characterizations
    if (sample.getCharacterizationCollection() != null) {
      for (Characterization achar : sample.getCharacterizationCollection()) {
        charService.deleteCharacterization(achar);
      }
    }

    // delete composition
    if (sample.getSampleComposition() != null) {
      compService.deleteComposition(sample.getSampleComposition());
    }
    sample.setSampleComposition(null);

    // remove publication associations
    if (sample.getPublicationCollection() != null) {
      sample.setPublicationCollection(null);
    }
    // remove keyword associations
    if (sample.getKeywordCollection() != null) {
      sample.setKeywordCollection(null);
    }
    appService.saveOrUpdate(sample);
    appService.delete(sample);
    // remove all csm entries associated with sample
    this.accessUtils.removeCSMEntries(sample.getId().toString());
  }
  private Sample findFullyLoadedSampleByName(String sampleName) throws Exception {
    CaNanoLabApplicationService appService =
        (CaNanoLabApplicationService) ApplicationServiceProvider.getApplicationService();
    // load composition and characterization separate because of Hibernate
    // join limitation
    DetachedCriteria crit =
        DetachedCriteria.forClass(Sample.class)
            .add(Property.forName("name").eq(sampleName).ignoreCase());
    Sample sample = null;

    // load composition and characterization separate because of
    // Hibernate join limitation
    crit.setFetchMode("primaryPointOfContact", FetchMode.JOIN);
    crit.setFetchMode("primaryPointOfContact.organization", FetchMode.JOIN);
    crit.setFetchMode("otherPointOfContactCollection", FetchMode.JOIN);
    crit.setFetchMode("otherPointOfContactCollection.organization", FetchMode.JOIN);
    crit.setFetchMode("keywordCollection", FetchMode.JOIN);
    crit.setFetchMode("publicationCollection", FetchMode.JOIN);
    crit.setFetchMode("publicationCollection.authorCollection", FetchMode.JOIN);
    crit.setFetchMode("publicationCollection.keywordCollection", FetchMode.JOIN);
    crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

    List result = appService.query(crit);
    if (!result.isEmpty()) {
      sample = (Sample) result.get(0);
    }
    if (sample == null) {
      throw new NotExistException("Sample doesn't exist in the database");
    }

    // fully load composition
    SampleComposition comp = this.loadComposition(sample.getId().toString());
    sample.setSampleComposition(comp);

    // fully load characterizations
    List<Characterization> chars = this.loadCharacterizations(sample.getId().toString());
    if (chars != null && !chars.isEmpty()) {
      sample.setCharacterizationCollection(new HashSet<Characterization>(chars));
    } else {
      sample.setCharacterizationCollection(null);
    }
    return sample;
  }
  public void deleteSample(String sampleName)
      throws SampleException, NoAccessException, NotExistException {
    if (user == null) {
      throw new NoAccessException();
    }
    Sample sample = null;
    try {
      // / / fully load original sample
      sample = findFullyLoadedSampleByName(sampleName);
      CaNanoLabApplicationService appService =
          (CaNanoLabApplicationService) ApplicationServiceProvider.getApplicationService();
      // / / delete characterizations
      if (sample.getCharacterizationCollection() != null) {
        for (Characterization achar : sample.getCharacterizationCollection()) {
          charService.deleteCharacterization(achar);
        }
      }

      // / / delete composition
      if (sample.getSampleComposition() != null) {
        compService.deleteComposition(sample.getSampleComposition());
      }
      sample.setSampleComposition(null);

      // / / remove publication associations
      if (sample.getPublicationCollection() != null) {
        sample.setPublicationCollection(null);
      }
      // / / remove keyword associations
      if (sample.getKeywordCollection() != null) {
        sample.setKeywordCollection(null);
      }
      appService.saveOrUpdate(sample);
      appService.delete(sample);
    } catch (NotExistException e) {
      throw e;
    } catch (Exception e) {
      String err = "Error in deleting the sample " + sampleName;
      logger.error(err, e);
      throw new SampleException(err, e);
    }
  }