      1 // The Antville Project
      2 // http://code.google.com/p/antville
      3 //
      4 // Copyright 2007-2011 by Tobi Schäfer.
      5 //
      6 // Copyright 2001–2007 Robert Gaggl, Hannes Wallnöfer, Tobi Schäfer,
      7 // Matthias & Michael Platzer, Christoph Lincke.
      8 //
      9 // Licensed under the Apache License, Version 2.0 (the ``License'');
     10 // you may not use this file except in compliance with the License.
     11 // You may obtain a copy of the License at
     12 //
     13 //    http://www.apache.org/licenses/LICENSE-2.0
     14 //
     15 // Unless required by applicable law or agreed to in writing, software
     16 // distributed under the License is distributed on an ``AS IS'' BASIS,
     17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     18 // See the License for the specific language governing permissions and
     19 // limitations under the License.
     20 //
     21 // $Revision$
     22 // $Author$
     23 // $Date$
     24 // $URL$
     25
     26 /**
     27  * @fileOverview Defines the Importer namespace.
     28  */
     29
     30 /**
     31  * The Importer namespace provides methods for importing a site.
     32  * @namespace
     33  */
     34 var Importer = {}
     35
     36 /**
     37  * Imports a site and its content for the specified user.
     38  * @param {Site} site The site to import.
     39  * @param {User} user The user who will become the creator of the site’s imported content.
     40  */
     41 Importer.run = function(site, user) {
     42    try {
     43       var xml = File.getById(site.import_id);
     44       if (xml) {
     45          var file = new java.io.File(xml.getFile());
     46          var reader = new rome.XmlReader(file);
     47          var input = new rome.SyndFeedInput(true);
     48          var feed = input.build(reader);
     49          Api.constrain(site, user);
     50          for (var i=0; i<feed.entries.size(); i+=1) {
     51             var entry = feed.entries.get(i);
     52             var category = entry.categories.get(0);
     53             if (category.name !== "http://schemas.google.com/blogger/2008/kind#post") {
     54                continue;
     55             }
     56             var story = Story.add({
     57                title: entry.title,
     58                text: entry.description || entry.contents.get(0).value,
     59                created: entry.publishedDate.format("yyyy-MM-dd HH:mm"),
     60                status: Story.CLOSED,
     61                mode: Story.FEATURED
     62             }, site, user);
     63          }
     64          File.remove.call(xml);
     65       }
     66    } catch (ex) {
     67       app.log(ex);
     68    }
     69
     70    // Reset the site’s export status
     71    site.job = null;
     72    site.import_id = null;
     73    return;
     74 }
     75