Clover coverage report -
Coverage timestamp: Sun Nov 1 2009 23:08:24 UTC
file stats: LOC: 288   Methods: 6
NCLOC: 224   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
CollectionIndexesViewer.java 0% 0% 0% 0%
coverage
 1    /*
 2    * Licensed to the Apache Software Foundation (ASF) under one or more
 3    * contributor license agreements. See the NOTICE file distributed with
 4    * this work for additional information regarding copyright ownership.
 5    * The ASF licenses this file to You under the Apache License, Version 2.0
 6    * (the "License"); you may not use this file except in compliance with
 7    * the License. You may obtain a copy of the License at
 8    *
 9    * http://www.apache.org/licenses/LICENSE-2.0
 10    *
 11    * Unless required by applicable law or agreed to in writing, software
 12    * distributed under the License is distributed on an "AS IS" BASIS,
 13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14    * See the License for the specific language governing permissions and
 15    * limitations under the License.
 16    *
 17    * $Id: CollectionIndexesViewer.java 711958 2008-11-06 20:12:54Z natalia $
 18    */
 19   
 20    package org.apache.xindice.webadmin.viewer.components;
 21   
 22    import java.io.IOException;
 23    import java.io.Writer;
 24   
 25    import javax.servlet.ServletException;
 26    import javax.servlet.http.HttpServletRequest;
 27    import javax.servlet.http.HttpServletResponse;
 28   
 29    import org.apache.xindice.core.Collection;
 30    import org.apache.xindice.core.DBException;
 31    import org.apache.xindice.core.indexer.Indexer;
 32    import org.apache.xindice.util.Configuration;
 33    import org.apache.xindice.util.StringUtilities;
 34    import org.apache.xindice.webadmin.viewer.HtmlViewerComponent;
 35    import org.apache.xindice.webadmin.viewer.HtmlCollectionViewer;
 36    import org.apache.xindice.xml.dom.DocumentImpl;
 37    import org.apache.commons.logging.Log;
 38    import org.apache.commons.logging.LogFactory;
 39    import org.w3c.dom.Document;
 40    import org.w3c.dom.Element;
 41   
 42    /**
 43    * Xindice Html Viewer for Collection Indexers.
 44    *
 45    * @author <a href="mailto:jmetzner@apache.org">Jan Metzner</a>
 46    * @version $Revision: 711958 $, $Date: 2008-11-06 20:12:54 +0000 (Thu, 06 Nov 2008) $
 47    */
 48    public class CollectionIndexesViewer extends HtmlViewerComponent implements HtmlCollectionViewer {
 49   
 50    protected static final String XINDICE_VAL_INDEXER = "org.apache.xindice.core.indexer.ValueIndexer";
 51    protected static final String XINDICE_NAME_INDEXER = "org.apache.xindice.core.indexer.NameIndexer";
 52   
 53    private static final Log log = LogFactory.getLog(CollectionIndexesViewer.class);
 54   
 55  0 public void execute(HttpServletRequest req, HttpServletResponse res, Collection col)
 56    throws ServletException, IOException {
 57   
 58  0 String path = col.getCanonicalName();
 59  0 String title = "Collection Indexes for " + path;
 60   
 61  0 Writer output = startViewer(title, path, res);
 62   
 63  0 String name = req.getParameter("name");
 64  0 String pattern = req.getParameter("pattern");
 65  0 String type = req.getParameter("type");
 66  0 String pagesize = req.getParameter("pagesize");
 67  0 String maxkeysize = req.getParameter("maxkeysize");
 68   
 69  0 String delete = req.getParameter("delete");
 70   
 71  0 if (name != null && pattern != null && name.length() > 0 && pattern.length() > 0) {
 72    // create index
 73  0 try {
 74  0 createIndex(col, name, pattern, type, pagesize, maxkeysize);
 75    } catch (DBException e) {
 76  0 log.error(e);
 77  0 printStartTable("Message", output);
 78  0 printStartSingleTableBox(output);
 79  0 output.write("<b>Cannot create Indexer " + name + ": " + e.getMessage() + "</b>\n");
 80  0 printEndSingleTableBox(output);
 81  0 printEndTable(output);
 82  0 output.write("<br />\n");
 83    }
 84  0 } else if (delete != null && delete.length() > 0) {
 85  0 printStartTable("Message", output);
 86  0 printStartSingleTableBox(output);
 87  0 try {
 88  0 deleteIndex(col, delete, output);
 89    } catch (DBException e) {
 90  0 log.error(e);
 91  0 output.write("<b>Cannot delete Indexer " + name + ": " + e.getMessage() + "</b>\n");
 92    }
 93  0 printEndSingleTableBox(output);
 94  0 printEndTable(output);
 95  0 output.write("<br />\n");
 96    }
 97   
 98    // form
 99  0 printStartTable("Add Index for " + path, output);
 100  0 printStartSingleTableBox(output);
 101  0 output.write(INDEXES_UPDATE_FORM_1);
 102  0 output.write(INDEXES_UPDATE_FORM_2);
 103  0 output.write(INDEXES_UPDATE_FORM_3);
 104  0 printTypeSelector(output);
 105  0 output.write(INDEXES_UPDATE_FORM_4);
 106  0 output.write(INDEXES_UPDATE_FORM_5);
 107  0 output.write(INDEXES_UPDATE_FORM_6);
 108  0 printEndSingleTableBox(output);
 109  0 printEndTable(output);
 110   
 111  0 output.write("<br />");
 112  0 output.flush();
 113   
 114  0 try {
 115  0 listIndexes(col, output);
 116    } catch (DBException e) {
 117  0 log.error(e);
 118  0 printStartTable("Error", output);
 119  0 printStartSingleTableBox(output);
 120  0 output.write("Sorry, cannot get Indexes: ");
 121  0 output.write(e.getMessage());
 122  0 printEndSingleTableBox(output);
 123  0 printEndTable(output);
 124    }
 125  0 finishViewer(output);
 126    }
 127   
 128  0 private void listIndexes(Collection col, Writer output) throws DBException, IOException {
 129  0 String[] indexes = col.listIndexers();
 130  0 if (indexes.length == 0) {
 131  0 String message = "Collection contains no Indexes!";
 132  0 printStartTable(message, output);
 133  0 printStartSingleTableBox(output);
 134  0 output.write(message);
 135  0 printEndSingleTableBox(output);
 136  0 printEndTable(output);
 137    } else {
 138  0 String[] columnTitles = {"name", "pattern", "class", "type",
 139    "pagesize", "max key size", "delete"};
 140  0 printStartTable(columnTitles, output);
 141  0 for (int i = 0; i < indexes.length; i++) {
 142  0 Configuration thisConf = col.getIndexer(indexes[i]).getConfig();
 143  0 String indexerName = thisConf.getAttribute("name");
 144  0 String indexerType = thisConf.getAttribute("type", "");
 145  0 String indexerClass = thisConf.getAttribute("class");
 146  0 if (indexerClass.equalsIgnoreCase(XINDICE_NAME_INDEXER)) {
 147  0 indexerClass = "NameIndexer";
 148  0 indexerType = "name";
 149    } else {
 150  0 indexerClass = "ValueIndexer";
 151    }
 152  0 String[] contents = {
 153    "<b>" + indexerName + "</b>",
 154    "<b>" + thisConf.getAttribute("pattern") + "</b>",
 155    indexerClass,
 156    indexerType,
 157    thisConf.getAttribute("pagesize", ""),
 158    thisConf.getAttribute("maxkeysize", ""),
 159    "<a href=\"?viewer=" + StringUtilities.escapePath(getName()) + "&delete="
 160    + indexerName + "\">delete</a>"
 161    };
 162  0 printTableRow(contents, output);
 163    }
 164  0 printEndTable(output);
 165    }
 166    }
 167   
 168  0 private void deleteIndex(Collection col, String name, Writer output) throws IOException, DBException {
 169    // delete indexer
 170  0 Indexer delIndex = col.getIndexer(name);
 171  0 if (delIndex != null) {
 172  0 if (col.dropIndexer(delIndex)) {
 173  0 output.write("<b>Indexer " + name + " deleted!</b>\n");
 174    } else {
 175  0 output.write("<b>Cannot delete Indexer " + name + "!</b>\n");
 176    }
 177    } else {
 178  0 output.write("<b>Cannot get Indexer " + name + "!</b>\n");
 179    }
 180    }
 181   
 182  0 private Indexer createIndex(Collection col, String name, String pattern, String type, String pagesize, String maxkeysize)
 183    throws DBException {
 184   
 185  0 Document doc = new DocumentImpl();
 186   
 187  0 Element idxEle = doc.createElement("index");
 188  0 idxEle.setAttribute("name", name);
 189  0 idxEle.setAttribute("pattern", pattern);
 190  0 idxEle.setAttribute("class", XINDICE_VAL_INDEXER);
 191   
 192  0 if (type != null && type.length() > 0) {
 193  0 if (type.equalsIgnoreCase("name")) {
 194  0 idxEle.setAttribute("class", XINDICE_NAME_INDEXER);
 195    } else {
 196  0 idxEle.setAttribute("type", type);
 197    }
 198    }
 199   
 200  0 if (pagesize != null && pagesize.length() > 0) {
 201  0 idxEle.setAttribute("pagesize", pagesize);
 202    }
 203   
 204  0 if (maxkeysize != null && maxkeysize.length() > 0) {
 205  0 idxEle.setAttribute("maxkeysize", maxkeysize);
 206    }
 207   
 208  0 doc.appendChild(idxEle);
 209  0 return col.createIndexer(new Configuration(doc, false));
 210    }
 211   
 212  0 private void printTypeSelector(Writer output) throws IOException {
 213  0 output.write("<select name=\"type\" size\"1\">\n");
 214  0 output.write("<option value=\"\">default</option>\n");
 215  0 for (int i = 0; i < IDX_TYPES.length; i += 2) {
 216  0 output.write("<option value=\"" + IDX_TYPES[i] + "\"");
 217  0 output.write(" >" + IDX_TYPES[i] + ": " + IDX_TYPES[i + 1] + "</option>");
 218    }
 219  0 output.write("</select>\n");
 220    }
 221   
 222    /**
 223    * index types as pair: type, description
 224    */
 225    private static final String[] IDX_TYPES = {
 226    "string", "Non-normalized string value",
 227    "trimmed", "Normalized (whitespace stripped) string value",
 228    "short", "16-bit signed short integer",
 229    "int", "32-bit signed integer",
 230    "long", "64-bit signed integer",
 231    "float", "32-bit floating point value",
 232    "double", "64-bit floating point value (XPath number)",
 233    "byte", "8-bit signed byte",
 234    "char", "16-bit signed character",
 235    "boolean", "8-bit boolean value",
 236    "name", "Store document keys that contain the pattern",
 237    };
 238   
 239    private static final String INDEXES_UPDATE_FORM_1 =
 240    "<form action=\"\" method=\"get\"> \n" +
 241    "<input name=\"viewer\" value=\"indexes\" type=\"hidden\" /> \n" +
 242    "<table class=\"upload-form\"> \n" +
 243    "<tr><td> \n" +
 244    " index name: \n" +
 245    "</td><td> \n" +
 246    " <input name=\"name\" type=\"text\" value=\"";
 247   
 248    private static final String INDEXES_UPDATE_FORM_2 =
 249    "\" /> \n" +
 250    "</td><td> \n" +
 251    " pattern: \n" +
 252    "</td><td> \n" +
 253    " <input name=\"pattern\" type=\"text\" value=\"";
 254   
 255    private static final String INDEXES_UPDATE_FORM_3 =
 256    "\" /> \n" +
 257    " </td></tr> \n" +
 258    " <tr><td colspan=\"4\">&nbsp;</td></tr> \n" +
 259    "<tr><td> \n" +
 260    " type: \n" +
 261    "</td><td colspan=\"3\"> \n";
 262   
 263    private static final String INDEXES_UPDATE_FORM_4 =
 264    " </td></tr> \n" +
 265    "<tr><td> \n" +
 266    " pagesize: \n" +
 267    "</td><td> \n" +
 268    " <input name=\"pagesize\" type=\"text\" value=\"";
 269   
 270    private static final String INDEXES_UPDATE_FORM_5 =
 271    "\" /> \n" +
 272    " </td><td> \n" +
 273    " max key size: \n" +
 274    "</td><td> \n" +
 275    " <input name=\"maxkeyesize\" type=\"text\" value=\"";
 276   
 277    private static final String INDEXES_UPDATE_FORM_6 =
 278    "\" /> \n" +
 279    " <tr><td colspan=\"4\" align=\"center\"> \n" +
 280    " <input type=\"submit\" name=\"submit\" value=\"add\" /> \n" +
 281    " </td></tr> \n" +
 282    " </table> \n" +
 283    " </form> \n";
 284   
 285  0 protected String getName() {
 286  0 return "indexes";
 287    }
 288    }