/**
   * Generate a list from a pathquery.
   *
   * @param pq The pathquery
   * @param name The name of the list
   * @param description The description of the list
   * @param tags A list of tags to add to the list
   * @param profile The profile the list should belong to
   * @throws ObjectStoreException If there is an issue running the queries that generate the list.
   * @throws PathException If the paths supplied are illegal.
   */
  protected void generateListFromQuery(
      PathQuery pq, String name, String description, List<String> tags, Profile profile)
      throws ObjectStoreException, PathException {

    Query q = getQuery(pq, profile);

    String tempName = name + TEMP;

    String viewPathString = pq.getView().get(0);
    Path viewPath = pq.makePath(viewPathString);
    String type = viewPath.getLastClassDescriptor().getUnqualifiedName();

    try {
      InterMineBag newList = profile.createBag(tempName, type, description, im.getClassKeys());
      newList.addToBagFromQuery(q);
      try {
        im.getBagManager().addTagsToBag(tags, newList, profile);
      } catch (TagManager.TagNameException e) {
        throw new BadRequestException(e.getMessage());
      } catch (TagManager.TagNamePermissionException e) {
        throw new ServiceForbiddenException(e.getMessage());
      }
      profile.renameBag(tempName, name);

      output.addResultItem(Arrays.asList("" + newList.size()));

    } catch (CompletelyFalseException e) {
      output.addResultItem(Arrays.asList("0"));
      throw new BadRequestException("List not created - it would be of size 0");
    } catch (UnknownBagTypeException e) {
      output.addResultItem(Arrays.asList("0"));
      throw new InternalErrorException(e.getMessage(), e);
    } catch (ClassKeysNotFoundException cke) {
      throw new BadRequestException("Bag has not class key set", cke);
    } finally {
      if (profile.getSavedBags().containsKey(tempName)) {
        profile.deleteBag(tempName);
      }
    }
  }