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

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

/*
* 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: MetadataCollectionViewer.java 564043 2007-08-08 23:11:58Z vgritsenko $
*/

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

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xindice.core.Collection;
import org.apache.xindice.core.DBException;
import org.apache.xindice.core.meta.MetaData;
import org.apache.xindice.util.XindiceException;
import org.apache.xindice.webadmin.viewer.HtmlCollectionViewer;
import org.apache.xindice.webadmin.viewer.HtmlViewerComponent;
import org.apache.xindice.xml.TextWriter;
import org.apache.xindice.xml.Xml2HtmlWriter;
import org.apache.xindice.xml.dom.DOMParser;
import org.apache.xindice.xml.dom.DocumentImpl;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

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

import java.io.IOException;
import java.util.Enumeration;
import java.util.Iterator;

/**
* Xindice Html Viewer for Collection Metadata.
*
* @author <a href="mailto:jmetzner@apache.org">Jan Metzner</a>
* @version $Revision: 564043 $, $Date: 2007-08-08 19:11:58 -0400 (Wed, 08 Aug 2007) $
*/
public class MetadataCollectionViewer extends HtmlViewerComponent implements HtmlCollectionViewer {

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

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

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

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

        String attrName = req.getParameter("attrname");
        if (attrName == null) {
            attrName = "";
        }
        String attrValue = req.getParameter("attrvalue");
        if (attrValue == null) {
            attrValue = "";
        }
        String updateAttr = req.getParameter("updateattr");
        String deleteAttr = req.getParameter("deleteattr");
        String custom = req.getParameter("custom");
        String deleteCustom = req.getParameter("deleteCustom");

        //  add attribute
        if (attrName.length() > 0 && col.isMetaEnabled() && updateAttr == null) {
            try {
                MetaData newMeta = col.getCollectionMeta();
                newMeta.setAttribute(attrName, attrValue);
                col.setCollectionMeta(newMeta);
                attrName = "";
                attrValue = "";
            } catch (DBException e1) {
                log.error(e1);
                printError("Cannot add Attribute " + attrName + ": " + e1.getMessage(), output);
            }
        } else if (updateAttr != null && updateAttr.length() > 0 && col.isMetaEnabled()) {
            attrName = updateAttr;
            try {
                attrValue = (String) col.getCollectionMeta().getAttribute(attrName);
            } catch (DBException e) {
                log.error(e);
                attrValue = "";
            }
        } else if (deleteAttr != null && deleteAttr.length() > 0 && col.isMetaEnabled()) {
            try {
                MetaData delMeta = col.getCollectionMeta();
                delMeta.removeAttribute(deleteAttr);
                col.setCollectionMeta(delMeta);
            } catch (DBException e) {
                log.error(e);
                printError("Cannot delete Attribute " + deleteAttr + ": " + e.getMessage(), output);
            }
        } else if (custom != null && custom.length() > 0 && col.isMetaEnabled()) {
            try {
                MetaData customMeta = col.getCollectionMeta();
                Document customDoc = DOMParser.toDocument(custom);
                customMeta.setCustomDocument(customDoc);
                col.setCollectionMeta(customMeta);
            } catch (DBException e) {
                log.error(e);
                printError("Cannot add/update Custom: " + e.getMessage(), output);
            } catch (XindiceException e) {
                log.error(e);
                printError("Cannot add/update Custom: " + e.getMessage(), output);
            }
        } else if (deleteCustom != null && deleteCustom.length() > 0 && col.isMetaEnabled()) {
            try {
                MetaData customMeta = col.getCollectionMeta();
                if (customMeta != null) {
                    customMeta.setCustomDocument(null);
                    col.setCollectionMeta(customMeta);
                }
            } catch (DBException e) {
                log.error(e);
                printError("Cannot delete Custom: " + e.getMessage(), output);
            }
        } else if (req.getMethod().equalsIgnoreCase("POST")) {
            byte[] content = new byte[0];
            try {
                //  Create a new file upload handler
                ServletFileUpload upload = new ServletFileUpload();
                // Set upload parameters
                upload.setSizeMax(-1);
                upload.setFileItemFactory(new DiskFileItemFactory());

                // Process the uploaded fields
                Iterator i = upload.parseRequest(req).iterator();
                while (i.hasNext()) {
                    FileItem item = (FileItem) i.next();
                    if (!item.isFormField()) {
                        if (item.getFieldName().equals("customfile")) {
                            content = item.get();
                        }
                    }
                    item.delete();
                }
            } catch (FileUploadException e) {
                log.error(e);
            }

            if (content.length > 0) {
                Document customDoc;
                try {
                    customDoc = DOMParser.toDocument(content);
                } catch (XindiceException e1) { // document fragment ?
                    log.error(e1);
                    StringBuffer newContent = new StringBuffer();
                    newContent.append("<custom>").append(new String(content)).append("</custom>");
                    try {
                        customDoc = DOMParser.toDocument(newContent.toString());
                    } catch (XindiceException e2) { // no xml content
                        log.error(e2);
                        printError("Cannot parse Custom: " + e2.getMessage(), output);
                        customDoc = null;
                    }
                }
                if (customDoc != null) {
                    if (!customDoc.getDocumentElement().getNodeName().equals("custom")) {
                        Document newCustomDoc = new DocumentImpl();
                        Element customEle = newCustomDoc.createElement("custom");
                        customEle.appendChild(customDoc.getDocumentElement());
                        newCustomDoc.appendChild(customEle);
                        customDoc = newCustomDoc;
                    }
                    try {
                        MetaData customMeta = col.getCollectionMeta();
                        customMeta.setCustomDocument(customDoc);
                        col.setCollectionMeta(customMeta);
                    } catch (DBException e2) {
                        log.error(e2);
                        printError("Cannot create Custom: " + e2.getMessage(), output);
                    }
                }
            }
        }

        printStartTable("Add/Update Metadata Attribute", output);
        printStartSingleTableBox(output);
        output.print(META_ATTR_UPDATE_FORM_START);
        output.print(attrName);
        output.print(META_ATTR_UPDATE_FORM_MIDDLE);
        output.print(attrValue);
        output.print(META_ATTR_UPDATE_FORM_END);
        printEndSingleTableBox(output);
        printEndTable(output);
        output.println("<br />\n");

        if (col.isMetaEnabled()) {
            MetaData metaData;
            try {
                metaData = col.getCollectionMeta();
            } catch (DBException e) {
                log.error(e);
                metaData = null;
            }
            if (metaData != null) {
                printAttributes(metaData, output);
                output.println("<br />\n");
                printCustom(metaData, output);
            }
        }

        finishViewer(output);
    }

    private void printAttributes(MetaData meta, ServletOutputStream output) throws IOException {

        String[] attrTitle = {"Attribute Name", "Attribute Value", "delete Attribute"};
        if (meta.getAttributeKeys().hasMoreElements()) {
            printStartTable(attrTitle, output);
            for (Enumeration e = meta.getAttributeKeys(); e.hasMoreElements();) {
                String name = (String) e.nextElement();
                String value = (String) meta.getAttribute(name);
                String[] contents = {
                        "<a href=\"?viewer=" + getName() + "&updateattr=" + name + "\">"
                        + name + "</a>",
                        value,
                        "<a href=\"?viewer=" + getName() + "&deleteattr=" + name + "\">"
                        + "delete</a>"};
                printTableRow(contents, output);
            }
            printEndTable(output);
        } else {
            String message = "No Attributes available!";
            printStartTable(message, output);
            printStartSingleTableBox(output);
            output.println(message);
            printEndSingleTableBox(output);
            printEndTable(output);
        }
    }

    private void printCustom(MetaData meta, ServletOutputStream output) throws IOException {
        Document custom = meta.getCustomDocument();
        printStartTable("Add/Update MetaData Custom", output);
        printStartSingleTableBox(output);
        output.println(META_CUSTOM_UPDATE_FORM_START);
        if (custom != null && custom.getDocumentElement() != null) {
            Element customEle = custom.getDocumentElement();
            if (!customEle.hasChildNodes()) {
                Document helper = new DocumentImpl();
                /*customEle = (Element) */customEle
                    .appendChild(helper.createTextNode("\n\n"));
            }
            TextWriter.write(custom.getDocumentElement(), output);
        }
        output.println(META_CUSTOM_UPDATE_FORM_END);
        printEndSingleTableBox(output);
        printStartSingleTableBox(output);
        output.println("<a href=\"?viewer=" + getName() + "&deleteCustom=true\">");
        output.println("delete Custom</a>");
        printEndSingleTableBox(output);
        printStartSingleTableBox(output);
        output.println(META_CUSTOM_UPLOAD_FORM);
        printEndSingleTableBox(output);
        printEndTable(output);
        output.println("<br />");
        printStartTable("MetaData Custom", output);
        printStartSingleTableBox(output);
        if (custom != null && custom.getDocumentElement() != null) {
            // print custom ontent (children)
            /*NodeList customChilds = custom.getDocumentElement().getChildNodes();
               for(int i = 0; i < customChilds.getLength(); i++) {
                   Xml2HtmlWriter.write(customChilds.item(i), output);
               }*/
            Xml2HtmlWriter.write(custom.getDocumentElement(), output);
        } else {
            output.println("No Custom Document!");
        }
        printEndSingleTableBox(output);
        printEndTable(output);
    }

    private void printError(String message, ServletOutputStream output) throws IOException {
        printStartTable("Error", output);
        printStartSingleTableBox(output);
        output.println(message);
        printEndSingleTableBox(output);
        printEndTable(output);
        output.println("<br />\n");
    }

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

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

    private static final String META_ATTR_UPDATE_FORM_END =
            "\"/></td></tr><tr><td>\n" +
            "<input type=\"submit\" name=\"submit\" value=\"add/update\" /> \n" +
            "</td></tr></table></form> \n";

    private static final String META_CUSTOM_UPDATE_FORM_START =
            "<form action=\"\" method=\"get\"> \n" +
            "<input name=\"viewer\" value=\"meta\" type=\"hidden\" /> \n" +
            "<table class=\"upload-form\"> \n" +
            "<tr><td> \n" +
            "<textarea name=\"custom\" cols=\"80\" rows=\"8\">";

    private static final String META_CUSTOM_UPDATE_FORM_END =
            "</textarea> \n" +
            "</td></tr><tr><td> \n" +
            "<input type=\"submit\" name=\"submit\" value=\"add/update\" /> \n" +
            "</td></tr></table></form> \n";

    private static final String META_CUSTOM_UPLOAD_FORM =
            "<form action=\"?viewer=meta\" method=\"post\" " +
            "enctype=\"multipart/form-data\"> \n" +
            "<table class=\"upload-form\"> \n" +
            "<tr><td>Upload Custom file: </td> \n" +
            "<td><input name=\"customfile\" type=\"file\" /></td> \n" +
            "<td><input type=\"submit\" name=\"submit\" value=\"upload\" /> \n" +
            "</td></tr> " +
            "</table> \n" +
            "</form> \n";

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

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

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.