📄 itemimport.java
字号:
{ mapOut.close(); } mapOut = null; c.abort(); e.printStackTrace(); System.out.println(e); } if (mapOut != null) { mapOut.close(); } if (isTest) { System.out.println("***End of Test Run***"); } } private void addItems(Context c, Collection[] mycollections, String sourceDir, String mapFile) throws Exception { Map skipItems = new HashMap(); // set of items to skip if in 'resume' // mode System.out.println("Adding items from directory: " + sourceDir); System.out.println("Generating mapfile: " + mapFile); // create the mapfile File outFile = null; if (!isTest) { // get the directory names of items to skip (will be in keys of // hash) if (isResume) { skipItems = readMapFile(mapFile); } // sneaky isResume == true means open file in append mode outFile = new File(mapFile); mapOut = new PrintWriter(new FileWriter(outFile, isResume)); if (mapOut == null) { throw new Exception("can't open mapfile: " + mapFile); } } // open and process the source directory File d = new java.io.File(sourceDir); if (d == null) { System.out.println("Error, cannot open source directory " + sourceDir); System.exit(1); } String[] dircontents = d.list(directoryFilter); for (int i = 0; i < dircontents.length; i++) { if (skipItems.containsKey(dircontents[i])) { System.out.println("Skipping import of " + dircontents[i]); } else { addItem(c, mycollections, sourceDir, dircontents[i], mapOut); System.out.println(i + " " + dircontents[i]); } } } private void replaceItems(Context c, Collection[] mycollections, String sourceDir, String mapFile) throws Exception { // verify the source directory File d = new java.io.File(sourceDir); if (d == null) { System.out.println("Error, cannot open source directory " + sourceDir); System.exit(1); } // read in HashMap first, to get list of handles & source dirs Map myhash = readMapFile(mapFile); // for each handle, re-import the item, discard the new handle // and re-assign the old handle Iterator i = myhash.keySet().iterator(); ArrayList itemsToDelete = new ArrayList(); while (i.hasNext()) { // get the old handle String newItemName = (String) i.next(); String oldHandle = (String) myhash.get(newItemName); Item oldItem = null; Item newItem = null; if (oldHandle.indexOf('/') != -1) { System.out.println("\tReplacing: " + oldHandle); // add new item, locate old one oldItem = (Item) HandleManager.resolveToObject(c, oldHandle); } else { oldItem = Item.find(c, Integer.parseInt(oldHandle)); } /* Rather than exposing public item methods to change handles -- * two handles can't exist at the same time due to key constraints * so would require temp handle being stored, old being copied to new and * new being copied to old, all a bit messy -- a handle file is written to * the import directory containing the old handle, the existing item is * deleted and then the import runs as though it were loading an item which * had already been assigned a handle (so a new handle is not even assigned). * As a commit does not occur until after a successful add, it is safe to * do a delete as any error results in an aborted transaction without harming * the original item */ File handleFile = new File(sourceDir + File.separatorChar + newItemName + File.separatorChar + "handle"); PrintWriter handleOut = new PrintWriter(new FileWriter(handleFile, true)); if (handleOut == null) { throw new Exception("can't open handle file: " + handleFile.getCanonicalPath()); } handleOut.println(oldHandle); handleOut.close(); deleteItem(c, oldItem); newItem = addItem(c, mycollections, sourceDir, newItemName, null); } } private void deleteItems(Context c, String mapFile) throws Exception { System.out.println("Deleting items listed in mapfile: " + mapFile); // read in the mapfile Map myhash = readMapFile(mapFile); // now delete everything that appeared in the mapFile Iterator i = myhash.keySet().iterator(); while (i.hasNext()) { String itemID = (String) myhash.get(i.next()); if (itemID.indexOf('/') != -1) { String myhandle = itemID; System.out.println("Deleting item " + myhandle); deleteItem(c, myhandle); } else { // it's an ID Item myitem = Item.find(c, Integer.parseInt(itemID)); System.out.println("Deleting item " + itemID); deleteItem(c, myitem); } } } /** * item? try and add it to the archive c mycollection path itemname handle - * non-null means we have a pre-defined handle already mapOut - mapfile * we're writing */ private Item addItem(Context c, Collection[] mycollections, String path, String itemname, PrintWriter mapOut) throws Exception { String mapOutput = null; System.out.println("Adding item from directory " + itemname); // create workspace item Item myitem = null; WorkspaceItem wi = null; if (!isTest) { wi = WorkspaceItem.create(c, mycollections[0], false); myitem = wi.getItem(); } // now fill out dublin core for item loadMetadata(c, myitem, path + File.separatorChar + itemname + File.separatorChar); // and the bitstreams from the contents file // process contents file, add bistreams and bundles processContentsFile(c, myitem, path + File.separatorChar + itemname, "contents"); if (useWorkflow) { // don't process handle file // start up a workflow if (!isTest) { WorkflowManager.startWithoutNotify(c, wi); // send ID to the mapfile mapOutput = itemname + " " + myitem.getID(); } } else { // only process handle file if not using workflow system String myhandle = processHandleFile(c, myitem, path + File.separatorChar + itemname, "handle"); // put item in system if (!isTest) { InstallItem.installItem(c, wi, myhandle); // find the handle, and output to map file myhandle = HandleManager.findHandle(c, myitem); mapOutput = itemname + " " + myhandle; } } // now add to multiple collections if requested if (mycollections.length > 1) { for (int i = 1; i < mycollections.length; i++) { if (!isTest) { mycollections[i].addItem(myitem); } } } // made it this far, everything is fine, commit transaction if (mapOut != null) { mapOut.println(mapOutput); } c.commit(); return myitem; } // remove, given the actual item private void deleteItem(Context c, Item myitem) throws Exception { if (!isTest) { Collection[] collections = myitem.getCollections(); // Remove item from all the collections it's in for (int i = 0; i < collections.length; i++) { collections[i].removeItem(myitem); } } } // remove, given a handle private void deleteItem(Context c, String myhandle) throws Exception { // bit of a hack - to remove an item, you must remove it // from all collections it's a part of, then it will be removed Item myitem = (Item) HandleManager.resolveToObject(c, myhandle); if (myitem == null) { System.out.println("Error - cannot locate item - already deleted?"); } else { deleteItem(c, myitem); } } //////////////////////////////////// // utility methods //////////////////////////////////// // read in the map file and generate a hashmap of (file,handle) pairs private Map readMapFile(String filename) throws Exception { Map myhash = new HashMap(); BufferedReader is = null; try { is = new BufferedReader(new FileReader(filename)); String line; while ((line = is.readLine()) != null) { String myfile; String myhandle; // a line should be archive filename<whitespace>handle StringTokenizer st = new StringTokenizer(line); if (st.hasMoreTokens()) { myfile = st.nextToken(); } else { throw new Exception("Bad mapfile line:\n" + line); } if (st.hasMoreTokens()) { myhandle = st.nextToken(); } else { throw new Exception("Bad mapfile line:\n" + line); } myhash.put(myfile, myhandle); } } finally { if (is != null) { is.close(); } } return myhash; } // Load all metadata schemas into the item. private void loadMetadata(Context c, Item myitem, String path) throws SQLException, IOException, ParserConfigurationException, SAXException, TransformerException { // Load the dublin core metadata loadDublinCore(c, myitem, path + "dublin_core.xml"); // Load any additional metadata schemas File folder = new File(path); File file[] = folder.listFiles(metadataFileFilter); for (int i = 0; i < file.length; i++) { loadDublinCore(c, myitem, file[i].getAbsolutePath()); } } private void loadDublinCore(Context c, Item myitem, String filename) throws SQLException, IOException, ParserConfigurationException, SAXException, TransformerException //, AuthorizeException { Document document = loadXML(filename); // Get the schema, for backward compatibility we will default to the // dublin core schema if the schema name is not available in the import // file String schema; NodeList metadata = XPathAPI.selectNodeList(document, "/dublin_core"); Node schemaAttr = metadata.item(0).getAttributes().getNamedItem( "schema"); if (schemaAttr == null) { schema = MetadataSchema.DC_SCHEMA; } else { schema = schemaAttr.getNodeValue(); } // Get the nodes corresponding to formats NodeList dcNodes = XPathAPI.selectNodeList(document, "/dublin_core/dcvalue"); System.out.println("\tLoading dublin core from " + filename); // Add each one as a new format to the registry for (int i = 0; i < dcNodes.getLength(); i++) { Node n = dcNodes.item(i); addDCValue(myitem, schema, n); } } private void addDCValue(Item i, String schema, Node n) throws TransformerException { String value = getStringValue(n); //n.getNodeValue(); // compensate for empty value getting read as "null", which won't display if (value == null) value = ""; // //getElementData(n, "element"); String element = getAttributeValue(n, "element"); String qualifier = getAttributeValue(n, "qualifier"); //NodeValue(); // //getElementData(n, // "qualifier"); String language = getAttributeValue(n, "language"); System.out.println("\tSchema: " + schema + " Element: " + element + " Qualifier: " + qualifier + " Value: " + value); if (qualifier.equals("none") || "".equals(qualifier)) { qualifier = null; } // if language isn't set, use the system's default value
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -