search cli: code cleanup
authorNiki Roo <niki@nikiroo.be>
Sun, 14 Apr 2019 15:57:13 +0000 (17:57 +0200)
committerNiki Roo <niki@nikiroo.be>
Sun, 14 Apr 2019 15:57:13 +0000 (17:57 +0200)
src/be/nikiroo/fanfix/reader/cli/CliReader.java
src/be/nikiroo/fanfix/searchable/BasicSearchable.java
src/be/nikiroo/fanfix/searchable/SearchableTag.java

index d8d3a6c1e70b9a5b317d4c62e9f6bcf89783fb16..3795cd8800a526cf28efe8efd0dc450974ff9029 100644 (file)
@@ -1,7 +1,6 @@
 package be.nikiroo.fanfix.reader.cli;
 
 import java.io.IOException;
-import java.util.ArrayList;
 import java.util.List;
 
 import be.nikiroo.fanfix.Instance;
@@ -130,32 +129,19 @@ class CliReader extends BasicReader {
        @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);
@@ -183,13 +169,7 @@ class CliReader extends BasicReader {
                                                        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
@@ -199,44 +179,27 @@ class CliReader extends BasicReader {
                                        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();
@@ -250,6 +213,26 @@ class CliReader extends BasicReader {
                }
        }
 
+       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) {
index 0449b818caa9ec88f9557acaa67460bfce084382..d8076fa7a1b87f99e452cddcef30100305884c7b 100644 (file)
@@ -34,6 +34,38 @@ public abstract class BasicSearchable {
                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.
         * 
@@ -189,7 +221,7 @@ public abstract class BasicSearchable {
         * 
         * @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) {
index c12b3c68eeeb5b0d3c2135b521bf5bbd354dbfa3..f73dd15d7ce627fc3d3f7a72eb16959db52c96a6 100644 (file)
@@ -43,7 +43,7 @@ public class SearchableTag {
         *            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);
@@ -60,7 +60,7 @@ public class SearchableTag {
         *            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
         */
@@ -92,6 +92,21 @@ public class SearchableTag {
                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
@@ -171,7 +186,7 @@ public class SearchableTag {
        /**
         * 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
         */
@@ -182,7 +197,7 @@ public class SearchableTag {
        /**
         * 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.
         *