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

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

Base64 encoding the filename that is to be src_replaced or exploded in order to preserve any special characters in the filename when transferring the filename data to the remote greenstone server.

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