/* * (non-Javadoc) * @see net.sourceforge.squirrel_sql.plugins.dataimport.importer.IFileImporter#reset() */ public boolean reset() throws IOException { if (reader != null) { reader.close(); } reader = new CsvReader( new InputStreamReader(new FileInputStream(importFile), settings.getImportCharset()), settings.getSeperator()); reader.setSafetySwitch(safetySwitch); return true; }
private static void filterCSV( String filename, LinkedList<String> filters, LinkedList<String> includes) throws IOException { FileReader fr = new FileReader(filename); CsvReader csvIn = new CsvReader(fr, SEPARATOR); csvIn.setSafetySwitch(false); if (csvIn.readHeaders()) { csvIn.readRecord(); System.out.println("'" + filename + "' has " + csvIn.getColumnCount() + " column."); int usedColumn = 0; String[] headers = csvIn.getHeaders(); StringBuffer newHeader = new StringBuffer(); StringBuffer newValues = new StringBuffer(); HashMap<String, String> data = new HashMap<String, String>(); allData.put(filename, data); for (String header : headers) { boolean matches = false; // check if a filter matches the entry for (String filter : filters) { if (header.contains(filter)) { matches = true; // ok, filter matches, but maybe it is on the include list? for (String include : includes) { if (header.contains(include)) { matches = false; break; } } break; } } if (!matches) { usedColumn++; String value = csvIn.get(header); newHeader.append(header); newHeader.append(SEPARATOR_OUT); newValues.append(value); newValues.append(SEPARATOR_OUT); if (data != null) { if (!keys.containsKey(header)) { keys.put(header, true); } data.put(header, value); } } } System.out.println(" -> " + usedColumn + " column remains"); FileWriter fw = new FileWriter(filename + FILENAME_POSTFIX, false); fw.write(newHeader.toString()); fw.write(NEW_LINE); fw.write(newValues.toString()); fw.close(); } else { System.err.println("Can not read header from '" + filename + "'"); } }
public void execute(JobExecutionContext context) throws JobExecutionException { JobDataMap properties = context.getMergedJobDataMap(); long language = 0; boolean isMultilanguage = false; try { language = Long.parseLong(properties.getString("language")); if (language == -1) { isMultilanguage = true; } } catch (Exception e4) { language = APILocator.getLanguageAPI().getDefaultLanguage().getId(); } String filePath = null; String logPath = ""; String fileName = ""; try { filePath = (String) properties.get("filePath"); logPath = pluginAPI.loadProperty("org.dotcms.plugins.contentImporter", "logFile"); File file = new File(filePath); fileName = file.getName(); int index = fileName.lastIndexOf("."); if (-1 < index) fileName = fileName.substring(0, index); } catch (Exception e) { } try { String structure = (String) properties.get("structure"); String[] fields = {}; if (UtilMethods.isSet(properties.get("fields"))) { String[] strFields = ((String) properties.get("fields")).split(","); List<String> longFields = new ArrayList<String>(strFields.length); for (String field : strFields) { longFields.add(field); } String[] tempArray = new String[longFields.size()]; for (int pos = 0; pos < longFields.size(); ++pos) { tempArray[pos] = longFields.get(pos); } fields = tempArray; } String reportEmail = (String) properties.get("reportEmail"); String csvSeparatorDelimiter = (String) properties.get("csvSeparatorDelimiter"); if (!UtilMethods.isSet(csvSeparatorDelimiter)) csvSeparatorDelimiter = ","; else csvSeparatorDelimiter = "" + csvSeparatorDelimiter.trim().charAt(0); String csvTextDelimiter = (String) properties.get("csvTextDelimiter"); if (UtilMethods.isSet(csvTextDelimiter)) csvTextDelimiter = "" + csvTextDelimiter.trim().charAt(0); boolean publishContent = new Boolean((String) properties.get("publishContent")); HashMap<String, List<String>> results = new HashMap<String, List<String>>(); results.put("warnings", new ArrayList<String>()); results.put("errors", new ArrayList<String>()); results.put("messages", new ArrayList<String>()); results.put("results", new ArrayList<String>()); File tempfile = new File(filePath); List<File> filesList = new ArrayList<File>(); if (!tempfile.exists()) { ((List<String>) results.get("errors")).add("File: " + filePath + " doesn't exist."); sendResults(results, reportEmail, fileName + " Import Results", logPath, fileName); } else if (tempfile.isDirectory()) { File[] files = tempfile.listFiles(); for (File f : files) { if (f.getName().toLowerCase().endsWith(".csv")) { filesList.add(f); } } } else { filesList.add(tempfile); } Collections.sort(filesList); for (File file : filesList) { if (!file.exists()) { ((List<String>) results.get("errors")).add("File: " + filePath + " doesn't exist."); sendResults(results, reportEmail, fileName + " Import Results", logPath, fileName); } else if (!file.isFile()) { ((List<String>) results.get("errors")).add(filePath + " isn't a file."); sendResults(results, reportEmail, fileName + " Import Results", logPath, fileName); } else if (!file.canRead()) { ((List<String>) results.get("errors")).add("File: " + filePath + " can't be readed."); sendResults(results, reportEmail, fileName + " Import Results", logPath, fileName); } else { Reader reader = null; CsvReader csvreader = null; try { File renameFile = new File(file.getPath() + ".lock"); boolean renamed = file.renameTo(renameFile); file = renameFile; reader = new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8")); csvreader = new CsvReader(reader, csvSeparatorDelimiter.charAt(0)); if (UtilMethods.isSet(csvTextDelimiter)) csvreader.setTextQualifier(csvTextDelimiter.charAt(0)); csvreader.setSafetySwitch(false); User user = APILocator.getUserAPI().getSystemUser(); if (csvreader.readHeaders()) { ContentletUtil contentletUtil = new ContentletUtil(reader, csvreader); results = contentletUtil.importFile( structure, fields, false, user, isMultilanguage, language, publishContent); } } catch (Exception e) { ((List<String>) results.get("errors")).add("Exception: " + e.toString()); Logger.error(ContentImporterThread.class, e.getMessage(), e); } finally { if (reader != null) { try { reader.close(); } catch (Exception e) { } } if (csvreader != null) { try { csvreader.close(); } catch (Exception e) { } } moveImportedFile(file.getPath()); sendResults(results, reportEmail, fileName + " Import Results", logPath, fileName); } } } } catch (Exception e1) { Logger.warn(this, e1.toString()); } }