package org.honours.agents; import java.awt.Color; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileWriter; import java.util.ArrayList; import java.util.List; import org.expeditee.agents.DefaultAgent; import org.expeditee.gui.Frame; import org.expeditee.gui.MessageBay; import org.honours.Main; import org.honours.collection.*; import org.honours.greenstone.CollectionContentEditor; import org.honours.greenstone.CollectionCustomizer; /** * This agent sends a collection (represented by an * Expeditee frameset) back to Greenstone. * @author Korii Scrivener */ public class CollectionToGreenstone extends DefaultAgent { private static String COLLECTIONS_HOME; private static String _collection; private static String _collectionPath; private static List _filesToMove = new ArrayList(); public static void addFileToMove(String filename, String assocfilepath){ _filesToMove.add(new String[]{filename,assocfilepath}); } public Frame process(Frame frame){ //Check if frame belongs to a collection and then obtain a //collection object. Collection collection = Collection.findCollection(frame); if(collection == null){ MessageBay.errorMessage("Error: No collection could be obtained for" + frame.getFramesetName() + ". GSDL exporter is now stopping."); return null; } _collection = collection.getName(); //Check if GSDL is set. if(Main.GSDL_HOME != null){ COLLECTIONS_HOME = Main.GSDL_HOME + File.separator + "sites" + File.separator + "localsite" + File.separator + "collect"; _collectionPath = COLLECTIONS_HOME + File.separator + _collection; }else{ MessageBay.errorMessage("ERROR: No Greenstone environment variable set. Now stopping"); return null; } File collectionFolder = new File(COLLECTIONS_HOME + File.separator + _collection); if(collectionFolder.isDirectory()) rebuildCollection(collection); MessageBay.displayMessage("Success: Collection has been successfully edited & rebuilt", Color.green); return null; } /** * Rebuild the Greenstone collection. * @param collection */ private void rebuildCollection(Collection collection){ try{ CollectionCustomizer customizer = CollectionCustomizer.getInstance(_collectionPath); CollectionContentEditor contentEditor = null; //loop through all the items in the collection and read their matching frames. for(int i = 0; i < collection.getItems().size(); i++){ CollectionItem currCI = collection.getItems().get(i); Frame currCiFrame = currCI.getFrame(); if(currCiFrame == null) continue; contentEditor = new CollectionContentEditor(currCI,_collectionPath); if(i == 0) customizer.customize(currCI); contentEditor.editCollectionItems(); } build(); CollectionCustomizer.reset(); /*sleep to give time for collection to be rebuilt before *proceeding to move files to index/assoc/assocfilepath directory. Otherwise *if you try to move files while rebuilding is still going, they will just be removed. *This is done for moving/adding extra files. Maybe it would be better to add them elsewhere *i.e. an "images folder...*/ Thread.sleep(45000); //move any images to index/assoc folder in GSDL once building is completed. moveFiles(); _filesToMove = new ArrayList(); }catch(Exception e){ e.printStackTrace(); return; } } /** * Method to move files currently not part of collection (e.g. images) * to the collection's index/assoc/assocfilepath directory. */ private static void moveFiles() { try{ String newPath = _collectionPath + File.separator + "index" + File.separator + "assoc" + File.separator; for(int i =0; i < _filesToMove.size(); i++){ String f = _filesToMove.get(i)[0]; String a = _filesToMove.get(i)[1]; newPath += a + File.separator; File origFile = new File(f); if(origFile.exists()){ FileInputStream fis = new FileInputStream(origFile); newPath += origFile.getName(); FileOutputStream fos = new FileOutputStream(newPath); byte[] buf = new byte[fis.available()]; fis.read(buf); fos.write(buf); } newPath = _collectionPath + File.separator + "index" + File.separator + "assoc" + File.separator; } }catch(Exception e){ e.printStackTrace(); } } /** * Run batch script to rebuild collection. */ private void build(){ try{ //"cmd /c start .../build.bat" String command = "cmd /c start collectionBuilder.bat"; createBatchFile(); Runtime.getRuntime().exec(command); } catch(Exception e){ MessageBay.errorMessage("ERROR: Failed to build collection."); e.printStackTrace(); return; } } /** * Method to create batch file which runs when * we want to export a frameset to Greenstone and * rebuild the matching collection. */ private void createBatchFile(){ StringBuffer cmds = new StringBuffer(""); cmds.append("@echo off\n"); //TODO: Change this so it uses a relative path to GSDL. String chdir = "C:\\Research\\Pei-Jones-Taonga\\expeditee-svn\\images\\greenstone3-svn4"; cmds.append("chdir \"" + ((chdir != null) ? chdir : "") + "\"\n"); cmds.append("call \"./gs3-setup.bat\"\n"); cmds.append("perl -S buildcol.pl -verbosity 5 -site localsite " + _collection + "\n"); //cmds.append("perl -S incremental-rebuild.pl -verbosity 5 -site localsite " + _collection + "\n"); cmds.append("perl -S activate.pl -site localsite " + _collection + "\n"); cmds.append("pause\n"); cmds.append("exit\n"); try{ FileWriter fwriter = new FileWriter("collectionBuilder.bat"); BufferedWriter out = new BufferedWriter(fwriter); out.write(cmds.toString()); out.close(); }catch(Exception e){ e.printStackTrace(); return; } } }