List of usage examples for org.apache.commons.csv CSVFormat parse
public CSVParser parse(final Reader in) throws IOException
From source file:net.tradelib.core.Series.java
static public Series fromCsv(String path, boolean header, DateTimeFormatter dtf, LocalTime lt) throws Exception { if (dtf == null) { if (lt == null) dtf = DateTimeFormatter.ISO_LOCAL_DATE_TIME; else// www . j a v a2 s . c om dtf = DateTimeFormatter.ISO_DATE; } // Parse and import the csv CSVFormat csvFmt = CSVFormat.DEFAULT.withCommentMarker('#').withIgnoreSurroundingSpaces(); if (header) csvFmt = csvFmt.withHeader(); CSVParser csv = csvFmt.parse(new BufferedReader(new FileReader(path))); int ncols = -1; Series result = null; double[] values = null; for (CSVRecord rec : csv.getRecords()) { if (result == null) { ncols = rec.size() - 1; values = new double[ncols]; result = new Series(ncols); } for (int ii = 0; ii < ncols; ++ii) { values[ii] = Double.parseDouble(rec.get(ii + 1)); } LocalDateTime ldt; if (lt != null) { ldt = LocalDate.parse(rec.get(0), dtf).atTime(lt); } else { ldt = LocalDateTime.parse(rec.get(0), dtf); } result.append(ldt, values); } if (header) { Map<String, Integer> headerMap = csv.getHeaderMap(); result.clearNames(); for (Map.Entry<String, Integer> me : headerMap.entrySet()) { if (me.getValue() > 0) result.setName(me.getKey(), me.getValue() - 1); } } return result; }
From source file:notaql.engines.csv.CSVEngineEvaluator.java
/** * Evaluates the given transformation.//from w w w .java2 s. com * * This first parses the document (with the first line being the header) and then evaluates on our framework. * * TODO: this assumes a header line. It might happen that it is not provided. * * @param transformation * @return */ @Override public JavaRDD<ObjectValue> evaluate(Transformation transformation) { final SparkTransformationEvaluator evaluator = new SparkTransformationEvaluator(transformation); final JavaSparkContext sc = NotaQL.SparkFactory.getSparkContext(); final CSVFormat format = CSVFormat.DEFAULT; final JavaRDD<String> csv = sc.textFile(path); final String first = csv.first(); final CSVRecord header; try { header = format.parse(new StringReader(first)).iterator().next(); } catch (IOException e) { e.printStackTrace(); throw new AssertionError("Header could not be read for some reason."); } String[] headerCols = new String[header.size()]; for (int i = 0; i < header.size(); i++) { headerCols[i] = header.get(i); } final CSVFormat headerFormat = CSVFormat.DEFAULT.withHeader(headerCols); final JavaRDD<CSVRecord> records = csv.filter(f -> !f.equals(first)) .map(line -> headerFormat.parse(new StringReader(line)).iterator().next()); final JavaRDD<Value> converted = records.map(ValueConverter::convertToNotaQL); final JavaRDD<Value> filtered = converted.filter(o -> transformation.satisfiesInPredicate((ObjectValue) o)); return evaluator.process(filtered); }
From source file:org.apache.batchee.csv.CommonsCsvReader.java
@Override public void open(final Serializable checkpoint) throws Exception { final CSVFormat csvFormat = newFormat(); parser = csvFormat.parse(newReader()); iterator = new IteratorReader<CSVRecord>(parser.iterator()); mapperInstance = mapper == null/*from w w w . j a v a 2 s . c o m*/ ? new BeanLocator.LocatorInstance<CsvReaderMapper>(mapping != null ? new DefaultMapper(Thread.currentThread().getContextClassLoader().loadClass(mapping)) : NOOP_MAPPER, null) : BeanLocator.Finder.get(locator).newInstance(CsvReaderMapper.class, mapper); super.open(checkpoint); }
From source file:org.apache.beam.sdk.extensions.sql.impl.schema.BeamTableUtils.java
public static BeamRecord csvLine2BeamSqlRow(CSVFormat csvFormat, String line, BeamRecordSqlType beamRecordSqlType) { List<Object> fieldsValue = new ArrayList<>(beamRecordSqlType.getFieldCount()); try (StringReader reader = new StringReader(line)) { CSVParser parser = csvFormat.parse(reader); CSVRecord rawRecord = parser.getRecords().get(0); if (rawRecord.size() != beamRecordSqlType.getFieldCount()) { throw new IllegalArgumentException(String.format("Expect %d fields, but actually %d", beamRecordSqlType.getFieldCount(), rawRecord.size())); } else {//from www . ja v a 2 s . co m for (int idx = 0; idx < beamRecordSqlType.getFieldCount(); idx++) { String raw = rawRecord.get(idx); fieldsValue.add(autoCastField(beamRecordSqlType.getFieldTypeByIndex(idx), raw)); } } } catch (IOException e) { throw new IllegalArgumentException("decodeRecord failed!", e); } return new BeamRecord(beamRecordSqlType, fieldsValue); }
From source file:org.apache.nifi.processors.csv.ExtractCSVHeader.java
@Override public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException { final FlowFile original = session.get(); if (original == null) { return;/*from ww w . j a va 2 s . c o m*/ } final AtomicBoolean lineFound = new AtomicBoolean(false); final Map<String, String> attrs = new HashMap<>(); final AtomicInteger headerLength = new AtomicInteger(0); session.read(original, new InputStreamCallback() { @Override public void process(InputStream inputStream) throws IOException { // TODO expose the charset property? LineIterator iterator = IOUtils.lineIterator(inputStream, UTF_8); if (iterator.hasNext()) { lineFound.set(true); final String header = iterator.nextLine(); final String format = context.getProperty(PROP_FORMAT).getValue(); final String delimiter = context.getProperty(PROP_DELIMITER) .evaluateAttributeExpressions(original).getValue(); final String prefix = context.getProperty(PROP_SCHEMA_ATTR_PREFIX) .evaluateAttributeExpressions(original).getValue(); attrs.put(prefix + ATTR_HEADER_ORIGINAL, header); // TODO validate delimiter in the callback first final CSVFormat csvFormat = buildFormat(format, delimiter, true, // we assume first line is the header null); // no custom header final CSVParser parser = csvFormat.parse(new StringReader(header)); final Map<String, Integer> headers = parser.getHeaderMap(); final int columnCount = headers.size(); attrs.put(prefix + ATTR_HEADER_COLUMN_COUNT, String.valueOf(columnCount)); for (Map.Entry<String, Integer> h : headers.entrySet()) { // CSV columns are 1-based in Excel attrs.put(prefix + (h.getValue() + 1), h.getKey()); } // strip the header and send to the 'content' relationship if (StringUtils.isNotBlank(header)) { int hLength = header.length(); // move past the new line if there are more lines if (original.getSize() > hLength + 1) { hLength++; } headerLength.set(hLength); } } } }); if (lineFound.get()) { FlowFile ff = session.putAllAttributes(original, attrs); int offset = headerLength.get(); if (offset > 0) { FlowFile contentOnly = session.clone(ff, offset, original.getSize() - offset); session.transfer(contentOnly, REL_CONTENT); } session.transfer(ff, REL_ORIGINAL); } else { session.transfer(original, REL_FAILURE); } }
From source file:org.apache.nifi.processors.csv.ParseCSVRecord.java
@Override public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException { final FlowFile original = session.get(); if (original == null) { return;//from ww w. j a va2 s.c o m } final AtomicBoolean lineFound = new AtomicBoolean(false); final Map<String, String> outputAttrs = new HashMap<>(); session.read(original, new InputStreamCallback() { @Override public void process(InputStream inputStream) throws IOException { final String fromAttribute = context.getProperty(PROP_RECORD_FROM_ATTRIBUTE).getValue(); String unparsedRecord; // data source is the attribute if (StringUtils.isNotBlank(fromAttribute)) { unparsedRecord = original.getAttribute(fromAttribute); if (StringUtils.isBlank(unparsedRecord)) { // will be routed to failure at the end of the method implementation return; } } else { // data source is the content // TODO expose the charset property? LineIterator iterator = IOUtils.lineIterator(inputStream, UTF_8); if (!iterator.hasNext()) { return; } unparsedRecord = iterator.next(); } lineFound.set(true); final String format = context.getProperty(PROP_FORMAT).getValue(); final String delimiter = context.getProperty(PROP_DELIMITER).evaluateAttributeExpressions(original) .getValue(); final String schemaPrefix = context.getProperty(PROP_SCHEMA_ATTR_PREFIX) .evaluateAttributeExpressions(original).getValue(); final String valuePrefix = context.getProperty(PROP_VALUE_ATTR_PREFIX) .evaluateAttributeExpressions(original).getValue(); final boolean trimValues = context.getProperty(PROP_TRIM_VALUES).asBoolean(); final CSVFormat csvFormat = buildFormat(format, delimiter, false, // this is a payload, not header anymore null); // no custom header final CSVParser parser = csvFormat.parse(new StringReader(unparsedRecord)); List<CSVRecord> records = parser.getRecords(); if (records.size() > 1) { // TODO revisit for NiFi's native micro-batching throw new ProcessException("Multi-line entries not supported"); } CSVRecord record = records.get(0); Map<String, String> originalAttrs = original.getAttributes(); // filter delimited schema attributes only Map<String, String> schemaAttrs = new HashMap<>(); for (String key : originalAttrs.keySet()) { if (key.startsWith(schemaPrefix)) { schemaAttrs.put(key, originalAttrs.get(key)); } } // put key/value pairs into attributes for (int i = 0; i < record.size(); i++) { String columnName = schemaAttrs.get(schemaPrefix + (i + 1)); // 1-based column numbering if (columnName == null) { // 1-based column index columnName = String.valueOf(i + 1); } // TODO indexed schemaless parsing vs auto-schema vs user-provided schema String columnValue = record.get(i); if (trimValues) { columnValue = columnValue.trim(); } String attrName = (StringUtils.isBlank(valuePrefix) ? "delimited.column." : valuePrefix) + columnName; outputAttrs.put(attrName, columnValue); } } }); if (lineFound.get()) { FlowFile ff = session.putAllAttributes(original, outputAttrs); session.transfer(ff, REL_SUCCESS); } else { session.transfer(original, REL_FAILURE); } }
From source file:org.apache.ofbiz.accounting.invoice.InvoiceServices.java
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"); 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<String>(); List<String> newErrMsgs = new LinkedList<String>(); String lastInvoiceId = null;//from ww w. j a v a 2 s . c om String currentInvoiceId = null; String newInvoiceId = null; int invoicesCreated = 0; if (fileBytes == null) { return ServiceUtil .returnError(UtilProperties.getMessage(resource, "AccountingUploadedFileDataNotFound", locale)); } 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 = new LinkedList<String>(); 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); } 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 = new LinkedList<String>(); 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(UtilProperties.getMessage(resource, "AccountingNewInvoicesCreated", UtilMisc.toMap("invoicesCreated", invoicesCreated), locale)); result.put("organizationPartyId", organizationPartyId); return result; }
From source file:org.apache.ofbiz.party.party.PartyServices.java
public static Map<String, Object> importParty(DispatchContext dctx, Map<String, Object> context) { Delegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); Locale locale = (Locale) context.get("locale"); GenericValue userLogin = (GenericValue) context.get("userLogin"); ByteBuffer fileBytes = (ByteBuffer) context.get("uploadedFile"); 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<String>(); List<String> newErrMsgs = new LinkedList<String>(); String lastPartyId = null; // last partyId read from the csv file String currentPartyId = null; // current partyId from the csv file String newPartyId = null; // new to create/update partyId in the system String newCompanyPartyId = null; int partiesCreated = 0; Map<String, Object> result = null; String newContactMechId = null; String currentContactMechTypeId = null; String lastAddress1 = null;// w ww .j a v a2 s .co m String lastAddress2 = null; String lastCity = null; String lastCountryGeoId = null; String lastEmailAddress = null; String lastCountryCode = null; String lastAreaCode = null; String lastContactNumber = null; String lastContactMechPurposeTypeId = null; String currentContactMechPurposeTypeId = null; Boolean addParty = false; // when modify party, contact mech not added again if (fileBytes == null) { return ServiceUtil .returnError(UtilProperties.getMessage(resourceError, "PartyUploadedFileDataNotFound", locale)); } try { for (final CSVRecord rec : fmt.parse(csvReader)) { if (UtilValidate.isNotEmpty(rec.get("partyId"))) { currentPartyId = rec.get("partyId"); } if (lastPartyId == null || !currentPartyId.equals(lastPartyId)) { newPartyId = null; currentContactMechPurposeTypeId = null; lastAddress1 = null; lastAddress2 = null; lastCity = null; lastCountryGeoId = null; lastEmailAddress = null; lastCountryCode = null; lastAreaCode = null; lastContactNumber = null; // party validation List<GenericValue> currencyCheck = EntityQuery.use(delegator).from("Uom").where("abbreviation", rec.get("preferredCurrencyUomId"), "uomTypeId", "CURRENCY_MEASURE").queryList(); if (UtilValidate.isNotEmpty(rec.get("preferredCurrencyUomId")) && currencyCheck.size() == 0) { newErrMsgs.add("Line number " + rec.getRecordNumber() + ": partyId: " + currentPartyId + "Currency code not found for: " + rec.get("preferredCurrencyUomId")); } if (UtilValidate.isEmpty(rec.get("roleTypeId"))) { newErrMsgs.add("Line number " + rec.getRecordNumber() + ": Mandatory roletype is missing, possible values: CUSTOMER, SUPPLIER, EMPLOYEE and more...."); } else if (EntityQuery.use(delegator).from("RoleType") .where("roleTypeId", rec.get("roleTypeId")).queryOne() == null) { newErrMsgs.add("Line number " + rec.getRecordNumber() + ": RoletypeId is not valid: " + rec.get("roleTypeId")); } if (UtilValidate.isNotEmpty(rec.get("contactMechTypeId")) && EntityQuery.use(delegator) .from("ContactMechType").where("contactMechTypeId", rec.get("contactMechTypeId")) .cache().queryOne() == null) { newErrMsgs.add("Line number " + rec.getRecordNumber() + ": partyId: " + currentPartyId + " contactMechTypeId code not found for: " + rec.get("contactMechTypeId")); } if (UtilValidate.isNotEmpty(rec.get("contactMechPurposeTypeId")) && EntityQuery.use(delegator).from("ContactMechPurposeType") .where("contactMechPurposeTypeId", rec.get("contactMechPurposeTypeId")).cache() .queryOne() == null) { newErrMsgs.add("Line number " + rec.getRecordNumber() + ": partyId: " + currentPartyId + "contactMechPurposeTypeId code not found for: " + rec.get("contactMechPurposeTypeId")); } if (UtilValidate.isNotEmpty(rec.get("contactMechTypeId")) && "POSTAL_ADDRESS".equals(rec.get("contactMechTypeId"))) { if (UtilValidate.isEmpty(rec.get("countryGeoId"))) { newErrMsgs.add("Line number " + rec.getRecordNumber() + ": partyId: " + currentPartyId + "Country code missing"); } else { List<GenericValue> countryCheck = EntityQuery.use(delegator).from("Geo") .where("geoTypeId", "COUNTRY", "abbreviation", rec.get("countryGeoId")) .queryList(); if (countryCheck.size() == 0) { newErrMsgs.add("Line number " + rec.getRecordNumber() + " partyId: " + currentPartyId + " Invalid Country code: " + rec.get("countryGeoId")); } } if (UtilValidate.isEmpty(rec.get("city"))) { newErrMsgs.add("Line number " + rec.getRecordNumber() + " partyId: " + currentPartyId + "City name is missing"); } if (UtilValidate.isNotEmpty(rec.get("stateProvinceGeoId"))) { List<GenericValue> stateCheck = EntityQuery.use(delegator).from("Geo") .where("geoTypeId", "STATE", "abbreviation", rec.get("stateProvinceGeoId")) .queryList(); if (stateCheck.size() == 0) { newErrMsgs .add("Line number " + rec.getRecordNumber() + " partyId: " + currentPartyId + " Invalid stateProvinceGeoId code: " + rec.get("countryGeoId")); } } } if (UtilValidate.isNotEmpty(rec.get("contactMechTypeId")) && "TELECOM_NUMBER".equals(rec.get("contactMechTypeId"))) { if (UtilValidate.isEmpty(rec.get("telAreaCode")) && UtilValidate.isEmpty(rec.get("telAreaCode"))) { newErrMsgs.add("Line number " + rec.getRecordNumber() + " partyId: " + currentPartyId + " telephone number missing"); } } if (UtilValidate.isNotEmpty(rec.get("contactMechTypeId")) && "EMAIL_ADDRESS".equals(rec.get("contactMechTypeId"))) { if (UtilValidate.isEmpty(rec.get("emailAddress"))) { newErrMsgs.add("Line number " + rec.getRecordNumber() + " partyId: " + currentPartyId + " email address missing"); } } if (errMsgs.size() == 0) { List<GenericValue> partyCheck = EntityQuery.use(delegator).from("PartyIdentification") .where("partyIdentificationTypeId", "PARTY_IMPORT", "idValue", rec.get("partyId")) .queryList(); addParty = partyCheck.size() == 0; if (!addParty) { // update party newPartyId = EntityUtil.getFirst(partyCheck).getString("partyId"); if (UtilValidate.isNotEmpty(rec.get("groupName"))) { Map<String, Object> partyGroup = UtilMisc.toMap("partyId", newPartyId, "preferredCurrencyUomId", rec.get("preferredCurrencyUomId"), "groupName", rec.get("groupName"), "userLogin", userLogin); result = dispatcher.runSync("updatePartyGroup", partyGroup); } else { // person Map<String, Object> person = UtilMisc.toMap("partyId", newPartyId, "firstName", rec.get("firstName"), "middleName", rec.get("middleName"), "lastName", rec.get("lastName"), "preferredCurrencyUomId", rec.get("preferredCurrencyUomId"), "userLogin", userLogin); result = dispatcher.runSync("updatePerson", person); } } else { // create new party if (UtilValidate.isNotEmpty(rec.get("groupName"))) { Map<String, Object> partyGroup = UtilMisc.toMap("preferredCurrencyUomId", rec.get("preferredCurrencyUomId"), "groupName", rec.get("groupName"), "userLogin", userLogin, "statusId", "PARTY_ENABLED"); result = dispatcher.runSync("createPartyGroup", partyGroup); } else { // person Map<String, Object> person = UtilMisc.toMap("firstName", rec.get("firstName"), "middleName", rec.get("middleName"), "lastName", rec.get("lastName"), "preferredCurrencyUomId", rec.get("preferredCurrencyUomId"), "statusId", "PARTY_ENABLED", "userLogin", userLogin); result = dispatcher.runSync("createPerson", person); } newPartyId = (String) result.get("partyId"); Map<String, Object> partyIdentification = UtilMisc.toMap("partyId", newPartyId, "partyIdentificationTypeId", "PARTY_IMPORT", "idValue", rec.get("partyId"), "userLogin", userLogin); result = dispatcher.runSync("createPartyIdentification", partyIdentification); Map<String, Object> partyRole = UtilMisc.toMap("partyId", newPartyId, "roleTypeId", rec.get("roleTypeId"), "userLogin", userLogin); dispatcher.runSync("createPartyRole", partyRole); if (UtilValidate.isNotEmpty(rec.get("companyPartyId"))) { List<GenericValue> companyCheck = EntityQuery.use(delegator) .from("PartyIdentification").where("partyIdentificationTypeId", "PARTY_IMPORT", "idValue", rec.get("partyId")) .queryList(); if (companyCheck.size() == 0) { // update party group // company does not exist so create Map<String, Object> companyPartyGroup = UtilMisc.toMap("partyId", newCompanyPartyId, "statusId", "PARTY_ENABLED", "userLogin", userLogin); result = dispatcher.runSync("createPartyGroup", companyPartyGroup); newCompanyPartyId = (String) result.get("partyId"); } else { newCompanyPartyId = EntityUtil.getFirst(companyCheck).getString("partyId"); } Map<String, Object> companyRole = UtilMisc.toMap("partyId", newCompanyPartyId, "roleTypeId", "ACCOUNT", "userLogin", userLogin); dispatcher.runSync("createPartyRole", companyRole); // company exist, so create link Map<String, Object> partyRelationship = UtilMisc.toMap("partyIdTo", newPartyId, "partyIdFrom", newCompanyPartyId, "roleTypeIdFrom", "ACCOUNT", "partyRelationshipTypeId", "EMPLOYMENT", "userLogin", userLogin); result = dispatcher.runSync("createPartyRelationship", partyRelationship); } } Debug.logInfo(" =========================================================party created id: " + newPartyId, module); partiesCreated++; } else { errMsgs.addAll(newErrMsgs); newErrMsgs = new LinkedList<String>(); } } currentContactMechTypeId = rec.get("contactMechTypeId"); currentContactMechPurposeTypeId = rec.get("contactMechPurposeTypeId"); // party correctly created (not updated) and contactMechtype provided? if (newPartyId != null && addParty && UtilValidate.isNotEmpty(currentContactMechTypeId)) { // fill maps and check changes Map<String, Object> emailAddress = UtilMisc.toMap("contactMechTypeId", "EMAIL_ADDRESS", "userLogin", userLogin); Boolean emailAddressChanged = false; if ("EMAIL_ADDRESS".equals(currentContactMechTypeId)) { emailAddress.put("infoString", rec.get("emailAddress")); emailAddressChanged = lastEmailAddress == null || !lastEmailAddress.equals(rec.get("emailAddress")); lastEmailAddress = rec.get("emailAddress"); } Map<String, Object> postalAddress = UtilMisc.toMap("userLogin", (Object) userLogin); // casting is here necessary for some compiler versions Boolean postalAddressChanged = false; if ("POSTAL_ADDRESS".equals(currentContactMechTypeId)) { postalAddress.put("address1", rec.get("address1")); postalAddress.put("address2", rec.get("address2")); postalAddress.put("city", rec.get("city")); postalAddress.put("stateProvinceGeoId", rec.get("stateProvinceGeoId")); postalAddress.put("countryGeoId", rec.get("countryGeoId")); postalAddress.put("postalCode", rec.get("postalCode")); postalAddressChanged = lastAddress1 == null || !lastAddress1.equals(postalAddress.get("address1")) || lastAddress2 == null || !lastAddress2.equals(postalAddress.get("address2")) || lastCity == null || !lastCity.equals(postalAddress.get("city")) || lastCountryGeoId == null || !lastCountryGeoId.equals(postalAddress.get("countryGeoId")); lastAddress1 = (String) postalAddress.get("address1"); lastAddress2 = (String) postalAddress.get("address2"); lastCity = (String) postalAddress.get("city"); lastCountryGeoId = (String) postalAddress.get("countryGeoId"); } Map<String, Object> telecomNumber = UtilMisc.toMap("userLogin", (Object) userLogin); // casting is here necessary for some compiler versions Boolean telecomNumberChanged = false; if ("TELECOM_NUMBER".equals(currentContactMechTypeId)) { telecomNumber.put("countryCode", rec.get("telCountryCode")); telecomNumber.put("areaCode", rec.get("telAreaCode")); telecomNumber.put("contactNumber", rec.get("telContactNumber")); telecomNumberChanged = lastCountryCode == null || !lastCountryCode.equals(telecomNumber.get("countryCode")) || lastAreaCode == null || !lastAreaCode.equals(telecomNumber.get("areaCode")) || lastContactNumber == null || !lastContactNumber.equals(telecomNumber.get("contactNumber")); lastCountryCode = (String) telecomNumber.get("countryCode"); lastAreaCode = (String) telecomNumber.get("areaCode"); lastContactNumber = (String) telecomNumber.get("contactNumber"); } Map<String, Object> partyContactMechPurpose = UtilMisc.toMap("partyId", newPartyId, "userLogin", userLogin); Boolean partyContactMechPurposeChanged = false; currentContactMechPurposeTypeId = rec.get("contactMechPurposeTypeId"); if (currentContactMechPurposeTypeId != null && ("TELECOM_NUMBER".equals(currentContactMechTypeId) || "POSTAL_ADDRESS".equals(currentContactMechTypeId) || "EMAIL_ADDRESS".equals(currentContactMechTypeId))) { partyContactMechPurpose.put("contactMechPurposeTypeId", currentContactMechPurposeTypeId); partyContactMechPurposeChanged = (lastContactMechPurposeTypeId == null || !lastContactMechPurposeTypeId.equals(currentContactMechPurposeTypeId)) && !telecomNumberChanged && !postalAddressChanged && !emailAddressChanged; Debug.logInfo( "===================================last:" + lastContactMechPurposeTypeId + " current: " + currentContactMechPurposeTypeId + " t :" + telecomNumberChanged + " p: " + postalAddressChanged + " e: " + emailAddressChanged + " result: " + partyContactMechPurposeChanged, module); } lastContactMechPurposeTypeId = currentContactMechPurposeTypeId; // update if (errMsgs.size() == 0) { if (postalAddressChanged) { result = dispatcher.runSync("createPostalAddress", postalAddress); newContactMechId = (String) result.get("contactMechId"); if (currentContactMechPurposeTypeId == null) { currentContactMechPurposeTypeId = "GENERAL_LOCATION"; } dispatcher.runSync("createPartyContactMech", UtilMisc.toMap("partyId", newPartyId, "contactMechId", newContactMechId, "contactMechPurposeTypeId", currentContactMechPurposeTypeId, "userLogin", userLogin)); } if (telecomNumberChanged) { result = dispatcher.runSync("createTelecomNumber", telecomNumber); newContactMechId = (String) result.get("contactMechId"); if (currentContactMechPurposeTypeId == null) { currentContactMechPurposeTypeId = "PHONE_WORK"; } dispatcher.runSync("createPartyContactMech", UtilMisc.toMap("partyId", newPartyId, "contactMechId", newContactMechId, "contactMechPurposeTypeId", currentContactMechPurposeTypeId, "userLogin", userLogin)); } if (emailAddressChanged) { result = dispatcher.runSync("createContactMech", emailAddress); newContactMechId = (String) result.get("contactMechId"); if (currentContactMechPurposeTypeId == null) { currentContactMechPurposeTypeId = "PRIMARY_EMAIL"; } dispatcher.runSync("createPartyContactMech", UtilMisc.toMap("partyId", newPartyId, "contactMechId", newContactMechId, "contactMechPurposeTypeId", currentContactMechPurposeTypeId, "userLogin", userLogin)); } if (partyContactMechPurposeChanged) { partyContactMechPurpose.put("contactMechId", newContactMechId); result = dispatcher.runSync("createPartyContactMechPurpose", partyContactMechPurpose); } lastPartyId = currentPartyId; errMsgs.addAll(newErrMsgs); newErrMsgs = new LinkedList<String>(); } } } } catch (GenericServiceException e) { Debug.logError(e, module); return ServiceUtil.returnError(e.getMessage()); } catch (GenericEntityException e) { 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); } result = ServiceUtil.returnSuccess(UtilProperties.getMessage(resource, "PartyNewPartiesCreated", UtilMisc.toMap("partiesCreated", partiesCreated), locale)); return result; }
From source file:org.asqatasun.referential.creator.CodeGeneratorMojo.java
/** * * @return/*from w w w .jav a 2 s. c o m*/ */ private Iterable<CSVRecord> getCsv() { // we parse the csv file to extract the first line and get the headers LineIterator lineIterator; try { lineIterator = FileUtils.lineIterator(dataFile, Charset.defaultCharset().name()); } catch (IOException ex) { Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ex); lineIterator = null; } String[] csvHeaders = lineIterator != null ? lineIterator.next().split(String.valueOf(delimiter)) : new String[0]; isCriterionPresent = extractCriterionFromCsvHeader(csvHeaders); try { extractAvailableLangsFromCsvHeader(csvHeaders); } catch (I18NLanguageNotFoundException ex) { Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ex); return null; } // from here we just add each line to a build to re-create the csv content // without the first line. StringBuilder strb = new StringBuilder(); while (lineIterator.hasNext()) { strb.append(lineIterator.next()); strb.append("\n"); } Reader in; try { in = new StringReader(strb.toString()); CSVFormat csvf = CSVFormat.newFormat(delimiter).withHeader(csvHeaders); return csvf.parse(in); } catch (FileNotFoundException ex) { Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ex); return null; } catch (IOException ex) { Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ex); return null; } }
From source file:org.asqatasun.rules.doc.utils.exportaw22torgaa3ruledesign.ExtractCsvAndCopy.java
public Iterable<CSVRecord> getCsv() throws IOException { // we parse the csv file to extract the first line and get the headers LineIterator lineIterator;//from w ww. j av a2 s .c o m lineIterator = FileUtils.lineIterator(DATA_FILE); csvHeaders = lineIterator.next().split(String.valueOf(DELIMITER)); StringBuilder strb = new StringBuilder(); while (lineIterator.hasNext()) { strb.append(lineIterator.next()); strb.append("\n"); } Reader in; try { in = new StringReader(strb.toString()); CSVFormat csvf = CSVFormat.newFormat(DELIMITER).withHeader(csvHeaders); return csvf.parse(in); } catch (FileNotFoundException ex) { return null; } catch (IOException ex) { return null; } }