Coverage Report - org.crosswire.jsword.index.lucene.IndexMetadata
 
Classes in this File Line Coverage Branch Coverage Complexity
IndexMetadata
0%
0/42
0%
0/10
1.778
 
 1  
 /**
 2  
  * Distribution License:
 3  
  * JSword is free software; you can redistribute it and/or modify it under
 4  
  * the terms of the GNU Lesser General Public License, version 2.1 or later
 5  
  * as published by the Free Software Foundation. This program is distributed
 6  
  * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 7  
  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 8  
  * See the GNU Lesser General Public License for more details.
 9  
  *
 10  
  * The License is available on the internet at:
 11  
  *      http://www.gnu.org/copyleft/lgpl.html
 12  
  * or by writing to:
 13  
  *      Free Software Foundation, Inc.
 14  
  *      59 Temple Place - Suite 330
 15  
  *      Boston, MA 02111-1307, USA
 16  
  *
 17  
  * © CrossWire Bible Society, 2007 - 2016
 18  
  *
 19  
  */
 20  
 package org.crosswire.jsword.index.lucene;
 21  
 
 22  
 import java.io.IOException;
 23  
 
 24  
 import org.crosswire.common.util.PropertyMap;
 25  
 import org.crosswire.common.util.ResourceUtil;
 26  
 import org.crosswire.jsword.book.Book;
 27  
 import org.crosswire.jsword.book.BookFilter;
 28  
 import org.crosswire.jsword.book.BookMetaData;
 29  
 import org.crosswire.jsword.book.Books;
 30  
 import org.crosswire.jsword.index.IndexManagerFactory;
 31  
 import org.slf4j.Logger;
 32  
 import org.slf4j.LoggerFactory;
 33  
 
 34  
 /**
 35  
  * A singleton that Reads and Maintains IndexMetadata from properties file All
 36  
  * version number in the properties file must be float.
 37  
  * 
 38  
  * @see gnu.lgpl.License The GNU Lesser General Public License for details.
 39  
  * @author Sijo Cherian
 40  
  */
 41  
 public final class IndexMetadata {
 42  
 
 43  
     /** latest version on top */
 44  
     public static final float INDEX_VERSION_1_2 = 1.2f;
 45  
     /**
 46  
      * A prior version.
 47  
      * 
 48  
      * @deprecated do not use
 49  
      */
 50  
     @Deprecated
 51  
     public static final float INDEX_VERSION_1_1 = 1.1f;
 52  
 
 53  
     public static final String LATEST_INDEX_VERSION = "Latest.Index.Version";
 54  
     public static final String LUCENE_VERSION = "Lucene.Version";
 55  
 
 56  
     public static final String PREFIX_LATEST_INDEX_VERSION_BOOK_OVERRIDE = "Latest.Index.Version.Book.";
 57  
     /**
 58  
      * @deprecated do not use
 59  
      */
 60  
     @Deprecated
 61  
     public static final String INDEX_VERSION = "Installed.Index.Version";
 62  
 
 63  
     /**
 64  
      * All access to IndexMetadata is through this single instance.
 65  
      * 
 66  
      * @return the singleton instance
 67  
      */
 68  
     public static IndexMetadata instance() {
 69  0
         return myInstance;
 70  
     }
 71  
 
 72  
     /**
 73  
      * default Installed IndexVersion
 74  
      * 
 75  
      * @return the index version
 76  
      * @deprecated see InstalledIndex.java
 77  
      */
 78  
     @Deprecated
 79  
     public float getInstalledIndexVersion() {
 80  0
         String value = props.get(INDEX_VERSION, "1.1"); // todo At some point
 81  
                                                         // default should be 1.2
 82  0
         return Float.parseFloat(value);
 83  
     }
 84  
 
 85  
     // Default Latest IndexVersion : Default version number of Latest indexing
 86  
     // schema: PerBook index version must be equal or greater than this
 87  
     public float getLatestIndexVersion() {
 88  0
         String value = props.get(LATEST_INDEX_VERSION, "1.2");
 89  0
         return Float.parseFloat(value);
 90  
     }
 91  
 
 92  
     public float getLatestIndexVersion(Book b) {
 93  0
         if (b == null) {
 94  0
             return getLatestIndexVersion();
 95  
         }
 96  
 
 97  0
         String value = props.get(PREFIX_LATEST_INDEX_VERSION_BOOK_OVERRIDE + IndexMetadata.getBookIdentifierPropSuffix(b.getBookMetaData()),
 98  
                 props.get(LATEST_INDEX_VERSION));
 99  0
         return Float.parseFloat(value);
 100  
     }
 101  
 
 102  
     public String getLatestIndexVersionStr() {
 103  0
         String value = props.get(LATEST_INDEX_VERSION, "1.2");
 104  0
         return value;
 105  
     }
 106  
 
 107  
     // used in property keys e.g. Installed.Index.Version.Book.ESV[1.0.1]
 108  
     public static String getBookIdentifierPropSuffix(BookMetaData meta) {
 109  0
         String moduleVer = null;
 110  0
         if (meta.getProperty("Version") != null) {
 111  0
             moduleVer = '[' + meta.getProperty("Version") + ']';
 112  
         }
 113  
 
 114  0
         return meta.getInitials() + moduleVer;
 115  
 
 116  
     }
 117  
 
 118  
     public float getLuceneVersion() {
 119  0
         return Float.parseFloat(props.get(LUCENE_VERSION));
 120  
     }
 121  
 
 122  0
     private IndexMetadata() {
 123  
         try {
 124  0
             props = ResourceUtil.getProperties(getClass());
 125  0
         } catch (IOException e) {
 126  0
             log.error("Property file read error", e);
 127  0
         }
 128  0
     }
 129  
 
 130  
     // a index status summary in English
 131  
     public static String generateInstalledBooksIndexVersionReport(BookFilter filter) {
 132  0
         StringBuilder toReturn = new StringBuilder();
 133  0
         int installedBookCount = 0;
 134  0
         int searchEnabledBookCount = 0;
 135  0
         int reindexMandatoryBookCount = 0;
 136  0
         LuceneIndexManager indexManager = (LuceneIndexManager) IndexManagerFactory.getIndexManager();
 137  0
         Books myBooks = Books.installed();
 138  0
         toReturn.append("InstalledBooks:");
 139  0
         for (Book insBook : myBooks.getBooks(filter)) {
 140  0
             installedBookCount++;
 141  0
             toReturn.append("\n\t").append(insBook.getBookMetaData().getInitials()).append(": ");
 142  0
             if (indexManager.isIndexed(insBook)) {
 143  0
                 searchEnabledBookCount++;
 144  0
                 toReturn.append("search enabled, ");
 145  0
                 if (indexManager.needsReindexing(insBook)) {
 146  0
                     reindexMandatoryBookCount++;
 147  0
                     toReturn.append("index outdated, ");
 148  
                 }
 149  
             }
 150  
 
 151  
         }
 152  0
         toReturn.append("\nSummary: installedBooks ").append(installedBookCount).append(", searchEnabledBooks ").append(searchEnabledBookCount)
 153  
                 .append(", booksWithOutdatedIndex ").append(reindexMandatoryBookCount).append("\n");
 154  0
         return toReturn.toString();
 155  
     }
 156  
 
 157  0
     private static final Logger log = LoggerFactory.getLogger(IndexMetadata.class);
 158  0
     private static IndexMetadata myInstance = new IndexMetadata();
 159  
     private PropertyMap props;
 160  
 }