private void moveImportedFile(String filePath) { try { String processedFilePath = pluginAPI.loadProperty("org.dotcms.plugins.contentImporter", "processedFilePath"); File processedFilePathDir = new File(processedFilePath); if (!processedFilePathDir.exists()) processedFilePathDir.mkdirs(); File file = new File(filePath); String fileName = file.getName(); String fileNameNoExt = ""; String fileExt = ""; int index = fileName.lastIndexOf("."); if (-1 < index) { fileNameNoExt = fileName.substring(0, index); fileExt = fileName.substring(index + 1); } file.renameTo( new File( processedFilePathDir.getPath() + File.separator + fileNameNoExt + " - " + UtilMethods.dateToHTMLDate(new Date(), "yyyyMMddHHmmss") + "." + fileExt)); } catch (Exception e) { Logger.info(this, e.toString()); } }
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()); } }