package org.honours.agents; import java.io.File; import org.expeditee.agents.DefaultAgent; import org.expeditee.gui.Frame; import org.expeditee.gui.FrameIO; import org.expeditee.items.Text; import org.honours.Main; import org.honours.collection.Collection; import org.honours.collection.CollectionItem; import org.honours.greenstone.ArchiveFileEditor; import org.honours.greenstone.ArchiveFileReader; import org.honours.gui.HonoursFrameIO; import org.honours.gui.Interfaces; /** * This is used to import items from GSDL not * already in Expeditee and to remove items * from Expeditee no longer in GSDL. At this stage, * this is done by comparing doc.xml files. Would be * better to do this from GSDL. * @author Korii */ public class UpdateCollection extends DefaultAgent { private Collection _collection; public Frame process(Frame frame) { //Obtain collection and its name. _collection = Collection.findCollection(frame); int index = Collection.getCollectionPosition(_collection); if(_collection == null) return null; //Obtain archives directory. String archivesDir = Main.GSDL_HOME + File.separator + "sites" + File.separator + "localsite" + File.separator + "collect" + File.separator + _collection.getName() + File.separator + "archives"; File archivesDirFolder = new File(archivesDir); //get all files/folders in archivesDirFolder. File[] archivesSubFolders = archivesDirFolder.listFiles(); for(File f : archivesSubFolders){ if(f.isDirectory()){ String docXML = findDocXML(f); //Check for an InExpeditee metadata. ArchiveFileReader afr = new ArchiveFileReader(docXML); String inExpediteeMetadata = afr.obtainElementText("InExpeditee"); String assocFilePathMetadata = afr.obtainElementText("assocfilepath"); String assocFileMetadata = afr.obtainElementText("gsdlassocfile").split(":")[0]; if(inExpediteeMetadata == null){ createNewCollectionItem(archivesDir, docXML,assocFilePathMetadata, assocFileMetadata); }else if(inExpediteeMetadata.equals("0")){ createNewCollectionItem(archivesDir, docXML,assocFilePathMetadata, assocFileMetadata); } } } //Replace collection with newly updated collection. Main._collections.remove(index); Main._collections.add(index, _collection); return frame; } /** * Recursive method to obtain doc.xml file * associated with this item. Sometimes there are * subfolders within an archive folder and we have * to search through these to obtain the doc.xml file. * @param subDir * @return */ private String findDocXML(File subDir){ File[] subDirFiles = subDir.listFiles(); String docXML = null; for(File f : subDirFiles){ if(f.getName().equals("doc.xml")){ docXML = f.getAbsolutePath(); return docXML; } } for(File f : subDirFiles){ if(f.isDirectory()){ return findDocXML(f); } } return docXML; } //TODO: Also generate layers associated with this frame. /** * Generate a new collection item. * @param archivesDir * @param docXML * @param assocFilePathMetadata * @param assocFileMetadata */ private void createNewCollectionItem(String archivesDir, String docXML, String assocFilePathMetadata, String assocFileMetadata) { //Make a new frame HonoursFrameIO.generateFrame((FrameIO.getLastNumber(_collection.getName())+1), _collection.getName()); Frame newCollectionItemFrame = FrameIO.LoadFrame(_collection.getName()+FrameIO.getLastNumber(_collection.getName())); //Make a new collectionItem CollectionItem newCollectionItem = new CollectionItem(); newCollectionItem.setDocXML(docXML); newCollectionItem.setAssocfilePath(assocFilePathMetadata); String newAssocFileMetadata = archivesDir + File.separator + assocFilePathMetadata + File.separator + assocFileMetadata; newCollectionItem.setAssociatedFile(newAssocFileMetadata); newCollectionItem.setFrame(newCollectionItemFrame); //need to write out newCollectionItem frame and assocfilepath to info object. _collection.getCollectionInformation().addRow("I", newCollectionItem.getFrame().getName(), assocFilePathMetadata); _collection.getCollectionInformation().sort(); Interfaces.setUpToolbarOverlay(newCollectionItem); Interfaces.setUpToggleToolbarOverlay(newCollectionItem); //Update docxml Text text = new Text("1"); text.setData("InExpeditee"); ArchiveFileEditor afe = new ArchiveFileEditor(newCollectionItem.getDocXML()); afe.editArchiveDocFile(text); afe.write(); _collection.addItem(newCollectionItem); //TODO: HACK for now just to show that this stuff works... Must fix this!! ArchiveFileReader afr = new ArchiveFileReader(docXML); String catNumber = afr.obtainElementText("Catalogue-Number"); String physicalLocation = afr.obtainElementText("Physical-Location"); String provenance = afr.obtainElementText("Provenance"); String description = afr.obtainElementText("Description"); newCollectionItem.getFrame().addText(100, 200, catNumber, null); newCollectionItem.getFrame().addText(100,250,physicalLocation,null); newCollectionItem.getFrame().addText(100,300,provenance,null); newCollectionItem.getFrame().addText(100,350,description,null); newCollectionItem.getFrame().addText(100,400,"@i: " + newAssocFileMetadata + " 0.5",null); } }