use of org.apache.commons.csv.CSVFormat in project jaqy by Teradata.
the class CSVExporterFactory method getHandler.
@Override
public JaqyExporter getHandler(CommandLine cmdLine, JaqyInterpreter interpreter) throws Exception {
Charset charset = DEFAULT_CHARSET;
CSVFormat format = CSVFormat.DEFAULT;
HashMap<Integer, CSVExportInfo> exportInfoMap = new HashMap<Integer, CSVExportInfo>();
CSVNameGen nameGen = new CSVNameGen(DEFAULT_NAME_PATTERN);
Charset encoding = DEFAULT_CHARSET;
for (Option option : cmdLine.getOptions()) {
switch(option.getOpt().charAt(0)) {
case 'c':
{
charset = Charset.forName(option.getValue());
break;
}
case 'd':
{
char delimiter = CSVUtils.getChar(option.getValue());
if (delimiter == 0)
throw new IllegalArgumentException("invalid delimiter: " + option.getValue());
format = format.withDelimiter(delimiter);
break;
}
case 't':
{
format = CSVUtils.getFormat(option.getValue());
break;
}
case 'n':
{
String fmt = option.getValue();
nameGen = new CSVNameGen(fmt);
// now check if the name is a valid format.
if (fmt.equals(nameGen.getName(1)))
interpreter.error("Invalid name pattern: " + fmt);
break;
}
case 'e':
{
encoding = Charset.forName(option.getValue());
break;
}
case 'f':
{
int column = Integer.parseInt(option.getValue());
if (column < 1) {
interpreter.error("Column index cannot be smaller than 1.");
}
CSVExportInfo info = new CSVExportInfo(nameGen, encoding);
exportInfoMap.put(column, info);
break;
}
}
}
String[] args = cmdLine.getArgs();
if (args.length == 0)
throw new IllegalArgumentException("missing file name.");
Path file = interpreter.getPath(args[0]);
return new CSVExporter(file, charset, format, exportInfoMap);
}
use of org.apache.commons.csv.CSVFormat in project jaqy by Teradata.
the class CSVImporterFactory method getHandler.
@Override
public CSVImporter getHandler(CommandLine cmdLine, JaqyInterpreter interpreter) throws Exception {
Charset charset = DEFAULT_CHARSET;
CSVFormat format = CSVFormat.DEFAULT;
HashMap<Integer, CSVImportInfo> importInfoMap = new HashMap<Integer, CSVImportInfo>();
boolean naFilter = false;
String[] naValues = null;
boolean precise = false;
// -1 indicates internal algorithm
long scanThreshold = -1;
Charset encoding = DEFAULT_CHARSET;
for (Option option : cmdLine.getOptions()) {
switch(option.getOpt().charAt(0)) {
case 'c':
{
charset = Charset.forName(option.getValue());
break;
}
case 'd':
{
char delimiter = CSVUtils.getChar(option.getValue());
if (delimiter == 0)
throw new IllegalArgumentException("invalid delimiter: " + option.getValue());
format = format.withDelimiter(delimiter);
break;
}
case 'h':
{
format = format.withHeader();
break;
}
case 't':
{
format = CSVUtils.getFormat(option.getValue());
break;
}
case 'f':
{
naFilter = true;
break;
}
case 'v':
{
naValues = option.getValue().split(",");
naFilter = true;
break;
}
case 'p':
{
precise = true;
break;
}
case 'r':
{
scanThreshold = Long.parseLong(option.getValue());
if (scanThreshold <= 0)
scanThreshold = 0;
break;
}
case 'e':
{
encoding = Charset.forName(option.getValue());
break;
}
case 'j':
{
int column = Integer.parseInt(option.getValue());
if (column < 1) {
interpreter.error("Column index cannot be smaller than 1.");
}
CSVImportInfo info = new CSVImportInfo(encoding);
importInfoMap.put(column - 1, info);
break;
}
case 'k':
{
int column = Integer.parseInt(option.getValue());
if (column < 1) {
interpreter.error("Column index cannot be smaller than 1.");
}
CSVImportInfo info = new CSVImportInfo(null);
importInfoMap.put(column - 1, info);
break;
}
}
}
String[] args = cmdLine.getArgs();
if (args.length == 0)
throw new IllegalArgumentException("missing file name.");
CSVImporter importer = new CSVImporter(interpreter.getPath(args[0]), charset, format, importInfoMap, precise, scanThreshold);
if (naFilter == true) {
importer.setNaFilter(true);
importer.setNaValues(naValues);
}
return importer;
}
use of org.apache.commons.csv.CSVFormat in project ofbiz-framework by apache.
the class InvoiceServices method importInvoice.
public static Map<String, Object> importInvoice(DispatchContext dctx, Map<String, Object> context) {
Locale locale = (Locale) context.get("locale");
Delegator delegator = dctx.getDelegator();
LocalDispatcher dispatcher = dctx.getDispatcher();
GenericValue userLogin = (GenericValue) context.get("userLogin");
ByteBuffer fileBytes = (ByteBuffer) context.get("uploadedFile");
if (fileBytes == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingUploadedFileDataNotFound", locale));
}
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 = new LinkedList<>();
List<String> newErrMsgs;
String lastInvoiceId = null;
String currentInvoiceId = null;
String newInvoiceId = null;
int invoicesCreated = 0;
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
newErrMsgs = new LinkedList<>();
try {
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);
}
Boolean isPurchaseInvoice = EntityTypeUtil.hasParentType(delegator, "InvoiceType", "invoiceTypeId", (String) invoice.get("invoiceTypeId"), "parentTypeId", "PURCHASE_INVOICE");
Boolean isSalesInvoice = EntityTypeUtil.hasParentType(delegator, "InvoiceType", "invoiceTypeId", (String) invoice.get("invoiceTypeId"), "parentTypeId", "SALES_INVOICE");
if (isPurchaseInvoice && !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 (isSalesInvoice && !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);
if (ServiceUtil.isError(invoiceResult)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(invoiceResult));
}
} 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
newErrMsgs = new LinkedList<>();
try {
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"))) {
newErrMsgs.add("Line number " + rec.getRecordNumber() + ": no Product Id given, no description given");
}
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 {
Map<String, Object> result = dispatcher.runSync("createInvoiceItem", invoiceItem);
if (ServiceUtil.isError(result)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result));
}
} 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(UtilProperties.getMessage(resource, "AccountingNewInvoicesCreated", UtilMisc.toMap("invoicesCreated", invoicesCreated), locale));
result.put("organizationPartyId", organizationPartyId);
return result;
}
use of org.apache.commons.csv.CSVFormat in project thingsboard by thingsboard.
the class CassandraDbHelper method dumpCfIfExists.
public static Path dumpCfIfExists(KeyspaceMetadata ks, Session session, String cfName, String[] columns, String[] defaultValues, String dumpPrefix, boolean printHeader) throws Exception {
if (ks.getTable(cfName) != null) {
Path dumpFile = Files.createTempFile(dumpPrefix, null);
Files.deleteIfExists(dumpFile);
CSVFormat csvFormat = CSV_DUMP_FORMAT;
if (printHeader) {
csvFormat = csvFormat.withHeader(columns);
}
try (CSVPrinter csvPrinter = new CSVPrinter(Files.newBufferedWriter(dumpFile), csvFormat)) {
Statement stmt = new SimpleStatement("SELECT * FROM " + cfName);
stmt.setFetchSize(1000);
ResultSet rs = session.execute(stmt);
Iterator<Row> iter = rs.iterator();
while (iter.hasNext()) {
Row row = iter.next();
if (row != null) {
dumpRow(row, columns, defaultValues, csvPrinter);
}
}
}
return dumpFile;
} else {
return null;
}
}
use of org.apache.commons.csv.CSVFormat in project midpoint by Evolveum.
the class CsvReportDataWriter method getStringData.
@Override
public String getStringData() {
try {
StringWriter stringWriter = new StringWriter();
CSVFormat csvFormat = createCsvFormat();
CSVPrinter printer = new CSVPrinter(stringWriter, csvFormat);
for (ExportedReportDataRow row : getDataRows()) {
printer.printRecord(createPhysicalColumnsList(row));
}
printer.flush();
return stringWriter.toString();
} catch (IOException e) {
throw new SystemException("Unexpected IOException: " + e.getMessage(), e);
}
}
Aggregations