Example usage for org.apache.commons.csv CSVFormat DEFAULT

List of usage examples for org.apache.commons.csv CSVFormat DEFAULT

Introduction

In this page you can find the example usage for org.apache.commons.csv CSVFormat DEFAULT.

Prototype

CSVFormat DEFAULT

To view the source code for org.apache.commons.csv CSVFormat DEFAULT.

Click Source Link

Document

Standard comma separated format, as for #RFC4180 but allowing empty lines.

Usage

From source file:org.apache.nifi.csv.TestWriteCSVResult.java

@Test
public void testMissingFieldWriteRecord() throws IOException {
    final CSVFormat csvFormat = CSVFormat.DEFAULT.withEscape('\\').withQuoteMode(QuoteMode.NONE)
            .withRecordSeparator("\n");
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("id", RecordFieldType.STRING.getDataType()));
    fields.add(new RecordField("name", RecordFieldType.STRING.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);

    final Map<String, Object> values = new LinkedHashMap<>();
    values.put("id", "1");
    final Record record = new MapRecord(schema, values);

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final String output;
    try (final WriteCSVResult writer = new WriteCSVResult(csvFormat, schema, new SchemaNameAsAttribute(), baos,
            RecordFieldType.DATE.getDefaultFormat(), RecordFieldType.TIME.getDefaultFormat(),
            RecordFieldType.TIMESTAMP.getDefaultFormat(), true, "ASCII")) {

        writer.beginRecordSet();//  w  ww  .  j a v a  2 s.  c o m
        writer.writeRecord(record);
        writer.finishRecordSet();
        writer.flush();
        output = baos.toString();
    }

    assertEquals("id,name\n1,\n", output);
}

From source file:org.apache.nifi.csv.TestWriteCSVResult.java

@Test
public void testMissingFieldWriteRawRecord() throws IOException {
    final CSVFormat csvFormat = CSVFormat.DEFAULT.withEscape('\\').withQuoteMode(QuoteMode.NONE)
            .withRecordSeparator("\n");
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("id", RecordFieldType.STRING.getDataType()));
    fields.add(new RecordField("name", RecordFieldType.STRING.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);

    final Map<String, Object> values = new LinkedHashMap<>();
    values.put("id", "1");
    final Record record = new MapRecord(schema, values);

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final String output;
    try (final WriteCSVResult writer = new WriteCSVResult(csvFormat, schema, new SchemaNameAsAttribute(), baos,
            RecordFieldType.DATE.getDefaultFormat(), RecordFieldType.TIME.getDefaultFormat(),
            RecordFieldType.TIMESTAMP.getDefaultFormat(), true, "ASCII")) {

        writer.beginRecordSet();/*from  www . j av a 2 s. co  m*/
        writer.writeRawRecord(record);
        writer.finishRecordSet();
        writer.flush();
        output = baos.toString();
    }

    assertEquals("id,name\n1,\n", output);
}

From source file:org.apache.nifi.csv.TestWriteCSVResult.java

@Test
public void testMissingAndExtraFieldWriteRecord() throws IOException {
    final CSVFormat csvFormat = CSVFormat.DEFAULT.withEscape('\\').withQuoteMode(QuoteMode.NONE)
            .withRecordSeparator("\n");
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("id", RecordFieldType.STRING.getDataType()));
    fields.add(new RecordField("name", RecordFieldType.STRING.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);

    final Map<String, Object> values = new LinkedHashMap<>();
    values.put("id", "1");
    values.put("dob", "1/1/1970");
    final Record record = new MapRecord(schema, values);

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final String output;
    try (final WriteCSVResult writer = new WriteCSVResult(csvFormat, schema, new SchemaNameAsAttribute(), baos,
            RecordFieldType.DATE.getDefaultFormat(), RecordFieldType.TIME.getDefaultFormat(),
            RecordFieldType.TIMESTAMP.getDefaultFormat(), true, "ASCII")) {

        writer.beginRecordSet();/*from  w w w .  j a va 2s .  c  om*/
        writer.writeRecord(record);
        writer.finishRecordSet();
        writer.flush();
        output = baos.toString();
    }

    assertEquals("id,name\n1,\n", output);
}

From source file:org.apache.nifi.csv.TestWriteCSVResult.java

@Test
public void testMissingAndExtraFieldWriteRawRecord() throws IOException {
    final CSVFormat csvFormat = CSVFormat.DEFAULT.withEscape('\\').withQuoteMode(QuoteMode.NONE)
            .withRecordSeparator("\n");
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("id", RecordFieldType.STRING.getDataType()));
    fields.add(new RecordField("name", RecordFieldType.STRING.getDataType()));
    final RecordSchema schema = new SimpleRecordSchema(fields);

    final Map<String, Object> values = new LinkedHashMap<>();
    values.put("id", "1");
    values.put("dob", "1/1/1970");
    final Record record = new MapRecord(schema, values);

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final String output;
    try (final WriteCSVResult writer = new WriteCSVResult(csvFormat, schema, new SchemaNameAsAttribute(), baos,
            RecordFieldType.DATE.getDefaultFormat(), RecordFieldType.TIME.getDefaultFormat(),
            RecordFieldType.TIMESTAMP.getDefaultFormat(), true, "ASCII")) {

        writer.beginRecordSet();// w  w w  .  jav a2 s  .  co m
        writer.writeRawRecord(record);
        writer.finishRecordSet();
        writer.flush();
        output = baos.toString();
    }

    assertEquals("id,dob,name\n1,1/1/1970,\n", output);
}

From source file:org.apache.nifi.processors.csv.CsvToJson.java

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    FlowFile flowFile = session.get();/*from w  ww  .j  av  a2  s .  c o  m*/
    if (flowFile == null) {
        return;
    }

    final ObjectHolder<String> contentHolder = new ObjectHolder<>(null);

    session.read(flowFile, new InputStreamCallback() {
        @Override
        public void process(InputStream inputStream) throws IOException {
            contentHolder.set(IOUtils.toString(inputStream));
        }
    });

    CSVFormat csvFileFormat = CSVFormat.DEFAULT.withHeader(headers);

    try {
        CSVParser csvParser = new CSVParser(new StringReader(contentHolder.get()), csvFileFormat);

        List<CSVRecord> records = csvParser.getRecords();
        if (records.size() == 0) {
            getLogger().error("No records found");
            session.transfer(flowFile, FAILURE);
        } else if (records.size() > 1) {
            getLogger().error("More than one record found");
            session.transfer(flowFile, FAILURE);
        } else {
            final CSVRecord record = records.get(0);

            flowFile = session.write(flowFile, new OutputStreamCallback() {
                @Override
                public void process(OutputStream outputStream) throws IOException {
                    try (JsonGenerator generator = jsonFactory.createJsonGenerator(outputStream)) {
                        generator.writeStartObject();

                        Map<String, String> recordMap = record.toMap();
                        for (Map.Entry<String, String> entry : recordMap.entrySet()) {
                            generator.writeStringField(entry.getKey(), entry.getValue());
                        }

                        generator.writeEndObject();
                    }
                }
            });

            session.transfer(flowFile, SUCCESS);
        }

    } catch (IOException e) {
        getLogger().error(e.getMessage(), e);
        session.transfer(flowFile, FAILURE);
    }

}

From source file:org.apache.nifi.processors.ParseCSV.ParseCSV.java

private CSVFormat buildFormat(String format, char delimiter, Boolean with_header, String custom_header) {
    CSVFormat csvFormat = null;//from  ww w . j a  v a2s  .  c om

    // set pre built format
    if (format.equals("DEFAULT")) {
        csvFormat = CSVFormat.DEFAULT;
    } else if (format.equals("EXCEL")) {
        csvFormat = CSVFormat.EXCEL;
    }

    if (with_header & custom_header != null) {
        csvFormat = csvFormat.withSkipHeaderRecord(true);
        csvFormat = csvFormat.withHeader(custom_header);
    } else if (with_header & custom_header == null) {
        csvFormat = csvFormat.withHeader();
    }

    if (delimiter > 0) {
        csvFormat = csvFormat.withDelimiter(delimiter);
    }
    return csvFormat;
}

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;//w  w w  .  j a  va2s.  com
    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;/*www . j  av a 2  s .  c  om*/
    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.apache.phoenix.pherf.result.impl.CSVFileResultHandler.java

public synchronized List<Result> read() throws IOException {
    CSVParser parser = null;//from   w ww .j ava2 s  .c o m
    util.ensureBaseResultDirExists();
    try {
        File file = new File(resultFileName);
        parser = CSVParser.parse(file, Charset.defaultCharset(), CSVFormat.DEFAULT);
        List<CSVRecord> records = parser.getRecords();
        List<Result> results = new ArrayList<>();
        String header = null;
        for (CSVRecord record : records) {

            // First record is the CSV Header
            if (record.getRecordNumber() == 1) {
                header = record.toString();
                continue;
            }
            List<ResultValue> resultValues = new ArrayList<>();
            for (String val : record.toString().split(PherfConstants.RESULT_FILE_DELIMETER)) {
                resultValues.add(new ResultValue(val));
            }
            Result result = new Result(resultFileDetails, header, resultValues);
            results.add(result);
        }
        return results;
    } finally {
        parser.close();
    }
}

From source file:org.apache.phoenix.pherf.result.impl.CSVFileResultHandler.java

@Override
protected void open(String header) throws IOException {
    // Check if already so we only open one writer
    if (csvPrinter != null) {
        return;// www  .  j  a v  a  2  s  .  c om
    }
    csvPrinter = new CSVPrinter(new PrintWriter(resultFileName), CSVFormat.DEFAULT);
    Object[] records = header.split(PherfConstants.RESULT_FILE_DELIMETER);
    csvPrinter.printRecord(records);
    isClosed = false;
}