source: other-projects/GlamED/trunk/src/org/honours/agents/ImportCollections.java@ 26756

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

Adjusted code for import agent so that it searches the appropriate GS collection's export for a frameset stored in its own subdirectory.

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