Ignore:
Timestamp:
2012-07-19T14:22:57+12:00 (12 years ago)
Author:
sjm84
Message:

Reformatting this file ahead of some changes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/CoverageMetadataRetrieve.java

    r25635 r25976  
    1414// XML classes
    1515import org.w3c.dom.Document;
    16 import org.w3c.dom.Element; 
     16import org.w3c.dom.Element;
    1717import org.w3c.dom.NodeList;
    1818
     
    2424
    2525// change the class name (and the filename) to something more appropriate
    26 public class CoverageMetadataRetrieve
    27     extends ServiceRack {
    28 
    29     // add in a logger for error messages
    30     static Logger logger = Logger.getLogger("CoverageMetadataRetrieve");
    31 
    32     protected SimpleCollectionDatabase coll_db = null;
    33     protected String index_stem = null;
    34 
    35 
    36     // the new service names
    37     protected static final String COVERAGE_SERVICE = "CoverageMetadataRetrieve";
    38 
    39     // initialize any custom variables
    40     public CoverageMetadataRetrieve() {
    41 
    42     }
    43 
    44     // clean up anything that we need to
    45     public void cleanUp() {
    46     super.cleanUp();
    47     }
    48    
    49     // Configure the class based in info in buildConfig.xml and collectionConfig.xml
    50     // info is the <serviceRack name="MyNewServicesTemplate"/> element from
    51     // buildConfig.xml, and extra_info is the whole collectionConfig.xml file
    52     // in case its needed
    53     public boolean configure(Element info, Element extra_info) {
    54 
    55     if (!super.configure(info, extra_info)) {
    56         return false;
    57     }
    58 
    59     logger.info("Configuring CoverageMetadataRetrieve...");
    60 
    61     // set up short_service_info - this currently is a list of services,
    62     // with their names and service types
    63     // we have two services, a new textquery, and a new one of a new type
    64     //Element tq_service = this.doc.createElement(GSXML.SERVICE_ELEM);
    65     //tq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
    66     //tq_service.setAttribute(GSXML.NAME_ATT, QUERY_SERVICE);
    67     //this.short_service_info.appendChild(tq_service);
    68 
    69     Element diff_service = this.doc.createElement(GSXML.SERVICE_ELEM);
    70     diff_service.setAttribute(GSXML.TYPE_ATT, "retrieve");
    71     diff_service.setAttribute(GSXML.NAME_ATT, COVERAGE_SERVICE);
    72     this.short_service_info.appendChild(diff_service);
    73 
    74     // the index stem is either specified in the config file or is  the collection name
    75     Element index_stem_elem = (Element) GSXML.getChildByTagName(info, GSXML.INDEX_STEM_ELEM);
    76     if (index_stem_elem != null) {
    77         this.index_stem = index_stem_elem.getAttribute(GSXML.NAME_ATT);
    78     }
    79     if (this.index_stem == null || this.index_stem.equals("")) {
    80         logger.error("CoverageMetadataRetrieve.configure(): indexStem element not found, stem will default to collection name");
    81         this.index_stem = this.cluster_name;
    82     }
    83 
    84     // find out what kind of database we have
    85     Element database_type_elem = (Element) GSXML.getChildByTagName(info, GSXML.DATABASE_TYPE_ELEM);
    86     String database_type = null;
    87     if (database_type_elem != null) {
    88       database_type = database_type_elem.getAttribute(GSXML.NAME_ATT);
    89     }
    90     if (database_type == null || database_type.equals("")) {
    91       database_type = "gdbm"; // the default
    92     }
    93     coll_db = new SimpleCollectionDatabase(database_type);
    94     if (!coll_db.databaseOK()) {
    95       logger.error("Couldn't create the collection database of type "+database_type);
    96       return false;
    97     }
    98 
    99     // Open database for querying
    100     String coll_db_file = GSFile.collectionDatabaseFile(this.site_home, this.cluster_name, this.index_stem, database_type);
    101     if (!this.coll_db.openDatabase(coll_db_file, SimpleCollectionDatabase.READ)) {
    102         logger.error("Could not open collection database!");
    103         return false;
    104     }
    105 
    106 
    107     // Extract any relevant information from info and extra_info
    108     // This can be used to set up variables.
    109 
    110     // If there is any formatting information, add it in to format_info_map
    111    
    112     // Do this for all services as appropriate
    113     Element format = null; // find it from info/extra_info
    114     if (format != null) {
    115         this.format_info_map.put(COVERAGE_SERVICE, this.doc.importNode(format, true));
    116     }
    117 
    118     return true;
    119    
    120     }
    121 
    122     // get the desription of a service. Could include parameter lists, displayText
    123     protected Element getServiceDescription(String service, String lang, String subset) {
    124 
    125     // check that we have been asked for the right service
    126     if (!service.equals(COVERAGE_SERVICE)) {
    127         return null;
    128     }
    129 
    130     /*
    131     if (service.equals(QUERY_SERVICE)) {
    132         Element tq_service = this.doc.createElement(GSXML.SERVICE_ELEM);
    133         tq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
    134         tq_service.setAttribute(GSXML.NAME_ATT, QUERY_SERVICE);
    135         if (subset==null || subset.equals(GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER)) {
    136         // add in any <displayText> elements
    137         // name, for example - get from properties file
    138         tq_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_NAME, getTextString(QUERY_SERVICE+".name", lang) ));
    139         }
    140    
    141         if (subset==null || subset.equals(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER)) {
    142         // add in a param list if this service has parameters
    143         Element param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
    144         tq_service.appendChild(param_list);
    145         // create any params and append to param_list
    146         }
    147         return tq_service;
    148     }
    149     */
    150 
    151     if (service.equals(COVERAGE_SERVICE)) {
    152         Element diff_service = this.doc.createElement(GSXML.SERVICE_ELEM);
    153         diff_service.setAttribute(GSXML.TYPE_ATT, "retrieve");
    154         diff_service.setAttribute(GSXML.NAME_ATT, COVERAGE_SERVICE);
    155         if (subset==null || subset.equals(GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER)) {
    156         // add in any <displayText> elements
    157         // name, for example - get from properties file
    158         diff_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_NAME, getTextString(COVERAGE_SERVICE+".name", lang) ));
    159         }
    160    
    161         if (subset==null || subset.equals(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER)) {
    162         // add in a param list if this service has parameters
    163         Element param_list = this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
    164         diff_service.appendChild(param_list);
    165         // create any params and append to param_list
    166         }
    167 
    168         return diff_service;
    169     }
    170    
    171     // not a valid service for this class
    172     return null;
    173    
    174 
    175 
    176     }
    177 
    178     /** This is the method that actually handles the TextQuery Service */
    179     //protected Element processTextQuery(Element request) {
     26public class CoverageMetadataRetrieve extends ServiceRack
     27{
     28
     29    // add in a logger for error messages
     30    static Logger logger = Logger.getLogger("CoverageMetadataRetrieve");
     31
     32    protected SimpleCollectionDatabase coll_db = null;
     33    protected String index_stem = null;
     34
     35    // the new service names
     36    protected static final String COVERAGE_SERVICE = "CoverageMetadataRetrieve";
     37
     38    // initialize any custom variables
     39    public CoverageMetadataRetrieve()
     40    {
     41
     42    }
     43
     44    // clean up anything that we need to
     45    public void cleanUp()
     46    {
     47        super.cleanUp();
     48    }
     49
     50    // Configure the class based in info in buildConfig.xml and collectionConfig.xml
     51    // info is the <serviceRack name="MyNewServicesTemplate"/> element from
     52    // buildConfig.xml, and extra_info is the whole collectionConfig.xml file
     53    // in case its needed
     54    public boolean configure(Element info, Element extra_info)
     55    {
     56
     57        if (!super.configure(info, extra_info))
     58        {
     59            return false;
     60        }
     61
     62        logger.info("Configuring CoverageMetadataRetrieve...");
     63
     64        // set up short_service_info - this currently is a list of services,
     65        // with their names and service types
     66        // we have two services, a new textquery, and a new one of a new type
     67        //Element tq_service = this.doc.createElement(GSXML.SERVICE_ELEM);
     68        //tq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
     69        //tq_service.setAttribute(GSXML.NAME_ATT, QUERY_SERVICE);
     70        //this.short_service_info.appendChild(tq_service);
     71
     72        Element diff_service = this.doc.createElement(GSXML.SERVICE_ELEM);
     73        diff_service.setAttribute(GSXML.TYPE_ATT, "retrieve");
     74        diff_service.setAttribute(GSXML.NAME_ATT, COVERAGE_SERVICE);
     75        this.short_service_info.appendChild(diff_service);
     76
     77        // the index stem is either specified in the config file or is  the collection name
     78        Element index_stem_elem = (Element) GSXML.getChildByTagName(info, GSXML.INDEX_STEM_ELEM);
     79        if (index_stem_elem != null)
     80        {
     81            this.index_stem = index_stem_elem.getAttribute(GSXML.NAME_ATT);
     82        }
     83        if (this.index_stem == null || this.index_stem.equals(""))
     84        {
     85            logger.error("CoverageMetadataRetrieve.configure(): indexStem element not found, stem will default to collection name");
     86            this.index_stem = this.cluster_name;
     87        }
     88
     89        // find out what kind of database we have
     90        Element database_type_elem = (Element) GSXML.getChildByTagName(info, GSXML.DATABASE_TYPE_ELEM);
     91        String database_type = null;
     92        if (database_type_elem != null)
     93        {
     94            database_type = database_type_elem.getAttribute(GSXML.NAME_ATT);
     95        }
     96        if (database_type == null || database_type.equals(""))
     97        {
     98            database_type = "gdbm"; // the default
     99        }
     100        coll_db = new SimpleCollectionDatabase(database_type);
     101        if (!coll_db.databaseOK())
     102        {
     103            logger.error("Couldn't create the collection database of type " + database_type);
     104            return false;
     105        }
     106
     107        // Open database for querying
     108        String coll_db_file = GSFile.collectionDatabaseFile(this.site_home, this.cluster_name, this.index_stem, database_type);
     109        if (!this.coll_db.openDatabase(coll_db_file, SimpleCollectionDatabase.READ))
     110        {
     111            logger.error("Could not open collection database!");
     112            return false;
     113        }
     114
     115        // Extract any relevant information from info and extra_info
     116        // This can be used to set up variables.
     117
     118        // If there is any formatting information, add it in to format_info_map
     119
     120        // Do this for all services as appropriate
     121        Element format = null; // find it from info/extra_info
     122        if (format != null)
     123        {
     124            this.format_info_map.put(COVERAGE_SERVICE, this.doc.importNode(format, true));
     125        }
     126
     127        return true;
     128
     129    }
     130
     131    // get the desription of a service. Could include parameter lists, displayText
     132    protected Element getServiceDescription(String service, String lang, String subset)
     133    {
     134
     135        // check that we have been asked for the right service
     136        if (!service.equals(COVERAGE_SERVICE))
     137        {
     138            return null;
     139        }
     140
     141        /*
     142         * if (service.equals(QUERY_SERVICE)) { Element tq_service =
     143         * this.doc.createElement(GSXML.SERVICE_ELEM);
     144         * tq_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_QUERY);
     145         * tq_service.setAttribute(GSXML.NAME_ATT, QUERY_SERVICE); if
     146         * (subset==null ||
     147         * subset.equals(GSXML.DISPLAY_TEXT_ELEM+GSXML.LIST_MODIFIER)) { // add
     148         * in any <displayText> elements // name, for example - get from
     149         * properties file
     150         * tq_service.appendChild(GSXML.createDisplayTextElement(this.doc,
     151         * GSXML.DISPLAY_TEXT_NAME, getTextString(QUERY_SERVICE+".name", lang)
     152         * )); }
     153         *
     154         * if (subset==null ||
     155         * subset.equals(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER)) { // add in a
     156         * param list if this service has parameters Element param_list =
     157         * this.doc.createElement(GSXML.PARAM_ELEM+GSXML.LIST_MODIFIER);
     158         * tq_service.appendChild(param_list); // create any params and append
     159         * to param_list } return tq_service; }
     160         */
     161
     162        if (service.equals(COVERAGE_SERVICE))
     163        {
     164            Element diff_service = this.doc.createElement(GSXML.SERVICE_ELEM);
     165            diff_service.setAttribute(GSXML.TYPE_ATT, "retrieve");
     166            diff_service.setAttribute(GSXML.NAME_ATT, COVERAGE_SERVICE);
     167            if (subset == null || subset.equals(GSXML.DISPLAY_TEXT_ELEM + GSXML.LIST_MODIFIER))
     168            {
     169                // add in any <displayText> elements
     170                // name, for example - get from properties file
     171                diff_service.appendChild(GSXML.createDisplayTextElement(this.doc, GSXML.DISPLAY_TEXT_NAME, getTextString(COVERAGE_SERVICE + ".name", lang)));
     172            }
     173
     174            if (subset == null || subset.equals(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER))
     175            {
     176                // add in a param list if this service has parameters
     177                Element param_list = this.doc.createElement(GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
     178                diff_service.appendChild(param_list);
     179                // create any params and append to param_list
     180            }
     181
     182            return diff_service;
     183        }
     184
     185        // not a valid service for this class
     186        return null;
     187
     188    }
     189
     190    /** This is the method that actually handles the TextQuery Service */
     191    //protected Element processTextQuery(Element request) {
    180192
    181193    //Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
     
    185197    // fill in the rest
    186198    //return result;
    187     //}
    188 
    189     /** This is the method that actually handles the MyDifferentService service */
    190     protected Element processCoverageMetadataRetrieve(Element request) {
    191 
    192    
    193     if (!this.coll_db.databaseOK()) {
    194     logger.error("No valid database found\n");
    195     return null;
    196     }
    197 
    198     DBInfo collection_info = this.coll_db.getInfo("collection");
    199    
    200     Set<String> keys = collection_info.getKeys();
    201 
    202     Vector<String> valid_keys = new Vector<String>();
    203 
    204     // Iterate over keys and add valid ones to the valid_keys vector
    205     String current_key = null;
    206     Iterator<String> iter = keys.iterator();
    207 
    208     while (iter.hasNext()) {
    209         current_key = iter.next();
    210         if(current_key.matches("^metadatalist-([a-zA-Z][^-])*$"))
    211         {
    212             logger.error("********** ADDING " + current_key + " TO VALID KEYS LIST **********\n");
    213             valid_keys.add(current_key);
    214         }
    215     }
    216    
    217     // Create response
    218     Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
    219     result.setAttribute(GSXML.FROM_ATT, COVERAGE_SERVICE);
    220     result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
    221 
    222     Element metadataSetList = this.doc.createElement("metadataSetList");
    223     result.appendChild(metadataSetList);
    224 
    225 
    226     // Iterate over valid keys and build up response
    227     Element metadataSet = null;
    228     Element metadata = null;
    229     String value = null;
    230     String name = null;
    231     iter = valid_keys.iterator();
    232 
    233     while (iter.hasNext()) {
    234         current_key = iter.next();
    235 
    236         // Create metadataSet using the current key and add to metadataSetList
    237         metadataSet = this.doc.createElement("metadataSet");
    238         if(current_key.indexOf("-") != -1)
    239         {
    240             name = current_key.split("-")[1];
    241         }
    242         metadataSet.setAttribute(GSXML.NAME_ATT, name);
    243         metadataSetList.appendChild(metadataSet);
    244 
    245         // Create a metadata element for each value and add to metadataSet
    246         Vector<String> sub_info = collection_info.getMultiInfo(current_key);
    247         Iterator<String> iter2 = sub_info.iterator();
    248         while (iter2.hasNext()) {
    249             value = iter2.next();     
    250             metadata = this.doc.createElement("metadata");
    251             metadata.setAttribute(GSXML.NAME_ATT, value);
    252             metadataSet.appendChild(metadata);
    253         }
    254 
    255     }
    256 
    257     return result;
    258 
    259     }
     199    //}
     200
     201    /** This is the method that actually handles the MyDifferentService service */
     202    protected Element processCoverageMetadataRetrieve(Element request)
     203    {
     204
     205        if (!this.coll_db.databaseOK())
     206        {
     207            logger.error("No valid database found\n");
     208            return null;
     209        }
     210
     211        DBInfo collection_info = this.coll_db.getInfo("collection");
     212
     213        Set<String> keys = collection_info.getKeys();
     214
     215        Vector<String> valid_keys = new Vector<String>();
     216
     217        // Iterate over keys and add valid ones to the valid_keys vector
     218        String current_key = null;
     219        Iterator<String> iter = keys.iterator();
     220
     221        while (iter.hasNext())
     222        {
     223            current_key = iter.next();
     224            if (current_key.matches("^metadatalist-([a-zA-Z][^-])*$"))
     225            {
     226                logger.error("********** ADDING " + current_key + " TO VALID KEYS LIST **********\n");
     227                valid_keys.add(current_key);
     228            }
     229        }
     230
     231        // Create response
     232        Element result = this.doc.createElement(GSXML.RESPONSE_ELEM);
     233        result.setAttribute(GSXML.FROM_ATT, COVERAGE_SERVICE);
     234        result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
     235
     236        Element metadataSetList = this.doc.createElement("metadataSetList");
     237        result.appendChild(metadataSetList);
     238
     239        // Iterate over valid keys and build up response
     240        Element metadataSet = null;
     241        Element metadata = null;
     242        String value = null;
     243        String name = null;
     244        iter = valid_keys.iterator();
     245
     246        while (iter.hasNext())
     247        {
     248            current_key = iter.next();
     249
     250            // Create metadataSet using the current key and add to metadataSetList
     251            metadataSet = this.doc.createElement("metadataSet");
     252            if (current_key.indexOf("-") != -1)
     253            {
     254                name = current_key.split("-")[1];
     255            }
     256            metadataSet.setAttribute(GSXML.NAME_ATT, name);
     257            metadataSetList.appendChild(metadataSet);
     258
     259            // Create a metadata element for each value and add to metadataSet
     260            Vector<String> sub_info = collection_info.getMultiInfo(current_key);
     261            Iterator<String> iter2 = sub_info.iterator();
     262            while (iter2.hasNext())
     263            {
     264                value = iter2.next();
     265                metadata = this.doc.createElement("metadata");
     266                metadata.setAttribute(GSXML.NAME_ATT, value);
     267                metadataSet.appendChild(metadata);
     268            }
     269
     270        }
     271
     272        return result;
     273
     274    }
    260275}
    261 
    262 
    263 
Note: See TracChangeset for help on using the changeset viewer.