/**
  * @param nanomaterialEntityClassNames
  * @param functionalizingEntityClassNames
  * @param otherFunctionalizingEntityTypes
  * @param functionClassNames
  * @param otherFunctionTypes
  * @param characterizationClassNames
  * @param wordList
  * @param samplePointOfContacts
  * @return
  * @throws SampleException
  */
 public List<String> findSampleIdsBy(
     String sampleName,
     String samplePointOfContact,
     String[] nanomaterialEntityClassNames,
     String[] otherNanomaterialEntityTypes,
     String[] functionalizingEntityClassNames,
     String[] otherFunctionalizingEntityTypes,
     String[] functionClassNames,
     String[] otherFunctionTypes,
     String[] characterizationClassNames,
     String[] otherCharacterizationTypes,
     String[] wordList)
     throws SampleException {
   try {
     List<String> sampleIds =
         helper.findSampleIdsBy(
             sampleName,
             samplePointOfContact,
             nanomaterialEntityClassNames,
             otherNanomaterialEntityTypes,
             functionalizingEntityClassNames,
             otherFunctionalizingEntityTypes,
             functionClassNames,
             otherFunctionTypes,
             characterizationClassNames,
             otherCharacterizationTypes,
             wordList);
     return sampleIds;
   } catch (Exception e) {
     String err = "Problem finding samples with the given search parameters.";
     logger.error(err, e);
     throw new SampleException(err, e);
   }
 }
 public int getNumberOfPublicSampleSources() throws SampleException {
   try {
     int count = helper.getNumberOfPublicSampleSources();
     return count;
   } catch (Exception e) {
     String err = "Error finding counts of public sample sources.";
     logger.error(err, e);
     throw new SampleException(err, e);
   }
 }
 public List<String> findSampleIdsByOwner(String currentOwner) throws SampleException {
   List<String> sampleIds = new ArrayList<String>();
   try {
     sampleIds = helper.findSampleIdsByOwner(currentOwner);
   } catch (Exception e) {
     String error = "Error in retrieving sampleIds by owner";
     throw new SampleException(error, e);
   }
   return sampleIds;
 }
 public PointOfContactBean findPointOfContactById(String pocId) throws PointOfContactException {
   PointOfContactBean pocBean = null;
   try {
     PointOfContact poc = helper.findPointOfContactById(pocId);
     pocBean = new PointOfContactBean(poc);
   } catch (Exception e) {
     String err = "Problem finding point of contact for the given id.";
     logger.error(err, e);
     throw new PointOfContactException(err, e);
   }
   return pocBean;
 }
  public List<String> findOtherSampleNamesFromSamePrimaryOrganization(String sampleId)
      throws SampleException {
    List<String> sortedNames = null;
    try {
      Set<String> names = helper.findOtherSamplesFromSamePrimaryOrganization(sampleId);
      sortedNames = new ArrayList<String>(names);
      Collections.sort(sortedNames, new Comparators.SortableNameComparator());

    } catch (Exception e) {
      String err = "Error in deleting the sample " + sampleId;
      logger.error(err, e);
      throw new SampleException(err, e);
    }
    return sortedNames;
  }
 public SampleBean findSampleByName(String sampleName) throws SampleException, NoAccessException {
   try {
     Sample sample = helper.findSampleByName(sampleName);
     SampleBean sampleBean = null;
     if (sample != null) {
       sampleBean = loadSampleBean(sample);
     }
     return sampleBean;
   } catch (NoAccessException e) {
     throw e;
   } catch (Exception e) {
     String err = "Problem finding the sample by name: " + sampleName;
     logger.error(err, e);
     throw new SampleException(err, e);
   }
 }
 /**
  * Only load sample core data.
  *
  * <p>Do not check read permission because workspace items are owned by user.
  */
 public SampleBasicBean findSWorkspaceSampleById(String sampleId, boolean loadAccessInfo)
     throws SampleException, NoAccessException {
   SampleBasicBean sampleBean = null;
   try {
     Sample sample = helper.findSampleBasicById(sampleId);
     if (sample != null) {
       if (loadAccessInfo) {
         sampleBean = loadSampleBean(sample, false);
       } else {
         sampleBean = new SampleBasicBean(sample);
       }
     }
   } catch (NoAccessException e) {
     throw e;
   } catch (Exception e) {
     String err = "Problem finding the sample by id: " + sampleId;
     logger.error(err, e);
     throw new SampleException(err, e);
   }
   return sampleBean;
 }
  public SortedSet<String> getAllOrganizationNames() throws PointOfContactException {
    try {
      SortedSet<String> names = new TreeSet<String>(new Comparators.SortableNameComparator());
      CaNanoLabApplicationService appService =
          (CaNanoLabApplicationService) ApplicationServiceProvider.getApplicationService();
      HQLCriteria crit =
          new HQLCriteria(
              "select org.name from gov.nih.nci.cananolab.domain.common.Organization org");
      List results = appService.query(crit);

      logger.debug(
          "Completed select org.name from gov.nih.nci.cananolab.domain.common.Organization org");
      for (int i = 0; i < results.size(); i++) {
        String name = ((String) results.get(i)).trim();
        names.add(name);
      }
      return names;
    } catch (Exception e) {
      String err = "Error finding organization for " + helper.getUser().getLoginName();
      logger.error(err, e);
      throw new PointOfContactException(err, e);
    }
  }
  public void savePointOfContact(PointOfContactBean pocBean)
      throws PointOfContactException, NoAccessException {
    if (user == null) {
      throw new NoAccessException();
    }
    try {
      PointOfContact dbPointOfContact = null;
      Long oldPOCId = null;
      String oldOrgName = null;
      Boolean newPOC = true;
      Boolean newOrg = true;
      CaNanoLabApplicationService appService =
          (CaNanoLabApplicationService) ApplicationServiceProvider.getApplicationService();
      PointOfContact domainPOC = pocBean.getDomain();
      Organization domainOrg = domainPOC.getOrganization();
      // get existing organization from database and reuse ID,
      // created by and created date
      // address information will be updated
      Organization dbOrganization = helper.findOrganizationByName(domainOrg.getName());
      if (dbOrganization != null) {
        domainOrg.setId(dbOrganization.getId());
        domainOrg.setCreatedBy(dbOrganization.getCreatedBy());
        domainOrg.setCreatedDate(dbOrganization.getCreatedDate());
        newOrg = false;
      }
      // create a new org if not an existing one
      else {
        domainOrg.setId(null);
      }
      // if point of contact has no ID
      if (domainPOC.getId() == null) {
        // check if org name, first name and last name matches existing
        // one
        dbPointOfContact =
            helper.findPointOfContactByNameAndOrg(
                domainPOC.getFirstName(),
                domainPOC.getLastName(),
                domainPOC.getOrganization().getName());
        // if found, reuse ID, created_date and created_by
        if (dbPointOfContact != null) {
          domainPOC.setId(dbPointOfContact.getId());
          domainPOC.setCreatedDate(dbPointOfContact.getCreatedDate());
          domainPOC.setCreatedBy(dbPointOfContact.getCreatedBy());
          newPOC = false;
        }
      } else {
        // check if organization is changed
        dbPointOfContact = helper.findPointOfContactById(domainPOC.getId().toString());
        Organization dbOrg = dbPointOfContact.getOrganization();
        // if organization information is changed, create a new POC
        if (!dbOrg.getName().equals(domainOrg.getName())) {
          oldPOCId = domainPOC.getId();
          oldOrgName = dbOrg.getName();
          domainPOC.setId(null);
          newPOC = true;
        }
        // if name information is changed, create a new POC
        else if (domainPOC.getFirstName() != null
                && !domainPOC.getFirstName().equalsIgnoreCase(dbPointOfContact.getFirstName())
            || domainPOC.getLastName() != null
                && !domainPOC.getLastName().equalsIgnoreCase(dbPointOfContact.getLastName())) {
          newPOC = true;
        } else {
          domainPOC.setId(dbPointOfContact.getId());
          domainPOC.setCreatedBy(dbPointOfContact.getCreatedBy());
          domainPOC.setCreatedDate(dbPointOfContact.getCreatedDate());
          newPOC = false;
        }
      }
      appService.saveOrUpdate(domainPOC);

      if (newPOC) {
        this.saveAccessibility(AccessibilityBean.CSM_PUBLIC_ACCESS, domainPOC.getId().toString());
      }
      if (newOrg) {
        this.saveAccessibility(
            AccessibilityBean.CSM_PUBLIC_ACCESS, domainPOC.getOrganization().getId().toString());
      }
    } catch (Exception e) {
      String err = "Error in saving the PointOfContact.";
      logger.error(err, e);
      throw new PointOfContactException(err, e);
    }
  }