/***************************************************************************
    author: Franz Philipp Moser (pm AT fpm-edv.at)
    based on a script of hns (http://www.henso.com/):
    http://www.antville.org/static/project/files/export.hac
    thx for the prework
 ***************************************************************************/

/***************************************************************************
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 ***************************************************************************/
 
/*
 * Exports all nodes of the antville installation 
 * using the NodeManager of helma and a second DbSource
 *
 * @param req.data.newdatabase (optional) the name of the new database
 *
 * @return a lot of debuging messages
 */
function export_action() {
   var newAntvilleTable = req.data.newdatabase ? req.data.newdatabase : "antville_new";;
   var nmgr = app.__app__.getNodeManager();
   var dbs = app.__app__.getDbSource(newAntvilleTable);
   
   if (!dbs) {
      res.debug("DbSource &quot;" + newAntvilleTable + "&quot; not found!");
      res.debug("check your db.properties");
      return;
   }
   // export each site
   for (var i = 0; i < root.size(); i++) {
      var aSite = root.get(i);
      try {
         nmgr.exportNode(aSite.__node__, dbs);
         res.debug("exporting site " + aSite);
      } catch (e) {
         res.debug("error: exporting site: " + aSite.alias);
         res.debug("THE ERROR: " + e);
         // if we got no site object we take the next site
         if (!aSite)
            continue;
      }
      
      // export the members of the site
      for (var j = 0; aSite.members && j < aSite.members.size(); j++) {
         var aMember = aSite.members.get(j);
         try {
            nmgr.exportNode(aMember.__node__, dbs);
            res.debug("exporting membership " + aMember.username);
         } catch (e) {
            res.debug("error: exporting membership: " + aMember);
            res.debug("THE ERROR: " + e);
         }
            
      }
      
      // export the files of the site
      for (var j = 0; aSite.files && j < aSite.files.size(); j++) {
         var aFile = aSite.files.get(j);
         try {
            nmgr.exportNode(aFile.__node__, dbs);
            res.debug("exporting file " + aFile.alias);
         } catch (e) {
            res.debug("error: exporting file: " + aFile);
            res.debug("THE ERROR: " + e);
         }
            
      }
      
      // export the images of the site
      for (var j = 0; aSite.images && j < aSite.images.size(); j++) {
         var anImage = aSite.images.get(j);
         try {
            nmgr.exportNode(anImage.__node__, dbs);
            res.debug("exporting image " + anImage.alias);
         } catch (e) {
            res.debug("error: exporting image: " + anImage);
            res.debug("THE ERROR: " + e);
         }
      }

      // export the layouts of the site
      for (var j = 0; aSite.layouts && j < aSite.layouts.size(); j++) {
         var aLayout = aSite.layouts.get(j);
         try {
            nmgr.exportNode(aLayout.__node__, dbs);
            res.debug("exporting layout " + aLayout.alias);
         } catch (e) {
            res.debug("error: exporting layout: " + aLayout);
            res.debug("THE ERROR: " + e);
            // if we got no layout object we take the next layout
            if (!aLayout)
               continue;
         }
         // exporting skins
         for (var k = 0; aLayout.skins && k < aLayout.skins.size(); k++) {
            // get grouped collection
            var aSkinGroup = aLayout.skins.get(k);
            for (var l = 0; aSkinGroup && l < aSkinGroup.size(); l++) {
            var aSkin = aSkinGroup.get(l);
               try {
                  nmgr.exportNode(aSkin.__node__, dbs);
                  res.debug("exporting skin " + aSkin.proto + "." + aSkin.name);
               } catch (e) {
                  res.debug("error: exporting skin: " + aSkin);
                  res.debug("THE ERROR: " + e);
               }
            }
         }
         // export layoutimages
         for (var k = 0; aLayout.images && k < aLayout.images.size(); k++) {
            var aLayoutImage = aLayout.images.get(k);
            try {
               nmgr.exportNode(aLayoutImage.__node__, dbs);
               res.debug("exporting skin " + aLayoutImage.alias);
            } catch (e) {
               res.debug("error: exporting skin: " + aLayoutImage);
               res.debug("THE ERROR: " + e);
            }
         }
      }
      
      // export the polls of the site
      for (var j = 0; aSite.polls && j < aSite.polls.size(); j++) {
         var aPoll = aSite.polls.get(j);
         try {
            nmgr.exportNode(aPoll.__node__, dbs);
            res.debug("exporting poll " + aPoll.question);
         } catch (e) {
            res.debug("error: exporting poll: " + aPoll);
            res.debug("THE ERROR: " + e);
            // if we got no poll object we take the next poll
            if (!aPoll)
               continue;
         }
         
         // exporting choices
         for (var k = 0; k < aPoll.size(); k++) {
            var aChoice = aPoll.get(k);
            try {
               nmgr.exportNode(aChoice.__node__, dbs);
               res.debug("exporting choice " + aChoice.title);
            } catch (e) {
               res.debug("error: exporting choice: " + aChoice);
               res.debug("THE ERROR: " + e);
            }
         }
         
         // export the votes to the polls
         for (var k = 0; aPoll.votes && k < aPoll.votes.size(); k++) {
            var aVote = aPoll.votes.get(k);
            try {
               nmgr.exportNode(aVote.__node__, dbs);
               res.debug("exporting vote " + aVote);
            } catch (e) {
               res.debug("error: exporting vote: " + aVote);
               res.debug("THE ERROR: " + e);
            }
         }
      }
      
   }
   
   root.storiesByID.prefetchChildren();
   
   // export Stories
   for (var i = 0; i < root.storiesByID.size(); i++) {
      var aStory = root.storiesByID.get(i);
      try {
         nmgr.exportNode(aStory.__node__, dbs);
         res.debug("exporting story " + aStory._id);
      } catch (e) {
         res.debug("error: exporting story: " + aStory);
         res.debug("THE ERROR: " + e);
      }
      if (!aStory || !aStory.comments)
         continue;
      
      aStory.comments.prefetchChildren();
      
      // export comment to stories
      for (var j = 0; j < aStory.comments.size(); j++) {
         var aComment = aStory.comments.get(j);
         try {
            nmgr.exportNode(aComment.__node__, dbs);
            res.debug("exporting comment " + aComment._id);
         } catch (e) {
            res.debug("error: exporting comment: " + aComment);
            res.debug("THE ERROR: " + e);
         }
      }
   }
   
   root.users.prefetchChildren();
   
   // export all users
   for (var i = 0; i < root.users.size(); i++) {
      var aUser = root.users.get(i);
      try {
         nmgr.exportNode(aUser.__node__, dbs)
         res.debug("exporting user: " + aUser.name);
      } catch (e) {
         res.debug("error: exporting user: " + aUser);
         res.debug("THE ERROR: " + e);
      }
   }

   root.manage.syslogs.prefetchChildren();
   // export all syslog entries
   for (var i = 0; root.manage.syslogs && i < root.manage.syslogs.size(); i++) {
      var aSyslogEntry = root.manage.syslogs.get(i);
      try {
         nmgr.exportNode(aSyslogEntry.__node__, dbs)
         res.debug("exporting syslogentry: " + aSyslogEntry);
      } catch (e) {
         res.debug("error: exporting syslogentry: " + aSyslogEntry);
         res.debug("THE ERROR: " + e);
      }

   }
   
   root.layouts.prefetchChildren();
   // export all rootlayouts
   for (var i = 0; root.layouts && i < root.layouts.size(); i++) {
      var aRootLayout = root.layouts.get(i);
      try {
         nmgr.exportNode(aRootLayout.__node__, dbs)
         res.debug("exporting layout: " + aRootLayout.alias);
      } catch (e) {
         res.debug("error: exporting layout: " + aRootLayout);
         res.debug("THE ERROR: " + e);
         if (!aRootLayout)
            continue;
      }
      // exporting skins
      for (var j = 0; aRootLayout.skins && j < aRootLayout.skins.size(); j++) {
         // get grouped collection
         var aSkinGroup = aRootLayout.skins.get(j);
         for (var k = 0; aSkinGroup && k < aSkinGroup.size(); k++) {
         var aSkin = aSkinGroup.get(k);
            try {
               nmgr.exportNode(aSkin.__node__, dbs);
               res.debug("exporting skin " + aSkin.proto + "." + aSkin.name);
            } catch (e) {
               res.debug("error: exporting skin: " + aSkin);
               res.debug("THE ERROR: " + e);
            }
         }
      }
      // export layoutimages
      res.debug("aRootLayout.images.size(): " + aRootLayout.images.size());
      for (var k = 0; aRootLayout.images && k < aRootLayout.images.size(); k++) {
         var aLayoutImage = aRootLayout.images.get(k);
         try {
            nmgr.exportNode(aLayoutImage.__node__, dbs);
            res.debug("exporting skin " + aLayoutImage.alias);
         } catch (e) {
            res.debug("error: exporting skin: " + aLayoutImage);
            res.debug("THE ERROR: " + e);
         }
      }
   }
}


