source: trunk/gli/src/org/greenstone/gatherer/collection/SaveCollectionTask.java@ 8243

Last change on this file since 8243 was 8243, checked in by mdewsnip, 20 years ago

Removed all occurrences of classes explicitly importing other classes in the same package.

  • Property svn:keywords set to Author Date Id Revision
File size: 12.3 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * <BR><BR>
9 *
10 * Author: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 New Zealand Digital Library Project
15 *
16 * <BR><BR>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * <BR><BR>
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * <BR><BR>
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *########################################################################
36 */
37package org.greenstone.gatherer.collection;
38
39import java.io.*;
40import java.util.*;
41import javax.swing.*;
42import org.greenstone.gatherer.Configuration;
43import org.greenstone.gatherer.DebugStream;
44import org.greenstone.gatherer.Gatherer;
45import org.greenstone.gatherer.util.Utility;
46
47/** The actually saving of a collection must run in a thread other than the AWTEvent thread, or else the save progress box will never get updated. To that end this class provides a threaded save method, which saves the various parts of the collection to the appropriate disk location.
48 * @author John Thompson, Greenstone Digital Library, University of Waikato
49 * @version 2.3
50 */
51public class SaveCollectionTask
52 extends Thread {
53 private boolean close_after = false;
54 /** Should we exit the Gatherer once this save is complete. */
55 private boolean exit_after = false;
56 /** Do we run the import scripts once this save is complete. */
57 private boolean import_after = false;
58 /** The current collection. */
59 private Collection collection = null;
60 /** The filename of the collection we are saving. */
61 private String name = null;
62 static final private int CLOSE_COLLECTION = 0;
63 static final private int COLLECTION_COPIED = 1;
64 static final private int COLLECTION_SAVED = 2;
65 static final private int COLLECTION_CFG_SAVED = 3;
66 static final private int COPY_COLLECTION = 4;
67 static final private int MAKE_COLLECTION = 5;
68 static final private int METADATA_SAVED = 6;
69 static final private int METADATA_XML_SAVED = 7;
70 static final private int OPEN_COLLECTION = 8;
71 static final private int RESTORE_COLLECTION = 9;
72 /** Constructor.
73 * @param collection The <strong>Collection</strong> we are saving.
74 * @see org.greenstone.gatherer.collection.Collection
75 */
76 public SaveCollectionTask(Collection collection) {
77 this.collection = collection;
78 }
79 /** Constructor.
80 * @param exit_after <i>true</i> to cause the Gatherer to exit once save is complete, <i>false</i> otherwise.
81 */
82 public SaveCollectionTask(Collection collection, boolean close_after, boolean exit_after) {
83 this.close_after = close_after;
84 this.collection = collection;
85 this.exit_after = exit_after;
86 }
87 /** Constructor.
88 * @param name The filename of the collection we are saving as.
89 */
90 public SaveCollectionTask(Collection collection, String name) {
91 this.collection = collection;
92 this.name = name;
93 }
94 /** <p>This method, when created in a forked process by start(), performs the actions necessary to save a collection. If this is a regular save it saves the state of the current collection, ensuring all files, metadata files, and configuration files are written to disk. If, instead, this is a "Save As" action then there as several more steps involved:</p>
95 * <p>1. Perform a regular collection save on what we will later refer to as the origin collection.</p>
96 * <p>2. Call CollectionManager.makeCollection() to create the collection structure to save into (copied collection).</p>
97 * <p>3. Copy files from the origin to the copied collections (except special collection files ending with ~).</p>
98 * <p>4. Restore origin collection by undoing file copy actions and removing the ~ from the end of collection files.</p>
99 * <p>5. Close the origin collection.</p>
100 * <p>6. Open the copied collection.</p>
101 * <p>Of course all of this takes a while, and depends on several other bits of code to work properly.</p>.
102 * @see org.greenstone.gatherer.collection.Collection
103 * @see org.greenstone.gatherer.gui.GUIManager
104 * @see org.greenstone.gatherer.util.Utility
105 */
106 public void run() {
107 // Change cursor to hourglass.
108 Gatherer.g_man.wait(true);
109 // Create progress monitor box. It will display itself as necessary.
110 // WARNING: ProgressMonitors seem to be extremely dodgy, and are not recommended!
111 // ProgressMonitor spd = new ProgressMonitor(Gatherer.g_man, Dictionary.get("SaveProgressDialog.Title", collection.getName()), null, 0, 100);
112 // spd.setMillisToDecideToPopup(100);
113 // spd.setMillisToPopup(100);
114 // 0. Force all remaining metadata.xml files to load.
115 // 1. Perform a regular collection save on what we will later refer to as the origin collection.
116 ///ystem.err.println("1. Save origin.");
117 String tmp_loc = Gatherer.c_man.getCollectionFilename();
118 String args[] = new String[1];
119 args[0] = collection.getName() + ".col";
120 try {
121 // Block until all of the metadata files have been read in.
122 collection.gdm.waitUntilComplete();
123 // Write out the metadata xml files. The destroy below is meant to do this, but never does.
124 collection.gdm.save();
125 // spd.setProgress(getValue(METADATA_XML_SAVED));
126
127 File file = new File(tmp_loc);
128 // Create backup
129 if(file.exists()) {
130 File backup = new File(tmp_loc + "~");
131 backup.deleteOnExit();
132 if(!file.renameTo(backup)) {
133 DebugStream.println("Error in CollectionManager.load(): FileRenamedException");
134 }
135 }
136
137 // Carry on.
138 collection.save();
139 // spd.setProgress(getValue(COLLECTION_SAVED));
140
141 // Write out the collection configuration file.
142 Gatherer.g_man.design_pane.saveConfiguration();
143 // spd.setProgress(getValue(COLLECTION_CFG_SAVED));
144
145 // Write out the metadata files.
146 Gatherer.c_man.msm.save();
147 // spd.setProgress(getValue(METADATA_SAVED));
148
149 collection.setSaved(true);
150 }
151 catch (Exception error) {
152 DebugStream.printStackTrace(error);
153 }
154 // Now we check whether we've finished, or is this a Save As action, in which case we've got miles to go.
155 if(name != null) {
156 // 2. Call CollectionManager.makeCollection() to create the collection structure to save into (copied collection).
157 ///ystem.err.println("2. Make copy.");
158 Gatherer.c_man.createCollection(null, null, name, null, null, null);
159 // spd.setProgress(getValue(MAKE_COLLECTION));
160 // 3. Copy files from the origin to the copied collections (except special collection files ending with ~).
161 // As part of this we must rename origin.col to copy.col
162 ///ystem.err.println("3. Copy origin.");
163 ArrayList files = new ArrayList();
164 File collection_file = new File(Gatherer.c_man.getCollectionFilename());
165 File copied_dir;
166 if (Gatherer.GS3) {
167 copied_dir = new File(Utility.getCollectionDir(Configuration.gsdl3_path, Configuration.site_name, name));
168 } else {
169 copied_dir = new File(Utility.getCollectionDir(Configuration.gsdl_path, name));
170 }
171 files.add(collection_file.getParentFile());
172 while(files.size() > 0) {
173 File file = (File) files.get(0);
174 files.remove(0);
175 if(file.isDirectory()) {
176 File children[] = file.listFiles();
177 for(int i = 0; i < children.length; i++) {
178 files.add(children[i]);
179 }
180 children = null;
181 }
182 else {
183 // Rename origin.col file.
184 if(file.equals(collection_file)) {
185 File copied_file = new File(copied_dir, name + ".col");
186 copy(collection_file, copied_file);
187 copied_file = null;
188 }
189 // Exclude *~ files from certain locations and lock.tmp file.
190 else if(!file.getName().endsWith("~") && !file.getName().equals(CollectionManager.LOCK_FILE)) {
191 StringTokenizer origin = new StringTokenizer(file.getAbsolutePath(), File.separator);
192 StringTokenizer destin = new StringTokenizer(copied_dir.getAbsolutePath(), File.separator);
193 while(destin.hasMoreTokens()) {
194 origin.nextToken();
195 destin.nextToken();
196 }
197 File copied_file = new File(copied_dir.getAbsolutePath());
198 while(origin.hasMoreTokens()) {
199 copied_file = new File(copied_file, origin.nextToken());
200 }
201 copy(file, copied_file);
202 copied_file = null;
203 origin = null;
204 destin = null;
205 }
206 }
207 file = null;
208 }
209 // spd.setProgress(getValue(COPY_COLLECTION));
210 // 4. Restore origin collection by undoing file copy actions and removing the ~ from the end of collection files.
211 ///ystem.err.println("4. Restore origin.");
212 // Gatherer.c_man.undo.undoAll();
213 ///ystem.err.println(" - UndoAll complete.");
214 // Recurse collection tree restoring *~
215 files.clear(); // Should be empty anyway.
216 files.add(collection_file.getParentFile());
217 while(files.size() > 0) {
218 File file = (File) files.get(0);
219 files.remove(0);
220 if(file.isDirectory()) {
221 File children[] = file.listFiles();
222 for(int i = 0; i < children.length; i++) {
223 files.add(children[i]);
224 }
225 children = null;
226 }
227 else if(file.getName().endsWith("~")) {
228 String filename = file.getAbsolutePath();
229 File original_file = new File(filename.substring(0, filename.length() - 1));
230 ///ystem.err.println("Renaming " + filename);
231 if(!file.renameTo(original_file)) {
232 DebugStream.println("Error in SaveCollectionTask.run(): FileRenameException");
233 }
234 }
235 }
236 ///ystem.err.println(" - Restore *~ complete.");
237 // spd.setProgress(getValue(RESTORE_COLLECTION));
238 // 5. Close the origin collection.
239 ///ystem.err.println("5. Close origin.");
240 collection.setSaved(true);
241 Gatherer.c_man.closeCollection();
242 // spd.setProgress(getValue(CLOSE_COLLECTION));
243 // 6. Open the copied collection.
244 ///ystem.err.println("6. Open copy.");
245 Gatherer.c_man.loadCollection(copied_dir.getAbsolutePath() + File.separator + name + ".col");
246 // spd.setProgress(getValue(OPEN_COLLECTION));
247 copied_dir = null;
248 }
249 // spd.close();
250 // spd = null;
251 tmp_loc = null;
252 args = null;
253 // Reset undo queue.
254 // if(Gatherer.c_man.ready()) {
255 // Gatherer.c_man.undo.clear();
256 // }
257 Gatherer.g_man.wait(false);
258
259 // Now we are finished action any necessary action.
260 if(import_after) {
261 Gatherer.c_man.importCollection();
262 }
263 if(close_after) {
264 Gatherer.c_man.closeCollection();
265 }
266 ///ystem.err.println("Save Complete.");
267 if(exit_after) {
268 Gatherer.self.exit();
269 }
270 }
271
272 public void setImportAfter(boolean state) {
273 import_after = true;
274 }
275
276 private void copy(File source, File destination) {
277 ///ystem.err.println("Copy: " + source.getAbsolutePath());
278 ///ystem.err.println(" to: " + destination.getAbsolutePath());
279 try {
280 destination.getParentFile().mkdirs();
281 FileInputStream f_in = new FileInputStream(source);
282 FileOutputStream f_out = new FileOutputStream(destination);
283 byte data[] = new byte[Utility.BUFFER_SIZE];
284 int data_size = 0;
285 while((data_size = f_in.read(data, 0, Utility.BUFFER_SIZE)) != -1) {
286 f_out.write(data, 0, data_size);
287 }
288 f_in.close();
289 f_out.close();
290 }
291 catch (Exception exception) {
292 DebugStream.printStackTrace(exception);
293 }
294 }
295
296 private int getValue(int reference) {
297 double multiplier;
298 if(name == null) {
299 multiplier = 1.0;
300 }
301 else {
302 multiplier = 0.25;
303 }
304 switch(reference) {
305 // Standard Save
306 case COLLECTION_SAVED:
307 return (int)((double)70 * multiplier);
308 case COLLECTION_CFG_SAVED:
309 case METADATA_SAVED:
310 case METADATA_XML_SAVED:
311 return (int)((double)10 * multiplier);
312 // Save As
313 case MAKE_COLLECTION:
314 return 5;
315 case COPY_COLLECTION:
316 return 30;
317 case RESTORE_COLLECTION:
318 return 30;
319 case CLOSE_COLLECTION:
320 return 10;
321 case OPEN_COLLECTION:
322 return 10;
323 }
324 return 0;
325 }
326}
327
328
329
330
331
Note: See TracBrowser for help on using the repository browser.