source: main/trunk/gli/src/org/greenstone/gatherer/cdm/SearchIndexManager.java@ 36158

Last change on this file since 36158 was 36158, checked in by kjdon, 2 years ago

this now inherits from BaseIndexManager

  • Property svn:keywords set to Author Date Id Revision
File size: 20.9 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: John Thompson, Greenstone Digital Library, University of Waikato
9 *
10 * Copyright (C) 1999 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.cdm;
28
29import java.awt.*;
30import java.awt.event.*;
31import java.util.*;
32import javax.swing.*;
33import javax.swing.event.*;
34import org.greenstone.gatherer.Configuration;
35import org.greenstone.gatherer.DebugStream;
36import org.greenstone.gatherer.Dictionary;
37import org.greenstone.gatherer.Gatherer;
38import org.greenstone.gatherer.gui.DesignPaneHeader;
39import org.greenstone.gatherer.gui.GComboBox;
40import org.greenstone.gatherer.gui.GLIButton;
41import org.greenstone.gatherer.gui.ModalDialog;
42import org.greenstone.gatherer.gui.SimpleMenuBar;
43import org.greenstone.gatherer.metadata.MetadataElement;
44import org.greenstone.gatherer.metadata.MetadataSetManager;
45import org.greenstone.gatherer.util.CheckList;
46import org.greenstone.gatherer.util.JarTools;
47import org.greenstone.gatherer.util.XMLTools;
48import org.greenstone.gatherer.util.StaticStrings;
49import org.w3c.dom.*;
50/** This class is resposible for storing the indexes which have been assigned to this collection and the default index, and providing methods for interacting with both these data pools. It also knows how to turn itself into a String as it would be displayed in the collection configuration file.
51 * @author John Thompson, Greenstone Digital Library, University of Waikato
52 * @version 2.3
53 */
54public class SearchIndexManager extends BaseIndexManager {
55
56 static final private Dimension FIELD_SIZE = new Dimension(200,30);
57 static final private String ALLFIELDS = "allfields";
58
59 public SearchIndexManager(Element indexes, String current_build_type) {
60
61 super(indexes, current_build_type, StaticStrings.INDEX_ELEMENT, StaticStrings.INDEX_DEFAULT_ELEMENT, (current_build_type.equals(BuildTypeManager.BUILD_TYPE_MG)?new MGIndex():new Index()));
62
63 this.controls_title_key = "CDM.IndexManager.Indexes";
64 this.new_button_tooltip_key = "CDM.IndexManager.New_Button_Tooltip";
65 this.edit_button_tooltip_key = "CDM.IndexManager.Edit_Button_Tooltip";
66 this.remove_button_tooltip_key = "CDM.IndexManager.Remove_Button_Tooltip";
67 this.nip_new_index_key = "CDM.IndexManager.New_Index";
68 this.nip_edit_index_key = "CDM.IndexManager.Edit_Index";
69 this.nip_source_label_key = "CDM.IndexManager.Source";
70 this.nip_add_index_button_key = "CDM.IndexManager.Add_Index";
71 this.nip_add_index_tooltip_key = "CDM.IndexManager.Add_Index_Tooltip";
72 this.nip_replace_index_button_key = "CDM.IndexManager.Replace_Index";
73 this.nip_replace_index_tooltip_key = "CDM.IndexManager.Replace_Index_Tooltip";
74
75 }
76
77
78 public void buildTypeChanged(String new_build_type) {
79 if (build_type.equals(new_build_type)) {
80 return;
81 }
82 // we don;t care about this if old or new is not MG as MGPP and
83 // Lucene have the same index specification
84 if (!build_type.equals(BuildTypeManager.BUILD_TYPE_MG) && !new_build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
85 return;
86 }
87 boolean mg_to_mgpp = true;
88 if (new_build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
89 mg_to_mgpp = false;
90 }
91 if (mg_to_mgpp) {
92 changeToMGPPIndexes();
93 } else {
94 changeToMGIndexes();
95 }
96 build_type = new_build_type;
97 // its really hard to transfer defaults between mgpp/lucene and mg indexes, so we'll just set the first one to be the default
98 Index first_index = (Index) getElementAt(0);
99 setDefault(first_index);
100 first_index = null;
101 }
102
103 private void changeToMGIndexes() {
104 this.setClassType(new MGIndex());
105 Element mgpp_element = root;
106 // Retrieve and assign MG element and default index element
107 Element mg_element = CollectionDesignManager.collect_config.getMGIndexes();
108 mg_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
109 NodeList indexes = mg_element.getElementsByTagName(StaticStrings.INDEX_ELEMENT);
110 // Replace mgpp element with mg element
111 setRoot(mg_element);
112
113 if(indexes.getLength() == 0) {
114 // we need to create some based on the mgpp indexes
115
116 // If the current mgpp index includes a text one, then generate text indexes for each of the registered levels.
117 Index index = getIndex(StaticStrings.TEXT_STR);
118 if(index != null) {
119 ArrayList levels = CollectionDesignManager.index_manager.getLevels();
120 int level_size = levels.size();
121 for(int i = 0; i < level_size; i++) {
122 IndexOption level = (IndexOption) levels.get(i);
123 Index new_index = new MGIndex(level.getName(), index.getSources());
124 // Try to retrieve existing metadatum
125 String source_str = new_index.getID();
126 CollectionMeta metadatum = CollectionDesignManager.collectionmeta_manager.getMetadatum(StaticStrings.STOP_CHARACTER + source_str, false);
127 // If no metadata was found, add new pseudo metadata using the id
128 if(metadatum == null) {
129 metadatum = new CollectionMeta(StaticStrings.STOP_CHARACTER + source_str);
130 metadatum.setAssigned(true);
131 metadatum.setValue(source_str);
132 }
133 // If it was found, ensure it is assigned
134 else {
135 metadatum.setAssigned(true);
136 }
137 source_str = null;
138 addIndex(new_index, metadatum);
139 new_index = null;
140 level = null;
141 }
142 }
143 }
144
145 // Unassign mgpp element
146 mgpp_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
147 mgpp_element = null;
148
149 }
150
151 private void changeToMGPPIndexes() {
152 this.setClassType(new Index());
153 Element mg_element = root;
154 // Retrieve and assign the MGPP indexes element.
155 Element mgpp_element = CollectionDesignManager.collect_config.getMGPPIndexes();
156 mgpp_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
157 NodeList indexes = mgpp_element.getElementsByTagName(StaticStrings.INDEX_ELEMENT);
158 if(indexes.getLength() != 0) {
159 // we just reinstate the indexes we had before the change
160 setRoot(mgpp_element);
161 } else {
162 // If the MGPP indexes element is empty (ie was created by CollectionConfiguration), generate new MGPP index from the existing index
163
164 ArrayList sources_list = new ArrayList();
165
166 // We first use details from the default index if any
167 if(default_index != null) {
168 ArrayList sources = default_index.getSources();
169 sources_list.addAll(sources);
170 }
171 int size = getSize();
172 for(int i = 0; i < size; i++) {
173 Index index = (Index) getElementAt(i);
174 ArrayList sources = index.getSources();
175 sources.removeAll(sources_list);
176 sources_list.addAll(sources);
177 index = null;
178 }
179 // Replace mg element with mgpp element
180 setRoot(mgpp_element);
181
182 // We now have a list of sources, so create new indexes based on these
183 int sources_list_size = sources_list.size();
184 for(int j = 0; j < sources_list_size; j++) {
185 Object source_object = sources_list.get(j);
186 String source_str = null;
187 if(source_object instanceof MetadataElement) {
188 source_str = ((MetadataElement) source_object).getFullName();
189 }
190 else {
191 source_str = source_object.toString();
192 }
193 ArrayList new_sources = new ArrayList();
194 new_sources.add(source_object);
195 source_object = null;
196 Index new_index = new Index(new_sources);
197 // Try to retrieve existing metadatum
198 source_str = new_index.getID();
199 CollectionMeta metadatum = CollectionDesignManager.collectionmeta_manager.getMetadatum(StaticStrings.STOP_CHARACTER + source_str, false);
200 // If no metadata was found, add new pseudo metadata using the id
201 if(metadatum == null) {
202 metadatum = new CollectionMeta(StaticStrings.STOP_CHARACTER + source_str);
203 metadatum.setAssigned(true);
204 metadatum.setValue(source_str);
205 }
206 // If it was found, ensure it is assigned
207 else {
208 metadatum.setAssigned(true);
209 }
210 source_str = null;
211 addIndex(new_index, metadatum);
212 metadatum = null;
213 new_index = null;
214 new_sources = null;
215 source_str = null;
216 }
217
218 }
219
220 // Unassign MG element
221 mg_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
222 mg_element = null;
223
224 }
225
226
227
228 public Control getControls() {
229 if (controls == null) {
230 controls = new SearchIndexControl();
231 }
232 return controls;
233 }
234
235
236
237
238 /** Method to set the default index.
239 * @param index the new default Index
240 * @see org.greenstone.gatherer.Gatherer
241 * @see org.greenstone.gatherer.collection.CollectionManager
242 */
243 public void setDefault(Index index) {
244 if(index != null) {
245 if(default_index == null) {
246 // Create the default index element, and place immediately after indexes element.
247 Element default_index_element = root.getOwnerDocument().createElement(StaticStrings.INDEX_DEFAULT_ELEMENT);
248 if (build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
249 default_index = new MGIndex(default_index_element);
250 } else {
251 default_index = new Index(default_index_element);
252 }
253 Node target_node = CollectionConfiguration.findInsertionPoint(default_index_element);
254 if(target_node != null) {
255 root.getOwnerDocument().getDocumentElement().insertBefore(default_index_element, target_node);
256 }
257 else {
258 root.getOwnerDocument().getDocumentElement().appendChild(default_index_element);
259 }
260 } else {
261 if (build_type.equals(BuildTypeManager.BUILD_TYPE_MG) &&! (default_index instanceof org.greenstone.gatherer.cdm.MGIndex)) {
262 default_index = new MGIndex(default_index.getElement());
263 } else if (!build_type.equals(BuildTypeManager.BUILD_TYPE_MG) && default_index instanceof org.greenstone.gatherer.cdm.MGIndex) {
264 default_index = new Index(default_index.getElement());
265 }
266 }
267 default_index.setAssigned(true);
268 default_index.setSources(index.getSources());
269 if (build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
270 ((MGIndex)default_index).setLevel(((MGIndex)index).getLevel());
271 }
272
273 }
274 else {
275 if(default_index != null) {
276 default_index.setAssigned(false);
277 }
278 }
279 }
280
281
282 private class SearchIndexControl
283 extends IndexControl {
284
285 public SearchIndexControl() {
286 super();
287 }
288
289
290 /** we want our own custom new index prompt for searhc indexes */
291 protected NewIndexPrompt createNewIndexPrompt(String build_type, Index index) {
292 return new NewSearchIndexPrompt(build_type, index);
293
294 }
295
296 /** we customise this to add text box, allfields, and add all, select all buttons etc */
297 protected class NewSearchIndexPrompt
298 extends NewIndexPrompt {
299
300 private JCheckBox text_checkbox;
301 // mg uses a level box
302 private JComboBox level_combobox;
303 // mgpp has a allfields selector
304 private JCheckBox allfields_box;
305
306 private JButton select_all_button;
307 private JButton select_none_button;
308 private JButton add_all_button;
309
310 private boolean mgpp_enabled = false;
311 private boolean editing = false;
312
313 public NewSearchIndexPrompt(String build_type, Index existing_index) {
314 super(build_type, existing_index);
315 }
316
317 /** inside here is where we customise our controls */
318 protected void generateContents(String build_type, Index existing_index) {
319 if (build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
320 mgpp_enabled = false;
321 } else {
322 mgpp_enabled = true;
323 }
324 super.generateContents(build_type, existing_index);
325 text_checkbox = new JCheckBox(Dictionary.get("CDM.IndexManager.Text_Source"));
326 text_checkbox.setToolTipText(Dictionary.get("CDM.IndexManager.Text_Source_Tooltip"));
327 text_checkbox.setComponentOrientation(Dictionary.getOrientation());
328 text_checkbox.addActionListener(new ActionListener() {
329 public void actionPerformed(ActionEvent event) {
330 validateAddOrReplaceButton();
331 }
332 });
333 if (existing_index == null && mgpp_enabled) {
334 button_pane.setLayout(new GridLayout(2,3,5,0));
335 JPanel tmp = new JPanel();
336 tmp.setComponentOrientation(Dictionary.getOrientation());
337 button_pane.add(tmp);
338 } else {
339 button_pane.setLayout(new GridLayout(2,2,5,0));
340 }
341
342 select_all_button = new GLIButton(Dictionary.get("CDM.IndexManager.Select_All"), Dictionary.get("CDM.IndexManager.Select_All_Tooltip"));
343 select_all_button.addActionListener(new ActionListener() {
344
345 public void actionPerformed(ActionEvent event) {
346 text_checkbox.setSelected(true);
347 source_list.setAllTicked();
348 validateAddOrReplaceButton();
349 }
350 });
351
352 select_none_button = new GLIButton(Dictionary.get("CDM.IndexManager.Select_None"), Dictionary.get("CDM.IndexManager.Select_None_Tooltip"));
353 select_none_button.addActionListener(new ActionListener() {
354
355 public void actionPerformed(ActionEvent event) {
356 text_checkbox.setSelected(false);
357 source_list.clearTicked();
358 validateAddOrReplaceButton();
359 }
360 });
361
362
363 button_pane.add(select_all_button);
364 button_pane.add(select_none_button);
365
366 if (existing_index == null && mgpp_enabled) {
367 add_all_button = new GLIButton(Dictionary.get("CDM.IndexManager.Add_All"), Dictionary.get("CDM.IndexManager.Add_All_Tooltip"));
368 add_all_button.setEnabled(true);
369 add_all_button.addActionListener(new AddAllIndexActionListener());
370 button_pane.add(add_all_button);
371 }
372 details_pane.add(text_checkbox, BorderLayout.NORTH);
373 // do type specific stuff
374 if (mgpp_enabled) {
375 // allfields
376 allfields_box = new JCheckBox(Dictionary.get("CDM.IndexManager.Allfields_Index"));
377 allfields_box.addItemListener(new AllFieldsBoxListener());
378 allfields_box.setComponentOrientation(Dictionary.getOrientation());
379 //JLabel allfields_label = new JLabel(Dictionary.get("CDM.IndexManager.Allfields_Index"));
380 details_pane.add(allfields_box, BorderLayout.SOUTH);
381
382
383 } else {
384 // index level
385 JLabel level_label = new JLabel(Dictionary.get("CDM.IndexManager.Level"));
386 level_label.setComponentOrientation(Dictionary.getOrientation());
387
388 level_combobox = new JComboBox();
389 level_combobox.setOpaque(false);
390 level_combobox.setPreferredSize(FIELD_SIZE);
391 // Note the order of these must be the same as the
392 // level order in Index
393 level_combobox.addItem(StaticStrings.DOCUMENT_STR);//Dictionary.get("CDM.LevelManager.Document"));
394 level_combobox.addItem(StaticStrings.SECTION_STR);//Dictionary.get("CDM.LevelManager.Section"));
395 level_combobox.addItem(StaticStrings.PARAGRAPH_STR);//Dictionary.get("CDM.LevelManager.Paragraph"));
396 level_combobox.setEditable(false);
397 level_combobox.setToolTipText(Dictionary.get("CDM.IndexManager.Level_Tooltip"));
398 level_combobox.setComponentOrientation(Dictionary.getOrientation());
399 level_combobox.addActionListener(new ActionListener() {
400 public void actionPerformed(ActionEvent event) {
401 validateAddOrReplaceButton();
402 }
403 });
404 JPanel level_pane = new JPanel();
405 level_pane.setComponentOrientation(Dictionary.getOrientation());
406 level_pane.setLayout(new BorderLayout());
407 level_pane.add(level_label, BorderLayout.LINE_START);
408 level_pane.add(level_combobox, BorderLayout.CENTER);
409 details_pane.add(level_pane, BorderLayout.SOUTH);
410
411 }
412 // if we are editing, fill in the controls
413 if (existing_index !=null) {
414 ArrayList sources = existing_index.getSources();
415 if (mgpp_enabled && sources.get(0).equals(ALLFIELDS)) {
416 allfields_box.setSelected(true);
417 source_list.setEnabled(false);
418 } else {
419 source_list.setTickedObjects(sources.toArray());
420 source_list.setEnabled(true);
421 if (sources.contains(StaticStrings.TEXT_STR)) {
422 text_checkbox.setSelected(true);
423 }
424 }
425 if (!mgpp_enabled && existing_index instanceof MGIndex) {
426 level_combobox.setSelectedIndex(((MGIndex)existing_index).getLevel());
427 }
428
429 }
430
431 }
432 // Checks that specified index not already in the collection
433 protected void validateAddOrReplaceButton() {
434 Index index;
435 ArrayList sources;
436 if (mgpp_enabled && allfields_box.isSelected()) {
437 sources = new ArrayList();
438 sources.add(ALLFIELDS);
439 index = new Index(sources);
440
441 } else if (text_checkbox.isSelected() ||
442 !source_list.isNothingTicked()) {
443 sources = source_list.getTicked();
444 if (text_checkbox.isSelected()) {
445 sources.add(0, StaticStrings.TEXT_STR);
446 }
447 if (mgpp_enabled) {
448 index = new Index(sources);
449 } else {
450 index = new MGIndex(level_combobox.getSelectedIndex(), sources);
451 }
452 } else {
453 // nothing selected
454 add_or_replace_button.setEnabled(false);
455 return;
456 }
457
458 sources = null;
459 if (index_model.contains(index)) {
460 add_or_replace_button.setEnabled(false);
461 }
462 else {
463 add_or_replace_button.setEnabled(true);
464 }
465
466 }
467
468 protected Index generateNewIndex() {
469 Index index = null;
470 ArrayList sources;
471 if (mgpp_enabled && allfields_box.isSelected()) {
472 sources = new ArrayList();
473 sources.add(ALLFIELDS);
474 index = new Index(sources);
475 }
476 else if (text_checkbox.isSelected() || !source_list.isNothingTicked()) {
477 sources = source_list.getTicked();
478 if (text_checkbox.isSelected()) {
479 sources.add(0, StaticStrings.TEXT_STR);
480 }
481 if(mgpp_enabled) {
482 index = new Index(sources);
483 } else {
484 index = new MGIndex(level_combobox.getSelectedIndex(), sources);
485 }
486 }
487 return index;
488 }
489
490
491 /** add all sources as separate indexes (fields). */
492 private class AddAllIndexActionListener
493 implements ActionListener {
494
495 public void actionPerformed(ActionEvent event) {
496 ArrayList all_sources = source_list.getAll();
497 all_sources.add(0, StaticStrings.TEXT_STR);
498 ArrayList new_sources = new ArrayList();
499 for(int i = 0; i < all_sources.size(); i++) {
500 Object source = all_sources.get(i);
501
502 // Create new index
503 new_sources.clear();
504 new_sources.add(source);
505 Index index = new Index(new_sources);
506 if(!index_model.contains(index)) {
507 // Determine the metadatum value
508 String name = source.toString();
509 if(name.startsWith(StaticStrings.EXTRACTED_NAMESPACE) && name.indexOf(StaticStrings.NS_SEP, StaticStrings.EXTRACTED_NAMESPACE.length()) == -1) {
510 name = name.substring(StaticStrings.EXTRACTED_NAMESPACE.length());
511 }
512 // Create new metadatum
513 CollectionMeta metadatum = new CollectionMeta(StaticStrings.STOP_CHARACTER + index.getID());
514 metadatum.setValue(name);
515 name = null;
516 // Assign new index
517 addIndex(index, metadatum);
518 }
519 source = null;
520 index = null;
521 }
522 new_sources = null;
523 new_index_prompt.dispose();
524
525 }
526 }
527
528 private class AllFieldsBoxListener
529 implements ItemListener {
530
531 public void itemStateChanged(ItemEvent event) {
532 if (event.getStateChange() == ItemEvent.SELECTED) {
533 source_list.setEnabled(false);
534 text_checkbox.setEnabled(false);
535 } else if (event.getStateChange() == ItemEvent.DESELECTED) {
536 source_list.setEnabled(true);
537 text_checkbox.setEnabled(true);
538 }
539 validateAddOrReplaceButton();
540 }
541
542 }
543
544
545 } // NewSearchIndexPrompt
546 }// SearchIndexControl
547} // SearchIndexManager
548
Note: See TracBrowser for help on using the repository browser.