Ignore:
Timestamp:
2012-11-06T15:27:10+13:00 (11 years ago)
Author:
kjdon
Message:

trying to tidy up the collection classes. Move some general stuff to servicecluster and reuse the code a bit more

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/collection/Collection.java

    r25989 r26446  
    3636import org.greenstone.gsdl3.util.GSXML;
    3737import org.greenstone.gsdl3.util.OAIXML;
     38import org.greenstone.gsdl3.util.SimpleMacroResolver;
    3839import org.greenstone.gsdl3.util.UserContext;
    3940import org.greenstone.gsdl3.util.XMLTransformer;
     
    5556    static Logger logger = Logger.getLogger(org.greenstone.gsdl3.collection.Collection.class.getName());
    5657
    57     /** is this collection being tidied */
     58    /** is this collection being tidied and therefore can support realistic book view?*/
    5859    protected boolean useBook = false;
    59     /** is this collection public or private */
     60    /** is this collection public or private  - public collections will
     61        appear on the home page, whereas private collections won't*/
    6062    protected boolean is_public = true;
     63    /** collection type : mg, mgpp or lucene */
     64    protected String col_type = "";
     65    /** database type : gdbm, jdbm or sqlite */
     66    protected String db_type = "";
    6167
    6268    /** does this collection provide the OAI service */
    63     protected boolean has_oai = true;
     69    protected boolean has_oai = false;
    6470    /** time when this collection was built */
    6571    protected long lastmodified = 0;
     
    7581    protected ArrayList<HashMap<String, ArrayList<String>>> _securityExceptions = new ArrayList<HashMap<String, ArrayList<String>>>();
    7682
    77     /**
    78      * An element containing the serviceRackList element of buildConfig.xml,
    79      * used to determine whether it contains the OAIPMH serviceRack
    80      */
    81     //protected Element service_rack_list = null;
    82 
     83 
    8384    protected XMLTransformer transformer = null;
    8485
     
    112113            return false;
    113114        }
     115       
     116        macro_resolver.addMacro("_httpcollection_", this.site_http_address + "/collect/" + this.cluster_name);
    114117
    115118        Element coll_config_xml = loadCollConfigFile();
     
    138141        }
    139142
     143        this.description.setAttribute(GSXML.TYPE_ATT, col_type);
     144        this.description.setAttribute(GSXML.DB_TYPE_ATT, db_type);
     145       
    140146        _globalFormat = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.FORMAT_ELEM);
    141 
    142         // process the metadata and display items
     147        // process the metadata and display items and default library params
     148        super.configureLocalData(coll_config_xml);
     149        super.configureLocalData(build_config_xml);
     150        // get extra collection specific stuff
    143151        findAndLoadInfo(coll_config_xml, build_config_xml);
    144152
     
    197205            return null;
    198206        }
    199         // get the xml for both files
     207        // get the xml
    200208        Document coll_config_doc = this.converter.getDOM(coll_config_file, CONFIG_ENCODING);
    201209        Element coll_config_elem = null;
     
    237245    protected boolean findAndLoadInfo(Element coll_config_xml, Element build_config_xml)
    238246    {
    239         Element meta_list = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
    240         addMetadata(meta_list);
    241         meta_list = (Element) GSXML.getChildByTagName(build_config_xml, GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
    242         addMetadata(meta_list);
    243 
    244         meta_list = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
    245         GSXML.addMetadata(this.doc, meta_list, "httpPath", this.site_http_address + "/collect/" + this.cluster_name);
    246         addMetadata(meta_list);
     247        // Element meta_list = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
     248        // addMetadata(meta_list);
     249        // meta_list = (Element) GSXML.getChildByTagName(build_config_xml, GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
     250        // addMetadata(meta_list);
     251        addMetadata("httpPath", this.site_http_address + "/collect/" + this.cluster_name);
    247252
    248253        // display stuff
    249         Element display_list = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.DISPLAY_TEXT_ELEM + GSXML.LIST_MODIFIER);
    250         if (display_list != null)
    251         {
    252             resolveMacros(display_list);
    253             addDisplayItems(display_list);
    254         }
     254        // Element display_list = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.DISPLAY_TEXT_ELEM + GSXML.LIST_MODIFIER);
     255        // if (display_list != null)
     256        // {
     257        // resolveMacros(display_list);
     258        // addDisplayItems(display_list);
     259        // }
    255260
    256261        //check whether the html are tidy or not
     
    259264        {
    260265            Element plugin_list = (Element) GSXML.getChildByTagName(import_list, GSXML.PLUGIN_ELEM + GSXML.LIST_MODIFIER);
    261             addPlugins(plugin_list);
     266            //addPlugins(plugin_list);
    262267            if (plugin_list != null)
    263268            {
     
    274279            }
    275280        }
    276         meta_list = this.doc.createElement(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
    277         if (useBook == true)
    278             GSXML.addMetadata(this.doc, meta_list, "tidyoption", "tidy");
    279         else
    280             GSXML.addMetadata(this.doc, meta_list, "tidyoption", "untidy");
    281         addMetadata(meta_list);
     281        String tidy = (useBook == true ? "tidy" : "untidy");
     282        addMetadata("tidyoption", tidy);
    282283
    283284        // check whether we are public or not
     
    389390        clearServices();
    390391        Element service_list = (Element) GSXML.getChildByTagName(build_config_xml, GSXML.SERVICE_CLASS_ELEM + GSXML.LIST_MODIFIER);
    391         configureServiceRackList(service_list, coll_config_xml);
    392 
     392        Element oai_service_rack = null;
     393        if (service_list != null) {
     394          configureServiceRackList(service_list, coll_config_xml);
     395          oai_service_rack = GSXML.getNamedElement(service_list, GSXML.SERVICE_CLASS_ELEM, OAIXML.NAME, OAIXML.OAIPMH);
     396        }
    393397        // collection Config may also contain manually added service racks
    394398        service_list = (Element) GSXML.getChildByTagName(coll_config_xml, GSXML.SERVICE_CLASS_ELEM + GSXML.LIST_MODIFIER);
    395399        if (service_list != null)
    396400        {
    397             configureServiceRackList(service_list, build_config_xml);
    398 
    399             // Check for oai
    400             Element oai_service_rack = GSXML.getNamedElement(service_list, GSXML.SERVICE_CLASS_ELEM, OAIXML.NAME, OAIXML.OAIPMH);
    401             if (oai_service_rack == null)
    402             {
    403                 has_oai = false;
    404                 logger.info("No oai for collection: " + this.cluster_name);
    405 
    406             }
    407             else
    408             {
    409                 has_oai = true;
    410 
    411                 // extract earliestDatestamp from the buildconfig.xml for OAI
    412                 Element metadata_list = (Element) GSXML.getChildByTagName(build_config_xml, GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
    413 
    414                 if (metadata_list != null)
    415                 {
    416                     NodeList children = metadata_list.getElementsByTagName(GSXML.METADATA_ELEM);
    417                     // can't do getChildNodes(), because whitespace, such as newlines, creates Text nodes
    418                     for (int i = 0; i < children.getLength(); i++)
    419                     {
    420                         Element metadata = (Element) children.item(i);
    421                         if (metadata.getAttribute(GSXML.NAME_ATT).equals(OAIXML.EARLIEST_DATESTAMP))
    422                         {
    423                             String earliestDatestampStr = GSXML.getValue(metadata);
    424                             if (!earliestDatestampStr.equals(""))
    425                             {
    426                                 earliestDatestamp = Long.parseLong(earliestDatestampStr);
    427                             }
    428                             break; // found a metadata element with name=earliestDatestamp in buildconfig
    429                         }
    430                     }
    431                 }
    432 
    433                 // If at the end of this, there is no value for earliestDatestamp, print out a warning
    434                 logger.warn("No earliestDatestamp in buildConfig.xml for collection: " + this.cluster_name + ". Defaulting to 0.");
    435 
    436             }
    437         }
    438         else
    439         { // no list of services (no ServiceRackList), so no oai_service_rack either
    440             // explicitly set has_oai to false here, since it's initialised to true by default
    441             has_oai = false;
    442         }
     401          configureServiceRackList(service_list, build_config_xml);
     402          // this oai used in preference to one in buildConfig.xml
     403          oai_service_rack = GSXML.getNamedElement(service_list, GSXML.SERVICE_CLASS_ELEM, OAIXML.NAME, OAIXML.OAIPMH);
     404        }
     405        // Check for oai
     406
     407        if (oai_service_rack != null)
     408          {
     409            has_oai = true;
     410            logger.info(this.cluster_name +" has OAI services");
     411            // extract earliestDatestamp from the buildconfig.xml for OAI
     412            Element metadata_list = (Element) GSXML.getChildByTagName(build_config_xml, GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER);
     413           
     414            if (metadata_list != null)
     415              {
     416            NodeList children = metadata_list.getElementsByTagName(GSXML.METADATA_ELEM);
     417            // can't do getChildNodes(), because whitespace, such as newlines, creates Text nodes
     418            for (int i = 0; i < children.getLength(); i++)
     419              {
     420                Element metadata = (Element) children.item(i);
     421                if (metadata.getAttribute(GSXML.NAME_ATT).equals(OAIXML.EARLIEST_DATESTAMP))
     422                  {
     423                String earliestDatestampStr = GSXML.getValue(metadata);
     424                if (!earliestDatestampStr.equals(""))
     425                  {
     426                    earliestDatestamp = Long.parseLong(earliestDatestampStr);
     427                  }
     428                break; // found a metadata element with name=earliestDatestamp in buildconfig
     429                  }
     430              }
     431              }
     432           
     433            // If at the end of this, there is no value for earliestDatestamp, print out a warning
     434            logger.warn("No earliestDatestamp in buildConfig.xml for collection: " + this.cluster_name + ". Defaulting to 0.");
     435           
     436          } // if oai_service_rack != null
     437   
    443438        return true;
    444439    }
    445440
    446     protected boolean resolveMacros(Element display_list)
    447     {
    448         if (display_list == null)
    449             return false;
    450         NodeList displaynodes = display_list.getElementsByTagName(GSXML.DISPLAY_TEXT_ELEM);
    451         if (displaynodes.getLength() > 0)
    452         {
    453             String http_site = this.site_http_address;
    454             String http_collection = this.site_http_address + "/collect/" + this.cluster_name;
    455             for (int k = 0; k < displaynodes.getLength(); k++)
    456             {
    457                 Element d = (Element) displaynodes.item(k);
    458                 String text = GSXML.getNodeText(d);
    459                 text = StringUtils.replace(text, "_httpsite_", http_site);
    460                 text = StringUtils.replace(text, "_httpcollection_", http_collection);
    461                 GSXML.setNodeText(d, text);
    462             }
    463         }
    464         return true;
    465     }
    466441
    467442    /**
     
    485460        }
    486461
    487         if (subset.equals(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER) || subset.equals(GSXML.DISPLAY_TEXT_ELEM + GSXML.LIST_MODIFIER) || subset.equals(GSXML.PLUGIN_ELEM + GSXML.LIST_MODIFIER))
    488         {
     462        if (subset.equals(GSXML.METADATA_ELEM + GSXML.LIST_MODIFIER) || subset.equals(GSXML.DISPLAY_TEXT_ELEM + GSXML.LIST_MODIFIER) || subset.equals("libraryParamList"))
     463        {
     464          configureLocalData(coll_config_elem);
     465          configureLocalData(build_config_elem);
    489466            return findAndLoadInfo(coll_config_elem, build_config_elem);
    490467
     
    504481    protected Element processMessage(Element request)
    505482    {
    506 
     483      String type = request.getAttribute(GSXML.TYPE_ATT);
     484        if (type.equals(GSXML.REQUEST_TYPE_FORMAT_STRING))
     485        {
     486          return processFormatStringRequest(request);
     487        }
     488        else if (type.equals(GSXML.REQUEST_TYPE_SECURITY))
     489        {
     490          return processSecurityRequest(request);
     491        }
     492        else if (type.equals(GSXML.REQUEST_TYPE_FORMAT))
     493        {
    507494        Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
    508495        response.setAttribute(GSXML.FROM_ATT, this.cluster_name);
    509         String type = request.getAttribute(GSXML.TYPE_ATT);
    510         String lang = request.getAttribute(GSXML.LANG_ATT);
    511         response.setAttribute(GSXML.TYPE_ATT, type);
    512 
    513         if (type.equals(GSXML.REQUEST_TYPE_FORMAT_STRING))
    514         {
    515             String subaction = request.getAttribute("subaction");
    516             String service = request.getAttribute("service");
    517 
    518             String classifier = null;
    519             if (service.equals("ClassifierBrowse"))
    520             {
    521                 classifier = request.getAttribute("classifier");
    522             }
    523 
    524             // check for version file
    525             String directory = new File(GSFile.collectionConfigFile(this.site_home, this.cluster_name)).getParent() + File.separator;
    526 
    527             String version_filename = "";
    528             if (service.equals("ClassifierBrowse"))
    529                 version_filename = directory + "browse_" + classifier + "_format_statement_version.txt";
    530             else
    531                 version_filename = directory + "query_format_statement_version.txt";
    532 
    533             File version_file = new File(version_filename);
    534 
    535             if (subaction.equals("update"))
    536             {
    537                 Element format_element = (Element) GSXML.getChildByTagName(request, GSXML.FORMAT_STRING_ELEM);
    538                 //String format_string = GSXML.getNodeText(format_element);
    539                 Element format_statement = (Element) format_element.getFirstChild();
    540 
    541                 String version_number = "1";
    542                 BufferedWriter writer;
    543 
    544                 try
    545                 {
    546 
    547                     if (version_file.exists())
    548                     {
    549                         // Read version
    550                         BufferedReader reader = new BufferedReader(new FileReader(version_filename));
    551                         version_number = reader.readLine();
    552                         int aInt = Integer.parseInt(version_number) + 1;
    553                         version_number = Integer.toString(aInt);
    554                         reader.close();
    555                     }
    556                     else
    557                     {
    558                         // Create
    559                         version_file.createNewFile();
    560                         writer = new BufferedWriter(new FileWriter(version_filename));
    561                         writer.write(version_number);
    562                         writer.close();
    563                     }
    564 
    565                     // Write version file
    566                     String format_statement_filename = "";
    567 
    568                     if (service.equals("ClassifierBrowse"))
    569                         format_statement_filename = directory + "browse_" + classifier + "_format_statement_v" + version_number + ".txt";
    570                     else
    571                         format_statement_filename = directory + "query_format_statement_v" + version_number + ".txt";
    572 
    573                     // Write format statement
    574                     String format_string = this.converter.getString(format_statement); //GSXML.xmlNodeToString(format_statement);
    575                     writer = new BufferedWriter(new FileWriter(format_statement_filename));
    576                     writer.write(format_string);
    577                     writer.close();
    578 
    579                     // Update version number
    580                     writer = new BufferedWriter(new FileWriter(version_filename));
    581                     writer.write(version_number);
    582                     writer.close();
    583 
    584                 }
    585                 catch (IOException e)
    586                 {
    587                     logger.error("IO Exception " + e);
    588                 }
    589             }
    590 
    591             if (subaction.equals("saveDocument"))
    592             {
    593                 Element format_element = (Element) GSXML.getChildByTagName(request, GSXML.FORMAT_STRING_ELEM);
    594                 //String format_string = GSXML.getNodeText(format_element);
    595                 // Get display tag
    596                 Element display_format = (Element) format_element.getFirstChild();
    597 
    598                 String collection_config = directory + "collectionConfig.xml";
    599                 Document config = this.converter.getDOM(new File(collection_config), "UTF-8");
    600 
    601                 Node current_node = GSXML.getChildByTagName(config, "CollectionConfig");
    602 
    603                 // Get display child
    604                 if (GSXML.getChildByTagName(current_node, "display") == null)
    605                 {
    606                     // well then create a format tag
    607                     Element display_tag = config.createElement("display");
    608                     current_node = (Node) current_node.appendChild(display_tag);
    609                 }
    610                 else
    611                 {
    612                     current_node = GSXML.getChildByTagName(current_node, "display");
    613                 }
    614 
    615                 if (GSXML.getChildByTagName(current_node, "format") == null)
    616                 {
    617                     // well then create a format tag
    618                     Element format_tag = config.createElement("format");
    619                     current_node.appendChild(format_tag);
    620                 }
    621 
    622                 current_node.replaceChild(config.importNode(display_format, true), GSXML.getChildByTagName(current_node, "format"));
    623 
    624                 String new_config = this.converter.getString(config);
    625 
    626                 new_config = StringUtils.replace(new_config, "&lt;", "<");
    627                 new_config = StringUtils.replace(new_config, "&gt;", ">");
    628                 new_config = StringUtils.replace(new_config, "&quot;", "\"");
    629 
    630                 try
    631                 {
    632                     // Write to file (not original! for now)
    633                     BufferedWriter writer = new BufferedWriter(new FileWriter(collection_config + ".new"));
    634                     writer.write(new_config);
    635                     writer.close();
    636                 }
    637                 catch (IOException e)
    638                 {
    639                     logger.error("IO Exception " + e);
    640                 }
    641             }
    642 
    643             if (subaction.equals("save"))
    644             {
    645                 Element format_element = (Element) GSXML.getChildByTagName(request, GSXML.FORMAT_STRING_ELEM);
    646                 Element format_statement = (Element) format_element.getFirstChild();
    647 
    648                 try
    649                 {
    650                     // open collectionConfig.xml and read in to w3 Document
    651                     String collection_config = directory + "collectionConfig.xml";
    652                     Document config = this.converter.getDOM(new File(collection_config), "UTF-8");
    653 
    654                     //String tag_name = "";
    655                     int k;
    656                     int index;
    657                     Element elem;
    658                     // Try importing entire tree to this.doc so we can add and remove children at ease
    659                     //Node current_node = this.doc.importNode(GSXML.getChildByTagName(config, "CollectionConfig"),true);
    660                     Node current_node = GSXML.getChildByTagName(config, "CollectionConfig");
    661                     NodeList current_node_list;
    662 
    663                     if (service.equals("ClassifierBrowse"))
    664                     {
    665                         //tag_name = "browse";
    666                         // if CLX then need to look in <classifier> X then <format>
    667                         // default is <browse><format>
    668 
    669                         current_node = GSXML.getChildByTagName(current_node, "browse");
    670 
    671                         // find CLX
    672                         if (classifier != null)
    673                         {
    674                             current_node_list = GSXML.getChildrenByTagName(current_node, "classifier");
    675                             index = Integer.parseInt(classifier.substring(2)) - 1;
    676 
    677                             // index should be given by X-1
    678                             current_node = current_node_list.item(index);
    679                             // what if classifier does not have a format tag?
    680                             if (GSXML.getChildByTagName(current_node, "format") == null)
    681                             {
    682                                 // well then create a format tag
    683                                 Element format_tag = config.createElement("format");
    684                                 current_node.appendChild(format_tag);
    685                             }
    686                         }
    687                         else
    688                         {
    689                             // To support all classifiers, set classifier to null?  There is the chance here that the format tag does not exist
    690                             if (GSXML.getChildByTagName(current_node, "format") == null)
    691                             {
    692                                 // well then create a format tag
    693                                 Element format_tag = config.createElement("format");
    694                                 current_node.appendChild(format_tag);
    695                             }
    696                         }
    697                     }
    698                     else if (service.equals("AllClassifierBrowse"))
    699                     {
    700                         current_node = GSXML.getChildByTagName(current_node, "browse");
    701                         if (GSXML.getChildByTagName(current_node, "format") == null)
    702                         {
    703                             // well then create a format tag
    704                             Element format_tag = config.createElement("format");
    705                             current_node.appendChild(format_tag);
    706                         }
    707                     }
    708                     else
    709                     {
    710                         // look in <format> with no attributes
    711                         current_node_list = GSXML.getChildrenByTagName(current_node, "search");
    712                         for (k = 0; k < current_node_list.getLength(); k++)
    713                         {
    714                             current_node = current_node_list.item(k);
    715                             // if current_node has no attributes then break
    716                             elem = (Element) current_node;
    717                             if (elem.hasAttribute("name") == false)
    718                                 break;
    719                         }
    720                     }
    721 
    722                     current_node.replaceChild(config.importNode(format_statement, true), GSXML.getChildByTagName(current_node, "format"));
    723 
    724                     // Now convert config document to string for writing to file
    725                     String new_config = this.converter.getString(config);
    726 
    727                     new_config = StringUtils.replace(new_config, "&lt;", "<");
    728                     new_config = StringUtils.replace(new_config, "&gt;", ">");
    729                     new_config = StringUtils.replace(new_config, "&quot;", "\"");
    730 
    731                     // Write to file (not original! for now)
    732                     BufferedWriter writer = new BufferedWriter(new FileWriter(collection_config + ".new"));
    733                     writer.write(new_config);
    734                     writer.close();
    735 
    736                 }
    737                 catch (Exception ex)
    738                 {
    739                     logger.error("There was an exception " + ex);
    740 
    741                     StringWriter sw = new StringWriter();
    742                     PrintWriter pw = new PrintWriter(sw, true);
    743                     ex.printStackTrace(pw);
    744                     pw.flush();
    745                     sw.flush();
    746                     logger.error(sw.toString());
    747                 }
    748 
    749             }
    750         }
    751         else if (type.equals(GSXML.REQUEST_TYPE_SECURITY))
    752         {
    753             String oid = request.getAttribute("oid");
    754             if (oid.contains("."))
    755             {
    756                 oid = oid.substring(0, oid.indexOf("."));
    757             }
    758 
    759             ArrayList<String> groups = getPermittedGroups(oid);
    760 
    761             Element groupList = this.doc.createElement(GSXML.GROUP_ELEM + GSXML.LIST_MODIFIER);
    762             response.appendChild(groupList);
    763 
    764             for (String groupName : groups)
    765             {
    766                 Element group = this.doc.createElement(GSXML.GROUP_ELEM);
    767                 groupList.appendChild(group);
    768                 group.setAttribute(GSXML.NAME_ATT, groupName);
    769             }
    770         }
    771         else if (type.equals(GSXML.REQUEST_TYPE_FORMAT))
    772         {
     496        response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_FORMAT);
    773497            if(_globalFormat != null)
    774498            {
    775                 response.appendChild(response.getOwnerDocument().importNode(_globalFormat, true));
    776             }
    777         }
    778         else
    779         { // unknown type
    780             return super.processMessage(request);
    781 
    782         }
    783         return response;
    784     }
     499                response.appendChild(this.doc.importNode(_globalFormat, true));
     500            }
     501            return response;
     502        }
     503        // unknown type
     504        return super.processMessage(request);
     505
     506    }
     507
     508  protected Element processSecurityRequest(Element request) {
     509    Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
     510    response.setAttribute(GSXML.FROM_ATT, this.cluster_name);
     511    response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_SECURITY);
     512   
     513    String oid = request.getAttribute("oid");
     514    if (oid.contains("."))
     515      {
     516    oid = oid.substring(0, oid.indexOf("."));
     517      }
     518   
     519    ArrayList<String> groups = getPermittedGroups(oid);
     520   
     521    Element groupList = this.doc.createElement(GSXML.GROUP_ELEM + GSXML.LIST_MODIFIER);
     522    response.appendChild(groupList);
     523   
     524    for (String groupName : groups)
     525      {
     526    Element group = this.doc.createElement(GSXML.GROUP_ELEM);
     527    groupList.appendChild(group);
     528    group.setAttribute(GSXML.NAME_ATT, groupName);
     529      }
     530    return response;
     531  }
    785532
    786533    protected ArrayList<String> getPermittedGroups(String oid)
     
    933680        return null;
    934681    }
     682
     683  protected Element processFormatStringRequest(Element request) {
     684    Element response = this.doc.createElement(GSXML.RESPONSE_ELEM);
     685    response.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_FORMAT_STRING);
     686    response.setAttribute(GSXML.FROM_ATT, this.cluster_name);
     687
     688    String subaction = request.getAttribute("subaction");
     689    String service = request.getAttribute("service");
     690   
     691    String classifier = null;
     692    if (service.equals("ClassifierBrowse"))
     693      {
     694    classifier = request.getAttribute("classifier");
     695      }
     696
     697    // check for version file
     698    String directory = new File(GSFile.collectionConfigFile(this.site_home, this.cluster_name)).getParent() + File.separator;
     699
     700    String version_filename = "";
     701    if (service.equals("ClassifierBrowse"))
     702      version_filename = directory + "browse_" + classifier + "_format_statement_version.txt";
     703    else
     704      version_filename = directory + "query_format_statement_version.txt";
     705
     706    File version_file = new File(version_filename);
     707
     708    if (subaction.equals("update"))
     709      {
     710    Element format_element = (Element) GSXML.getChildByTagName(request, GSXML.FORMAT_STRING_ELEM);
     711    //String format_string = GSXML.getNodeText(format_element);
     712    Element format_statement = (Element) format_element.getFirstChild();
     713
     714    String version_number = "1";
     715    BufferedWriter writer;
     716
     717    try
     718      {
     719
     720        if (version_file.exists())
     721          {
     722        // Read version
     723        BufferedReader reader = new BufferedReader(new FileReader(version_filename));
     724        version_number = reader.readLine();
     725        int aInt = Integer.parseInt(version_number) + 1;
     726        version_number = Integer.toString(aInt);
     727        reader.close();
     728          }
     729        else
     730          {
     731        // Create
     732        version_file.createNewFile();
     733        writer = new BufferedWriter(new FileWriter(version_filename));
     734        writer.write(version_number);
     735        writer.close();
     736          }
     737
     738        // Write version file
     739        String format_statement_filename = "";
     740
     741        if (service.equals("ClassifierBrowse"))
     742          format_statement_filename = directory + "browse_" + classifier + "_format_statement_v" + version_number + ".txt";
     743        else
     744          format_statement_filename = directory + "query_format_statement_v" + version_number + ".txt";
     745
     746        // Write format statement
     747        String format_string = this.converter.getString(format_statement); //GSXML.xmlNodeToString(format_statement);
     748        writer = new BufferedWriter(new FileWriter(format_statement_filename));
     749        writer.write(format_string);
     750        writer.close();
     751
     752        // Update version number
     753        writer = new BufferedWriter(new FileWriter(version_filename));
     754        writer.write(version_number);
     755        writer.close();
     756
     757      }
     758    catch (IOException e)
     759      {
     760        logger.error("IO Exception " + e);
     761      }
     762      }
     763
     764    if (subaction.equals("saveDocument"))
     765      {
     766    Element format_element = (Element) GSXML.getChildByTagName(request, GSXML.FORMAT_STRING_ELEM);
     767    //String format_string = GSXML.getNodeText(format_element);
     768    // Get display tag
     769    Element display_format = (Element) format_element.getFirstChild();
     770
     771    String collection_config = directory + "collectionConfig.xml";
     772    Document config = this.converter.getDOM(new File(collection_config), "UTF-8");
     773
     774    Node current_node = GSXML.getChildByTagName(config, "CollectionConfig");
     775
     776    // Get display child
     777    if (GSXML.getChildByTagName(current_node, "display") == null)
     778      {
     779        // well then create a format tag
     780        Element display_tag = config.createElement("display");
     781        current_node = (Node) current_node.appendChild(display_tag);
     782      }
     783    else
     784      {
     785        current_node = GSXML.getChildByTagName(current_node, "display");
     786      }
     787
     788    if (GSXML.getChildByTagName(current_node, "format") == null)
     789      {
     790        // well then create a format tag
     791        Element format_tag = config.createElement("format");
     792        current_node.appendChild(format_tag);
     793      }
     794
     795    current_node.replaceChild(config.importNode(display_format, true), GSXML.getChildByTagName(current_node, "format"));
     796
     797    String new_config = this.converter.getString(config);
     798
     799    new_config = StringUtils.replace(new_config, "&lt;", "<");
     800    new_config = StringUtils.replace(new_config, "&gt;", ">");
     801    new_config = StringUtils.replace(new_config, "&quot;", "\"");
     802
     803    try
     804      {
     805        // Write to file (not original! for now)
     806        BufferedWriter writer = new BufferedWriter(new FileWriter(collection_config + ".new"));
     807        writer.write(new_config);
     808        writer.close();
     809      }
     810    catch (IOException e)
     811      {
     812        logger.error("IO Exception " + e);
     813      }
     814      }
     815
     816    if (subaction.equals("save"))
     817      {
     818    Element format_element = (Element) GSXML.getChildByTagName(request, GSXML.FORMAT_STRING_ELEM);
     819    Element format_statement = (Element) format_element.getFirstChild();
     820
     821    try
     822      {
     823        // open collectionConfig.xml and read in to w3 Document
     824        String collection_config = directory + "collectionConfig.xml";
     825        Document config = this.converter.getDOM(new File(collection_config), "UTF-8");
     826
     827        //String tag_name = "";
     828        int k;
     829        int index;
     830        Element elem;
     831        // Try importing entire tree to this.doc so we can add and remove children at ease
     832        //Node current_node = this.doc.importNode(GSXML.getChildByTagName(config, "CollectionConfig"),true);
     833        Node current_node = GSXML.getChildByTagName(config, "CollectionConfig");
     834        NodeList current_node_list;
     835
     836        if (service.equals("ClassifierBrowse"))
     837          {
     838        //tag_name = "browse";
     839        // if CLX then need to look in <classifier> X then <format>
     840        // default is <browse><format>
     841
     842        current_node = GSXML.getChildByTagName(current_node, "browse");
     843
     844        // find CLX
     845        if (classifier != null)
     846          {
     847            current_node_list = GSXML.getChildrenByTagName(current_node, "classifier");
     848            index = Integer.parseInt(classifier.substring(2)) - 1;
     849
     850            // index should be given by X-1
     851            current_node = current_node_list.item(index);
     852            // what if classifier does not have a format tag?
     853            if (GSXML.getChildByTagName(current_node, "format") == null)
     854              {
     855            // well then create a format tag
     856            Element format_tag = config.createElement("format");
     857            current_node.appendChild(format_tag);
     858              }
     859          }
     860        else
     861          {
     862            // To support all classifiers, set classifier to null?  There is the chance here that the format tag does not exist
     863            if (GSXML.getChildByTagName(current_node, "format") == null)
     864              {
     865            // well then create a format tag
     866            Element format_tag = config.createElement("format");
     867            current_node.appendChild(format_tag);
     868              }
     869          }
     870          }
     871        else if (service.equals("AllClassifierBrowse"))
     872          {
     873        current_node = GSXML.getChildByTagName(current_node, "browse");
     874        if (GSXML.getChildByTagName(current_node, "format") == null)
     875          {
     876            // well then create a format tag
     877            Element format_tag = config.createElement("format");
     878            current_node.appendChild(format_tag);
     879          }
     880          }
     881        else
     882          {
     883        // look in <format> with no attributes
     884        current_node_list = GSXML.getChildrenByTagName(current_node, "search");
     885        for (k = 0; k < current_node_list.getLength(); k++)
     886          {
     887            current_node = current_node_list.item(k);
     888            // if current_node has no attributes then break
     889            elem = (Element) current_node;
     890            if (elem.hasAttribute("name") == false)
     891              break;
     892          }
     893          }
     894
     895        current_node.replaceChild(config.importNode(format_statement, true), GSXML.getChildByTagName(current_node, "format"));
     896
     897        // Now convert config document to string for writing to file
     898        String new_config = this.converter.getString(config);
     899
     900        new_config = StringUtils.replace(new_config, "&lt;", "<");
     901        new_config = StringUtils.replace(new_config, "&gt;", ">");
     902        new_config = StringUtils.replace(new_config, "&quot;", "\"");
     903
     904        // Write to file (not original! for now)
     905        BufferedWriter writer = new BufferedWriter(new FileWriter(collection_config + ".new"));
     906        writer.write(new_config);
     907        writer.close();
     908
     909      }
     910    catch (Exception ex)
     911      {
     912        logger.error("There was an exception " + ex);
     913
     914        StringWriter sw = new StringWriter();
     915        PrintWriter pw = new PrintWriter(sw, true);
     916        ex.printStackTrace(pw);
     917        pw.flush();
     918        sw.flush();
     919        logger.error(sw.toString());
     920      }
     921
     922      }
     923
     924    return response;
     925  }
    935926}
Note: See TracChangeset for help on using the changeset viewer.