source: other-projects/GlamED/trunk/src/org/honours/agents/CollectionToGreenstone.java@ 26600

Last change on this file since 26600 was 26600, checked in by davidb, 11 years ago

Changed filepath of greenstone3-svn.

File size: 5.9 KB
Line 
1package org.honours.agents;
2
3import java.awt.Color;
4import java.io.BufferedWriter;
5import java.io.File;
6import java.io.FileInputStream;
7import java.io.FileOutputStream;
8import java.io.FileWriter;
9import java.util.ArrayList;
10import java.util.List;
11
12import org.expeditee.agents.DefaultAgent;
13import org.expeditee.gui.Frame;
14import org.expeditee.gui.MessageBay;
15
16import org.honours.Main;
17import org.honours.collection.*;
18import org.honours.greenstone.CollectionContentEditor;
19import org.honours.greenstone.CollectionCustomizer;
20
21/**
22 * This agent sends a collection (represented by an
23 * Expeditee frameset) back to Greenstone.
24 * @author Korii Scrivener
25 */
26public class CollectionToGreenstone extends DefaultAgent {
27
28 private static String COLLECTIONS_HOME;
29 private static String _collection;
30 private static String _collectionPath;
31 private static List<String[]> _filesToMove = new ArrayList<String[]>();
32
33
34 public static void addFileToMove(String filename, String assocfilepath){
35 _filesToMove.add(new String[]{filename,assocfilepath});
36 }
37
38 public Frame process(Frame frame){
39
40 //Check if frame belongs to a collection and then obtain a
41 //collection object.
42 Collection collection = Collection.findCollection(frame);
43
44 if(collection == null){
45 MessageBay.errorMessage("Error: No collection could be obtained for" + frame.getFramesetName() +
46 ". GSDL exporter is now stopping.");
47 return null;
48 }
49
50 _collection = collection.getName();
51
52
53 //Check if GSDL is set.
54 if(Main.GSDL_HOME != null){
55 COLLECTIONS_HOME = Main.GSDL_HOME + File.separator + "sites" + File.separator + "localsite" + File.separator + "collect";
56 _collectionPath = COLLECTIONS_HOME + File.separator + _collection;
57 }else{
58 MessageBay.errorMessage("ERROR: No Greenstone environment variable set. Now stopping");
59 return null;
60 }
61
62 File collectionFolder = new File(COLLECTIONS_HOME + File.separator + _collection);
63
64 if(collectionFolder.isDirectory())
65 rebuildCollection(collection);
66
67 MessageBay.displayMessage("Success: Collection has been successfully edited & rebuilt", Color.green);
68
69 return null;
70 }
71
72 /**
73 * Rebuild the Greenstone collection.
74 * @param collection
75 */
76 private void rebuildCollection(Collection collection){
77 try{
78 CollectionCustomizer customizer = CollectionCustomizer.getInstance(_collectionPath);
79
80 CollectionContentEditor contentEditor = null;
81
82 //loop through all the items in the collection and read their matching frames.
83 for(int i = 0; i < collection.getItems().size(); i++){
84
85 CollectionItem currCI = collection.getItems().get(i);
86 Frame currCiFrame = currCI.getFrame();
87
88 if(currCiFrame == null)
89 continue;
90
91 contentEditor = new CollectionContentEditor(currCI,_collectionPath);
92
93 if(i == 0)
94 customizer.customize(currCI);
95
96 contentEditor.editCollectionItems();
97 }
98
99 build();
100 CollectionCustomizer.reset();
101
102 /*sleep to give time for collection to be rebuilt before
103 *proceeding to move files to index/assoc/assocfilepath directory. Otherwise
104 *if you try to move files while rebuilding is still going, they will just be removed.
105 *This is done for moving/adding extra files. Maybe it would be better to add them elsewhere
106 *i.e. an "images folder...*/
107 Thread.sleep(45000);
108
109 //move any images to index/assoc folder in GSDL once building is completed.
110 moveFiles();
111
112 _filesToMove = new ArrayList<String[]>();
113
114 }catch(Exception e){
115 e.printStackTrace();
116 return;
117 }
118 }
119
120 /**
121 * Method to move files currently not part of collection (e.g. images)
122 * to the collection's index/assoc/assocfilepath directory.
123 */
124 private static void moveFiles()
125 {
126 try{
127
128 String newPath = _collectionPath + File.separator + "index" + File.separator + "assoc" + File.separator;
129
130 for(int i =0; i < _filesToMove.size(); i++){
131 String f = _filesToMove.get(i)[0];
132 String a = _filesToMove.get(i)[1];
133
134 newPath += a + File.separator;
135
136 File origFile = new File(f);
137 if(origFile.exists()){
138 FileInputStream fis = new FileInputStream(origFile);
139 newPath += origFile.getName();
140
141 FileOutputStream fos = new FileOutputStream(newPath);
142
143 byte[] buf = new byte[fis.available()];
144 fis.read(buf);
145 fos.write(buf);
146 }
147
148 newPath = _collectionPath + File.separator + "index" + File.separator + "assoc" + File.separator;
149 }
150 }catch(Exception e){
151 e.printStackTrace();
152 }
153 }
154
155
156 /**
157 * Run batch script to rebuild collection.
158 */
159 private void build(){
160
161 try{
162 //"cmd /c start .../build.bat"
163 String command = "cmd /c start collectionBuilder.bat";
164 createBatchFile();
165 Runtime.getRuntime().exec(command);
166 }
167 catch(Exception e){
168 MessageBay.errorMessage("ERROR: Failed to build collection.");
169 e.printStackTrace();
170 return;
171 }
172 }
173
174 /**
175 * Method to create batch file which runs when
176 * we want to export a frameset to Greenstone and
177 * rebuild the matching collection.
178 */
179 private void createBatchFile(){
180
181 StringBuffer cmds = new StringBuffer("");
182 cmds.append("@echo off\n");
183
184 //TODO: Change this so it uses a relative path to GSDL.
185 String chdir = "greenstone3-svn";
186 cmds.append("chdir \"" + ((chdir != null) ? chdir : "") + "\"\n");
187 cmds.append("call \"./gs3-setup.bat\"\n");
188
189 cmds.append("perl -S buildcol.pl -verbosity 5 -site localsite " + _collection + "\n");
190 //cmds.append("perl -S incremental-rebuild.pl -verbosity 5 -site localsite " + _collection + "\n");
191 cmds.append("perl -S activate.pl -site localsite " + _collection + "\n");
192 cmds.append("pause\n");
193 cmds.append("exit\n");
194
195 try{
196 FileWriter fwriter = new FileWriter("collectionBuilder.bat");
197 BufferedWriter out = new BufferedWriter(fwriter);
198 out.write(cmds.toString());
199 out.close();
200 }catch(Exception e){
201 e.printStackTrace();
202 return;
203 }
204 }
205
206}
Note: See TracBrowser for help on using the repository browser.