package be.nikiroo.fanfix.reader.cli;
import java.io.IOException;
-import java.util.ArrayList;
import java.util.List;
import be.nikiroo.fanfix.Instance;
@Override
public void searchTag(SupportType searchOn, int page, int item,
boolean sync, Integer... tags) throws IOException {
- BasicSearchable search = BasicSearchable.getSearchable(searchOn);
- List<SearchableTag> stags = search.getTags();
- String fqnTag = "";
- SearchableTag stag = null;
- for (Integer tagIndex : tags) {
- // ! 1-based index !
- if (tagIndex == null || tagIndex <= 0 | tagIndex > stags.size()) {
- throw new IOException("Index out of bounds: " + tagIndex);
- }
+ BasicSearchable search = BasicSearchable.getSearchable(searchOn);
+ SearchableTag stag = search.getTag(tags);
- stag = stags.get(tagIndex - 1);
- if (stag != null) {
- search.fillTag(stag);
- stags = stag.getChildren();
- if (!fqnTag.isEmpty()) {
- fqnTag += " / ";
- }
- fqnTag += stag.getName();
- } else {
- stags = new ArrayList<SearchableTag>();
- break;
+ if (stag == null) {
+ // TODO i18n
+ System.out.println("Known tags: ");
+ int i = 1;
+ for (SearchableTag s : search.getTags()) {
+ System.out.println(String.format("%d: %s", i, s.getName()));
+ i++;
}
- }
-
- if (stag != null) {
+ } else {
if (page <= 0) {
if (stag.isLeaf()) {
search.search(stag, 1);
displayStory(meta);
} else {
SearchableTag subtag = subtags.get(item - 1);
-
- // TODO: i18n
- String stories = "stories";
- String num = StringUtils.formatNumber(subtag
- .getCount());
- System.out.println(String.format("%s (%s), %s %s",
- subtag.getName(), fqnTag, num, stories));
+ displayTag(subtag);
}
} else {
System.out.println("Invalid item: only " + count
if (metas != null) {
// TODO i18n
System.out.println(String.format("Content of %s: ",
- fqnTag));
+ stag.getFqName()));
displayStories(metas);
} else {
// TODO i18n
System.out.println(String.format("Subtags of %s: ",
- fqnTag));
- int i = 1;
- for (SearchableTag subtag : subtags) {
- String total = "";
- if (subtag.getCount() > 0) {
- total = StringUtils.formatNumber(subtag
- .getCount());
- }
-
- if (total.isEmpty()) {
- System.out.println(String.format("%d: %s", i,
- subtag.getName()));
- } else {
- System.out.println(String.format("%d: %s (%s)",
- i, subtag.getName(), total));
- }
-
- i++;
- }
+ stag.getFqName()));
+ displayTags(subtags);
}
}
}
- } else {
- // TODO i18n
- System.out.println("Known tags: ");
- int i = 1;
- for (SearchableTag s : stags) {
- System.out.println(String.format("%d: %s", i, s.getName()));
- i++;
- }
}
}
+ private void displayTag(SearchableTag subtag) {
+ // TODO: i18n
+ String stories = "stories";
+ String num = StringUtils.formatNumber(subtag.getCount());
+ System.out.println(String.format("%s (%s), %s %s", subtag.getName(),
+ subtag.getFqName(), num, stories));
+ }
+
private void displayStory(MetaData meta) {
System.out.println(meta.getTitle());
System.out.println();
}
}
+ private void displayTags(List<SearchableTag> subtags) {
+ int i = 1;
+ for (SearchableTag subtag : subtags) {
+ String total = "";
+ if (subtag.getCount() > 0) {
+ total = StringUtils.formatNumber(subtag.getCount());
+ }
+
+ if (total.isEmpty()) {
+ System.out
+ .println(String.format("%d: %s", i, subtag.getName()));
+ } else {
+ System.out.println(String.format("%d: %s (%s)", i,
+ subtag.getName(), total));
+ }
+
+ i++;
+ }
+ }
+
private void displayStories(List<MetaData> metas) {
int i = 1;
for (MetaData meta : metas) {
support = BasicSupport.getSupport(getType(), null);
}
+ /**
+ * Find the given tag by its hierarchical IDs.
+ * <p>
+ * I.E., it will take the tag A, subtag B, subsubtag C...
+ *
+ * @param ids
+ * the IDs to look for
+ *
+ * @return the appropriate tag fully filled, or NULL if not found
+ *
+ * @throws IOException
+ * in case of I/O error
+ */
+ public SearchableTag getTag(Integer... ids) throws IOException {
+ SearchableTag tag = null;
+ List<SearchableTag> tags = getTags();
+
+ for (Integer tagIndex : ids) {
+ // ! 1-based index !
+ if (tagIndex == null || tags == null || tagIndex <= 0
+ || tagIndex > tags.size()) {
+ return null;
+ }
+
+ tag = tags.get(tagIndex - 1);
+ fillTag(tag);
+ tags = tag.getChildren();
+ }
+
+ return tag;
+ }
+
/**
* The support type.
*
*
* @return an implementation that supports it, or NULL
*/
- public static BasicSearchable getSearchable(SupportType type) {
+ static public BasicSearchable getSearchable(SupportType type) {
BasicSearchable support = null;
switch (type) {
* the tag is a leaf tag, that is, it will not return subtags
* with {@link BasicSearchable#fillTag(SearchableTag)} but will
* return stories with
- * {@link BasicSearchable#search(SearchableTag)}
+ * {@link BasicSearchable#search(SearchableTag, int)}
*/
public SearchableTag(String id, String name, boolean leaf) {
this(id, name, leaf, true);
* the tag is a leaf tag, that is, it will not return subtags
* with {@link BasicSearchable#fillTag(SearchableTag)} but will
* return stories with
- * {@link BasicSearchable#search(SearchableTag)}
+ * {@link BasicSearchable#search(SearchableTag, int)}
* @param complete
* the tag {@link SearchableTag#isComplete()} or not
*/
return name;
}
+ /**
+ * The fully qualified tag name, which can be displayed to the user.
+ * <p>
+ * It will display all the tags that lead to this one as well as this one.
+ *
+ * @return the fully qualified name
+ */
+ public String getFqName() {
+ if (parent != null) {
+ return parent.getFqName() + " / " + name;
+ }
+
+ return name;
+ }
+
/**
* Non-complete, non-leaf tags can still be completed via a
* {@link BasicSearchable#fillTag(SearchableTag)} operation from a
/**
* This tag is a leaf tag, that is, it will not return other subtags with
* {@link BasicSearchable#fillTag(SearchableTag)} but will return stories
- * with {@link BasicSearchable#search(SearchableTag)}.
+ * with {@link BasicSearchable#search(SearchableTag, int)}.
*
* @return TRUE if it is
*/
/**
* This tag is a leaf tag, that is, it will not return other subtags with
* {@link BasicSearchable#fillTag(SearchableTag)} but will return stories
- * with {@link BasicSearchable#search(SearchableTag)}.
+ * with {@link BasicSearchable#search(SearchableTag, int)}.
* <p>
* Will reset the number of pages to -1.
*