List of usage examples for org.apache.commons.csv CSVFormat DEFAULT
CSVFormat DEFAULT
To view the source code for org.apache.commons.csv CSVFormat DEFAULT.
Click Source Link
From source file:org.nuxeo.ecm.platform.filemanager.service.extension.CSVZipImporter.java
@Override public DocumentModel create(CoreSession documentManager, Blob content, String path, boolean overwrite, String filename, TypeManager typeService) throws IOException { ZipFile zip = null;/*from www . ja v a 2s . co m*/ try (CloseableFile source = content.getCloseableFile()) { zip = getArchiveFileIfValid(source.getFile()); if (zip == null) { return null; } DocumentModel container = documentManager.getDocument(new PathRef(path)); ZipEntry index = zip.getEntry(MARKER); try (Reader reader = new InputStreamReader(zip.getInputStream(index)); CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT.withHeader());) { Map<String, Integer> header = csvParser.getHeaderMap(); for (CSVRecord csvRecord : csvParser) { String type = null; String id = null; Map<String, String> stringValues = new HashMap<>(); for (String headerValue : header.keySet()) { String lineValue = csvRecord.get(headerValue); if ("type".equalsIgnoreCase(headerValue)) { type = lineValue; } else if ("id".equalsIgnoreCase(headerValue)) { id = lineValue; } else { stringValues.put(headerValue, lineValue); } } boolean updateDoc = false; // get doc for update DocumentModel targetDoc = null; if (id != null) { // update ? String targetPath = new Path(path).append(id).toString(); if (documentManager.exists(new PathRef(targetPath))) { targetDoc = documentManager.getDocument(new PathRef(targetPath)); updateDoc = true; } } // create doc if needed if (targetDoc == null) { if (type == null) { log.error("Can not create doc without a type, skipping line"); continue; } if (id == null) { id = IdUtils.generateStringId(); } targetDoc = documentManager.createDocumentModel(path, id, type); } // update doc properties DocumentType targetDocType = targetDoc.getDocumentType(); for (String fname : stringValues.keySet()) { String stringValue = stringValues.get(fname); Field field = null; boolean usePrefix = false; String schemaName = null; String fieldName = null; if (fname.contains(":")) { if (targetDocType.hasField(fname)) { field = targetDocType.getField(fname); usePrefix = true; } } else if (fname.contains(".")) { String[] parts = fname.split("\\."); schemaName = parts[0]; fieldName = parts[1]; if (targetDocType.hasSchema(schemaName)) { field = targetDocType.getField(fieldName); usePrefix = false; } } else { if (targetDocType.hasField(fname)) { field = targetDocType.getField(fname); usePrefix = false; schemaName = field.getDeclaringType().getSchemaName(); } } if (field != null) { Serializable fieldValue = getFieldValue(field, stringValue, zip); if (fieldValue != null) { if (usePrefix) { targetDoc.setPropertyValue(fname, fieldValue); } else { targetDoc.setProperty(schemaName, fieldName, fieldValue); } } } } if (updateDoc) { documentManager.saveDocument(targetDoc); } else { documentManager.createDocument(targetDoc); } } } return container; } finally { IOUtils.closeQuietly(zip); } }
From source file:org.nuxeo.ecm.ui.web.auth.digest.DigestAuthenticator.java
public static Map<String, String> splitParameters(String auth) { Map<String, String> map = new HashMap<>(); try (CSVParser reader = new CSVParser(new StringReader(auth), CSVFormat.DEFAULT)) { Iterator<CSVRecord> iterator = reader.iterator(); if (iterator.hasNext()) { CSVRecord record = iterator.next(); for (String itemPairStr : record) { itemPairStr = StringUtils.remove(itemPairStr, QUOTE); String[] parts = itemPairStr.split(EQUAL_SEPARATOR, 2); if (parts == null) { continue; } else { map.put(parts[0].trim(), parts[1].trim()); }//from www .j av a2 s .co m } } } catch (IOException e) { log.error(e.getMessage(), e); } return map; }
From source file:org.nuxeo.ecm.user.center.profile.UserProfileImporter.java
public void doImport(CoreSession session) { UserProfileService ups = Framework.getLocalService(UserProfileService.class); config = ups.getImporterConfig();/*from w w w .j a v a 2 s . c om*/ if (config == null) { log.error("No importer configuration could be found"); return; } dataFileName = config.getDataFileName(); if (dataFileName == null) { log.error("No importer dataFileName was supplied"); return; } InputStream is = getResourceAsStream(dataFileName); if (is == null) { log.error("Error locating CSV data file: " + dataFileName); return; } Reader in = new BufferedReader(new InputStreamReader(is)); CSVParser parser = null; try { parser = CSVFormat.DEFAULT.withEscape(escapeCharacter).withHeader().parse(in); doImport(session, parser, ups); } catch (IOException e) { log.error("Unable to read CSV file", e); } finally { if (parser != null) { try { parser.close(); } catch (IOException e) { log.debug(e, e); } } } }
From source file:org.nuxeo.theme.presets.PaletteParser.java
public static String renderPaletteAsCsv(byte[] bytes, String fileName) { StringWriter sw = new StringWriter(); try (CSVPrinter writer = new CSVPrinter(sw, CSVFormat.DEFAULT.withDelimiter('\t'))) { for (Map.Entry<String, String> entry : parse(bytes, fileName).entrySet()) { writer.printRecord(entry.getKey(), entry.getValue()); }/*w w w .jav a 2 s. c om*/ } catch (PaletteIdentifyException e) { log.warn("Could not identify palette type: " + fileName); } catch (PaletteParseException e) { log.warn("Could not parse palette: " + fileName); } catch (IOException e) { log.error(e.getMessage(), e); } return sw.toString(); }
From source file:org.nuxeo.theme.presets.PaletteParser.java
public static Map<String, String> parseCsv(String text) { Map<String, String> properties = new HashMap<>(); if (text == null) { return properties; }/*from w w w. j a v a 2s . com*/ try (StringReader sr = new StringReader(text); CSVParser reader = new CSVParser(sr, CSVFormat.DEFAULT.withDelimiter('\t'))) { for (CSVRecord record : reader) { properties.put(record.get(0), record.get(1)); } } catch (IOException e) { log.error(e, e); } return properties; }
From source file:org.nuxeo.theme.Utils.java
public static String listToCsv(List<String> list) { StringWriter sw = new StringWriter(); try (CSVPrinter writer = new CSVPrinter(sw, CSVFormat.DEFAULT.withDelimiter(',').withQuoteMode(QuoteMode.ALL))) { writer.printRecord(list);//from w ww .j a va 2 s . co m } catch (IOException e) { log.error(e.getMessage(), e); } return sw.toString(); }
From source file:org.nuxeo.theme.Utils.java
public static List<String> csvToList(String str) throws IOException { if ("".equals(str) || str == null) { return new ArrayList<>(); }//from w ww.j a va 2s . c om StringReader sr = new StringReader(str); try (CSVParser reader = new CSVParser(sr, CSVFormat.DEFAULT.withDelimiter(','))) { Iterator<CSVRecord> iterator = reader.iterator(); if (!iterator.hasNext()) { return new ArrayList<>(); } else { CSVRecord nextRecord = iterator.next(); List<String> result = new ArrayList<>(nextRecord.size()); for (String value : nextRecord) { result.add(value); } return result; } } }
From source file:org.nuxeo.theme.vocabularies.VocabularyManager.java
public List<VocabularyItem> getItems(String name) { VocabularyType vocabularyType = (VocabularyType) Manager.getTypeRegistry().lookup(TypeFamily.VOCABULARY, name);//from w ww .j a va2 s .c o m if (vocabularyType == null) { return null; } final String path = vocabularyType.getPath(); final String className = vocabularyType.getClassName(); if (path == null && className == null) { log.error("Must specify a class name or a path for vocabulary: " + name); return null; } if (path != null && className != null) { log.error("Cannot specify both a class name and a path for vocabulary: " + name); return null; } if (className != null) { Vocabulary vocabulary = getInstance(className); if (vocabulary == null) { log.error("Vocabulary class not found: " + className); return null; } return vocabulary.getItems(); } if (path != null) { if (!path.endsWith(".csv")) { log.error("Only .csv vocabularies are supported: " + path); return null; } final List<VocabularyItem> items = new ArrayList<>(); try (InputStream is = getClass().getClassLoader().getResourceAsStream(path)) { if (is == null) { log.error("Vocabulary file not found: " + path); return null; } try (CSVParser reader = new CSVParser(new InputStreamReader(is, Charsets.UTF_8), CSVFormat.DEFAULT)) { for (CSVRecord record : reader) { final String value = record.get(0); String label = value; if (record.size() >= 2) { label = record.get(1); } items.add(new VocabularyItem(value, label)); } } } catch (IOException e) { log.error("Could not read vocabulary file: " + path, e); } return items; } return null; }
From source file:org.ofbiz.accounting.invoice.InvoiceServices.java
public static Map<String, Object> importInvoice(DispatchContext dctx, Map<String, Object> context) { Delegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); GenericValue userLogin = (GenericValue) context.get("userLogin"); ByteBuffer fileBytes = (ByteBuffer) context.get("uploadedFile"); String organizationPartyId = (String) context.get("organizationPartyId"); String encoding = System.getProperty("file.encoding"); String csvString = Charset.forName(encoding).decode(fileBytes).toString(); final BufferedReader csvReader = new BufferedReader(new StringReader(csvString)); CSVFormat fmt = CSVFormat.DEFAULT.withHeader(); List<String> errMsgs = FastList.newInstance(); List<String> newErrMsgs = FastList.newInstance(); String lastInvoiceId = null;//from ww w . j ava2s. com String currentInvoiceId = null; String newInvoiceId = null; int invoicesCreated = 0; if (fileBytes == null) { return ServiceUtil.returnError("Uploaded file data not found"); } try { for (final CSVRecord rec : fmt.parse(csvReader)) { currentInvoiceId = rec.get("invoiceId"); if (lastInvoiceId == null || !currentInvoiceId.equals(lastInvoiceId)) { newInvoiceId = null; Map<String, Object> invoice = UtilMisc.toMap("invoiceTypeId", rec.get("invoiceTypeId"), "partyIdFrom", rec.get("partyIdFrom"), "partyId", rec.get("partyId"), "invoiceDate", rec.get("invoiceDate"), "dueDate", rec.get("dueDate"), "currencyUomId", rec.get("currencyUomId"), "description", rec.get("description"), "referenceNumber", rec.get("referenceNumber") + " Imported: orginal InvoiceId: " + currentInvoiceId, "userLogin", userLogin); // replace values if required if (UtilValidate.isNotEmpty(rec.get("partyIdFromTrans"))) { invoice.put("partyIdFrom", rec.get("partyIdFromTrans")); } if (UtilValidate.isNotEmpty(rec.get("partyIdTrans"))) { invoice.put("partyId", rec.get("partyIdTrans")); } // invoice validation try { newErrMsgs = FastList.newInstance(); if (UtilValidate.isEmpty(invoice.get("partyIdFrom"))) { newErrMsgs.add("Line number " + rec.getRecordNumber() + ": Mandatory Party Id From and Party Id From Trans missing for invoice: " + currentInvoiceId); } else if (EntityQuery.use(delegator).from("Party") .where("partyId", invoice.get("partyIdFrom")).queryOne() == null) { newErrMsgs.add("Line number " + rec.getRecordNumber() + ": partyIdFrom: " + invoice.get("partyIdFrom") + " not found for invoice: " + currentInvoiceId); } if (UtilValidate.isEmpty(invoice.get("partyId"))) { newErrMsgs.add("Line number " + rec.getRecordNumber() + ": Mandatory Party Id and Party Id Trans missing for invoice: " + currentInvoiceId); } else if (EntityQuery.use(delegator).from("Party").where("partyId", invoice.get("partyId")) .queryOne() == null) { newErrMsgs.add("Line number " + rec.getRecordNumber() + ": partyId: " + invoice.get("partyId") + " not found for invoice: " + currentInvoiceId); } if (UtilValidate.isEmpty(invoice.get("invoiceTypeId"))) { newErrMsgs.add("Line number " + rec.getRecordNumber() + ": Mandatory Invoice Type missing for invoice: " + currentInvoiceId); } else if (EntityQuery.use(delegator).from("InvoiceType") .where("invoiceTypeId", invoice.get("invoiceTypeId")).queryOne() == null) { newErrMsgs.add("Line number " + rec.getRecordNumber() + ": InvoiceItem type id: " + invoice.get("invoiceTypeId") + " not found for invoice: " + currentInvoiceId); } GenericValue invoiceType = EntityQuery.use(delegator).from("InvoiceType") .where("invoiceTypeId", invoice.get("invoiceTypeId")).queryOne(); if ("PURCHASE_INVOICE".equals(invoiceType.getString("parentTypeId")) && !invoice.get("partyId").equals(organizationPartyId)) { newErrMsgs.add("Line number " + rec.getRecordNumber() + ": A purchase type invoice should have the partyId 'To' being the organizationPartyId(=" + organizationPartyId + ")! however is " + invoice.get("partyId") + "! invoice: " + currentInvoiceId); } if ("SALES_INVOICE".equals(invoiceType.getString("parentTypeId")) && !invoice.get("partyIdFrom").equals(organizationPartyId)) { newErrMsgs.add("Line number " + rec.getRecordNumber() + ": A sales type invoice should have the partyId 'from' being the organizationPartyId(=" + organizationPartyId + ")! however is " + invoice.get("partyIdFrom") + "! invoice: " + currentInvoiceId); } } catch (GenericEntityException e) { Debug.logError("Valication checking problem against database. due to " + e.getMessage(), module); } if (newErrMsgs.size() > 0) { errMsgs.addAll(newErrMsgs); } else { Map<String, Object> invoiceResult = null; try { invoiceResult = dispatcher.runSync("createInvoice", invoice); } catch (GenericServiceException e) { csvReader.close(); Debug.logError(e, module); return ServiceUtil.returnError(e.getMessage()); } newInvoiceId = (String) invoiceResult.get("invoiceId"); invoicesCreated++; } lastInvoiceId = currentInvoiceId; } if (newInvoiceId != null) { Map<String, Object> invoiceItem = UtilMisc.toMap("invoiceId", newInvoiceId, "invoiceItemSeqId", rec.get("invoiceItemSeqId"), "invoiceItemTypeId", rec.get("invoiceItemTypeId"), "productId", rec.get("productId"), "description", rec.get("itemDescription"), "amount", rec.get("amount"), "quantity", rec.get("quantity"), "userLogin", userLogin); if (UtilValidate.isNotEmpty(rec.get("productIdTrans"))) { invoiceItem.put("productId", rec.get("productIdTrans")); } // invoice item validation try { newErrMsgs = FastList.newInstance(); if (UtilValidate.isEmpty(invoiceItem.get("invoiceItemSeqId"))) { newErrMsgs.add("Line number " + rec.getRecordNumber() + ": Mandatory item sequence Id missing for invoice: " + currentInvoiceId); } if (UtilValidate.isEmpty(invoiceItem.get("invoiceItemTypeId"))) { newErrMsgs.add("Line number " + rec.getRecordNumber() + ": Mandatory invoice item type missing for invoice: " + currentInvoiceId); } else if (EntityQuery.use(delegator).from("InvoiceItemType") .where("invoiceItemTypeId", invoiceItem.get("invoiceItemTypeId")) .queryOne() == null) { newErrMsgs.add("Line number " + rec.getRecordNumber() + ": InvoiceItem Item type id: " + invoiceItem.get("invoiceItemTypeId") + " not found for invoice: " + currentInvoiceId + " Item seqId:" + invoiceItem.get("invoiceItemSeqId")); } if (UtilValidate.isEmpty(invoiceItem.get("productId")) && UtilValidate.isEmpty(invoiceItem.get("description"))) { } if (UtilValidate.isNotEmpty(invoiceItem.get("productId")) && EntityQuery.use(delegator).from("Product") .where("productId", invoiceItem.get("productId")).queryOne() == null) { newErrMsgs.add("Line number " + rec.getRecordNumber() + ": Product Id: " + invoiceItem.get("productId") + " not found for invoice: " + currentInvoiceId + " Item seqId:" + invoiceItem.get("invoiceItemSeqId")); } if (UtilValidate.isEmpty(invoiceItem.get("amount")) && UtilValidate.isEmpty(invoiceItem.get("quantity"))) { newErrMsgs.add("Line number " + rec.getRecordNumber() + ": Either or both quantity and amount is required for invoice: " + currentInvoiceId + " Item seqId:" + invoiceItem.get("invoiceItemSeqId")); } } catch (GenericEntityException e) { Debug.logError("Validation checking problem against database. due to " + e.getMessage(), module); } if (newErrMsgs.size() > 0) { errMsgs.addAll(newErrMsgs); } else { try { dispatcher.runSync("createInvoiceItem", invoiceItem); } catch (GenericServiceException e) { csvReader.close(); Debug.logError(e, module); return ServiceUtil.returnError(e.getMessage()); } } } } } catch (IOException e) { Debug.logError(e, module); return ServiceUtil.returnError(e.getMessage()); } if (errMsgs.size() > 0) { return ServiceUtil.returnError(errMsgs); } Map<String, Object> result = ServiceUtil.returnSuccess(invoicesCreated + " new invoice(s) created"); result.put("organizationPartyId", organizationPartyId); return result; }
From source file:org.ofbiz.magento.CatalogServices.java
public static Map<String, Object> importMagentoProducts(DispatchContext ctx, Map<String, ? extends Object> context) { Delegator delegator = ctx.getDelegator(); LocalDispatcher dispatcher = ctx.getDispatcher(); GenericValue userLogin = (GenericValue) context.get("userLogin"); Locale locale = (Locale) context.get("locale"); Map<String, Object> serviceResult = new HashMap<String, Object>(); int errorRecords = 0; int processedRecords = 0; try {//ww w . j a va2 s . c o m serviceResult = dispatcher.runSync("getMagentoProducts", context); if (ServiceUtil.isError(serviceResult)) { ServiceUtil.returnError( UtilProperties.getMessage(resource, "MagentoErrorInGettingProductsFromMagento", locale)); } serviceResult.clear(); File csvFile = new File(System.getProperty("ofbiz.home") + "/runtime/tmp/MagentoProductInfo.csv"); BufferedReader reader = new BufferedReader(new FileReader(csvFile)); String fieldDelimiter = ","; String fieldEncapsulator = "\""; CSVFormat csvFormat = CSVFormat.DEFAULT.withDelimiter(fieldDelimiter.charAt(0)) .withQuote(fieldEncapsulator.charAt(0)).withIgnoreEmptyLines(true) .withIgnoreSurroundingSpaces(true); CSVParser parser = new CSVParser(reader, csvFormat); Boolean isFirstLine = true; String[] mappedKeys = null; Map<String, Object> processedResult = new HashMap<String, Object>(); Map<String, Object> serviceResp = new HashMap<String, Object>(); for (CSVRecord csvRecord : csvFormat.parse(reader)) { int csvRecordSize = csvRecord.size(); if (isFirstLine) { mappedKeys = new String[csvRecordSize]; for (int i = 0; i < csvRecordSize; i++) { mappedKeys[i] = csvRecord.get(i); } isFirstLine = false; } else { Map<String, Object> mappedValues = new HashMap<String, Object>(); for (int i = 0; i < mappedKeys.length; i++) { String csvValue = csvRecord.get(i); String value = (i < csvRecordSize ? csvValue : ""); if (UtilValidate.isNotEmpty(value)) { value = value.trim().replaceAll(">|<", ""); } mappedValues.put(mappedKeys[i], value); } Map<String, Object> serviceInMap = UtilMisc.toMap("userLogin", userLogin, "productId", mappedValues.get("Product Id"), "externalId", mappedValues.get("Product Id"), "sku", mappedValues.get("SKU"), "productTypeId", mappedValues.get("Product Type Id"), "productName", mappedValues.get("Product Name"), "bundleParentId", mappedValues.get("Bundle Parent Id"), "configurableParentId", mappedValues.get("Configurable Parent Id"), "groupedParentId", mappedValues.get("Grouped Parent Id"), "description", mappedValues.get("Description"), "longDescription", mappedValues.get("Long Description"), "price", mappedValues.get("Price"), "taxClassId", mappedValues.get("Tax Class Id"), "categoryIds", mappedValues.get("Category Ids"), "webSiteIds", mappedValues.get("Web Site Ids"), "thumbnailImageUrl", mappedValues.get("Thumbnail Image Url"), "smallImageUrl", mappedValues.get("Small Image Url"), "originalImageUrl", mappedValues.get("Original Image Url"), "urlKey", mappedValues.get("Url Key"), "urlPath", mappedValues.get("Url Path"), "metaDescription", mappedValues.get("Meta Description"), "metaKeyword", mappedValues.get("Meta Keyword"), "metaTitle", mappedValues.get("Meta Title"), "status", mappedValues.get("Status"), "specialFromDate", mappedValues.get("Special From Date"), "specialPrice", mappedValues.get("Special Price"), "createdDate", mappedValues.get("Created At"), "lastModifiedDate", mappedValues.get("Updated At")); Boolean isError = false; if (UtilValidate.isEmpty(serviceInMap.get("productId"))) { isError = true; Debug.logError("Product ID is missing : ", module); } String productId = (String) serviceInMap.get("productId"); productId = "MAG-" + productId.trim(); serviceInMap.put("productId", productId); String bundleParentId = ((String) serviceInMap.get("bundleParentId")).trim(); if (!("NA".equalsIgnoreCase(bundleParentId))) { bundleParentId = "MAG-" + bundleParentId; } serviceInMap.put("bundleParentId", bundleParentId); String configurableParentId = ((String) serviceInMap.get("configurableParentId")).trim(); if (!("NA".equalsIgnoreCase(configurableParentId))) { configurableParentId = "MAG-" + configurableParentId; } serviceInMap.put("configurableParentId", configurableParentId); String groupedParentId = ((String) serviceInMap.get("groupedParentId")).trim(); if (!("NA".equalsIgnoreCase(groupedParentId))) { groupedParentId = "MAG-" + groupedParentId; } serviceInMap.put("groupedParentId", groupedParentId); if (UtilValidate.isEmpty(serviceInMap.get("productTypeId"))) { isError = true; Debug.logError("Product Type ID is missing for product id : " + productId, module); } if (UtilValidate.isEmpty(serviceInMap.get("productName"))) { isError = true; Debug.logError("Name is missing for product id : " + productId, module); } if (UtilValidate.isEmpty(serviceInMap.get("price"))) { isError = true; Debug.logError("Price is missing for product id : " + productId, module); } Debug.logInfo("Begin processing for productId [" + productId + "]", module); if (!isError) { Debug.logInfo("Create / Update product having productId [" + productId + "]", module); serviceResult = dispatcher.runSync("createMagentoProducts", serviceInMap, 600, true); } if (ServiceUtil.isError(serviceResult) || isError) { errorRecords++; processedResult.put(productId, "Error"); Debug.logInfo("Completed processing for productId [" + productId + "] with ERROR ", module); if (ServiceUtil.isError(serviceResult)) { Debug.logInfo(ServiceUtil.getErrorMessage(serviceResp), module); } } else { processedResult.put(productId, "Success"); Debug.logInfo("Processing successfully completed for product [" + productId + "]", module); } processedRecords++; } } } catch (GenericServiceException e) { Debug.logError(e, e.getMessage(), module); e.printStackTrace(); } catch (IOException e) { Debug.logError(e, e.getMessage(), module); e.printStackTrace(); } return ServiceUtil.returnSuccess(UtilProperties.getMessage(resource, "MagentoProductsHaveBeenImportedSuccessfully", UtilMisc.toMap("processedRecords", processedRecords, "successRecords", (processedRecords - errorRecords)), locale)); }