Changeset 6086 for trunk/gli/src/org/greenstone/gatherer/collection
- Timestamp:
- 2003-12-02T17:15:47+13:00 (21 years ago)
- Location:
- trunk/gli/src/org/greenstone/gatherer/collection
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gli/src/org/greenstone/gatherer/collection/Collection.java
r6051 r6086 62 62 */ 63 63 public class Collection { 64 /** A reference to the BuildOptions. */ 65 public BuildOptions build_options; 66 /** A reference to the Collection Design Manager. */ 67 public CollectionDesignManager cdm; 68 /** A reference to the Greenstone Directory Metadata Manager. */ 69 public GDMManager gdm; 70 /** A reference to the Metadata Set Manager. */ 71 public MetadataSetManager msm; 72 /** <i>true</i> if the currently loaded collection has been saved since the last significant change, <i>false</i> otherwise. */ 73 private boolean saved = false; 74 /** The document around which this collection class is based. */ 75 private Document document; 76 /** The file the collection is in (the file may not actually exist, such in the case of a legacy collection)! */ 77 private File file; 78 /** The name of the argument element. */ 79 static final private String ARGUMENT = "Argument"; 80 static final private String BASE_COLLECTION = "base_collection"; 81 /** The name of the build element. */ 82 static final private String BUILD = "Build"; 83 /** The name of the build config element. */ 84 static final private String BUILD_CONFIG = "BuildConfig"; 85 /** The name of the collection xml template. */ 86 static final private String COLLECTION_XML_TEMPLATE = "xml/template.col"; 87 /** The name of the directory mappings element. */ 88 static final private String DIRECTORY_MAPPINGS = "DirectoryMappings"; 89 /** The name of the file attribute. */ 90 static final private String FILE = "file"; 91 /** The name of the import element. */ 92 static final private String IMPORT = "Import"; 93 /** The name of the imported attribute. */ 94 static final private String IMPORTED = "imported"; 95 /** The name of the mapping element. */ 96 static final private String MAPPING = "Mapping"; 97 /** The name of the name attribute. */ 98 static final private String NAME = "name"; 99 /** The name of the true value. */ 100 static final private String TRUE = "true"; 101 /** Constructor. */ 102 public Collection(File collection_xml) { 103 this.file = collection_xml; 104 // Try to load this collections details. 105 document = Utility.parse(collection_xml, false); 106 // If that fails load the default settings for a collection. 107 if(document == null) { 108 document = Utility.parse(COLLECTION_XML_TEMPLATE, true); 109 } 110 // Point the Configuration class at our gatherer config arguments. 111 Gatherer.config.setCollectionConfiguration(document); 112 // Finally create all of the child managers that are directly dependant on a collection 113 build_options = new BuildOptions(getBuildValues(), getImportValues()); 114 } 115 116 /** Add a special directory mapping. */ 117 public boolean addDirectoryMapping(String name, File file) { 118 boolean result = false; 119 try { 120 Element document_element = document.getDocumentElement(); 121 Element directory_mappings_element = (Element) MSMUtils.getNodeFromNamed(document_element, DIRECTORY_MAPPINGS); 122 // Ensure the name isn't already in use. 123 boolean found = false; 124 NodeList mappings = directory_mappings_element.getElementsByTagName(MAPPING); 125 for(int i = 0; !found && i < mappings.getLength(); i++) { 126 Element mapping_element = (Element) mappings.item(i); 127 if(mapping_element.getAttribute(NAME).equalsIgnoreCase(name)) { 128 found = true; 129 } 130 mapping_element = null; 131 } 132 // Otherwise add the mapping. 133 if(!found) { 134 Element mapping_element = document.createElement(MAPPING); 135 mapping_element.setAttribute(NAME, name); 136 mapping_element.setAttribute(FILE, file.toString()); 137 directory_mappings_element.appendChild(mapping_element); 138 result = true; 139 mapping_element = null; 140 } 141 mappings = null; 142 directory_mappings_element = null; 143 document_element = null; 144 saved = false; 145 } 146 catch (Exception error) { 147 Gatherer.printStackTrace(error); 148 } 149 return result; 150 } 151 152 /** Destructor. 153 * @see org.greenstone.gatherer.collection.CollectionModel */ 154 public void destroy() { 64 /** A reference to the BuildOptions. */ 65 public BuildOptions build_options; 66 /** A reference to the Collection Design Manager. */ 67 public CollectionDesignManager cdm; 68 /** A reference to the Greenstone Directory Metadata Manager. */ 69 public GDMManager gdm; 70 /** A reference to the Metadata Set Manager. */ 71 public MetadataSetManager msm; 72 /** <i>true</i> if the currently loaded collection has been saved since the last significant change, <i>false</i> otherwise. */ 73 private boolean saved = false; 74 /** The document around which this collection class is based. */ 75 private Document document; 76 /** The file the collection is in (the file may not actually exist, such in the case of a legacy collection)! */ 77 private File file; 78 /** The name of the argument element. */ 79 static final private String ARGUMENT = "Argument"; 80 static final private String BASE_COLLECTION = "base_collection"; 81 /** The name of the build element. */ 82 static final private String BUILD = "Build"; 83 /** The name of the build config element. */ 84 static final private String BUILD_CONFIG = "BuildConfig"; 85 /** The name of the collection xml template. */ 86 static final private String COLLECTION_XML_TEMPLATE = "xml/template.col"; 87 /** The name of the import element. */ 88 static final private String IMPORT = "Import"; 89 /** The name of the imported attribute. */ 90 static final private String IMPORTED = "imported"; 91 92 /** Constructor. */ 93 public Collection(File collection_xml) { 94 this.file = collection_xml; 95 // Try to load this collections details. 96 document = Utility.parse(collection_xml, false); 97 // If that fails load the default settings for a collection. 98 if(document == null) { 99 document = Utility.parse(COLLECTION_XML_TEMPLATE, true); 100 } 101 // Point the Configuration class at our gatherer config arguments. 102 Gatherer.config.setCollectionConfiguration(document); 103 // Finally create all of the child managers that are directly dependant on a collection 104 build_options = new BuildOptions(getBuildValues(), getImportValues()); 105 } 106 107 /** Destructor. 108 * @see org.greenstone.gatherer.collection.CollectionModel */ 109 public void destroy() { 155 110 cdm.destroy(); 156 111 gdm.destroy(); … … 161 116 gdm = null; 162 117 msm = null; 163 164 165 /** Determine the path to the base collection.166 167 168 public String getBaseCollection() {169 170 }171 172 173 118 } 119 120 /** Determine the path to the base collection. 121 * @return the path as a String 122 */ 123 public String getBaseCollection() { 124 return getString(BASE_COLLECTION); 125 } 126 127 /** Determine the number of documents and folders in this collection. */ 128 public int getCount() { 174 129 return getCount((TreeNode)Gatherer.c_man.getRecordSet().getRoot(), true, true); 175 176 177 178 130 } 131 132 /** Calculates the number of documents in this collection. */ 133 public int getDocumentCount() { 179 134 return getCount((TreeNode)Gatherer.c_man.getRecordSet().getRoot(), false, true); 180 } 181 182 /** Retrieve the description of this collection. 183 * @return a String 184 */ 185 public String getDescription() { 186 if(cdm == null) { 187 return StaticStrings.EMPTY_STR; 188 } 189 CollectionMeta collection_extra_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONEXTRA_STR); 190 return collection_extra_collectionmeta.getValue(CollectionMeta.TEXT); 191 } 192 193 /** Retrieve the special directory mappings associated with this collection. 194 * @return A <strong>HashMap</strong> containing mappings from names to directories. 195 */ 196 public HashMap getDirectoryMappings() { 197 HashMap special_directories = new HashMap(); 198 try { 199 Element document_element = document.getDocumentElement(); 200 Element directory_mappings_element = (Element) MSMUtils.getNodeFromNamed(document_element, DIRECTORY_MAPPINGS); 201 // Ensure the name isn't already in use. 202 boolean found = false; 203 NodeList mappings = directory_mappings_element.getElementsByTagName(MAPPING); 204 for(int i = 0; !found && i < mappings.getLength(); i++) { 205 Element mapping_element = (Element) mappings.item(i); 206 String name = mapping_element.getAttribute(NAME); 207 File file = new File(mapping_element.getAttribute(FILE)); 208 special_directories.put(name, file); 209 file = null; 210 name = null; 211 mapping_element = null; 212 } 213 mappings = null; 214 directory_mappings_element = null; 215 document_element = null; 216 } 217 catch(Exception error) { 218 Gatherer.printStackTrace(error); 219 } 220 return special_directories; 221 } 222 /** Retrieve the authors email for this collection. 223 * @return The email as a <strong>String</strong>. 224 */ 225 public String getEmail() { 226 if(cdm == null) { 227 return StaticStrings.EMPTY_STR; 228 } 229 CollectionMeta creator_collectionmeta = new CollectionMeta(CollectionDesignManager.collect_config.getCreator()); 230 return creator_collectionmeta.getValue(CollectionMeta.TEXT); 231 } 232 233 /** Retrieve the short name for this collection. 234 * @return The name as a <strong>String</strong>. 235 */ 236 public String getName() { 237 return file.getParentFile().getName(); 238 } 239 240 /** Determine if this collection has been saved since the last major change. 241 * @return <i>true</i> if it has been saved recently, <i>false</i> otherwise. 242 */ 243 public boolean getSaved() { 244 return saved; 245 } 246 247 /** Retrieve the title of this collection. 248 * @return The title as a <strong>String</strong>. 249 */ 250 public String getTitle() { 251 if(cdm == null) { 252 return StaticStrings.EMPTY_STR; 253 } 254 CollectionMeta collection_name_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR); 255 return collection_name_collectionmeta.getValue(CollectionMeta.TEXT); 256 } 257 /** Remove a previously defined special directory mapping. 258 * @param name The name of the mapping to remove as a <strong>String</strong>. 259 * @return The <strong>File</strong> of the mapping removed. 260 */ 261 public File removeDirectoryMapping(String name) { 262 File file = null; 263 try { 264 Element document_element = document.getDocumentElement(); 265 Element directory_mappings_element = (Element) MSMUtils.getNodeFromNamed(document_element, DIRECTORY_MAPPINGS); 266 // Ensure the name isn't already in use. 267 boolean found = false; 268 NodeList mappings = directory_mappings_element.getElementsByTagName(MAPPING); 269 for(int i = 0; !found && i < mappings.getLength(); i++) { 270 Element mapping_element = (Element) mappings.item(i); 271 if(mapping_element.getAttribute(NAME).equalsIgnoreCase(name)) { 272 file = new File(MSMUtils.getValue(mapping_element)); 273 directory_mappings_element.removeChild(mapping_element); 274 found = true; 275 } 276 mapping_element = null; 277 } 278 mappings = null; 279 directory_mappings_element = null; 280 document_element = null; 281 saved = false; 282 } 283 catch(Exception error) { 284 Gatherer.printStackTrace(error); 285 } 286 return file; 287 } 288 /** Save this xml document to the given file. */ 289 public void save() { 135 } 136 137 /** Retrieve the description of this collection. 138 * @return a String 139 */ 140 public String getDescription() { 141 if(cdm == null) { 142 return StaticStrings.EMPTY_STR; 143 } 144 CollectionMeta collection_extra_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONEXTRA_STR); 145 return collection_extra_collectionmeta.getValue(CollectionMeta.TEXT); 146 } 147 148 /** Retrieve the authors email for this collection. 149 * @return The email as a <strong>String</strong>. 150 */ 151 public String getEmail() { 152 if(cdm == null) { 153 return StaticStrings.EMPTY_STR; 154 } 155 CollectionMeta creator_collectionmeta = new CollectionMeta(CollectionDesignManager.collect_config.getCreator()); 156 return creator_collectionmeta.getValue(CollectionMeta.TEXT); 157 } 158 159 /** Retrieve the short name for this collection. 160 * @return The name as a <strong>String</strong>. 161 */ 162 public String getName() { 163 return file.getParentFile().getName(); 164 } 165 166 /** Determine if this collection has been saved since the last major change. 167 * @return <i>true</i> if it has been saved recently, <i>false</i> otherwise. 168 */ 169 public boolean getSaved() { 170 return saved; 171 } 172 173 /** Retrieve the title of this collection. 174 * @return The title as a <strong>String</strong>. 175 */ 176 public String getTitle() { 177 if(cdm == null) { 178 return StaticStrings.EMPTY_STR; 179 } 180 CollectionMeta collection_name_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR); 181 return collection_name_collectionmeta.getValue(CollectionMeta.TEXT); 182 } 183 184 /** Save this xml document to the given file. */ 185 public void save() { 290 186 Utility.export(document, file); 291 292 293 public void setBaseCollection(String base_collection) {294 295 }296 297 298 299 300 187 } 188 189 public void setBaseCollection(String base_collection) { 190 set(BASE_COLLECTION, base_collection); 191 } 192 193 /** Set the value of imported to the given value. 194 * @param value The new value for imported, <i>true</i> if the collection has been imported successfully, <i>false</i> otherwise. 195 */ 196 public void setImported(boolean value) { 301 197 set(IMPORTED, value); 302 198 saved = false; 303 } 304 /** Set the value of saved to the given value. 305 * @param value The new value for saved, <i>true</i> if the collection has been saved recently, <i>false</i> otherwise. 306 */ 307 public void setSaved(boolean value) { 199 } 200 201 /** Set the value of saved to the given value. 202 * @param value The new value for saved, <i>true</i> if the collection has been saved recently, <i>false</i> otherwise. 203 */ 204 public void setSaved(boolean value) { 308 205 saved = value; 309 } 310 /** Set the value of title to the given value. 311 * @param title The new <strong>String</strong> title. 312 */ 313 public void setTitle(String title) { 314 if(cdm != null) { 315 CollectionMeta collection_name_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR); 316 collection_name_collectionmeta.setValue(title); 317 } 318 } 319 /** Method called to return a textual representation of a class, which in this case is the collections title. 320 * @return A <strong>String</strong> containing the collections title. 321 */ 322 public String toString() { 323 return getTitle(); 324 } 325 /** Get the value of a collection argument. */ 326 private boolean get(String name) { 206 } 207 208 /** Set the value of title to the given value. 209 * @param title The new <strong>String</strong> title. 210 */ 211 public void setTitle(String title) { 212 if(cdm != null) { 213 CollectionMeta collection_name_collectionmeta = cdm.collectionmeta_manager.getMetadatum(StaticStrings.COLLECTIONMETADATA_COLLECTIONNAME_STR); 214 collection_name_collectionmeta.setValue(title); 215 } 216 } 217 218 /** Method called to return a textual representation of a class, which in this case is the collections title. 219 * @return A <strong>String</strong> containing the collections title. 220 */ 221 public String toString() { 222 return getTitle(); 223 } 224 225 /** Get the value of a collection argument. */ 226 private boolean get(String name) { 327 227 boolean result = false; 328 228 try { 329 330 331 332 229 Element document_element = document.getDocumentElement(); 230 NodeList arguments = document_element.getElementsByTagName(ARGUMENT); 231 boolean found = false; 232 for(int i = 0; !found && i < arguments.getLength(); i++) { 333 233 Element argument_element = (Element) arguments.item(i); 334 234 if(argument_element.getParentNode() == document_element) { 335 if(argument_element.getAttribute(NAME).equalsIgnoreCase(name)) {235 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) { 336 236 String value = MSMUtils.getValue(argument_element); 337 if(value.equalsIgnoreCase( TRUE)) {338 237 if(value.equalsIgnoreCase(StaticStrings.TRUE_STR)) { 238 result = true; 339 239 } 340 240 found = true; 341 241 value = null; 342 242 } 343 243 } 344 244 argument_element = null; 345 346 347 348 } 349 catch (Exception error) { 350 245 } 246 arguments = null; 247 document_element = null; 248 } 249 catch (Exception error) { 250 Gatherer.printStackTrace(error); 351 251 } 352 252 return result; 353 354 355 356 253 } 254 255 /** Get the value of a collection argument. */ 256 private String getString(String name) { 357 257 String result = ""; 358 258 try { 359 360 361 362 259 Element document_element = document.getDocumentElement(); 260 NodeList arguments = document_element.getElementsByTagName(ARGUMENT); 261 boolean found = false; 262 for(int i = 0; !found && i < arguments.getLength(); i++) { 363 263 Element argument_element = (Element) arguments.item(i); 364 264 if(argument_element.getParentNode() == document_element) { 365 if(argument_element.getAttribute(NAME).equalsIgnoreCase(name)) {265 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) { 366 266 result = MSMUtils.getValue(argument_element); 367 267 found = true; 368 268 } 369 269 } 370 270 argument_element = null; 371 372 373 374 } 375 catch (Exception error) { 376 271 } 272 arguments = null; 273 document_element = null; 274 } 275 catch (Exception error) { 276 Gatherer.printStackTrace(error); 377 277 } 378 278 return result; 379 380 381 382 279 } 280 281 /** Method to retrieve the current build options associated with this Collection. */ 282 private Element getBuildValues() { 383 283 Element build_values_element = null; 384 284 try { 385 386 387 388 389 390 } 391 catch (Exception error) { 392 285 Element document_element = document.getDocumentElement(); 286 Element build_config_element = (Element) MSMUtils.getNodeFromNamed(document_element, BUILD_CONFIG); 287 build_values_element = (Element) MSMUtils.getNodeFromNamed(build_config_element, BUILD); 288 build_config_element = null; 289 document_element = null; 290 } 291 catch (Exception error) { 292 Gatherer.printStackTrace(error); 393 293 } 394 294 return build_values_element; 395 396 397 398 295 } 296 297 /** Count either documents or folders, depending on the state of the given boolean. */ 298 private int getCount(TreeNode node, boolean count_folders, boolean count_files) { 399 299 int count = 0; 400 300 File file = ((FileNode)node).getFile(); 401 301 if(file.isFile() && count_files) { 402 302 count++; 403 303 } 404 304 else if(file.isDirectory() && count_folders) { 405 305 count++; 406 306 } 407 307 for(int i = 0; i < node.getChildCount(); i++) { 408 308 count = count + getCount(node.getChildAt(i), count_folders, count_files); 409 309 } 410 310 return count; 411 412 413 414 311 } 312 313 /** Method to retrieve the current import options associated with this Collection. */ 314 public Element getImportValues() { 415 315 Element import_values_element = null; 416 316 try { 417 418 419 420 421 422 } 423 catch (Exception error) { 424 317 Element document_element = document.getDocumentElement(); 318 Element build_config_element = (Element) MSMUtils.getNodeFromNamed(document_element, BUILD_CONFIG); 319 import_values_element = (Element) MSMUtils.getNodeFromNamed(build_config_element, IMPORT); 320 build_config_element = null; 321 document_element = null; 322 } 323 catch (Exception error) { 324 Gatherer.printStackTrace(error); 425 325 } 426 326 return import_values_element; 427 428 429 /** Set the value of a collection argument. */430 private void set(String name, boolean value) {431 432 }433 434 private void set(String name, String value) {435 436 Element document_element = document.getDocumentElement();437 NodeList arguments = document_element.getElementsByTagName(ARGUMENT);438 boolean found = false;439 for(int i = 0; !found && i < arguments.getLength(); i++) {327 } 328 329 /** Set the value of a collection argument. */ 330 private void set(String name, boolean value) { 331 set(name, (value ? StaticStrings.TRUE_STR : StaticStrings.FALSE_STR)); 332 } 333 334 private void set(String name, String value) { 335 try { 336 Element document_element = document.getDocumentElement(); 337 NodeList arguments = document_element.getElementsByTagName(ARGUMENT); 338 boolean found = false; 339 for(int i = 0; !found && i < arguments.getLength(); i++) { 440 340 Element argument_element = (Element) arguments.item(i); 441 341 if(argument_element.getParentNode() == document_element) { 442 if(argument_element.getAttribute(NAME).equalsIgnoreCase(name)) {443 444 445 argument_element.removeChild(argument_element.getFirstChild());446 447 448 449 450 }342 if(argument_element.getAttribute(StaticStrings.NAME_ATTRIBUTE).equalsIgnoreCase(name)) { 343 // Strip any current value nodes. 344 while(argument_element.hasChildNodes()) { 345 argument_element.removeChild(argument_element.getFirstChild()); 346 } 347 // Append new value 348 argument_element.appendChild(document.createTextNode(value)); 349 found = true; 350 } 451 351 } 452 352 argument_element = null; 453 }454 // Append it455 if(!found) {353 } 354 // Append it 355 if(!found) { 456 356 Element argument_element = document.createElement(ARGUMENT); 457 argument_element.setAttribute( NAME, name);357 argument_element.setAttribute(StaticStrings.NAME_ATTRIBUTE, name); 458 358 argument_element.appendChild(document.createTextNode(value)); 459 359 document_element.appendChild(argument_element); 460 360 argument_element = null; 461 }462 arguments = null;463 document_element = null;464 465 466 Gatherer.printStackTrace(error);467 468 }361 } 362 arguments = null; 363 document_element = null; 364 } 365 catch (Exception error) { 366 Gatherer.printStackTrace(error); 367 } 368 } 469 369 } -
trunk/gli/src/org/greenstone/gatherer/collection/CollectionManager.java
r6061 r6086 139 139 */ 140 140 public void addDirectoryMapping(String name, File file) { 141 if(ready()) { 142 // Update the information stored in the collection 143 collection.addDirectoryMapping(name, file); 144 // Now update the tree 145 Gatherer.g_man.collection_pane.refreshWorkspaceTree(WorkspaceTree.MAPPED_DIRECTORIES_CHANGED); 146 } 141 // Update the information stored in the Gatherer config 142 Gatherer.config.addDirectoryMapping(name, file); 143 // Now update the tree 144 Gatherer.g_man.collection_pane.refreshWorkspaceTree(WorkspaceTree.MAPPED_DIRECTORIES_CHANGED); 147 145 } 148 146 /** This method calls the builcol.pl scripts via a GShell so as to not lock up the processor. … … 667 665 static public FileNode[] getCollectionSpecificMappings() 668 666 { 669 if (!ready()) {670 return null;671 }672 673 667 // Return any predefined special directories 674 HashMap mappings = collection.getDirectoryMappings();668 HashMap mappings = Gatherer.config.getDirectoryMappings(); 675 669 FileNode[] mapping_nodes = new FileNode[mappings.size()]; 676 670 Iterator mappings_iterator = mappings.keySet().iterator(); … … 680 674 mapping_nodes[i] = new FileNode(mapping_file, mapping_name); 681 675 } 682 683 676 return mapping_nodes; 684 677 } … … 1136 1129 */ 1137 1130 public File removeDirectoryMapping(FileNode target) { 1138 File file = null; 1139 if(ready()) { 1140 // Remove from collection, remembering file 1141 file = collection.removeDirectoryMapping(target.toString()); 1142 // Update tree. 1143 Gatherer.g_man.collection_pane.refreshWorkspaceTree(WorkspaceTree.MAPPED_DIRECTORIES_CHANGED); 1144 } 1131 // Remove from config, remembering file 1132 File file = Gatherer.config.removeDirectoryMapping(target.toString()); 1133 // Update tree. 1134 Gatherer.g_man.collection_pane.refreshWorkspaceTree(WorkspaceTree.MAPPED_DIRECTORIES_CHANGED); 1145 1135 return file; 1146 1136 }
Note:
See TracChangeset
for help on using the changeset viewer.