private SampleBean loadSampleBean(Sample sample) throws Exception {
   SampleBean sampleBean = new SampleBean(sample);
   if (user != null) {
     List<AccessibilityBean> groupAccesses =
         super.findGroupAccessibilities(sample.getId().toString());
     List<AccessibilityBean> userAccesses =
         super.findUserAccessibilities(sample.getId().toString());
     sampleBean.setUserAccesses(userAccesses);
     sampleBean.setGroupAccesses(groupAccesses);
     sampleBean.setUser(user);
   }
   return sampleBean;
 }
  public SampleBean cloneSample(String originalSampleName, String newSampleName)
      throws SampleException, NoAccessException, DuplicateEntriesException, NotExistException {
    if (user == null) {
      throw new NoAccessException();
    }
    SampleBean newSampleBean = null;
    Sample origSample = null;
    SampleBean origSampleBean = null;
    Sample newSample0 = new Sample();
    try {
      CaNanoLabApplicationService appService =
          (CaNanoLabApplicationService) ApplicationServiceProvider.getApplicationService();
      Sample dbNewSample = (Sample) appService.getObject(Sample.class, "name", newSampleName);
      if (dbNewSample != null) {
        throw new DuplicateEntriesException();
      }
      // fully load original sample
      origSample = findFullyLoadedSampleByName(originalSampleName);
      origSampleBean = new SampleBean(origSample);
      newSample0.setName(newSampleName);
      newSample0.setCreatedBy(user.getLoginName() + ":" + Constants.AUTO_COPY_ANNOTATION_PREFIX);
      newSample0.setCreatedDate(new Date());
      // save the sample so later up just update the cloned the
      // associations.
      SampleBean newSampleBean0 = new SampleBean(newSample0);
      // save the sample to get an ID before saving associations
      saveSample(newSampleBean0);
    } catch (NotExistException e) {
      throw e;
    } catch (DuplicateEntriesException e) {
      throw e;
    } catch (Exception e) {
      String err = "Error in loading the original sample " + originalSampleName;
      logger.error(err, e);
      throw new SampleException(err, e);
    }
    try {
      // clone the sample
      Sample newSample = origSampleBean.getDomainCopy(user.getLoginName());
      newSample.setName(newSampleName);
      // keep the id
      newSample.setId(newSample0.getId());
      newSampleBean = new SampleBean(newSample);

      // retrieve accessibilities of the original sample
      List<AccessibilityBean> groupAccesses =
          super.findGroupAccessibilities(origSample.getId().toString());
      List<AccessibilityBean> userAccesses =
          super.findUserAccessibilities(origSample.getId().toString());
      origSampleBean.setGroupAccesses(groupAccesses);
      origSampleBean.setUserAccesses(userAccesses);

      // need to save associations one by one (except keywords)
      // Hibernate mapping settings for most use cases
      saveClonedPOCs(newSampleBean);
      saveClonedCharacterizations(origSample.getName(), newSampleBean);
      saveClonedComposition(origSampleBean, newSampleBean);
      saveClonedPublications(origSampleBean, newSampleBean);
      saveSample(newSampleBean);
      newSampleBean.setUser(user);
      // assign accessibility for the new sample
      for (AccessibilityBean access : origSampleBean.getAllAccesses()) {
        this.assignAccessibility(access, newSampleBean.getDomain());
      }
    } catch (Exception e) {
      // delete the already persisted new sample in case of error
      try {
        this.deleteSampleWhenError(newSample0.getName());
      } catch (Exception ex) {
        String err = "Error in deleting the errored cloned-sample " + newSample0.getName();
        logger.error(err, e);
        throw new SampleException(err, ex);
      }
      String err = "Error in cloning the sample " + originalSampleName;
      logger.error(err, e);
      throw new SampleException(err, e);
    }
    return newSampleBean;
  }