/*
   * Gets a List of all type tuples for a specified name.
   */
  public RESTTagV1 getTypeByName(final String name) {
    final List<RESTTagV1> tags = getTagsByName(name);

    // Iterate through the list of tags and check if the tag is a Type and
    // matches the name.
    if (tags != null) {
      for (final RESTTagV1 tag : tags) {
        if (ComponentTagV1.containedInCategory(tag, CSConstants.TYPE_CATEGORY_ID)
            && tag.getName().equals(name)) {
          return tag;
        }
      }
    }
    return null;
  }
  /*
   * Gets a ContentSpec tuple for a specified id.
   */
  public RESTTopicV1 getContentSpecById(
      final int id, final Integer rev, final boolean expandTranslations) {
    final RESTTopicV1 cs = getTopicById(id, rev, expandTranslations);
    if (cs == null) return null;

    final List<RESTTagV1> topicTypes =
        ComponentBaseTopicV1.returnTagsInCategoriesByID(
            cs, CollectionUtilities.toArrayList(CSConstants.TYPE_CATEGORY_ID));
    for (final RESTTagV1 type : topicTypes) {
      if (type.getId().equals(CSConstants.CONTENT_SPEC_TAG_ID)) {
        return cs;
      }
    }
    return null;
  }
  /*
   * Gets a ContentSpec tuple for a specified id.
   */
  public RESTTranslatedTopicV1 getTranslatedContentSpecById(
      final int id, final Integer rev, final String locale) {
    if (locale == null) return null;
    final RESTTopicV1 cs = getTopicById(id, rev, true);
    if (cs == null) return null;

    final List<RESTTagV1> topicTypes =
        ComponentBaseTopicV1.returnTagsInCategoriesByID(
            cs, CollectionUtilities.toArrayList(CSConstants.TYPE_CATEGORY_ID));
    if (cs.getTranslatedTopics_OTM() != null && cs.getTranslatedTopics_OTM().getItems() != null) {
      for (final RESTTagV1 type : topicTypes) {
        if (type.getId().equals(CSConstants.CONTENT_SPEC_TAG_ID)) {
          for (final RESTTranslatedTopicV1 topic : cs.getTranslatedTopics_OTM().getItems()) {
            if (topic.getLocale().equals(locale)) return topic;
          }
        }
      }
    }
    return null;
  }
  /*
   * Gets a List of all tag tuples for a specified name.
   */
  public List<RESTTagV1> getTagsByName(final String name) {
    final List<RESTTagV1> output = new ArrayList<RESTTagV1>();

    try {

      BaseRestCollectionV1<RESTTagV1, RESTTagCollectionV1> tags =
          collectionsCache.get(RESTTagV1.class, RESTTagCollectionV1.class);
      if (tags.getItems() == null) {
        /* We need to expand the Tags & Categories collection */
        final ExpandDataTrunk expand = new ExpandDataTrunk();
        final ExpandDataTrunk expandTags = new ExpandDataTrunk(new ExpandDataDetails("tags"));
        expandTags.setBranches(
            CollectionUtilities.toArrayList(
                new ExpandDataTrunk(new ExpandDataDetails("categories"))));
        expand.setBranches(CollectionUtilities.toArrayList(expandTags));

        final String expandString = mapper.writeValueAsString(expand);
        // final String expandEncodedString = URLEncoder.encode(expandString, "UTF-8");

        tags = client.getJSONTags(expandString);
        collectionsCache.add(RESTTagV1.class, tags);
      }

      // Iterate through the list of tags and check if the tag is a Type
      // and matches the name.
      if (tags != null) {
        for (final RESTTagV1 tag : tags.getItems()) {
          if (tag.getName().equals(name)) {
            output.add(tag);
          }
        }
      }

      return output;
    } catch (Exception e) {
      log.error(ExceptionUtilities.getStackTrace(e));
    }
    return null;
  }
  /*
   * Gets a Category item assuming that tags can only have one category
   */
  public RESTCategoryV1 getCategoryByTagId(final int tagId) {
    final RESTTagV1 tag = getTagById(tagId);
    if (tag == null) return null;

    return tag.getCategories().getItems().size() > 0 ? tag.getCategories().getItems().get(0) : null;
  }