List of usage examples for java.io FileNotFoundException getClass
@HotSpotIntrinsicCandidate public final native Class<?> getClass();
From source file:org.apache.manifoldcf.agents.output.filesystem.FileOutputConnector.java
/** Remove a document using the connector. * Note that the last outputDescription is included, since it may be necessary for the connector to use such information to know how to properly remove the document. *@param documentURI is the URI of the document. The URI is presumed to be the unique identifier which the output data store will use to process * and serve the document. This URI is constructed by the repository connector which fetches the document, and is thus universal across all output connectors. *@param outputDescription is the last description string that was constructed for this document by the getOutputDescription() method above. *@param activities is the handle to an object that the implementer of an output connector may use to perform operations, such as logging processing activity. */// w w w . j av a 2s . c o m @Override public void removeDocument(String documentURI, String outputDescription, IOutputRemoveActivity activities) throws ManifoldCFException, ServiceInterruption { // Establish a session getSession(); String errorCode = "OK"; String errorDesc = null; FileOutputConfig config = getConfigParameters(null); StringBuffer path = new StringBuffer(); try { // We cannot remove documents, because it is unsafe to do so. // Paths that were created when the document existed will not // be found if it goes away. So we have to leave a grave marker, // in this case a zero-length file, instead. // If the path does not yet exist at the root level, it is dangerous to create it. File currentPath = new File(path.toString()); if (!currentPath.exists()) return; if (!currentPath.isDirectory()) return; String filePath = documentURItoFilePath(documentURI); // Build path one level at a time. This is needed because there may be a collision at // every level. If we don't find a directory where we expect it, we just exit. int index = 0; while (true) { int currentIndex = filePath.indexOf("/", index); if (currentIndex == -1) break; String dirName = filePath.substring(index, currentIndex); File newPath = new File(currentPath, dirName); index = currentIndex + 1; int suffix = 1; while (true) { if (!newPath.exists()) return; if (newPath.isDirectory()) break; // It's a file. Move on to the next one. newPath = new File(currentPath, dirName + "." + suffix); suffix++; } // Directory successfully created! currentPath = newPath; // Go on to the next level. } // Path found. Now, see if we can find the file to null out. FileOutputStream output = null; String fileName = filePath.substring(index); File outputPath = new File(currentPath, fileName); int fileSuffix = 1; while (true) { if (!outputPath.exists()) return; if (!outputPath.isFile()) { // Try a new one outputPath = new File(currentPath, fileName + "." + fileSuffix); fileSuffix++; continue; } // Null it out! try { output = new FileOutputStream(outputPath); break; } catch (FileNotFoundException e) { // Probably some other error errorCode = e.getClass().getSimpleName().toUpperCase(Locale.ROOT); errorDesc = "Couldn't delete the file due to:" + e.getMessage(); throw new ManifoldCFException("Could not zero out file '" + outputPath + "': " + e.getMessage(), e); } } // Just close it, to make a zero-length grave marker. output.close(); } catch (URISyntaxException e) { errorCode = e.getClass().getSimpleName().toUpperCase(Locale.ROOT); errorDesc = "Failed to delete document due to: " + e.getMessage(); handleURISyntaxException(e); } catch (FileNotFoundException e) { errorCode = e.getClass().getSimpleName().toUpperCase(Locale.ROOT); errorDesc = "Failed to delete document due to: " + e.getMessage(); handleFileNotFoundException(e); } catch (SecurityException e) { errorCode = e.getClass().getSimpleName().toUpperCase(Locale.ROOT); errorDesc = "Failed to delete document due to: " + e.getMessage(); handleSecurityException(e); } catch (IOException e) { errorCode = e.getClass().getSimpleName().toUpperCase(Locale.ROOT); errorDesc = "Failed to delete document due to: " + e.getMessage(); handleIOException(e); } finally { activities.recordActivity(null, REMOVE_ACTIVITY, null, documentURI, errorCode, errorDesc); } }
From source file:org.apache.manifoldcf.agents.output.filesystem.FileOutputConnector.java
/** Add (or replace) a document in the output data store using the connector. * This method presumes that the connector object has been configured, and it is thus able to communicate with the output data store should that be * necessary./* www.j ava2s . c o m*/ * The OutputSpecification is *not* provided to this method, because the goal is consistency, and if output is done it must be consistent with the * output description, since that was what was partly used to determine if output should be taking place. So it may be necessary for this method to decode * an output description string in order to determine what should be done. *@param documentURI is the URI of the document. The URI is presumed to be the unique identifier which the output data store will use to process * and serve the document. This URI is constructed by the repository connector which fetches the document, and is thus universal across all output connectors. *@param outputDescription is the description string that was constructed for this document by the getOutputDescription() method. *@param document is the document data to be processed (handed to the output data store). *@param authorityNameString is the name of the authority responsible for authorizing any access tokens passed in with the repository document. May be null. *@param activities is the handle to an object that the implementer of an output connector may use to perform operations, such as logging processing activity. *@return the document status (accepted or permanently rejected). */ @Override public int addOrReplaceDocumentWithException(String documentURI, VersionContext outputDescription, RepositoryDocument document, String authorityNameString, IOutputAddActivity activities) throws ManifoldCFException, ServiceInterruption, IOException { // Establish a session getSession(); FileOutputConfig config = getConfigParameters(null); FileOutputSpecs specs = new FileOutputSpecs(getSpecNode(outputDescription.getSpecification())); ; StringBuffer path = new StringBuffer(); String errorCode = "OK"; String errorDesc = null; try { /* * make file path */ if (specs.getRootPath() != null) { path.append(specs.getRootPath()); } // If the path does not yet exist at the root level, it is dangerous to create it. File currentPath = new File(path.toString()); if (!currentPath.exists()) throw new ManifoldCFException("Root path does not yet exist: '" + currentPath + "'"); if (!currentPath.isDirectory()) throw new ManifoldCFException("Root path is not a directory: '" + currentPath + "'"); String filePath = documentURItoFilePath(documentURI); // Build path one level at a time. This is needed because there may be a collision at // every level. int index = 0; while (true) { int currentIndex = filePath.indexOf("/", index); if (currentIndex == -1) break; String dirName = filePath.substring(index, currentIndex); File newPath = new File(currentPath, dirName); index = currentIndex + 1; int suffix = 1; while (true) { if (newPath.exists() && newPath.isDirectory()) break; // Try to create it. If we fail, check if it now exists as a file. if (newPath.mkdir()) break; // Hmm, didn't create. If it is a file, we suffered a collision, so try again with ".N" as a suffix. if (newPath.exists()) { if (newPath.isDirectory()) break; newPath = new File(currentPath, dirName + "." + suffix); suffix++; } else { errorCode = activities.CREATED_DIRECTORY; errorDesc = "Could not create directory '\"+newPath+\"'. Permission issue?"; throw new ManifoldCFException(errorDesc); } } // Directory successfully created! currentPath = newPath; // Go on to the next one. } // Path successfully created. Now create file. FileOutputStream output = null; String fileName = filePath.substring(index); File outputPath = new File(currentPath, fileName); int fileSuffix = 1; while (true) { try { output = new FileOutputStream(outputPath); break; } catch (FileNotFoundException e) { // Figure out why it could not be created. if (outputPath.exists() && !outputPath.isFile()) { // try a new file outputPath = new File(currentPath, fileName + "." + fileSuffix); fileSuffix++; continue; } // Probably some other error errorCode = e.getClass().getSimpleName().toUpperCase(Locale.ROOT); errorDesc = "Could not create file '" + outputPath + "': " + e.getMessage(); throw new ManifoldCFException(errorDesc, e); } } try { /* * lock file */ FileChannel channel = output.getChannel(); FileLock lock = channel.tryLock(); if (lock == null) { errorCode = ServiceInterruption.class.getSimpleName().toUpperCase(Locale.ROOT); errorDesc = "Could not lock file: '" + outputPath + "'"; throw new ServiceInterruption(errorDesc, null, 1000L, -1L, 10, false); } try { /* * write file */ InputStream input = document.getBinaryStream(); byte buf[] = new byte[65536]; int len; while ((len = input.read(buf)) != -1) { output.write(buf, 0, len); } output.flush(); } finally { // Unlock try { if (lock != null) { lock.release(); } } catch (ClosedChannelException e) { } } } finally { try { output.close(); } catch (IOException e) { } } } catch (URISyntaxException e) { errorCode = e.getClass().getSimpleName().toUpperCase(Locale.ROOT); errorDesc = "Failed to write document due to: " + e.getMessage(); handleURISyntaxException(e); return DOCUMENTSTATUS_REJECTED; } catch (FileNotFoundException e) { errorCode = e.getClass().getSimpleName().toUpperCase(Locale.ROOT); errorDesc = "Failed to write document due to: " + e.getMessage(); handleFileNotFoundException(e); return DOCUMENTSTATUS_REJECTED; } catch (SecurityException e) { errorCode = e.getClass().getSimpleName().toUpperCase(Locale.ROOT); errorDesc = "Failed to write document due to: " + e.getMessage(); handleSecurityException(e); return DOCUMENTSTATUS_REJECTED; } catch (IOException e) { errorCode = e.getClass().getSimpleName().toUpperCase(Locale.ROOT); errorDesc = "Failed to write document due to: " + e.getMessage(); handleIOException(e); return DOCUMENTSTATUS_REJECTED; } finally { activities.recordActivity(null, INGEST_ACTIVITY, new Long(document.getBinaryLength()), documentURI, errorCode, errorDesc); } return DOCUMENTSTATUS_ACCEPTED; }
From source file:org.multibit.file.FileHandler.java
private boolean isWalletSerialised(File walletFile) { boolean isWalletSerialised = false; InputStream stream = null;// w ww .j a va 2s . c om try { // Determine what kind of wallet stream this is: Java Serialization // or protobuf format. stream = new BufferedInputStream(new FileInputStream(walletFile)); isWalletSerialised = stream.read() == 0xac && stream.read() == 0xed; } catch (FileNotFoundException e) { log.error(e.getClass().getCanonicalName() + " " + e.getMessage()); } catch (IOException e) { log.error(e.getClass().getCanonicalName() + " " + e.getMessage()); } finally { if (stream != null) { try { stream.close(); } catch (IOException e) { log.error(e.getClass().getCanonicalName() + " " + e.getMessage()); } } } return isWalletSerialised; }
From source file:cc.redberry.core.tensor.BulkTestsForParser.java
private static void testParseRecurrently(File file, Counter containsParseLinesCounter, Counter matchedLinesCounter, DescriptiveStatistics statistics) { if (file.isFile()) { if (file.getName().equals(BulkTestsForParser.class.getSimpleName() + ".java")) return; if (file.getName().equals(ParserTest.class.getSimpleName() + ".java")) return; if (file.getName().equals(NumberParserTest.class.getSimpleName() + ".java")) return; FileInputStream fileInputStream; try {// w w w . ja v a 2 s .co m fileInputStream = new FileInputStream(file); } catch (FileNotFoundException e) { throw new RuntimeException(); } DataInputStream dataInputStream = new DataInputStream(fileInputStream); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(dataInputStream)); String string; try { boolean containsParse; boolean matchedParse; int lineNumber = -1; String bufferedString = null; while ((string = bufferedReader.readLine()) != null) { ++lineNumber; if (bufferedString != null) { string = bufferedString + "\n" + string; bufferedString = null; } matchedParse = false; if (string.contains("IndexMappingTestUtils.parse") || string.contains("ParserIndices.parse")) continue; containsParse = (string.contains("parse(") || string.contains("parseExpression(") || string.contains("parseSimple(")) && string.contains("\""); string = string.trim(); if (string.length() > 0) { char c = string.charAt(string.length() - 1); if (c == '\"' || c == '+' || c == '(') { bufferedString = string; continue; } } string = string.replaceAll("\n", ""); string = string.replaceAll("\"[\\s]*\\+[\\+]*[\\s]*\"", ""); Matcher matcher = pattern.matcher(string); String tensorString; Tensor tensor; while (matcher.find()) { matchedParse = true; tensorString = matcher.group(2); //if (tensorString.length() > 100) // System.out.println("\"" + tensorString + "\","); tensorString = tensorString.replace("\\\\", "\\"); if (tensorString.contains("\"")) { tensorString = tensorString.split("\"")[0]; } try { statistics.addValue(tensorString.length()); tensor = Tensors.parse(tensorString); checkTensor(tensor); } catch (AssertionError | RuntimeException e) { System.out.println(e.getClass().getSimpleName() + ":"); System.out.println(tensorString); System.out.println(file + " line: " + lineNumber); System.out.println(); throw new RuntimeException(e); } } if (containsParse && !matchedParse && bufferedString == null) { System.out.println("Parse but not matched:"); System.out.println(string); System.out.println(file + " line: " + lineNumber); System.out.println(); } if (containsParse && bufferedString == null) containsParseLinesCounter.increase(); if (matchedParse) matchedLinesCounter.increase(); } bufferedReader.close(); dataInputStream.close(); } catch (IOException e) { throw new RuntimeException(); } } else if (file.isDirectory()) { File[] listOfFiles = file.listFiles(); if (listOfFiles != null) { for (int i = 0; i < listOfFiles.length; i++) testParseRecurrently(listOfFiles[i], containsParseLinesCounter, matchedLinesCounter, statistics); } } else throw new RuntimeException(); }
From source file:org.paxle.core.io.temp.impl.CommandTempReleaser.java
private void releaseCommandFiles(final ICommand cmd, final Long id) { try {// www . j a v a 2 s . c o m File file; final ICrawlerDocument cdoc = cmd.getCrawlerDocument(); if (cdoc != null && (file = cdoc.getContent()) != null) { if (tfm.isKnown(file)) { try { tfm.releaseTempFile(file); } catch (FileNotFoundException e) { this.logger.warn("downloaded crawler-data not available for release"); } } else { this.logger.debug(String.format("Crawlerdoc tempfile %s not managed by tempfilemanager", file.toString())); } } final Queue<Map.Entry<String, IParserDocument>> pdocs = new LinkedList<Map.Entry<String, IParserDocument>>(); IParserDocument pdoc = cmd.getParserDocument(); Map.Entry<String, IParserDocument> entry = null; if (pdoc != null) { do { if (entry != null) { pdoc = entry.getValue(); } if ((file = pdoc.getTextFile()) != null) { if (tfm.isKnown(file)) { try { tfm.releaseTempFile(file); } catch (FileNotFoundException e) { final String msg = (entry == null) ? "parser-document" : "sub parser-document '" + entry.getKey() + "'"; logger.warn(String.format("data of %s of cmd [%06d] not available for release", msg, id)); } } else { this.logger.debug(String.format("Parserdoc tempfile %s not managed by tempfilemanager", file.toString())); } } pdocs.addAll(pdoc.getSubDocs().entrySet()); } while ((entry = pdocs.poll()) != null); } } catch (Throwable e) { this.logger.error(String.format("Unexpected '%s' while releasing temporary files of command '%s'.", e.getClass().getName(), cmd.getLocation()), e); } }
From source file:edu.harvard.mcz.imagecapture.loader.JobVerbatimFieldLoad.java
@Override public void start() { startDateTime = new Date(); Singleton.getSingletonInstance().getJobList().addJob((RunnableJob) this); runStatus = RunStatus.STATUS_RUNNING; String selectedFilename = ""; if (file == null) { final JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); if (Singleton.getSingletonInstance().getProperties().getProperties() .getProperty(ImageCaptureProperties.KEY_LASTLOADPATH) != null) { fileChooser.setCurrentDirectory(new File(Singleton.getSingletonInstance().getProperties() .getProperties().getProperty(ImageCaptureProperties.KEY_LASTLOADPATH))); }/*ww w. j a v a 2 s .co m*/ int returnValue = fileChooser.showOpenDialog(Singleton.getSingletonInstance().getMainFrame()); if (returnValue == JFileChooser.APPROVE_OPTION) { file = fileChooser.getSelectedFile(); } } if (file != null) { log.debug("Selected file to load: " + file.getName() + "."); if (file.exists() && file.isFile() && file.canRead()) { // Save location Singleton.getSingletonInstance().getProperties().getProperties() .setProperty(ImageCaptureProperties.KEY_LASTLOADPATH, file.getPath()); selectedFilename = file.getName(); String[] headers = new String[] {}; CSVFormat csvFormat = CSVFormat.DEFAULT.withHeader(headers); int rows = 0; try { rows = readRows(file, csvFormat); } catch (FileNotFoundException e) { JOptionPane.showMessageDialog(Singleton.getSingletonInstance().getMainFrame(), "Unable to load data, file not found: " + e.getMessage(), "Error: File Not Found", JOptionPane.OK_OPTION); errors.append("File not found ").append(e.getMessage()).append("\n"); log.error(e.getMessage(), e); } catch (IOException e) { errors.append("Error loading csv format, trying tab delimited: ").append(e.getMessage()) .append("\n"); log.debug(e.getMessage()); try { // try reading as tab delimited format, if successful, use that format. CSVFormat tabFormat = CSVFormat.newFormat('\t').withIgnoreSurroundingSpaces(true) .withHeader(headers).withQuote('"'); rows = readRows(file, tabFormat); csvFormat = tabFormat; } catch (IOException e1) { errors.append("Error Loading data: ").append(e1.getMessage()).append("\n"); log.error(e.getMessage(), e1); } } try { Reader reader = new FileReader(file); CSVParser csvParser = new CSVParser(reader, csvFormat); Map<String, Integer> csvHeader = csvParser.getHeaderMap(); headers = new String[csvHeader.size()]; int i = 0; for (String header : csvHeader.keySet()) { headers[i++] = header; log.debug(header); } boolean okToRun = true; //TODO: Work picking/checking responsibility into a FieldLoaderWizard List<String> headerList = Arrays.asList(headers); if (!headerList.contains("barcode")) { log.error("Input file " + file.getName() + " header does not contain required field 'barcode'."); // no barcode field, we can't match the input to specimen records. errors.append("Field \"barcode\" not found in csv file headers. Unable to load data.") .append("\n"); okToRun = false; } if (okToRun) { Iterator<CSVRecord> iterator = csvParser.iterator(); FieldLoader fl = new FieldLoader(); if (headerList.size() == 3 && headerList.contains("verbatimUnclassifiedText") && headerList.contains("questions") && headerList.contains("barcode")) { log.debug("Input file matches case 1: Unclassified text only."); // Allowed case 1a: unclassified text only int confirm = JOptionPane.showConfirmDialog( Singleton.getSingletonInstance().getMainFrame(), "Confirm load from file " + selectedFilename + " (" + rows + " rows) with just barcode and verbatimUnclassifiedText", "Verbatim unclassified Field found for load", JOptionPane.OK_CANCEL_OPTION); if (confirm == JOptionPane.OK_OPTION) { String barcode = ""; int lineNumber = 0; while (iterator.hasNext()) { lineNumber++; counter.incrementSpecimens(); CSVRecord record = iterator.next(); try { String verbatimUnclassifiedText = record.get("verbatimUnclassifiedText"); barcode = record.get("barcode"); String questions = record.get("questions"); fl.load(barcode, verbatimUnclassifiedText, questions, true); counter.incrementSpecimensUpdated(); } catch (IllegalArgumentException e) { RunnableJobError error = new RunnableJobError(file.getName(), barcode, Integer.toString(lineNumber), e.getClass().getSimpleName(), e, RunnableJobError.TYPE_LOAD_FAILED); counter.appendError(error); log.error(e.getMessage(), e); } catch (LoadException e) { RunnableJobError error = new RunnableJobError(file.getName(), barcode, Integer.toString(lineNumber), e.getClass().getSimpleName(), e, RunnableJobError.TYPE_LOAD_FAILED); counter.appendError(error); log.error(e.getMessage(), e); } percentComplete = (int) ((lineNumber * 100f) / rows); this.setPercentComplete(percentComplete); } } else { errors.append("Load canceled by user.").append("\n"); } } else if (headerList.size() == 4 && headerList.contains("verbatimUnclassifiedText") && headerList.contains("questions") && headerList.contains("barcode") && headerList.contains("verbatimClusterIdentifier")) { log.debug( "Input file matches case 1: Unclassified text only (with cluster identifier)."); // Allowed case 1b: unclassified text only (including cluster identifier) int confirm = JOptionPane.showConfirmDialog( Singleton.getSingletonInstance().getMainFrame(), "Confirm load from file " + selectedFilename + " (" + rows + " rows) with just barcode and verbatimUnclassifiedText", "Verbatim unclassified Field found for load", JOptionPane.OK_CANCEL_OPTION); if (confirm == JOptionPane.OK_OPTION) { String barcode = ""; int lineNumber = 0; while (iterator.hasNext()) { lineNumber++; counter.incrementSpecimens(); CSVRecord record = iterator.next(); try { String verbatimUnclassifiedText = record.get("verbatimUnclassifiedText"); String verbatimClusterIdentifier = record.get("verbatimClusterIdentifier"); barcode = record.get("barcode"); String questions = record.get("questions"); fl.load(barcode, verbatimUnclassifiedText, verbatimClusterIdentifier, questions, true); counter.incrementSpecimensUpdated(); } catch (IllegalArgumentException e) { RunnableJobError error = new RunnableJobError(file.getName(), barcode, Integer.toString(lineNumber), e.getClass().getSimpleName(), e, RunnableJobError.TYPE_LOAD_FAILED); counter.appendError(error); log.error(e.getMessage(), e); } catch (LoadException e) { RunnableJobError error = new RunnableJobError(file.getName(), barcode, Integer.toString(lineNumber), e.getClass().getSimpleName(), e, RunnableJobError.TYPE_LOAD_FAILED); counter.appendError(error); log.error(e.getMessage(), e); } percentComplete = (int) ((lineNumber * 100f) / rows); this.setPercentComplete(percentComplete); } } else { errors.append("Load canceled by user.").append("\n"); } } else if (headerList.size() == 8 && headerList.contains("verbatimUnclassifiedText") && headerList.contains("questions") && headerList.contains("barcode") && headerList.contains("verbatimLocality") && headerList.contains("verbatimDate") && headerList.contains("verbatimNumbers") && headerList.contains("verbatimCollector") && headerList.contains("verbatimCollection")) { // Allowed case two, transcription into verbatim fields, must be exact list of all // verbatim fields, not including cluster identifier or other metadata. log.debug("Input file matches case 2: Full list of verbatim fields."); int confirm = JOptionPane.showConfirmDialog( Singleton.getSingletonInstance().getMainFrame(), "Confirm load from file " + selectedFilename + " (" + rows + " rows) with just barcode and verbatim fields.", "Verbatim Fields found for load", JOptionPane.OK_CANCEL_OPTION); if (confirm == JOptionPane.OK_OPTION) { String barcode = ""; int lineNumber = 0; while (iterator.hasNext()) { lineNumber++; counter.incrementSpecimens(); CSVRecord record = iterator.next(); try { String verbatimLocality = record.get("verbatimLocality"); String verbatimDate = record.get("verbatimDate"); String verbatimCollector = record.get("verbatimCollector"); String verbatimCollection = record.get("verbatimCollection"); String verbatimNumbers = record.get("verbatimNumbers"); String verbatimUnclasifiedText = record.get("verbatimUnclassifiedText"); barcode = record.get("barcode"); String questions = record.get("questions"); fl.load(barcode, verbatimLocality, verbatimDate, verbatimCollector, verbatimCollection, verbatimNumbers, verbatimUnclasifiedText, questions); counter.incrementSpecimensUpdated(); } catch (IllegalArgumentException e) { RunnableJobError error = new RunnableJobError(file.getName(), barcode, Integer.toString(lineNumber), e.getClass().getSimpleName(), e, RunnableJobError.TYPE_LOAD_FAILED); counter.appendError(error); log.error(e.getMessage(), e); } catch (LoadException e) { RunnableJobError error = new RunnableJobError(file.getName(), barcode, Integer.toString(lineNumber), e.getClass().getSimpleName(), e, RunnableJobError.TYPE_LOAD_FAILED); counter.appendError(error); log.error(e.getMessage(), e); } percentComplete = (int) ((lineNumber * 100f) / rows); this.setPercentComplete(percentComplete); } } else { errors.append("Load canceled by user.").append("\n"); } } else { // allowed case three, transcription into arbitrary sets verbatim or other fields log.debug("Input file case 3: Arbitrary set of fields."); // Check column headers before starting run. boolean headersOK = false; try { HeaderCheckResult headerCheck = fl.checkHeaderList(headerList); if (headerCheck.isResult()) { int confirm = JOptionPane.showConfirmDialog( Singleton.getSingletonInstance().getMainFrame(), "Confirm load from file " + selectedFilename + " (" + rows + " rows) with headers: \n" + headerCheck.getMessage().replaceAll(":", ":\n"), "Fields found for load", JOptionPane.OK_CANCEL_OPTION); if (confirm == JOptionPane.OK_OPTION) { headersOK = true; } else { errors.append("Load canceled by user.").append("\n"); } } else { int confirm = JOptionPane.showConfirmDialog( Singleton.getSingletonInstance().getMainFrame(), "Problem found with headers in file, try to load anyway?\nHeaders: \n" + headerCheck.getMessage().replaceAll(":", ":\n"), "Problem in fields for load", JOptionPane.OK_CANCEL_OPTION); if (confirm == JOptionPane.OK_OPTION) { headersOK = true; } else { errors.append("Load canceled by user.").append("\n"); } } } catch (LoadException e) { errors.append("Error loading data: \n").append(e.getMessage()).append("\n"); JOptionPane.showMessageDialog(Singleton.getSingletonInstance().getMainFrame(), e.getMessage().replaceAll(":", ":\n"), "Error Loading Data: Problem Fields", JOptionPane.ERROR_MESSAGE); log.error(e.getMessage(), e); } if (headersOK) { int lineNumber = 0; while (iterator.hasNext()) { lineNumber++; Map<String, String> data = new HashMap<String, String>(); CSVRecord record = iterator.next(); String barcode = record.get("barcode"); Iterator<String> hi = headerList.iterator(); boolean containsNonVerbatim = false; while (hi.hasNext()) { String header = hi.next(); // Skip any fields prefixed by the underscore character _ if (!header.equals("barcode") && !header.startsWith("_")) { data.put(header, record.get(header)); if (!header.equals("questions") && MetadataRetriever.isFieldExternallyUpdatable(Specimen.class, header) && MetadataRetriever.isFieldVerbatim(Specimen.class, header)) { containsNonVerbatim = true; } } } if (data.size() > 0) { try { boolean updated = false; if (containsNonVerbatim) { updated = fl.loadFromMap(barcode, data, WorkFlowStatus.STAGE_CLASSIFIED, true); } else { updated = fl.loadFromMap(barcode, data, WorkFlowStatus.STAGE_VERBATIM, true); } counter.incrementSpecimens(); if (updated) { counter.incrementSpecimensUpdated(); } } catch (HibernateException e1) { // Catch (should just be development) problems with the underlying query StringBuilder message = new StringBuilder(); message.append("Query Error loading row (").append(lineNumber) .append(")[").append(barcode).append("]") .append(e1.getMessage()); RunnableJobError err = new RunnableJobError(selectedFilename, barcode, Integer.toString(lineNumber), e1.getMessage(), e1, RunnableJobError.TYPE_LOAD_FAILED); counter.appendError(err); log.error(e1.getMessage(), e1); } catch (LoadException e) { StringBuilder message = new StringBuilder(); message.append("Error loading row (").append(lineNumber).append(")[") .append(barcode).append("]").append(e.getMessage()); RunnableJobError err = new RunnableJobError(selectedFilename, barcode, Integer.toString(lineNumber), e.getMessage(), e, RunnableJobError.TYPE_LOAD_FAILED); counter.appendError(err); // errors.append(message.append("\n").toString()); log.error(e.getMessage(), e); } } percentComplete = (int) ((lineNumber * 100f) / rows); this.setPercentComplete(percentComplete); } } else { String message = "Can't load data, problem with headers."; errors.append(message).append("\n"); log.error(message); } } } csvParser.close(); reader.close(); } catch (FileNotFoundException e) { JOptionPane.showMessageDialog(Singleton.getSingletonInstance().getMainFrame(), "Unable to load data, file not found: " + e.getMessage(), "Error: File Not Found", JOptionPane.OK_OPTION); errors.append("File not found ").append(e.getMessage()).append("\n"); log.error(e.getMessage(), e); } catch (IOException e) { errors.append("Error Loading data: ").append(e.getMessage()).append("\n"); log.error(e.getMessage(), e); } } } else { //TODO: handle error condition log.error("File selection cancelled by user."); } report(selectedFilename); done(); }