Previous | Next
WireHose Developers Guide

Fetching dictionaries

Now you'll start building Hello World's importer. This class will use the WHImporter utility class to do most of its heavy lifting.

  1. Expand the Classes group in the Files pane.
  2. Chose New File... from File menu. Scroll down to the Pure Java Java Class template and click Next.

  3. Name it Importer.java, add it to the Application Server target in the Hello World project, and click Finish.

  4. Add these lines to Importer.java
    import com.wirehose._util.*;
    import com.wirehose.base.*;
  5. Add this method:
    public static void importFeeds() {
       
        // fetch feeds list as dictionary
        NSMutableDictionary rss = WHImporter.fetchDictionaryFromURL(
          "SampleRSSFeeds.xml", 
          "Contents/Resources/rss20MappingModel.xml");
       
        // extract feeds from dictionary and clean up tags
        NSMutableArray snapshots = 
          cleanSnapshots(rss.valueForKeyPath("channel.items"));
                
        EOEditingContext ec = new EOEditingContext();
        ec.lock();
        try {
       
            // insert resources into editing context
            WHImporter.insertResources(
               ec, snapshots, "RSSFeed", "Feeds/", null, 
               WHImporter.IgnoreAndTag, true, true, true, true, false);
       
            ec.saveChanges();
        } catch (Exception e) {
            System.out.println("Error importing resources: "+e);
            e.printStackTrace();
        }
       
        try {
            ec.saveChanges();
        } catch (Exception e) {
            System.out.println("Exception saving changes: "+e);
        }
        ec.unlock();
        ec.dispose();
    }

    This method uses two methods from WHImporter. The first, fetchDictionaryFromURL, takes two arguments which specify the location of an XML file to be imported, and the mapping model which will turn the XML into a dictionary.

    The other WHImporter method, insertResources, is a general-purpose utility for inserting resources into a database. It takes several arguments which specify how to handle resources which already exist in the database, whether or not to index keywords for newly inserted resources, whether to add tags to resources, etc.

    The arguments used here ensure that if a new resource already exists in the database, the new resource will be ignored rather than inserted. If any tags are specified on the new resource, those tags will be assigned to the already existing resource. This handles the case where an identical resource has been imported multiple times in multiple categories, by having only a single resource with multiple tags.

    The "Feeds/" argument specifies a tag path prefix. Any categories described in the feed will be appended to this string before being turned into tags. For example, if a feed has an assigned category of "Consumer/Gardening", the tag used will actually be "Feeds/Consumer/Gardening".


Previous | Next