Java tutorial
/** * Copyright 2015 J. Patrick Meyer * <p/> * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * <p/> * http://www.apache.org/licenses/LICENSE-2.0 * <p/> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.itemanalysis.jmetrik.file; import com.itemanalysis.jmetrik.manager.DelimiterType; import com.itemanalysis.jmetrik.manager.ExportDataCommand; import com.itemanalysis.psychometrics.data.ItemType; import com.itemanalysis.psychometrics.data.VariableAttributes; import com.itemanalysis.psychometrics.data.VariableName; import com.itemanalysis.psychometrics.tools.StopWatch; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVPrinter; import org.apache.log4j.Logger; import javax.swing.*; import java.io.*; import java.nio.file.Files; import java.nio.file.OpenOption; import java.util.LinkedHashMap; public class JmetrikFileExporter extends SwingWorker<String, Void> { private ExportDataCommand command = null; private boolean hasHeader = true; private boolean empty = true; private boolean overwrite = false; private boolean scored = false; private char delimiter = ','; private File dataFile = null; private File outputFile = null; private Throwable theException = null; private StopWatch sw = null; private LinkedHashMap<VariableName, VariableAttributes> variableAttributeMap = null; static Logger logger = Logger.getLogger("jmetrik-logger"); static Logger scriptLogger = Logger.getLogger("jmetrik-script-logger"); public JmetrikFileExporter(ExportDataCommand command) { this.command = command; variableAttributeMap = new LinkedHashMap<VariableName, VariableAttributes>(); sw = new StopWatch(); } private void parseCommand() { dataFile = command.getDataFile(); outputFile = command.getOutputFile(); hasHeader = command.headerIncluded(); overwrite = command.overwrite(); scored = command.scored(); empty = command.empty(); delimiter = command.getDelimiter(); } private boolean exportFile() { JmetrikFileReader reader = null; BufferedWriter writer = null; CSVPrinter printer = null; if (outputFile.exists() && !overwrite) return false; try { reader = new JmetrikFileReader(dataFile); writer = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(outputFile.toPath()))); reader.openConnection(); if (hasHeader) { String[] colNames = reader.getColumnNames(); printer = new CSVPrinter(writer, CSVFormat.DEFAULT.withCommentMarker('#').withDelimiter(delimiter).withHeader(colNames)); } else { printer = new CSVPrinter(writer, CSVFormat.DEFAULT.withCommentMarker('#').withDelimiter(delimiter)); } LinkedHashMap<VariableName, VariableAttributes> variableAttributes = reader.getVariableAttributes(); JmetrikCSVRecord record = null; VariableAttributes tempAttributes = null; //print scored values if (scored) { String tempValue = ""; while (reader.hasNext()) { record = reader.next(); for (VariableName v : variableAttributes.keySet()) { tempAttributes = variableAttributes.get(v); tempValue = record.originalValue(v); if (tempAttributes.getItemType() == ItemType.NOT_ITEM) { //write original string if not an item if (record.isMissing(v, tempValue) && empty) { printer.print(""); } else { printer.print(tempValue); } } else { //write scored value if a test item if (record.isMissing(v, tempValue) && empty) { printer.print(""); } else { printer.print(tempAttributes.getItemScoring().computeItemScore(tempValue)); } } } printer.println(); } //print original values } else { String tempValue = ""; while (reader.hasNext()) { record = reader.next(); for (VariableName v : variableAttributes.keySet()) { tempValue = record.originalValue(v); if (record.isMissing(v, tempValue) && empty) { printer.print(""); } else { printer.print(tempValue); } } printer.println(); } } } catch (IOException ex) { theException = ex; } finally { try { if (reader != null) reader.close(); if (printer != null) printer.close(); if (writer != null) writer.close(); } catch (IOException ex) { theException = ex; } } return true; } @Override public String doInBackground() { firePropertyChange("status", "", "Exporting file..."); parseCommand(); if (exportFile()) { return "The file " + dataFile.getAbsolutePath() + " was successfully exported."; } else { return "The file " + outputFile.getAbsolutePath() + " already exists and you chose to not overwrite it."; } } @Override protected void done() { try { if (theException == null) { logger.info(get()); scriptLogger.info(command.toString()); firePropertyChange("status", "", "Done: " + sw.getElapsedTime()); } else { logger.fatal(theException.getMessage(), theException); firePropertyChange("error", "", "Error - Check log for details."); } } catch (Exception ex) { logger.fatal(ex.getMessage(), ex); firePropertyChange("error", "", "Error - Check log for details."); } } }