+ public void searchBooksByKeyword(SupportType searchOn, String keywords,
+ int page, int item) throws IOException {
+ BasicSearchable search = BasicSearchable.getSearchable(searchOn);
+
+ if (page == 0) {
+ System.out.println(search.searchPages(keywords));
+ } else {
+ List<MetaData> metas = search.search(keywords, page);
+
+ if (item == 0) {
+ System.out.println(
+ "Page " + page + " of stories for: " + keywords);
+ displayStories(metas);
+ } else {
+ // ! 1-based index !
+ if (item <= 0 || item > metas.size()) {
+ throw new IOException("Index out of bounds: " + item);
+ }
+
+ MetaData meta = metas.get(item - 1);
+ displayStory(meta);
+ }
+ }
+ }
+
+ public void searchBooksByTag(SupportType searchOn, int page, int item,
+ Integer... tags) throws IOException {
+
+ BasicSearchable search = BasicSearchable.getSearchable(searchOn);
+ SearchableTag stag = search.getTag(tags);
+
+ 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++;
+ }
+ } else {
+ if (page <= 0) {
+ if (stag.isLeaf()) {
+ System.out.println(search.searchPages(stag));
+ } else {
+ System.out.println(stag.getCount());
+ }
+ } else {
+ List<MetaData> metas = null;
+ List<SearchableTag> subtags = null;
+ int count;
+
+ if (stag.isLeaf()) {
+ metas = search.search(stag, page);
+ count = metas.size();
+ } else {
+ subtags = stag.getChildren();
+ count = subtags.size();
+ }
+
+ if (item > 0) {
+ if (item <= count) {
+ if (metas != null) {
+ MetaData meta = metas.get(item - 1);
+ displayStory(meta);
+ } else {
+ SearchableTag subtag = subtags.get(item - 1);
+ displayTag(subtag);
+ }
+ } else {
+ System.out.println(
+ "Invalid item: only " + count + " items found");
+ }
+ } else {
+ if (metas != null) {
+ // TODO i18n
+ System.out.println(String.format("Content of %s: ",
+ stag.getFqName()));
+ displayStories(metas);
+ } else {
+ // TODO i18n
+ System.out.println(String.format("Subtags of %s: ",
+ stag.getFqName()));
+ displayTags(subtags);
+ }
+ }