1 | package org.honours.agents;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 |
|
---|
5 | import org.expeditee.agents.DefaultAgent;
|
---|
6 | import org.expeditee.gui.Frame;
|
---|
7 | import org.expeditee.gui.FrameIO;
|
---|
8 | import org.expeditee.items.Text;
|
---|
9 |
|
---|
10 | import org.honours.Main;
|
---|
11 | import org.honours.collection.Collection;
|
---|
12 |
|
---|
13 | import org.honours.collection.CollectionItem;
|
---|
14 | import org.honours.greenstone.ArchiveFileEditor;
|
---|
15 | import org.honours.greenstone.ArchiveFileReader;
|
---|
16 | import org.honours.gui.HonoursFrameIO;
|
---|
17 | import org.honours.gui.Interfaces;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * This is used to import items from GSDL not
|
---|
21 | * already in Expeditee and to remove items
|
---|
22 | * from Expeditee no longer in GSDL. At this stage,
|
---|
23 | * this is done by comparing doc.xml files. Would be
|
---|
24 | * better to do this from GSDL.
|
---|
25 | * @author Korii
|
---|
26 | */
|
---|
27 | public class UpdateCollection extends DefaultAgent {
|
---|
28 |
|
---|
29 | private Collection _collection;
|
---|
30 |
|
---|
31 | public Frame process(Frame frame) {
|
---|
32 |
|
---|
33 | //Obtain collection and its name.
|
---|
34 | _collection = Collection.findCollection(frame);
|
---|
35 | int index = Collection.getCollectionPosition(_collection);
|
---|
36 |
|
---|
37 | if(_collection == null)
|
---|
38 | return null;
|
---|
39 |
|
---|
40 | //Obtain archives directory.
|
---|
41 | String archivesDir = Main.GSDL_HOME + File.separator + "sites" + File.separator + "localsite" +
|
---|
42 | File.separator + "collect" + File.separator + _collection.getName() + File.separator + "archives";
|
---|
43 | File archivesDirFolder = new File(archivesDir);
|
---|
44 |
|
---|
45 | //get all files/folders in archivesDirFolder.
|
---|
46 | File[] archivesSubFolders = archivesDirFolder.listFiles();
|
---|
47 |
|
---|
48 | for(File f : archivesSubFolders){
|
---|
49 |
|
---|
50 | if(f.isDirectory()){
|
---|
51 | String docXML = findDocXML(f);
|
---|
52 |
|
---|
53 | //Check for an InExpeditee metadata.
|
---|
54 | ArchiveFileReader afr = new ArchiveFileReader(docXML);
|
---|
55 | String inExpediteeMetadata = afr.obtainElementText("InExpeditee");
|
---|
56 | String assocFilePathMetadata = afr.obtainElementText("assocfilepath");
|
---|
57 | String assocFileMetadata = afr.obtainElementText("gsdlassocfile").split(":")[0];
|
---|
58 |
|
---|
59 | if(inExpediteeMetadata == null){
|
---|
60 | createNewCollectionItem(archivesDir, docXML,assocFilePathMetadata, assocFileMetadata);
|
---|
61 | }else if(inExpediteeMetadata.equals("0")){
|
---|
62 | createNewCollectionItem(archivesDir, docXML,assocFilePathMetadata, assocFileMetadata);
|
---|
63 | }
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | //Replace collection with newly updated collection.
|
---|
68 | Main._collections.remove(index);
|
---|
69 | Main._collections.add(index, _collection);
|
---|
70 |
|
---|
71 | return frame;
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Recursive method to obtain doc.xml file
|
---|
76 | * associated with this item. Sometimes there are
|
---|
77 | * subfolders within an archive folder and we have
|
---|
78 | * to search through these to obtain the doc.xml file.
|
---|
79 | * @param subDir
|
---|
80 | * @return
|
---|
81 | */
|
---|
82 | private String findDocXML(File subDir){
|
---|
83 |
|
---|
84 | File[] subDirFiles = subDir.listFiles();
|
---|
85 | String docXML = null;
|
---|
86 |
|
---|
87 | for(File f : subDirFiles){
|
---|
88 |
|
---|
89 | if(f.getName().equals("doc.xml")){
|
---|
90 | docXML = f.getAbsolutePath();
|
---|
91 | return docXML;
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | for(File f : subDirFiles){
|
---|
96 | if(f.isDirectory()){
|
---|
97 | return findDocXML(f);
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | return docXML;
|
---|
102 | }
|
---|
103 |
|
---|
104 | //TODO: Also generate layers associated with this frame.
|
---|
105 | /**
|
---|
106 | * Generate a new collection item.
|
---|
107 | * @param archivesDir
|
---|
108 | * @param docXML
|
---|
109 | * @param assocFilePathMetadata
|
---|
110 | * @param assocFileMetadata
|
---|
111 | */
|
---|
112 | private void createNewCollectionItem(String archivesDir, String docXML, String assocFilePathMetadata, String assocFileMetadata) {
|
---|
113 |
|
---|
114 | //Make a new frame
|
---|
115 | HonoursFrameIO.generateFrame((FrameIO.getLastNumber(_collection.getName())+1), _collection.getName());
|
---|
116 | Frame newCollectionItemFrame = FrameIO.LoadFrame(_collection.getName()+FrameIO.getLastNumber(_collection.getName()));
|
---|
117 |
|
---|
118 | //Make a new collectionItem
|
---|
119 | CollectionItem newCollectionItem = new CollectionItem();
|
---|
120 | newCollectionItem.setDocXML(docXML);
|
---|
121 | newCollectionItem.setAssocfilePath(assocFilePathMetadata);
|
---|
122 |
|
---|
123 | String newAssocFileMetadata = archivesDir + File.separator + assocFilePathMetadata + File.separator + assocFileMetadata;
|
---|
124 | newCollectionItem.setAssociatedFile(newAssocFileMetadata);
|
---|
125 |
|
---|
126 | newCollectionItem.setFrame(newCollectionItemFrame);
|
---|
127 |
|
---|
128 | //need to write out newCollectionItem frame and assocfilepath to info object.
|
---|
129 | _collection.getCollectionInformation().addRow("I", newCollectionItem.getFrame().getName(), assocFilePathMetadata);
|
---|
130 | _collection.getCollectionInformation().sort();
|
---|
131 |
|
---|
132 | Interfaces.setUpToolbarOverlay(newCollectionItem);
|
---|
133 | Interfaces.setUpToggleToolbarOverlay(newCollectionItem);
|
---|
134 |
|
---|
135 | //Update docxml
|
---|
136 | Text text = new Text("1");
|
---|
137 | text.setData("InExpeditee");
|
---|
138 | ArchiveFileEditor afe = new ArchiveFileEditor(newCollectionItem.getDocXML());
|
---|
139 | afe.editArchiveDocFile(text);
|
---|
140 | afe.write();
|
---|
141 |
|
---|
142 | _collection.addItem(newCollectionItem);
|
---|
143 |
|
---|
144 | //TODO: HACK for now just to show that this stuff works... Must fix this!!
|
---|
145 | ArchiveFileReader afr = new ArchiveFileReader(docXML);
|
---|
146 | String catNumber = afr.obtainElementText("Catalogue-Number");
|
---|
147 | String physicalLocation = afr.obtainElementText("Physical-Location");
|
---|
148 | String provenance = afr.obtainElementText("Provenance");
|
---|
149 | String description = afr.obtainElementText("Description");
|
---|
150 |
|
---|
151 | newCollectionItem.getFrame().addText(100, 200, catNumber, null);
|
---|
152 | newCollectionItem.getFrame().addText(100,250,physicalLocation,null);
|
---|
153 | newCollectionItem.getFrame().addText(100,300,provenance,null);
|
---|
154 | newCollectionItem.getFrame().addText(100,350,description,null);
|
---|
155 | newCollectionItem.getFrame().addText(100,400,"@i: " + newAssocFileMetadata + " 0.5",null);
|
---|
156 | }
|
---|
157 |
|
---|
158 | } |
---|