private AffiliationVO getAffiliation(AffiliationRO affiliationRO) throws Exception {
   for (AffiliationVO element :
       ((ApplicationBean) getApplicationBean(ApplicationBean.class)).getOuList()) {
     if (element.getReference().equals(affiliationRO)) {
       return element;
     }
   }
   SearchQuery searchQuery =
       new PlainCqlQuery("(escidoc.objid=\"" + affiliationRO.getObjectId() + "\")");
   OrgUnitsSearchResult searchResult = this.search.searchForOrganizationalUnits(searchQuery);
   List<AffiliationVO> resultList = searchResult.getResults();
   if (resultList.size() == 0) {
     logger.warn(
         "'"
             + affiliationRO.getObjectId()
             + "' was declared as a parent ou but it was not found.");
   } else if (resultList.size() > 1) {
     logger.warn(
         "Unexpectedly more than one ou with the id '"
             + affiliationRO.getObjectId()
             + "' was found.");
   } else {
     ((ApplicationBean) getApplicationBean(ApplicationBean.class))
         .getOuList()
         .add(resultList.get(0));
     return resultList.get(0);
   }
   return null;
 }
  private List<List<AffiliationVO>> getPaths(List<AffiliationVO> currentPath) throws Exception {

    List<List<AffiliationVO>> result = new ArrayList<List<AffiliationVO>>();
    AffiliationVO affiliationVO = currentPath.get(currentPath.size() - 1);
    if (affiliationVO.getParentAffiliations().isEmpty()) {
      result.add(currentPath);
    } else {
      for (AffiliationRO parent : affiliationVO.getParentAffiliations()) {
        List<AffiliationVO> list = new ArrayList<AffiliationVO>();
        list.addAll(currentPath);
        AffiliationVO parentVO = getAffiliation(parent);
        list.add(parentVO);
        result.addAll(getPaths(list));
      }
    }
    return result;
  }
  public OrganizationSuggest() throws Exception {
    // Get query from URL parameters
    FacesContext context = FacesContext.getCurrentInstance();
    Map<String, String> parameters = context.getExternalContext().getRequestParameterMap();
    String query = parameters.get("q");

    // Initialize search service
    try {
      InitialContext initialContext = new InitialContext();
      this.search = (Search) initialContext.lookup("java:global/pubman_ear/search/SearchBean");
    } catch (NamingException ne) {
      throw new RuntimeException("Search service not initialized", ne);
    }

    // Perform search request
    if (query != null) {
      String queryString = "";
      for (String snippet : query.split(" ")) {
        if (!"".equals(queryString)) {
          queryString += " and ";
        }
        queryString +=
            "(escidoc.title=\"" + snippet + "*\"  or escidoc.alternative=\"" + snippet + "*\")";
      }
      SearchQuery searchQuery = new PlainCqlQuery(queryString);
      searchQuery.setMaximumRecords("50");

      OrgUnitsSearchResult searchResult = this.search.searchForOrganizationalUnits(searchQuery);
      for (AffiliationVO affiliationVO : searchResult.getResults()) {
        List<AffiliationVO> initList = new ArrayList<AffiliationVO>();
        initList.add(affiliationVO);
        List<List<AffiliationVO>> pathList = getPaths(initList);
        for (List<AffiliationVO> path : pathList) {
          OrganizationVOPresentation organizationVOPresentation = new OrganizationVOPresentation();
          organizationVOPresentation.setIdentifier(affiliationVO.getReference().getObjectId());

          String city = affiliationVO.getDefaultMetadata().getCity();
          String countryCode = affiliationVO.getDefaultMetadata().getCountryCode();

          String address = "";
          if (city != null) {
            address += city;
          }
          if (city != null && countryCode != null) {
            address += ", ";
          }
          if (countryCode != null) {
            address += countryCode;
          }

          // TODO: remove this if address is wanted
          // address = "";

          organizationVOPresentation.setAddress(address);
          String name = "";
          for (AffiliationVO affVO : path) {

            if (!"".equals(name)) {
              name = name + ", ";
            }

            name = name + affVO.getDefaultMetadata().getName();
          }
          organizationVOPresentation.setName(new TextVO(name));
          organizationVOPresentation.setBean(this);

          getCreatorOrganizations().add(organizationVOPresentation);
        }
      }
    }
  }