/* Copyright (c) 2016 W.T.J. Riezebos
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.riezebos.thoth.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.riezebos.thoth.beans.Book;
import net.riezebos.thoth.beans.BookClassification;

/**
 * @author wido
 */
public class Classifier {
  public List<BookClassification> getClassifications(List<Book> books, String metaTagName, String defaultValue) {
    Map<String, BookClassification> classificationMap = new HashMap<>();

    for (Book book : books) {

      String classificationSpec = book.getMetaTag(metaTagName);

      if (classificationSpec == null) {
        if ("folder".equalsIgnoreCase(metaTagName))
          classificationSpec = book.getFolder();
        else
          classificationSpec = defaultValue;
      }

      for (String classificationName : ThothUtil.tokenize(classificationSpec)) {
        BookClassification classification = classificationMap.get(classificationName);
        if (classification == null) {
          classification = new BookClassification(classificationName);
          classificationMap.put(classificationName, classification);
        }
        classification.getBooks().add(book);
      }
    }

    for (BookClassification classification : classificationMap.values())
      classification.sortBooks();

    List<BookClassification> result = new ArrayList<>();
    result.addAll(classificationMap.values());
    Collections.sort(result);
    return result;
  }

}

@@ -760,16 +760,62 @@
 l.Map;%0A%0A
+import org.apache.commons.lang3.StringUtils;%0A%0A
 import n
@@ -1263,32 +1263,78 @@
 Spec == null) %7B%0A
+        // Special case for meta tag 'folder'%0A
         if (%22fol
@@ -1368,16 +1368,18 @@
 agName))
+ %7B
 %0A       
@@ -1427,16 +1427,111 @@
 %0A       
+   if (StringUtils.isBlank(classificationSpec))%0A            classificationSpec = %22/%22;%0A        %7D
  else%0A  

Classify empty folder as root