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" + 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 | private static void importEachCollection(String[] children) throws Exception{
|
---|
72 |
|
---|
73 | List<Collection> tempCollections = new ArrayList<Collection>();
|
---|
74 | Frame getFreeFrame = null;
|
---|
75 | Point getFreePoint = null;
|
---|
76 | String link = null;
|
---|
77 |
|
---|
78 | for(int i = 0; i < children.length; i++){
|
---|
79 |
|
---|
80 | //Name of the GSDL collection
|
---|
81 | String collectionName = children[i];
|
---|
82 | String filename = _gsdlCollectionsDir + File.separator + collectionName;
|
---|
83 |
|
---|
84 | File dir = new File(filename);
|
---|
85 |
|
---|
86 | if(dir.isDirectory()){
|
---|
87 | File exports = getExportDirectory(filename);
|
---|
88 |
|
---|
89 | if(exports != null){
|
---|
90 | //Search exports folder for 0.exp, 1.exp & frame.inf
|
---|
91 | String[] filesToMove = getFiles(exports);
|
---|
92 |
|
---|
93 | if(filesToMove == null){
|
---|
94 | String errorMsg = "Failed to import collection " + collectionName;
|
---|
95 | MessageBay.errorMessage("ERROR: " + errorMsg);
|
---|
96 | throw new Exception(errorMsg);
|
---|
97 | }
|
---|
98 |
|
---|
99 | File matchingFrameset = matchingFrameset(collectionName);
|
---|
100 |
|
---|
101 | //Obtain first free frame for displaying collection row with links
|
---|
102 | Object[] freeFrameInfo = findFreeFrame();
|
---|
103 | getFreeFrame = (Frame)freeFrameInfo[0];
|
---|
104 | getFreePoint = (Point)freeFrameInfo[1];
|
---|
105 |
|
---|
106 | if(getFreeFrame == null){
|
---|
107 | int num = FrameIO.getLastNumber(_collectionFramesetName) + 1;
|
---|
108 | HonoursFrameIO.generateFrame(num,_collectionFramesetName);
|
---|
109 | getFreeFrame = FrameIO.LoadFrame(_collectionFramesetName + FrameIO.getLastNumber(_collectionFramesetName));
|
---|
110 | }
|
---|
111 |
|
---|
112 | link = collectionName + "1";
|
---|
113 |
|
---|
114 | //Import collection into Expeditee.
|
---|
115 | importIntoExpeditee(matchingFrameset,exports,collectionName,tempCollections);
|
---|
116 |
|
---|
117 | }
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | for(Collection t : tempCollections){
|
---|
122 |
|
---|
123 |
|
---|
124 | Main._collections.add(t);
|
---|
125 | }
|
---|
126 |
|
---|
127 | //Display links for each collection.
|
---|
128 | for(Collection c : Main._collections){
|
---|
129 |
|
---|
130 | for(CollectionItem cItem : c.getItems()){
|
---|
131 | System.err.println("Setting up interface for " + cItem.getFrame().getName());
|
---|
132 | Interfaces.setUpToolbarOverlay(cItem);
|
---|
133 | Interfaces.setUpToggleToolbarOverlay(cItem);
|
---|
134 | Interfaces.setUpNotesOverlay(cItem);
|
---|
135 | //Interfaces.setUpBox(cItem);
|
---|
136 | }
|
---|
137 |
|
---|
138 | String cName = c.getName();
|
---|
139 |
|
---|
140 | //Check if a row of links already exists for the current collection.
|
---|
141 | boolean linkExists = linkExists(cName);
|
---|
142 | System.err.println("Link exists: " + linkExists);
|
---|
143 |
|
---|
144 | if(!linkExists){
|
---|
145 | System.err.println("Adding row of link for collection " + cName);
|
---|
146 | displayRowsOfLinks(cName, getFreeFrame, getFreePoint, link);
|
---|
147 | }
|
---|
148 |
|
---|
149 | }
|
---|
150 |
|
---|
151 | }
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Import current collection into Expeditee from GSDL.
|
---|
155 | * @param matchingFrameset - location of an existing matching frameset.
|
---|
156 | * @param collectionName - name of the collection to import
|
---|
157 | **/
|
---|
158 | private static void importIntoExpeditee(File matchingFrameset, File exports, String collectionName, List<Collection> tempCollections){
|
---|
159 |
|
---|
160 | if(matchingFrameset == null){
|
---|
161 | MessageBay.displayMessage("Importing from GSDL: " + collectionName);
|
---|
162 |
|
---|
163 | //Create new frameset directory.
|
---|
164 | //String newPath = HonoursFrameIO.COLLECTIONS_PATH + collectionName;
|
---|
165 | String newPath = FrameIO.FRAME_PATH + collectionName;
|
---|
166 |
|
---|
167 | new File(newPath).mkdir();
|
---|
168 |
|
---|
169 | matchingFrameset = new File(newPath);
|
---|
170 |
|
---|
171 | //move frames from export to matching frameset.
|
---|
172 | moveFiles(exports,matchingFrameset);
|
---|
173 |
|
---|
174 | Collection collect = Main.obtainCollectionItems(collectionName);
|
---|
175 |
|
---|
176 | if(collect.getItems().size() == 0){
|
---|
177 | MessageBay.errorMessage("Cannot import collection " + collectionName + ". No items/assocfilepaths could be found.");
|
---|
178 | FrameIO.deleteFrameset(collectionName);
|
---|
179 | }else{
|
---|
180 | tempCollections.add(collect);
|
---|
181 | }
|
---|
182 |
|
---|
183 |
|
---|
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 | getFreeFrame.addText(getFreePoint.x, getFreePoint.y, collectionName, null,link);
|
---|
202 |
|
---|
203 | //TODO: will need to change it so it runs an action - if a collection space does NOT
|
---|
204 | //exist it will be generated, otherwise user will just simply navigate to it.
|
---|
205 | getFreeFrame.addText(getFreePoint.x+250,getFreePoint.y,"View Collection Space",null,"collection-space-"+collectionName+"1");
|
---|
206 |
|
---|
207 | getFreeFrame.addText(getFreePoint.x+500,getFreePoint.y,"Export to GSDL","CollectionToGreenstone",link);
|
---|
208 |
|
---|
209 | getFreeFrame.addText(getFreePoint.x+750,getFreePoint.y,"Complete Reimport", "OverwriteUpdate " + collectionName + "1");
|
---|
210 |
|
---|
211 | getFreeFrame.addText(getFreePoint.x+1000,getFreePoint.y,"Incremental Reimport","AddDeleteItemsOnlyUpdate " + collectionName + "1");
|
---|
212 | }
|
---|
213 |
|
---|
214 | ////////////
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * Check if a link on the collections list page/frame for the passed
|
---|
218 | * collection name exists.
|
---|
219 | * @param collectionName
|
---|
220 | * @return
|
---|
221 | */
|
---|
222 | private static boolean linkExists(String collectionName){
|
---|
223 |
|
---|
224 | //loop through each collections frame looking for link
|
---|
225 | for(int j = 1; j <= FrameIO.getLastNumber(_collectionFramesetName);j++){
|
---|
226 |
|
---|
227 | Frame curr = FrameIO.LoadFrame(_collectionFramesetName + j);
|
---|
228 | if(curr == null)
|
---|
229 | continue;
|
---|
230 |
|
---|
231 | List<Item> items = curr.getItems();
|
---|
232 | for(Item item : items){
|
---|
233 | if(item.hasLink()){
|
---|
234 | if(item.getLink().equals(collectionName + "1")){
|
---|
235 | return true;
|
---|
236 | }
|
---|
237 | }
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | return false;
|
---|
242 | }
|
---|
243 |
|
---|
244 | ////////////
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Return a point & a frame
|
---|
248 | * @param collectionName
|
---|
249 | * @return
|
---|
250 | */
|
---|
251 | private static Object[] findFreeFrame(){
|
---|
252 |
|
---|
253 | for(int i = 1; i <= FrameIO.getLastNumber(_collectionFramesetName); i++){
|
---|
254 |
|
---|
255 | Frame curr = FrameIO.LoadFrame(_collectionFramesetName + i);
|
---|
256 | if(curr == null)
|
---|
257 | continue;
|
---|
258 |
|
---|
259 | int y = _startY;
|
---|
260 | for(int j = 0; j < _maxRows; j++){
|
---|
261 |
|
---|
262 | Item check = Misc.getItemAtPosition(_startX, y, curr);
|
---|
263 | if(check == null){
|
---|
264 | return new Object[]{curr,new Point(_startX,y)};
|
---|
265 | }
|
---|
266 | y += _rowSize + _gap;
|
---|
267 | }
|
---|
268 | }
|
---|
269 |
|
---|
270 | //otherwise need to go on to next frame (which doesn't exist probably).
|
---|
271 | Frame toCreate = FrameIO.LoadFrame(_collectionFramesetName + FrameIO.getLastNumber(_collectionFramesetName)+1);
|
---|
272 |
|
---|
273 | return new Object[]{toCreate,new Point(_startX,_startY)};
|
---|
274 | }
|
---|
275 |
|
---|
276 | ////////////
|
---|
277 |
|
---|
278 | private static File matchingFrameset(String collectionName){
|
---|
279 |
|
---|
280 | String[] framesets = FrameIO.getFramesetList().split("\n");
|
---|
281 |
|
---|
282 | for(int i = 0; i < framesets.length; i++){
|
---|
283 |
|
---|
284 | if(framesets[i].equals(collectionName))
|
---|
285 | //return new File(HonoursFrameIO.COLLECTIONS_PATH + framesets[i]);
|
---|
286 | return new File(FrameIO.FRAME_PATH + framesets[i]);
|
---|
287 | }
|
---|
288 |
|
---|
289 | return null;
|
---|
290 | }
|
---|
291 |
|
---|
292 | ////////////
|
---|
293 |
|
---|
294 | private static String[] getFiles(File exports){
|
---|
295 |
|
---|
296 | String[] expFiles = exports.list();
|
---|
297 |
|
---|
298 | boolean hasZeroFrame = false;
|
---|
299 | boolean hasFirstFrame = false;
|
---|
300 | boolean hasFrameInf = false;
|
---|
301 |
|
---|
302 | for(int i = 0; i < expFiles.length; i++){
|
---|
303 |
|
---|
304 | String s = expFiles[i];
|
---|
305 | if(s.equals("0.exp"))
|
---|
306 | hasZeroFrame = true;
|
---|
307 |
|
---|
308 | if(s.equals("1.exp"))
|
---|
309 | hasFirstFrame = true;
|
---|
310 |
|
---|
311 | if(s.equals("frame.inf"))
|
---|
312 | hasFrameInf = true;
|
---|
313 |
|
---|
314 | if(hasZeroFrame && hasFirstFrame && hasFrameInf)
|
---|
315 | return expFiles;
|
---|
316 | }
|
---|
317 |
|
---|
318 | return null;
|
---|
319 | }
|
---|
320 |
|
---|
321 | ////////////
|
---|
322 |
|
---|
323 | private static File getExportDirectory(String filename){
|
---|
324 |
|
---|
325 | String[] newChildren = new File(filename).list();
|
---|
326 |
|
---|
327 | for(int j = 0; j < newChildren.length; j++){
|
---|
328 | if(newChildren[j].equals("export"))
|
---|
329 | return new File(filename + File.separator + "export");
|
---|
330 | }
|
---|
331 |
|
---|
332 | return null;
|
---|
333 | }
|
---|
334 |
|
---|
335 | ////////////
|
---|
336 |
|
---|
337 | /**
|
---|
338 | * Method to copy files from one directory
|
---|
339 | * to another.
|
---|
340 | * @param src
|
---|
341 | * @param dest
|
---|
342 | */
|
---|
343 | public static void moveFiles(File src, File dest){
|
---|
344 |
|
---|
345 | //get all files in source/original directory.
|
---|
346 | File[] filesToMove = src.listFiles();
|
---|
347 |
|
---|
348 | for(File f : filesToMove){
|
---|
349 | String filename = dest.getAbsolutePath() + File.separator + f.getName();
|
---|
350 |
|
---|
351 | File newFile = new File(filename);
|
---|
352 |
|
---|
353 | if(!newFile.exists()){
|
---|
354 |
|
---|
355 | try{
|
---|
356 | newFile.createNewFile();
|
---|
357 | newFile = new File(filename);
|
---|
358 |
|
---|
359 | }catch(Exception e){
|
---|
360 |
|
---|
361 | e.printStackTrace();
|
---|
362 | return;
|
---|
363 | }
|
---|
364 | }else{
|
---|
365 | try{
|
---|
366 | newFile.delete();
|
---|
367 | newFile.createNewFile();
|
---|
368 | newFile = new File(filename);
|
---|
369 | }catch(Exception e){
|
---|
370 | e.printStackTrace();
|
---|
371 | return;
|
---|
372 | }
|
---|
373 | }
|
---|
374 |
|
---|
375 | InputStream in = null;
|
---|
376 | OutputStream out = null;
|
---|
377 |
|
---|
378 | try{
|
---|
379 | in = new FileInputStream(f);
|
---|
380 | out = new FileOutputStream(newFile);
|
---|
381 |
|
---|
382 | //transfer bytes from in to out
|
---|
383 | byte[] buf = new byte[1024];
|
---|
384 | int len;
|
---|
385 | while((len = in.read(buf)) > 0){
|
---|
386 | out.write(buf,0,len);
|
---|
387 | }
|
---|
388 | in.close();
|
---|
389 | out.close();
|
---|
390 | }catch(Exception e){
|
---|
391 | e.printStackTrace();
|
---|
392 | return;
|
---|
393 | }
|
---|
394 | }
|
---|
395 | }
|
---|
396 |
|
---|
397 | }
|
---|