Package org.apache.xindice.webadmin.viewer.components

Source Code of org.apache.xindice.webadmin.viewer.components.CollectionIndexesViewer

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*
* $Id: CollectionIndexesViewer.java 541515 2007-05-25 02:45:06Z vgritsenko $
*/

package org.apache.xindice.webadmin.viewer.components;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.xindice.core.Collection;
import org.apache.xindice.core.DBException;
import org.apache.xindice.core.indexer.Indexer;
import org.apache.xindice.util.Configuration;
import org.apache.xindice.webadmin.viewer.HtmlViewerComponent;
import org.apache.xindice.webadmin.viewer.HtmlCollectionViewer;
import org.apache.xindice.xml.dom.DocumentImpl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
* Xindice Html Viewer for Collection Indexers.
*
* @author <a href="mailto:jmetzner@apache.org">Jan Metzner</a>
* @version $Revision: 541515 $, $Date: 2007-05-24 22:45:06 -0400 (Thu, 24 May 2007) $
*/
public class CollectionIndexesViewer extends HtmlViewerComponent implements HtmlCollectionViewer {

    protected static final String XINDICE_VAL_INDEXER = "org.apache.xindice.core.indexer.ValueIndexer";
    protected static final String XINDICE_NAME_INDEXER = "org.apache.xindice.core.indexer.NameIndexer";

    private static final Log log = LogFactory.getLog(CollectionIndexesViewer.class);

    public void execute(HttpServletRequest req, HttpServletResponse res, Collection col)
            throws ServletException, IOException {

        String path = col.getCanonicalName();
        String title = "Collection Indexes for " + path;

        ServletOutputStream output = startViewer(title, path, res);

        String name = req.getParameter("name");
        String pattern = req.getParameter("pattern");
        String type = req.getParameter("type");
        String pagesize = req.getParameter("pagesize");
        String maxkeysize = req.getParameter("maxkeysize");

        String delete = req.getParameter("delete");

        if (name != null && pattern != null && name.length() > 0 && pattern.length() > 0) {
            // create index
            try {
                createIndex(col, name, pattern, type, pagesize, maxkeysize);
            } catch (DBException e) {
                log.error(e);
                printStartTable("Message", output);
                printStartSingleTableBox(output);
                output.println("<b>Cannot create Indexer " + name + ": " + e.getMessage() + "</b>");
                printEndSingleTableBox(output);
                printEndTable(output);
                output.println("<br />\n");
            }
        } else if (delete != null && delete.length() > 0) {
            printStartTable("Message", output);
            printStartSingleTableBox(output);
            try {
                deleteIndex(col, delete, output);
            } catch (DBException e) {
                log.error(e);
                output.println("<b>Cannot delete Indexer " + name + ": " + e.getMessage() + "</b>");
            }
            printEndSingleTableBox(output);
            printEndTable(output);
            output.println("<br />\n");
        }

        // form
        printStartTable("Add Index for " + path, output);
        printStartSingleTableBox(output);
        output.print(INDEXES_UPDATE_FORM_1);
        output.print(INDEXES_UPDATE_FORM_2);
        output.print(INDEXES_UPDATE_FORM_3);
        printTypeSelector(output);
        output.print(INDEXES_UPDATE_FORM_4);
        output.print(INDEXES_UPDATE_FORM_5);
        output.print(INDEXES_UPDATE_FORM_6);
        printEndSingleTableBox(output);
        printEndTable(output);

        output.print("<br />");
        output.flush();

        try {
            listIndexes(col, output);
        } catch (DBException e) {
            log.error(e);
            printStartTable("Error", output);
            printStartSingleTableBox(output);
            output.print("Sorry, cannot get Indexes: ");
            output.print(e.getMessage());
            printEndSingleTableBox(output);
            printEndTable(output);
        }
        finishViewer(output);
    }

    private void listIndexes(Collection col, ServletOutputStream output) throws DBException, IOException {
        String[] indexes = col.listIndexers();
        if (indexes.length == 0) {
            String message = "Collection contains no Indexes!";
            printStartTable(message, output);
            printStartSingleTableBox(output);
            output.print(message);
            printEndSingleTableBox(output);
            printEndTable(output);
        } else {
            String[] columnTitles = {"name", "pattern", "class", "type",
                                     "pagesize", "max key size", "delete"};
            printStartTable(columnTitles, output);
            for (int i = 0; i < indexes.length; i++) {
                Configuration thisConf = col.getIndexer(indexes[i]).getConfig();
                String indexerName = thisConf.getAttribute("name");
                String indexerType = thisConf.getAttribute("type", "");
                String indexerClass = thisConf.getAttribute("class");
                if (indexerClass.equalsIgnoreCase(XINDICE_NAME_INDEXER)) {
                    indexerClass = "NameIndexer";
                    indexerType = "name";
                } else {
                    indexerClass = "ValueIndexer";
                }
                String[] contents = {
                        "<b>" + indexerName + "</b>",
                        "<b>" + thisConf.getAttribute("pattern") + "</b>",
                        indexerClass,
                        indexerType,
                        thisConf.getAttribute("pagesize", ""),
                        thisConf.getAttribute("maxkeysize", ""),
                        "<a href=\"?viewer=" + getName() + "&delete="
                        + indexerName + "\">delete</a>"
                };
                printTableRow(contents, output);
            }
            printEndTable(output);
        }
    }

    private void deleteIndex(Collection col, String name, ServletOutputStream output) throws IOException, DBException {
        // delete indexer
        Indexer delIndex = col.getIndexer(name);
        if (delIndex != null) {
            if (col.dropIndexer(delIndex)) {
                output.println("<b>Indexer " + name + " deleted!</b>");
            } else {
                output.println("<b>Cannot delete Indexer " + name + "!</b>");
            }
        } else {
            output.println("<b>Cannot get Indexer " + name + "!</b>");
        }
    }

    private Indexer createIndex(Collection col, String name, String pattern, String type, String pagesize, String maxkeysize)
            throws DBException {

        Document doc = new DocumentImpl();

        Element idxEle = doc.createElement("index");
        idxEle.setAttribute("name", name);
        idxEle.setAttribute("pattern", pattern);
        idxEle.setAttribute("class", XINDICE_VAL_INDEXER);

        if (type != null && type.length() > 0) {
            if (type.equalsIgnoreCase("name")) {
                idxEle.setAttribute("class", XINDICE_NAME_INDEXER);
            } else {
                idxEle.setAttribute("type", type);
            }
        }

        if (pagesize != null && pagesize.length() > 0) {
            idxEle.setAttribute("pagesize", pagesize);
        }

        if (maxkeysize != null && maxkeysize.length() > 0) {
            idxEle.setAttribute("maxkeysize", maxkeysize);
        }

        doc.appendChild(idxEle);
        return col.createIndexer(new Configuration(doc, false));
    }

    private void printTypeSelector(ServletOutputStream output) throws IOException {
        output.println("<select name=\"type\" size\"1\">");
        output.println("<option value=\"\">default</option>");
        for (int i = 0; i < IDX_TYPES.length; i += 2) {
            output.print("<option value=\"" + IDX_TYPES[i] + "\"");
            output.print(" >" + IDX_TYPES[i] + ": " + IDX_TYPES[i + 1] + "</option>");
        }
        output.println("</select>");
    }

    /**
     * index types as pair: type, description
     */
    private static final String[] IDX_TYPES = {
            "string", "Non-normalized string value",
            "trimmed", "Normalized (whitespace stripped) string value",
            "short", "16-bit signed short integer",
            "int", "32-bit signed integer",
            "long", "64-bit signed integer",
            "float", "32-bit floating point value",
            "double", "64-bit floating point value (XPath number)",
            "byte", "8-bit signed byte",
            "char", "16-bit signed character",
            "boolean", "8-bit boolean value",
            "name", "Store document keys that contain the pattern",
    };

    private static final String INDEXES_UPDATE_FORM_1 =
            "<form action=\"\" method=\"get\"> \n" +
            "<input name=\"viewer\" value=\"indexes\" type=\"hidden\" /> \n" +
            "<table class=\"upload-form\"> \n" +
            "<tr><td> \n" +
            "  index name:  \n" +
            "</td><td> \n" +
            "  <input name=\"name\" type=\"text\" value=\"";

    private static final String INDEXES_UPDATE_FORM_2 =
            "\" /> \n" +
            "</td><td> \n" +
            "  pattern:  \n" +
            "</td><td> \n" +
            "  <input name=\"pattern\" type=\"text\" value=\"";

    private static final String INDEXES_UPDATE_FORM_3 =
            "\" /> \n" +
            " </td></tr> \n" +
            " <tr><td colspan=\"4\">&nbsp;</td></tr> \n" +
            "<tr><td> \n" +
            "  type:  \n" +
            "</td><td colspan=\"3\"> \n";

    private static final String INDEXES_UPDATE_FORM_4 =
            " </td></tr> \n" +
            "<tr><td> \n" +
            "  pagesize:  \n" +
            "</td><td> \n" +
            "  <input name=\"pagesize\" type=\"text\" value=\"";

    private static final String INDEXES_UPDATE_FORM_5 =
            "\" /> \n" +
            " </td><td> \n" +
            "  max key size:  \n" +
            "</td><td> \n" +
            "  <input name=\"maxkeyesize\" type=\"text\" value=\"";

    private static final String INDEXES_UPDATE_FORM_6 =
            "\" /> \n" +
            " <tr><td colspan=\"4\" align=\"center\"> \n" +
            "  <input type=\"submit\" name=\"submit\" value=\"add\" /> \n" +
            " </td></tr> \n" +
            " </table> \n" +
            " </form> \n";

    protected String getName() {
        return "indexes";
    }
}
TOP

Related Classes of org.apache.xindice.webadmin.viewer.components.CollectionIndexesViewer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.