1 | package org.honours.agents;
|
---|
2 |
|
---|
3 | import java.awt.*;
|
---|
4 | import java.io.*;
|
---|
5 | import java.util.ArrayList;
|
---|
6 | import java.util.List;
|
---|
7 |
|
---|
8 | import org.expeditee.actions.Misc;
|
---|
9 | import org.expeditee.agents.DefaultAgent;
|
---|
10 | import org.expeditee.items.*;
|
---|
11 | import org.expeditee.gui.Frame;
|
---|
12 | import org.expeditee.gui.*;
|
---|
13 |
|
---|
14 | import org.honours.Main;
|
---|
15 | import org.honours.collection.*;
|
---|
16 | import org.honours.greenstone.CollectionConfigReader;
|
---|
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" + 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 | * TODO: Change this method so we only display the full name of the collection and the collection's description (the first 10 words).
|
---|
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 | File exports = getExportDirectory(filename);
|
---|
93 |
|
---|
94 | if(exports != null){
|
---|
95 | //Search exports folder for 0.exp, 1.exp & frame.inf
|
---|
96 | String[] filesToMove = getFiles(exports);
|
---|
97 |
|
---|
98 | if(filesToMove == null){
|
---|
99 | String errorMsg = "Failed to import collection " + collectionName;
|
---|
100 | MessageBay.errorMessage("ERROR: " + errorMsg);
|
---|
101 | throw new Exception(errorMsg);
|
---|
102 | }
|
---|
103 |
|
---|
104 | File matchingFrameset = matchingFrameset(collectionName);
|
---|
105 |
|
---|
106 | //Obtain first free frame for displaying collection row with links
|
---|
107 | Object[] freeFrameInfo = findFreeFrame();
|
---|
108 | getFreeFrame = (Frame)freeFrameInfo[0];
|
---|
109 | getFreePoint = (Point)freeFrameInfo[1];
|
---|
110 |
|
---|
111 | if(getFreeFrame == null){
|
---|
112 | int num = FrameIO.getLastNumber(_collectionFramesetName) + 1;
|
---|
113 | HonoursFrameIO.generateFrame(num,_collectionFramesetName);
|
---|
114 | getFreeFrame = FrameIO.LoadFrame(_collectionFramesetName + FrameIO.getLastNumber(_collectionFramesetName));
|
---|
115 | }
|
---|
116 |
|
---|
117 | link = collectionName + "1";
|
---|
118 |
|
---|
119 | //Import collection into Expeditee.
|
---|
120 | importIntoExpeditee(matchingFrameset,exports,collectionName,tempCollections);
|
---|
121 |
|
---|
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 | ////////////
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * This method displays rows of links for accessing
|
---|
195 | * collections along with options to export collection
|
---|
196 | * to GSDL, view a collection space, etc.
|
---|
197 | * @param collectionName
|
---|
198 | * @param getFreeFrame
|
---|
199 | * @param getFreePoint
|
---|
200 | * @param link
|
---|
201 | */
|
---|
202 | private static void displayRowsOfLinks(String collectionName,Frame getFreeFrame, Point getFreePoint, String link) {
|
---|
203 |
|
---|
204 | CollectionConfigReader ccr = new CollectionConfigReader(collectionName);
|
---|
205 |
|
---|
206 | String description = ccr.retrieveDescription();
|
---|
207 | String fullCollName = ccr.retrieveFullCollectionName();
|
---|
208 |
|
---|
209 | if(fullCollName != null){
|
---|
210 | getFreeFrame.addText(getFreePoint.x, getFreePoint.y, fullCollName, null, link);
|
---|
211 | }else{
|
---|
212 | getFreeFrame.addText(getFreePoint.x, getFreePoint.y, collectionName, null, link);
|
---|
213 | }
|
---|
214 |
|
---|
215 | //Display collection description retrieved from collectionConfig.xml file - only first 25 words if it's a long description.
|
---|
216 | if(description != null){
|
---|
217 | description = description.replaceAll("\\<[^>]*>","");
|
---|
218 |
|
---|
219 | String[] descriptionWords = description.split(" ");
|
---|
220 | String outputDescription = "";
|
---|
221 |
|
---|
222 | if(descriptionWords.length > 25){
|
---|
223 |
|
---|
224 | for(int i = 0; i < 25; i++){
|
---|
225 | outputDescription += descriptionWords[i] + " ";
|
---|
226 | }
|
---|
227 | outputDescription += "...";
|
---|
228 |
|
---|
229 | }else{
|
---|
230 | outputDescription = description;
|
---|
231 | }
|
---|
232 |
|
---|
233 | getFreeFrame.addText(getFreePoint.x+250, getFreePoint.y, outputDescription, null);
|
---|
234 | }
|
---|
235 | }
|
---|
236 |
|
---|
237 | ////////////
|
---|
238 |
|
---|
239 | /**
|
---|
240 | * Check if a link on the collections list page/frame for the passed
|
---|
241 | * collection name exists.
|
---|
242 | * @param collectionName
|
---|
243 | * @return
|
---|
244 | */
|
---|
245 | private static boolean linkExists(String collectionName){
|
---|
246 |
|
---|
247 | //loop through each collections frame looking for link
|
---|
248 | for(int j = 1; j <= FrameIO.getLastNumber(_collectionFramesetName);j++){
|
---|
249 |
|
---|
250 | Frame curr = FrameIO.LoadFrame(_collectionFramesetName + j);
|
---|
251 | if(curr == null)
|
---|
252 | continue;
|
---|
253 |
|
---|
254 | List<Item> items = curr.getItems();
|
---|
255 | for(Item item : items){
|
---|
256 | if(item.hasLink()){
|
---|
257 | if(item.getLink().equals(collectionName + "1")){
|
---|
258 | return true;
|
---|
259 | }
|
---|
260 | }
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|
264 | return false;
|
---|
265 | }
|
---|
266 |
|
---|
267 | ////////////
|
---|
268 |
|
---|
269 | /**
|
---|
270 | * Return a point & a frame
|
---|
271 | * @param collectionName
|
---|
272 | * @return
|
---|
273 | */
|
---|
274 | private static Object[] findFreeFrame(){
|
---|
275 |
|
---|
276 | for(int i = 1; i <= FrameIO.getLastNumber(_collectionFramesetName); i++){
|
---|
277 |
|
---|
278 | Frame curr = FrameIO.LoadFrame(_collectionFramesetName + i);
|
---|
279 | if(curr == null)
|
---|
280 | continue;
|
---|
281 |
|
---|
282 | int y = _startY;
|
---|
283 | for(int j = 0; j < _maxRows; j++){
|
---|
284 |
|
---|
285 | Item check = Misc.getItemAtPosition(_startX, y, curr);
|
---|
286 | if(check == null){
|
---|
287 | return new Object[]{curr,new Point(_startX,y)};
|
---|
288 | }
|
---|
289 | y += _rowSize + _gap;
|
---|
290 | }
|
---|
291 | }
|
---|
292 |
|
---|
293 | //otherwise need to go on to next frame (which doesn't exist probably).
|
---|
294 | Frame toCreate = FrameIO.LoadFrame(_collectionFramesetName + FrameIO.getLastNumber(_collectionFramesetName)+1);
|
---|
295 |
|
---|
296 | return new Object[]{toCreate,new Point(_startX,_startY)};
|
---|
297 | }
|
---|
298 |
|
---|
299 | ////////////
|
---|
300 |
|
---|
301 | private static File matchingFrameset(String collectionName){
|
---|
302 |
|
---|
303 | String[] framesets = FrameIO.getFramesetList().split("\n");
|
---|
304 |
|
---|
305 | for(int i = 0; i < framesets.length; i++){
|
---|
306 |
|
---|
307 | if(framesets[i].equals(collectionName))
|
---|
308 | //return new File(HonoursFrameIO.COLLECTIONS_PATH + framesets[i]);
|
---|
309 | return new File(FrameIO.FRAME_PATH + framesets[i]);
|
---|
310 | }
|
---|
311 |
|
---|
312 | return null;
|
---|
313 | }
|
---|
314 |
|
---|
315 | ////////////
|
---|
316 |
|
---|
317 | private static String[] getFiles(File exports){
|
---|
318 |
|
---|
319 | String[] expFiles = exports.list();
|
---|
320 |
|
---|
321 | boolean hasZeroFrame = false;
|
---|
322 | boolean hasFirstFrame = false;
|
---|
323 | boolean hasFrameInf = false;
|
---|
324 |
|
---|
325 | for(int i = 0; i < expFiles.length; i++){
|
---|
326 |
|
---|
327 | String s = expFiles[i];
|
---|
328 | if(s.equals("0.exp"))
|
---|
329 | hasZeroFrame = true;
|
---|
330 |
|
---|
331 | if(s.equals("1.exp"))
|
---|
332 | hasFirstFrame = true;
|
---|
333 |
|
---|
334 | if(s.equals("frame.inf"))
|
---|
335 | hasFrameInf = true;
|
---|
336 |
|
---|
337 | if(hasZeroFrame && hasFirstFrame && hasFrameInf)
|
---|
338 | return expFiles;
|
---|
339 | }
|
---|
340 |
|
---|
341 | return null;
|
---|
342 | }
|
---|
343 |
|
---|
344 | ////////////
|
---|
345 |
|
---|
346 | private static File getExportDirectory(String filename){
|
---|
347 |
|
---|
348 | String[] newChildren = new File(filename).list();
|
---|
349 |
|
---|
350 | for(int j = 0; j < newChildren.length; j++){
|
---|
351 | if(newChildren[j].equals("export"))
|
---|
352 | return new File(filename + File.separator + "export");
|
---|
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 | }
|
---|