source: gli/trunk/src/org/greenstone/gatherer/gui/ExplodeMetadataDatabasePrompt.java@ 18376

Last change on this file since 18376 was 18376, checked in by ak19, 15 years ago

Prepended the xml declaration to the Options tag, so that the XMLTools.java parser does not complain about the missing xml declaration and crash GLI.

  • Property svn:keywords set to Author Date Id Revision
File size: 14.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 * Author: Katherine Don, Greenstone Digital Library, University of Waikato
9 *
10 * Copyright (C) 2005 New Zealand Digital Library Project
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 *########################################################################
26 */
27package org.greenstone.gatherer.gui;
28
29import java.awt.*;
30import java.awt.event.*;
31import javax.swing.*;
32import javax.swing.event.*;
33import javax.swing.text.*;
34import java.io.*;
35import java.util.ArrayList;
36
37import org.w3c.dom.Document;
38
39import org.greenstone.gatherer.Dictionary;
40import org.greenstone.gatherer.Configuration;
41import org.greenstone.gatherer.Gatherer;
42import org.greenstone.gatherer.cdm.Argument;
43import org.greenstone.gatherer.cdm.ArgumentControl;
44import org.greenstone.gatherer.cdm.CollectionDesignManager;
45import org.greenstone.gatherer.cdm.Plugin;
46import org.greenstone.gatherer.collection.CollectionManager;
47import org.greenstone.gatherer.collection.ScriptOptions;
48import org.greenstone.gatherer.collection.Collection;
49import org.greenstone.gatherer.greenstone.LocalGreenstone;
50import org.greenstone.gatherer.gui.tree.DragTree;
51import org.greenstone.gatherer.metadata.MetadataXMLFileManager;
52import org.greenstone.gatherer.metadata.MetadataElement;
53import org.greenstone.gatherer.metadata.MetadataSet;
54import org.greenstone.gatherer.metadata.MetadataSetManager;
55import org.greenstone.gatherer.metadata.MetadataTools;
56import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
57import org.greenstone.gatherer.shell.GShell;
58import org.greenstone.gatherer.shell.GShellEvent;
59import org.greenstone.gatherer.shell.GShellListener;
60import org.greenstone.gatherer.util.Utility;
61import org.greenstone.gatherer.util.XMLTools;
62
63public class ExplodeMetadataDatabasePrompt
64 extends ModalDialog
65 implements GShellListener
66{
67 /** The size of this new collection dialog box. */
68 static private Dimension SIZE = new Dimension(675, 350);
69 private JDialog self;
70
71 /** the file we wil be exploding */
72 private File metadata_file = null;
73 /** the list of potential plugins to be used */
74 private Argument plugin_arg = null;
75 /** holds all the available options for the exploding script */
76 private ScriptOptions options = null;
77 /** the pane containing the options */
78 private JPanel options_pane = null;
79 /** the error message if any */
80 private StringBuffer error_message = null;
81 /** whether we were successful or not */
82 private boolean successful;
83
84
85 public ExplodeMetadataDatabasePrompt(File source_file) {
86 super(Gatherer.g_man, true);
87 this.self = this;
88 this.metadata_file = source_file;
89 this.setComponentOrientation(Dictionary.getOrientation());
90 // check that we actually have an explodable file
91 ArrayList exp_plugins = CollectionDesignManager.plugin_manager.getExploderPlugins(source_file);
92 if (exp_plugins.size() == 0) {
93 JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("ExplodeMetadataPrompt.NotExplodable"), Dictionary.get("ExplodeMetadataPrompt.Title"), JOptionPane.ERROR_MESSAGE);
94 return;
95 }
96 plugin_arg = createPluginArgument(exp_plugins);
97 setJMenuBar(new SimpleMenuBar("explodingmetadata"));
98 setSize(SIZE);
99 setTitle(Dictionary.get("ExplodeMetadataPrompt.Title"));
100
101 // set up the script options
102 // we have empty initial values
103 String dom_string = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Options/>";
104 Document doc = XMLTools.parseXML(new StringReader(dom_string));
105 options = new ScriptOptions(doc.getDocumentElement(), "explode_metadata_database.pl");
106
107 // Creation
108 JPanel content_pane = (JPanel) getContentPane();
109 content_pane.setComponentOrientation(Dictionary.getOrientation());
110 content_pane.setOpaque(true);
111
112 options_pane = new JPanel();
113 options_pane.setComponentOrientation(Dictionary.getOrientation());
114 addScriptOptions(options_pane);
115 JScrollPane middle_pane = new JScrollPane(options_pane);
116 middle_pane.setComponentOrientation(Dictionary.getOrientation());
117
118 JTextArea instructions_area = new JTextArea(Dictionary.get("ExplodeMetadataPrompt.Instructions"));
119 instructions_area.setComponentOrientation(Dictionary.getOrientation());
120 instructions_area.setEditable(false);
121 instructions_area.setLineWrap(true);
122 instructions_area.setRows(5);
123 instructions_area.setWrapStyleWord(true);
124
125 JPanel button_pane = new JPanel();
126 button_pane.setComponentOrientation(Dictionary.getOrientation());
127 JButton explode_button = new GLIButton(Dictionary.get("ExplodeMetadataPrompt.Explode"), Dictionary.get("ExplodeMetadataPrompt.Explode_Tooltip"));
128 JButton cancel_button = new GLIButton(Dictionary.get("General.Cancel"), Dictionary.get("General.Cancel_Tooltip"));
129
130 // Connection
131 cancel_button.addActionListener(new CancelListener());
132 explode_button.addActionListener(new ExplodeListener());
133
134 // Layout
135
136 button_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
137 button_pane.setLayout(new GridLayout(1,2));
138 button_pane.add(explode_button);
139 button_pane.add(cancel_button);
140
141 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
142 content_pane.setLayout(new BorderLayout());
143 content_pane.add(instructions_area, BorderLayout.NORTH);
144 content_pane.add(middle_pane, BorderLayout.CENTER);
145 content_pane.add(button_pane, BorderLayout.SOUTH);
146
147 // Final dialog setup & positioning.
148 Dimension screen_size = Configuration.screen_size;
149 setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
150 setVisible(true);
151 }
152
153 public void destroy()
154 {
155
156 }
157
158 /** All implementation of GShellListener must include this method so the listener can be informed of messages from the GShell.
159 * @param event A <strong>GShellEvent</strong> that contains, amoung other things, the message.
160 */
161 public synchronized void message(GShellEvent event) {
162 String message = event.getMessage();
163 if (message.startsWith("explode_metadata_database.pl>")) {
164 message = message.substring(29);
165 error_message.append(message);
166 error_message.append("\n");
167 }
168 }
169
170 /** All implementation of GShellListener must include this method so the listener can be informed when a GShell begins its task. Implementation side-effect, not actually used.
171 * @param event A <strong>GShellEvent</strong> that contains details of the initial state of the <strong>GShell</strong> before task comencement.
172 */
173 public synchronized void processBegun(GShellEvent event) {
174 // We don't care.
175 }
176
177 /** All implementation of GShellListener must include this method so the listener can be informed when a GShell completes its task.
178 * @param event A <strong>GShellEvent</strong> that contains details of the final state of the <strong>GShell</strong> after task completion.
179 */
180 public synchronized void processComplete(GShellEvent event) {
181 successful = false;
182 if(event.getStatus() == GShell.OK) {
183 successful = true;
184 }
185 }
186
187 private Argument createPluginArgument(ArrayList plugin_list) {
188 Argument arg = new Argument();
189 arg.setName("plugin");
190 arg.setDescription(Dictionary.get("ExplodeMetadataPrompt.Plugin"));
191 arg.setRequired(true);
192 arg.setType(Argument.ENUM);
193 for (int i=0; i<plugin_list.size(); i++) {
194 Plugin p = (Plugin)plugin_list.get(i);
195 arg.addOption(p.getName(), p.getName());
196 }
197 return arg;
198 }
199 private void addScriptOptions(JPanel options_pane) {
200
201 options_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
202 options_pane.setBackground(Configuration.getColor("coloring.collection_heading_background", false));
203 options_pane.setLayout(new BoxLayout(options_pane, BoxLayout.Y_AXIS));
204
205 int current_mode = Configuration.getMode();
206 int total_argument_count = options.getArgumentCount();
207
208 // first we add the plugin arg
209 ArgumentControl argument_control = new ArgumentControl(plugin_arg, true, null);
210 argument_control.setBackground(Configuration.getColor("coloring.collection_heading_background", false));
211 options_pane.add((JComponent)argument_control);
212 for(int i = 0; i < total_argument_count; i++) {
213 // Retrieve the argument so we know how to format the control.
214 Argument argument = options.getArgument(i);
215 if(!argument.isHiddenGLI() && argument.getModeLevel() <= current_mode) {
216 if (argument.getName().equals("metadata_set")) {
217 argument.setType(Argument.METADATA_SET_NAMESPACE);
218 argument_control = new ArgumentControl(argument, true, "Exploded Metadata Set (exp)");
219 }
220 else {
221 // by default, all args are disabled, and no value
222 argument_control = new ArgumentControl(argument, false, null);
223 }
224 // make sure they are coloured the way we want - this is not the standard arg control coloring
225 argument_control.setBackground(Configuration.getColor("coloring.collection_heading_background", false));
226 options_pane.add((JComponent)argument_control);
227 }
228 }
229 }
230
231 private void updateScriptOptions() {
232 for(int i = 0; i < options_pane.getComponentCount(); i++) {
233 Component component = options_pane.getComponent(i);
234 if(component instanceof ArgumentControl) {
235 ArgumentControl ac = (ArgumentControl)component;
236 String name = ac.getArgumentName();
237 String value = ac.getValue();
238 boolean enabled = ac.isEnabled();
239 if (!enabled && value==null) {
240 // flag type, remove from options altogether
241 options.removeValue(name);
242 } else {
243 if (value.equals("")) {
244 // if the value is empty, we don't want it to be passed to the script
245 options.setValue(name, false, value);
246 } else {
247 options.setValue(name, enabled, value);
248 }
249 }
250
251 }
252 }
253 }
254
255 private int explodeMetadata()
256 {
257 // Generate the explode_metadata_database.pl command
258 ArrayList command_parts_list = new ArrayList();
259 if (Utility.isWindows() && (!Gatherer.isGsdlRemote)) {
260 command_parts_list.add(Configuration.perl_path);
261 command_parts_list.add("-S");
262 }
263 command_parts_list.add(LocalGreenstone.getBinScriptDirectoryPath() + "explode_metadata_database.pl");
264
265 // Add in all the options from the user
266 String[] explode_options = options.getValues();
267 for (int i = 0; i < explode_options.length; i++) {
268 command_parts_list.add(explode_options[i]);
269 }
270
271 // Local case
272 if (!Gatherer.isGsdlRemote) {
273 // Add in the filename
274 command_parts_list.add(metadata_file.getPath());
275 }
276 // Remote case
277 else {
278 // Add in the filename, relative to the collection directory
279 String collection_name = CollectionManager.getLoadedCollectionName();
280 String collection_directory_path = CollectionManager.getCollectionDirectoryPath(collection_name);
281 String metadata_file_relative_path = Gatherer.remoteGreenstoneServer.getPathRelativeToDirectory(
282 metadata_file, collection_directory_path);
283 command_parts_list.add("-file");
284 command_parts_list.add(metadata_file_relative_path);
285
286 // When running remotely we also need the collection name as the last argument
287 command_parts_list.add(collection_name);
288 }
289
290 // Run the explode_metadata_database.pl command
291 String[] command_parts = (String[]) command_parts_list.toArray(new String[0]);
292 this.error_message = new StringBuffer();
293 GShell process = new GShell(command_parts, GShell.EXPLODE, 3, this, null, GShell.GSHELL_EXPLODE);
294 //process.start();
295 process.run();
296
297 if (successful) {
298 return 0;
299 }
300 return -1;
301 }
302
303 private void resultPrompt(boolean success, String message)
304 {
305 if (success) {
306 // !!! TO DO: Get explode_metadata_database.pl strings translated and use SimpleResultDialog below
307 JOptionPane.showMessageDialog(null, Dictionary.get("ExplodeMetadataPrompt.Successful_Explode", metadata_file.getName()), Dictionary.get("ExplodeMetadataPrompt.Successful_Title"), JOptionPane.INFORMATION_MESSAGE);
308 }
309 else {
310 String title = Dictionary.get("ExplodeMetadataPrompt.Failed_Title");
311 String label = Dictionary.get("ExplodeMetadataPrompt.Failed_Explode", metadata_file.getName());
312 SimpleResultDialog result_dialog = new SimpleResultDialog(title, label, message);
313 result_dialog.setVisible(true); // Blocks
314 result_dialog.dispose();
315 result_dialog = null;
316 }
317 }
318
319 private class CancelListener
320 implements ActionListener {
321 public void actionPerformed(ActionEvent event) {
322 self.dispose();
323 }
324 }
325
326 private class ExplodeListener
327 implements ActionListener {
328
329 public void actionPerformed(ActionEvent event) {
330 Gatherer.g_man.wait(true);
331 // update the options
332 updateScriptOptions();
333 self.dispose();
334 (new ExplodeMetadataDatabaseTask()).start();
335 }
336 }
337
338
339 private class ExplodeMetadataDatabaseTask
340 extends Thread
341 {
342 public ExplodeMetadataDatabaseTask()
343 {
344 }
345
346 public void run()
347 {
348 int exit_value = explodeMetadata();
349 if (exit_value == 0) { // success
350 // Clear out the old directory containing the metadata file and download the new directory
351 if (Gatherer.isGsdlRemote) {
352 Utility.delete(metadata_file.getParentFile());
353 Gatherer.remoteGreenstoneServer.downloadCollectionFile(
354 CollectionManager.getLoadedCollectionName(), metadata_file.getParentFile());
355 }
356
357 // Load the new metadata.xml files
358 Collection collection = Gatherer.c_man.getCollection();
359 MetadataXMLFileManager.loadMetadataXMLFiles(metadata_file.getParentFile(),collection.toSkimFile());
360 Gatherer.g_man.refreshCollectionTree(DragTree.COLLECTION_CONTENTS_CHANGED);
361 resultPrompt(true, error_message.toString());
362 } else { // failure
363 resultPrompt(false, error_message.toString());
364 }
365 error_message = null;
366 Gatherer.g_man.wait(false);
367 }
368 }
369}
Note: See TracBrowser for help on using the repository browser.