/*
   * Gets a translated topic based on a topic id and locale
   */
  public RESTTranslatedTopicV1 getTranslatedTopicByTopicId(
      final Integer id, final Integer rev, final String locale) {
    if (locale == null) return null;
    final RESTTopicV1 topic = getTopicById(id, rev, true);
    if (topic == null) return null;

    for (final RESTTranslatedTopicV1 translatedTopic : topic.getTranslatedTopics_OTM().getItems()) {
      if (translatedTopic.getLocale().equals(locale)) return translatedTopic;
    }

    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 collection of translated topics based on the list of topic ids
   * passed.
   */
  public RESTTranslatedTopicCollectionV1 getTranslatedTopicsByTopicIds(
      final List<Integer> ids, final String locale) {
    if (ids.isEmpty()) return null;

    try {
      final RESTTranslatedTopicCollectionV1 topics = new RESTTranslatedTopicCollectionV1();
      final StringBuffer urlVars = new StringBuffer("query;latestTranslations=true;topicIds=");
      // final String encodedComma = URLEncoder.encode(",", "UTF-8");

      for (final Integer id : ids) {
        if (!entityCache.containsKeyValue(RESTTranslatedTopicV1.class, id)
            && !entityCache.containsKeyValue(RESTTranslatedTopicV1.class, (id * -1))) {
          urlVars.append(id + ",");
        } else if (entityCache.containsKeyValue(RESTTranslatedTopicV1.class, (id * -1))) {
          topics.addItem(entityCache.get(RESTTranslatedTopicV1.class, (id * -1)));
        } else {
          topics.addItem(entityCache.get(RESTTranslatedTopicV1.class, id));
        }
      }

      String query = urlVars.toString();

      if (query.length() != "query;latestTranslations=true;topicIds=".length()) {
        query = query.substring(0, query.length() - 1);

        /* Add the locale to the query if one was passed */
        if (locale != null && !locale.isEmpty()) query += ";locale1=" + locale + "1";

        PathSegment path = new PathSegmentImpl(query, false);

        /*
         * We need to expand the all the items in the translatedtopic
         * collection
         */
        final ExpandDataTrunk expand = new ExpandDataTrunk();

        final ExpandDataTrunk translatedTopicsExpand =
            new ExpandDataTrunk(new ExpandDataDetails("translatedtopics"));
        final ExpandDataTrunk topicExpandTranslatedTopics =
            new ExpandDataTrunk(new ExpandDataDetails(RESTTopicV1.TRANSLATEDTOPICS_NAME));
        final ExpandDataTrunk tags = new ExpandDataTrunk(new ExpandDataDetails("tags"));
        final ExpandDataTrunk properties =
            new ExpandDataTrunk(new ExpandDataDetails(RESTBaseTopicV1.PROPERTIES_NAME));
        final ExpandDataTrunk categories = new ExpandDataTrunk(new ExpandDataDetails("categories"));
        final ExpandDataTrunk parentTags = new ExpandDataTrunk(new ExpandDataDetails("parenttags"));
        final ExpandDataTrunk outgoingRelationships =
            new ExpandDataTrunk(
                new ExpandDataDetails(RESTTranslatedTopicV1.ALL_LATEST_OUTGOING_NAME));
        final ExpandDataTrunk topicsExpand =
            new ExpandDataTrunk(new ExpandDataDetails(RESTTranslatedTopicV1.TOPIC_NAME));

        /* We need to expand the categories collection on the topic tags */
        tags.setBranches(CollectionUtilities.toArrayList(categories, parentTags, properties));
        outgoingRelationships.setBranches(
            CollectionUtilities.toArrayList(tags, properties, topicsExpand));

        topicsExpand.setBranches(CollectionUtilities.toArrayList(topicExpandTranslatedTopics));

        translatedTopicsExpand.setBranches(
            CollectionUtilities.toArrayList(tags, outgoingRelationships, properties, topicsExpand));

        expand.setBranches(CollectionUtilities.toArrayList(translatedTopicsExpand));

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

        final RESTTranslatedTopicCollectionV1 downloadedTopics =
            client.getJSONTranslatedTopicsWithQuery(path, expandString);
        entityCache.add(downloadedTopics);

        /* Transfer the downloaded data to the current topic list */
        if (downloadedTopics != null && downloadedTopics.getItems() != null) {
          for (final RESTTranslatedTopicV1 item : downloadedTopics.getItems()) {
            entityCache.add(item, item.getTopicId(), false);
            topics.addItem(item);
          }
        }
      }

      return topics;
    } catch (Exception e) {
      log.error(ExceptionUtilities.getStackTrace(e));
    }
    return null;
  }