1 | package org.honours.agents;
|
---|
2 |
|
---|
3 | import java.awt.Color;
|
---|
4 | import java.awt.Point;
|
---|
5 | import java.io.*;
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.List;
|
---|
8 |
|
---|
9 | import org.expeditee.actions.Misc;
|
---|
10 | import org.expeditee.agents.DefaultAgent;
|
---|
11 | import org.expeditee.gui.*;
|
---|
12 | import org.expeditee.items.Item;
|
---|
13 | import org.expeditee.items.Text;
|
---|
14 |
|
---|
15 | import org.honours.Main;
|
---|
16 | import org.honours.collection.*;
|
---|
17 | import org.honours.gui.*;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * This is an Expeditee agent to allow a user
|
---|
21 | * to import/move Expeditee framesets generated
|
---|
22 | * in Greenstone to the system's "collections" directory.
|
---|
23 | * @author Korii
|
---|
24 | */
|
---|
25 | public class ImportCollections extends DefaultAgent {
|
---|
26 |
|
---|
27 | //Starting position of displaying rows containing links to access and work with imported collections/framesets.
|
---|
28 | private static final int _startX = 75;
|
---|
29 | private static final int _startY = 100;
|
---|
30 |
|
---|
31 | //Declare gap between rows and height of each row.
|
---|
32 | private static final int _gap = 30;
|
---|
33 | private static final int _rowSize = 60;
|
---|
34 |
|
---|
35 | //Maximum rows that can be displayed on each frame.
|
---|
36 | private static final int _maxRows = 5;
|
---|
37 |
|
---|
38 | //Main frameset where user accesses imported collections/framesets from.
|
---|
39 | private static final String _collectionFramesetName = "collectionsmain";
|
---|
40 |
|
---|
41 | //GSDL collect directory link.
|
---|
42 | private static String _gsdlCollectionsDir = Main.GSDL_HOME + File.separator + "sites" +
|
---|
43 | File.separator + "localsite" + File.separator + "collect" + File.separator;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Imports all collections that can be obtained from GSDL and
|
---|
47 | * are NOT currently in Expeditee.
|
---|
48 | * NOTE: null can be passed to this method as
|
---|
49 | * we don't actually rely on a frame for this agent to be run.
|
---|
50 | */
|
---|
51 | public Frame process(Frame frame) {
|
---|
52 |
|
---|
53 | File collectDir = new File(_gsdlCollectionsDir);
|
---|
54 |
|
---|
55 | String[] children = collectDir.list();
|
---|
56 | if(children == null){
|
---|
57 | MessageBay.errorMessage("No GSDL Collections Could Be Found.");
|
---|
58 | return null;
|
---|
59 | }
|
---|
60 |
|
---|
61 | try{
|
---|
62 | importEachCollection(children);
|
---|
63 | }catch(Exception e){
|
---|
64 | e.printStackTrace();
|
---|
65 | return null;
|
---|
66 | }
|
---|
67 |
|
---|
68 | MessageBay.displayMessage("Collection Importing Complete!", Color.green);
|
---|
69 | return null;
|
---|
70 | }
|
---|
71 |
|
---|
72 | private static void importEachCollection(String[] children) throws Exception{
|
---|
73 |
|
---|
74 | List<Collection> tempCollections = new ArrayList<Collection>();
|
---|
75 | Frame getFreeFrame = null;
|
---|
76 | Point getFreePoint = null;
|
---|
77 | String link = null;
|
---|
78 |
|
---|
79 | for(int i = 0; i < children.length; i++){
|
---|
80 |
|
---|
81 | //Name of the GSDL collection
|
---|
82 | String collectionName = children[i];
|
---|
83 | String filename = _gsdlCollectionsDir + File.separator + collectionName;
|
---|
84 |
|
---|
85 | File dir = new File(filename);
|
---|
86 |
|
---|
87 | if(dir.isDirectory()){
|
---|
88 | File exports = getExportDirectory(filename);
|
---|
89 |
|
---|
90 | if(exports != null){
|
---|
91 | //Search exports folder for 0.exp, 1.exp & frame.inf
|
---|
92 | String[] filesToMove = getFiles(exports);
|
---|
93 |
|
---|
94 | if(filesToMove == null){
|
---|
95 | String errorMsg = "Failed to import collection " + collectionName;
|
---|
96 | MessageBay.errorMessage("ERROR: " + errorMsg);
|
---|
97 | throw new Exception(errorMsg);
|
---|
98 | }
|
---|
99 |
|
---|
100 | File matchingFrameset = matchingFrameset(collectionName);
|
---|
101 |
|
---|
102 | //Obtain first free frame for displaying collection row with links
|
---|
103 | Object[] freeFrameInfo = findFreeFrame();
|
---|
104 | getFreeFrame = (Frame)freeFrameInfo[0];
|
---|
105 | getFreePoint = (Point)freeFrameInfo[1];
|
---|
106 |
|
---|
107 | if(getFreeFrame == null){
|
---|
108 | int num = FrameIO.getLastNumber(_collectionFramesetName) + 1;
|
---|
109 | HonoursFrameIO.generateFrame(num,_collectionFramesetName);
|
---|
110 | getFreeFrame = FrameIO.LoadFrame(_collectionFramesetName + FrameIO.getLastNumber(_collectionFramesetName));
|
---|
111 | }
|
---|
112 |
|
---|
113 | link = collectionName + "1";
|
---|
114 |
|
---|
115 | //Import collection into Expeditee.
|
---|
116 | importIntoExpeditee(matchingFrameset,exports,collectionName,tempCollections);
|
---|
117 |
|
---|
118 | }
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | for(Collection t : tempCollections){
|
---|
123 |
|
---|
124 |
|
---|
125 | Main._collections.add(t);
|
---|
126 | }
|
---|
127 |
|
---|
128 | //Display links for each collection.
|
---|
129 | for(Collection c : Main._collections){
|
---|
130 |
|
---|
131 | for(CollectionItem cItem : c.getItems()){
|
---|
132 | System.err.println("Setting up interface for " + cItem.getFrame().getName());
|
---|
133 | Interfaces.setUpToolbarOverlay(cItem);
|
---|
134 | Interfaces.setUpToggleToolbarOverlay(cItem);
|
---|
135 | Interfaces.setUpNotesOverlay(cItem);
|
---|
136 | //Interfaces.setUpBox(cItem);
|
---|
137 | }
|
---|
138 |
|
---|
139 | String cName = c.getName();
|
---|
140 |
|
---|
141 | //Check if a row of links already exists for the current collection.
|
---|
142 | boolean linkExists = linkExists(cName);
|
---|
143 | System.err.println("Link exists: " + linkExists);
|
---|
144 |
|
---|
145 | if(!linkExists){
|
---|
146 | System.err.println("Adding row of link for collection " + cName);
|
---|
147 | displayRowsOfLinks(cName, getFreeFrame, getFreePoint, link);
|
---|
148 | }
|
---|
149 |
|
---|
150 | }
|
---|
151 |
|
---|
152 | }
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Import current collection into Expeditee from GSDL.
|
---|
156 | * @param matchingFrameset - location of an existing matching frameset.
|
---|
157 | * @param collectionName - name of the collection to import
|
---|
158 | **/
|
---|
159 | private static void importIntoExpeditee(File matchingFrameset, File exports, String collectionName, List<Collection> tempCollections){
|
---|
160 |
|
---|
161 | if(matchingFrameset == null){
|
---|
162 | MessageBay.displayMessage("Importing from GSDL: " + collectionName);
|
---|
163 |
|
---|
164 | //Create new frameset directory.
|
---|
165 | //String newPath = HonoursFrameIO.COLLECTIONS_PATH + collectionName;
|
---|
166 | String newPath = FrameIO.FRAME_PATH + collectionName;
|
---|
167 |
|
---|
168 | new File(newPath).mkdir();
|
---|
169 |
|
---|
170 | matchingFrameset = new File(newPath);
|
---|
171 |
|
---|
172 | //move frames from export to matching frameset.
|
---|
173 | moveFiles(exports,matchingFrameset);
|
---|
174 |
|
---|
175 | Collection collect = Main.obtainCollectionItems(collectionName);
|
---|
176 |
|
---|
177 | if(collect.getItems().size() == 0){
|
---|
178 | MessageBay.errorMessage("Cannot import collection " + collectionName + ". No items/assocfilepaths could be found.");
|
---|
179 | FrameIO.deleteFrameset(collectionName);
|
---|
180 | }else{
|
---|
181 | tempCollections.add(collect);
|
---|
182 | }
|
---|
183 |
|
---|
184 |
|
---|
185 | }
|
---|
186 |
|
---|
187 | }
|
---|
188 |
|
---|
189 | ////////////
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * This method displays rows of links for accessing
|
---|
193 | * collections along with options to export collection
|
---|
194 | * to GSDL, view a collection space, etc.
|
---|
195 | * @param collectionName
|
---|
196 | * @param getFreeFrame
|
---|
197 | * @param getFreePoint
|
---|
198 | * @param link
|
---|
199 | */
|
---|
200 | private static void displayRowsOfLinks(String collectionName,Frame getFreeFrame, Point getFreePoint, String link) {
|
---|
201 |
|
---|
202 | getFreeFrame.addText(getFreePoint.x, getFreePoint.y, collectionName, null,link);
|
---|
203 |
|
---|
204 | //TODO: will need to change it so it runs an action - if a collection space does NOT
|
---|
205 | //exist it will be generated, otherwise user will just simply navigate to it.
|
---|
206 | getFreeFrame.addText(getFreePoint.x+250,getFreePoint.y,"View Collection Space",null,"collection-space-"+collectionName+"1");
|
---|
207 |
|
---|
208 | getFreeFrame.addText(getFreePoint.x+500,getFreePoint.y,"Export to GSDL","CollectionToGreenstone",link);
|
---|
209 |
|
---|
210 | getFreeFrame.addText(getFreePoint.x+750,getFreePoint.y,"Complete Reimport", "OverwriteUpdate " + collectionName + "1");
|
---|
211 |
|
---|
212 | getFreeFrame.addText(getFreePoint.x+1000,getFreePoint.y,"Incremental Reimport","AddDeleteItemsOnlyUpdate " + collectionName + "1");
|
---|
213 | }
|
---|
214 |
|
---|
215 | ////////////
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * Check if a link on the collections list page/frame for the passed
|
---|
219 | * collection name exists.
|
---|
220 | * @param collectionName
|
---|
221 | * @return
|
---|
222 | */
|
---|
223 | private static boolean linkExists(String collectionName){
|
---|
224 |
|
---|
225 | //loop through each collections frame looking for link
|
---|
226 | for(int j = 1; j <= FrameIO.getLastNumber(_collectionFramesetName);j++){
|
---|
227 |
|
---|
228 | Frame curr = FrameIO.LoadFrame(_collectionFramesetName + j);
|
---|
229 | if(curr == null)
|
---|
230 | continue;
|
---|
231 |
|
---|
232 | List<Item> items = curr.getItems();
|
---|
233 | for(Item item : items){
|
---|
234 | if(item.hasLink()){
|
---|
235 | if(item.getLink().equals(collectionName + "1")){
|
---|
236 | return true;
|
---|
237 | }
|
---|
238 | }
|
---|
239 | }
|
---|
240 | }
|
---|
241 |
|
---|
242 | return false;
|
---|
243 | }
|
---|
244 |
|
---|
245 | ////////////
|
---|
246 |
|
---|
247 | /**
|
---|
248 | * Return a point & a frame
|
---|
249 | * @param collectionName
|
---|
250 | * @return
|
---|
251 | */
|
---|
252 | private static Object[] findFreeFrame(){
|
---|
253 |
|
---|
254 | for(int i = 1; i <= FrameIO.getLastNumber(_collectionFramesetName); i++){
|
---|
255 |
|
---|
256 | Frame curr = FrameIO.LoadFrame(_collectionFramesetName + i);
|
---|
257 | if(curr == null)
|
---|
258 | continue;
|
---|
259 |
|
---|
260 | int y = _startY;
|
---|
261 | for(int j = 0; j < _maxRows; j++){
|
---|
262 |
|
---|
263 | Item check = Misc.getItemAtPosition(_startX, y, curr);
|
---|
264 | if(check == null){
|
---|
265 | return new Object[]{curr,new Point(_startX,y)};
|
---|
266 | }
|
---|
267 | y += _rowSize + _gap;
|
---|
268 | }
|
---|
269 | }
|
---|
270 |
|
---|
271 | //otherwise need to go on to next frame (which doesn't exist probably).
|
---|
272 | Frame toCreate = FrameIO.LoadFrame(_collectionFramesetName + FrameIO.getLastNumber(_collectionFramesetName)+1);
|
---|
273 |
|
---|
274 | return new Object[]{toCreate,new Point(_startX,_startY)};
|
---|
275 | }
|
---|
276 |
|
---|
277 | ////////////
|
---|
278 |
|
---|
279 | private static File matchingFrameset(String collectionName){
|
---|
280 |
|
---|
281 | String[] framesets = FrameIO.getFramesetList().split("\n");
|
---|
282 |
|
---|
283 | for(int i = 0; i < framesets.length; i++){
|
---|
284 |
|
---|
285 | if(framesets[i].equals(collectionName))
|
---|
286 | //return new File(HonoursFrameIO.COLLECTIONS_PATH + framesets[i]);
|
---|
287 | return new File(FrameIO.FRAME_PATH + framesets[i]);
|
---|
288 | }
|
---|
289 |
|
---|
290 | return null;
|
---|
291 | }
|
---|
292 |
|
---|
293 | ////////////
|
---|
294 |
|
---|
295 | private static String[] getFiles(File exports){
|
---|
296 |
|
---|
297 | String[] expFiles = exports.list();
|
---|
298 |
|
---|
299 | boolean hasZeroFrame = false;
|
---|
300 | boolean hasFirstFrame = false;
|
---|
301 | boolean hasFrameInf = false;
|
---|
302 |
|
---|
303 | for(int i = 0; i < expFiles.length; i++){
|
---|
304 |
|
---|
305 | String s = expFiles[i];
|
---|
306 | if(s.equals("0.exp"))
|
---|
307 | hasZeroFrame = true;
|
---|
308 |
|
---|
309 | if(s.equals("1.exp"))
|
---|
310 | hasFirstFrame = true;
|
---|
311 |
|
---|
312 | if(s.equals("frame.inf"))
|
---|
313 | hasFrameInf = true;
|
---|
314 |
|
---|
315 | if(hasZeroFrame && hasFirstFrame && hasFrameInf)
|
---|
316 | return expFiles;
|
---|
317 | }
|
---|
318 |
|
---|
319 | return null;
|
---|
320 | }
|
---|
321 |
|
---|
322 | ////////////
|
---|
323 |
|
---|
324 | private static File getExportDirectory(String filename){
|
---|
325 |
|
---|
326 | String[] newChildren = new File(filename).list();
|
---|
327 |
|
---|
328 | for(int j = 0; j < newChildren.length; j++){
|
---|
329 | if(newChildren[j].equals("export"))
|
---|
330 | return new File(filename + File.separator + "export");
|
---|
331 | }
|
---|
332 |
|
---|
333 | return null;
|
---|
334 | }
|
---|
335 |
|
---|
336 | ////////////
|
---|
337 |
|
---|
338 | /**
|
---|
339 | * Method to copy files from one directory
|
---|
340 | * to another.
|
---|
341 | * @param src
|
---|
342 | * @param dest
|
---|
343 | */
|
---|
344 | public static void moveFiles(File src, File dest){
|
---|
345 |
|
---|
346 | //get all files in source/original directory.
|
---|
347 | File[] filesToMove = src.listFiles();
|
---|
348 |
|
---|
349 | for(File f : filesToMove){
|
---|
350 | String filename = dest.getAbsolutePath() + File.separator + f.getName();
|
---|
351 |
|
---|
352 | File newFile = new File(filename);
|
---|
353 |
|
---|
354 | if(!newFile.exists()){
|
---|
355 |
|
---|
356 | try{
|
---|
357 | newFile.createNewFile();
|
---|
358 | newFile = new File(filename);
|
---|
359 |
|
---|
360 | }catch(Exception e){
|
---|
361 |
|
---|
362 | e.printStackTrace();
|
---|
363 | return;
|
---|
364 | }
|
---|
365 | }else{
|
---|
366 | try{
|
---|
367 | newFile.delete();
|
---|
368 | newFile.createNewFile();
|
---|
369 | newFile = new File(filename);
|
---|
370 | }catch(Exception e){
|
---|
371 | e.printStackTrace();
|
---|
372 | return;
|
---|
373 | }
|
---|
374 | }
|
---|
375 |
|
---|
376 | InputStream in = null;
|
---|
377 | OutputStream out = null;
|
---|
378 |
|
---|
379 | try{
|
---|
380 | in = new FileInputStream(f);
|
---|
381 | out = new FileOutputStream(newFile);
|
---|
382 |
|
---|
383 | //transfer bytes from in to out
|
---|
384 | byte[] buf = new byte[1024];
|
---|
385 | int len;
|
---|
386 | while((len = in.read(buf)) > 0){
|
---|
387 | out.write(buf,0,len);
|
---|
388 | }
|
---|
389 | in.close();
|
---|
390 | out.close();
|
---|
391 | }catch(Exception e){
|
---|
392 | e.printStackTrace();
|
---|
393 | return;
|
---|
394 | }
|
---|
395 | }
|
---|
396 | }
|
---|
397 |
|
---|
398 | }
|
---|