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 | */
|
---|
27 | package org.greenstone.gatherer.gui;
|
---|
28 |
|
---|
29 | import java.awt.*;
|
---|
30 | import java.awt.event.*;
|
---|
31 | import javax.swing.*;
|
---|
32 | import javax.swing.event.*;
|
---|
33 | import javax.swing.text.*;
|
---|
34 | import java.io.*;
|
---|
35 | import java.util.ArrayList;
|
---|
36 |
|
---|
37 | import org.w3c.dom.Document;
|
---|
38 |
|
---|
39 | import org.greenstone.gatherer.Dictionary;
|
---|
40 | import org.greenstone.gatherer.Configuration;
|
---|
41 | import org.greenstone.gatherer.Gatherer;
|
---|
42 | import org.greenstone.gatherer.cdm.Argument;
|
---|
43 | import org.greenstone.gatherer.cdm.ArgumentControl;
|
---|
44 | import org.greenstone.gatherer.cdm.CollectionDesignManager;
|
---|
45 | import org.greenstone.gatherer.cdm.Plugin;
|
---|
46 | import org.greenstone.gatherer.collection.CollectionManager;
|
---|
47 | import org.greenstone.gatherer.collection.ScriptOptions;
|
---|
48 | import org.greenstone.gatherer.collection.Collection;
|
---|
49 | import org.greenstone.gatherer.feedback.Base64;
|
---|
50 | import org.greenstone.gatherer.greenstone.LocalGreenstone;
|
---|
51 | import org.greenstone.gatherer.gui.tree.DragTree;
|
---|
52 | import org.greenstone.gatherer.metadata.MetadataXMLFileManager;
|
---|
53 | import org.greenstone.gatherer.metadata.MetadataElement;
|
---|
54 | import org.greenstone.gatherer.metadata.MetadataSet;
|
---|
55 | import org.greenstone.gatherer.metadata.MetadataSetManager;
|
---|
56 | import org.greenstone.gatherer.metadata.MetadataTools;
|
---|
57 | import org.greenstone.gatherer.remote.RemoteGreenstoneServer;
|
---|
58 | import org.greenstone.gatherer.shell.GShell;
|
---|
59 | import org.greenstone.gatherer.shell.GShellEvent;
|
---|
60 | import org.greenstone.gatherer.shell.GShellListener;
|
---|
61 | import org.greenstone.gatherer.util.Utility;
|
---|
62 | import org.greenstone.gatherer.util.XMLTools;
|
---|
63 |
|
---|
64 | public 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 (!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 | // Add in the options we preset in GLI
|
---|
266 | if (Gatherer.GS3) {
|
---|
267 | command_parts_list.add("-site");
|
---|
268 | command_parts_list.add(Configuration.site_name);
|
---|
269 | }
|
---|
270 | command_parts_list.add("-collection");
|
---|
271 | command_parts_list.add(Gatherer.c_man.getCollection ().getName ());
|
---|
272 | command_parts_list.add("-use_collection_plugin_options");
|
---|
273 | // Add in all the options from the user
|
---|
274 | String[] explode_options = options.getValues();
|
---|
275 | for (int i = 0; i < explode_options.length; i++) {
|
---|
276 | command_parts_list.add(explode_options[i]);
|
---|
277 | }
|
---|
278 |
|
---|
279 | // Local case
|
---|
280 | if (!Gatherer.isGsdlRemote) {
|
---|
281 | // Add in the filename
|
---|
282 | command_parts_list.add(metadata_file.getPath());
|
---|
283 | }
|
---|
284 | // Remote case
|
---|
285 | else {
|
---|
286 | // Add in the filename, relative to the collection directory
|
---|
287 | String collection_name = CollectionManager.getLoadedCollectionName();
|
---|
288 | String collection_directory_path = CollectionManager.getCollectionDirectoryPath(collection_name);
|
---|
289 | String metadata_file_relative_path = Gatherer.remoteGreenstoneServer.getPathRelativeToDirectory(
|
---|
290 | metadata_file, collection_directory_path);
|
---|
291 | metadata_file_relative_path = metadata_file_relative_path.replace(File.separatorChar, '|');
|
---|
292 | command_parts_list.add("-file");
|
---|
293 | command_parts_list.add(Base64.encodeBytes(metadata_file_relative_path.getBytes()));
|
---|
294 |
|
---|
295 | // When running remotely we also need the collection name as the last argument
|
---|
296 | command_parts_list.add(collection_name);
|
---|
297 | }
|
---|
298 |
|
---|
299 | // Run the explode_metadata_database.pl command
|
---|
300 | String[] command_parts = (String[]) command_parts_list.toArray(new String[0]);
|
---|
301 | this.error_message = new StringBuffer();
|
---|
302 | GShell process = new GShell(command_parts, GShell.EXPLODE, 3, this, null, GShell.GSHELL_EXPLODE);
|
---|
303 | //process.start();
|
---|
304 | process.run();
|
---|
305 |
|
---|
306 | if (successful) {
|
---|
307 | return 0;
|
---|
308 | }
|
---|
309 | return -1;
|
---|
310 | }
|
---|
311 |
|
---|
312 | private void resultPrompt(boolean success, String message)
|
---|
313 | {
|
---|
314 | if (success) {
|
---|
315 | // !!! TO DO: Get explode_metadata_database.pl strings translated and use SimpleResultDialog below
|
---|
316 | JOptionPane.showMessageDialog(null, Dictionary.get("ExplodeMetadataPrompt.Successful_Explode", metadata_file.getName()), Dictionary.get("ExplodeMetadataPrompt.Successful_Title"), JOptionPane.INFORMATION_MESSAGE);
|
---|
317 | }
|
---|
318 | else {
|
---|
319 | String title = Dictionary.get("ExplodeMetadataPrompt.Failed_Title");
|
---|
320 | String label = Dictionary.get("ExplodeMetadataPrompt.Failed_Explode", metadata_file.getName());
|
---|
321 | SimpleResultDialog result_dialog = new SimpleResultDialog(title, label, message);
|
---|
322 | result_dialog.setVisible(true); // Blocks
|
---|
323 | result_dialog.dispose();
|
---|
324 | result_dialog = null;
|
---|
325 | }
|
---|
326 | }
|
---|
327 |
|
---|
328 | private class CancelListener
|
---|
329 | implements ActionListener {
|
---|
330 | public void actionPerformed(ActionEvent event) {
|
---|
331 | self.dispose();
|
---|
332 | }
|
---|
333 | }
|
---|
334 |
|
---|
335 | private class ExplodeListener
|
---|
336 | implements ActionListener {
|
---|
337 |
|
---|
338 | public void actionPerformed(ActionEvent event) {
|
---|
339 | Gatherer.g_man.wait(true);
|
---|
340 | // update the options
|
---|
341 | updateScriptOptions();
|
---|
342 | self.dispose();
|
---|
343 | (new ExplodeMetadataDatabaseTask()).start();
|
---|
344 | }
|
---|
345 | }
|
---|
346 |
|
---|
347 |
|
---|
348 | private class ExplodeMetadataDatabaseTask
|
---|
349 | extends Thread
|
---|
350 | {
|
---|
351 | public ExplodeMetadataDatabaseTask()
|
---|
352 | {
|
---|
353 | }
|
---|
354 |
|
---|
355 | public void run()
|
---|
356 | {
|
---|
357 | int exit_value = explodeMetadata();
|
---|
358 | if (exit_value == 0) { // success
|
---|
359 | // Clear out the old directory containing the metadata file and download the new directory
|
---|
360 | if (Gatherer.isGsdlRemote) {
|
---|
361 | Utility.delete(metadata_file.getParentFile());
|
---|
362 | Gatherer.remoteGreenstoneServer.downloadCollectionFile(
|
---|
363 | CollectionManager.getLoadedCollectionName(), metadata_file.getParentFile());
|
---|
364 | }
|
---|
365 |
|
---|
366 | // Load the new metadata.xml files
|
---|
367 | Collection collection = Gatherer.c_man.getCollection();
|
---|
368 | MetadataXMLFileManager.loadMetadataXMLFiles(metadata_file.getParentFile(),collection.toSkimFile());
|
---|
369 | Gatherer.g_man.refreshCollectionTree(DragTree.COLLECTION_CONTENTS_CHANGED);
|
---|
370 | resultPrompt(true, error_message.toString());
|
---|
371 | } else { // failure
|
---|
372 | resultPrompt(false, error_message.toString());
|
---|
373 | }
|
---|
374 | error_message = null;
|
---|
375 | Gatherer.g_man.wait(false);
|
---|
376 | }
|
---|
377 | }
|
---|
378 | }
|
---|