com.revorg.goat.Document.java Source code

Java tutorial

Introduction

Here is the source code for com.revorg.goat.Document.java

Source

package com.revorg.goat;

import java.lang.String;

import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

/*
 * Copyright (c) Grover C. Fields, http://www.groverfields.com/, 2005-2009.
 * All rights reserved. Software written by Grover C. Fields and others.
 * $Id: LICENSE,v 1.0 2008/12/01 05:00:00 Grover Exp $
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * Java, the Duke mascot, and all variants of Sun's Java "steaming coffee
 * cup" logo are trademarks of Sun Microsystems. Sun's, and James Gosling's,
 * pioneering role in inventing and promulgating (and standardizing) the Java
 * language and environment is gratefully acknowledged.
 *
 * The pioneering role of Dennis Ritchie and Bjarne Stroustrup, of AT&T, for
 * inventing predecessor languages C and C++ is also gratefully acknowledged.
 */

/**
 * This class manages the Documents API Calls
 *
 * @version 1.0
 * @author  Grover C. Fields  (grover_fields@yahoo.com)
 * @author  http://www.groverfields.com
 */

public class Document implements java.io.Serializable {

    private static String ActionResult;
    private static String ActionResultError;
    private static IndexReader reader;
    private static IndexWriter writer;

    public Document() {
    }

    /*
     * Deletes a document from an index based on the "PrimaryKey" Field.
     * @param indexPath Directory that contains the Lucene Collection
     * @param primaryKey    Primary Key for the database row
     * @throws Exception
     * @return ActionResult
     */
    public static String deleteDBDocument(String indexPath, String primaryKey) {

        try {
            XMLReader readerXML = new XMLReader(); //XML Reader Class   
            String configFile = ConfigFiles.getSchemaFile(indexPath);
            String[] indexTypeArray = new String[Integer.parseInt(readerXML.getTotalNodes(configFile))];
            String[] columnNamesArray = new String[Integer.parseInt(readerXML.getTotalNodes(configFile))];
            int totalNodes = columnNamesArray.length;
            String fieldName = "";
            //Get Column Names
            for (int i = 0; i < totalNodes; i++) {
                columnNamesArray[i] = readerXML.getNodeValueByFile(configFile, i, "columnname");
                indexTypeArray[i] = readerXML.getNodeValueByFile(configFile, i, "indextype");
                //System.out.println(indexTypeArray[i] + " " + columnNamesArray[i]);
                if (indexTypeArray[i].equalsIgnoreCase("primarykey")) {
                    fieldName = columnNamesArray[i];
                    break;
                }
            }
            int totalDeleted = 0;
            int totalTerms = 0;
            int deleted = 0;
            Directory directory = FSDirectory.getDirectory(indexPath);
            IndexReader reader = IndexReader.open(directory);
            StringTokenizer tokenizer = new StringTokenizer(primaryKey, ",");
            String nextTerm;
            Term term = new Term("goat", "goat");
            while (tokenizer.hasMoreTokens()) {
                totalTerms++; //Total Terms
                nextTerm = tokenizer.nextToken().trim();
                term = new Term(fieldName, nextTerm);
                deleted = reader.deleteDocuments(term);
                if (deleted != 0) {
                    totalDeleted++;
                }

            }
            reader.close();
            directory.close();

            if (totalDeleted > 0) {
                return "Success: Total of " + totalDeleted + "/" + totalTerms + " documents deleted";
            } else {
                return "Failure: No Documents Match the term " + term;
            }
        } catch (Exception e) {
            ActionResultError = " caught a " + e.getClass() + " with message: " + e.getMessage();
            System.out.println("Failure to count index: " + indexPath);
        }
        ActionResult = "Failure";
        return ActionResult + ActionResultError;
    }

    /**
     * Returns the list of fields for a particular Document.
     * @param indexPath Directory that contains the Lucene Collection
     * @throws Exception
     * @return ActionResult
     */
    public static List getDocumentFields(String indexPath) {
        //Assign Document to Lucene Document
        org.apache.lucene.document.Document doc = new org.apache.lucene.document.Document();

        try {

            IndexReader reader = IndexReader.open(indexPath);
            doc = reader.document(0);
            reader.close();
            List AllTheFields = doc.getFields();
            return AllTheFields;

        } catch (Exception e) {
            ActionResultError = " caught a " + e.getClass() + " with message: " + e.getMessage();
            System.out.println("Failure on getDocumentFields ");
        }
        ActionResult = "Failure";
        return new LinkedList();
    }

    /**
     * Expunges all deletes from the index.
     *
     * @param indexPath Directory that contains the Lucene Collection
     * @throws Exception
     * @return ActionResult
     */
    private static String expungeDBDeletes(String indexPath) {
        try {
            IndexWriter writer = new IndexWriter(indexPath, new StandardAnalyzer(), false,
                    IndexWriter.MaxFieldLength.LIMITED);
            int totalInIndex = writer.maxDoc();
            ActionResult = Integer.toString(totalInIndex);
            writer.expungeDeletes();
            return ActionResult;
        } catch (Exception e) {
            ActionResultError = " caught a " + e.getClass() + " with message: " + e.getMessage();
            System.out.println("Failure to optimize index: " + indexPath);
        }
        ActionResult = "Failure";
        return ActionResult + ActionResultError;
    }
}