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

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

Introduction

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

Prototype

public CSVParser parse(final Reader in) throws IOException 

Source Link

Document

Parses the specified content.

Usage

From source file:org.nuxeo.ecm.csv.core.CSVImporterWork.java

@Override
public void work() {
    TransientStore store = getStore();// w w w  . j a v  a2 s .com
    setStatus("Importing");
    openUserSession();
    CSVFormat csvFormat = CSVFormat.DEFAULT.withHeader().withEscape(options.getEscapeCharacter())
            .withCommentMarker(options.getCommentMarker());
    try (Reader in = newReader(getBlob()); CSVParser parser = csvFormat.parse(in)) {
        doImport(parser);
    } catch (IOException e) {
        logError(0, "Error while doing the import: %s", LABEL_CSV_IMPORTER_ERROR_DURING_IMPORT, e.getMessage());
        log.debug(e, e);
    }
    store.putParameter(id, "logs", importLogs);
    if (options.sendEmail()) {
        setStatus("Sending email");
        sendMail();
    }
    setStatus(null);
}

From source file:org.ofbiz.accounting.invoice.InvoiceServices.java

public static Map<String, Object> importInvoice(DispatchContext dctx, Map<String, Object> context) {
    Delegator delegator = dctx.getDelegator();
    LocalDispatcher dispatcher = dctx.getDispatcher();
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    ByteBuffer fileBytes = (ByteBuffer) context.get("uploadedFile");
    String organizationPartyId = (String) context.get("organizationPartyId");
    String encoding = System.getProperty("file.encoding");
    String csvString = Charset.forName(encoding).decode(fileBytes).toString();
    final BufferedReader csvReader = new BufferedReader(new StringReader(csvString));
    CSVFormat fmt = CSVFormat.DEFAULT.withHeader();
    List<String> errMsgs = FastList.newInstance();
    List<String> newErrMsgs = FastList.newInstance();
    String lastInvoiceId = null;//from  ww w .  j  a va 2  s.c  om
    String currentInvoiceId = null;
    String newInvoiceId = null;
    int invoicesCreated = 0;

    if (fileBytes == null) {
        return ServiceUtil.returnError("Uploaded file data not found");
    }

    try {
        for (final CSVRecord rec : fmt.parse(csvReader)) {
            currentInvoiceId = rec.get("invoiceId");
            if (lastInvoiceId == null || !currentInvoiceId.equals(lastInvoiceId)) {
                newInvoiceId = null;
                Map<String, Object> invoice = UtilMisc.toMap("invoiceTypeId", rec.get("invoiceTypeId"),
                        "partyIdFrom", rec.get("partyIdFrom"), "partyId", rec.get("partyId"), "invoiceDate",
                        rec.get("invoiceDate"), "dueDate", rec.get("dueDate"), "currencyUomId",
                        rec.get("currencyUomId"), "description", rec.get("description"), "referenceNumber",
                        rec.get("referenceNumber") + "   Imported: orginal InvoiceId: " + currentInvoiceId,
                        "userLogin", userLogin);

                // replace values if required
                if (UtilValidate.isNotEmpty(rec.get("partyIdFromTrans"))) {
                    invoice.put("partyIdFrom", rec.get("partyIdFromTrans"));
                }
                if (UtilValidate.isNotEmpty(rec.get("partyIdTrans"))) {
                    invoice.put("partyId", rec.get("partyIdTrans"));
                }

                // invoice validation
                try {
                    newErrMsgs = FastList.newInstance();
                    if (UtilValidate.isEmpty(invoice.get("partyIdFrom"))) {
                        newErrMsgs.add("Line number " + rec.getRecordNumber()
                                + ": Mandatory Party Id From and Party Id From Trans missing for invoice: "
                                + currentInvoiceId);
                    } else if (EntityQuery.use(delegator).from("Party")
                            .where("partyId", invoice.get("partyIdFrom")).queryOne() == null) {
                        newErrMsgs.add("Line number " + rec.getRecordNumber() + ": partyIdFrom: "
                                + invoice.get("partyIdFrom") + " not found for invoice: " + currentInvoiceId);
                    }
                    if (UtilValidate.isEmpty(invoice.get("partyId"))) {
                        newErrMsgs.add("Line number " + rec.getRecordNumber()
                                + ": Mandatory Party Id and Party Id Trans missing for invoice: "
                                + currentInvoiceId);
                    } else if (EntityQuery.use(delegator).from("Party").where("partyId", invoice.get("partyId"))
                            .queryOne() == null) {
                        newErrMsgs.add("Line number " + rec.getRecordNumber() + ": partyId: "
                                + invoice.get("partyId") + " not found for invoice: " + currentInvoiceId);
                    }
                    if (UtilValidate.isEmpty(invoice.get("invoiceTypeId"))) {
                        newErrMsgs.add("Line number " + rec.getRecordNumber()
                                + ": Mandatory Invoice Type missing for invoice: " + currentInvoiceId);
                    } else if (EntityQuery.use(delegator).from("InvoiceType")
                            .where("invoiceTypeId", invoice.get("invoiceTypeId")).queryOne() == null) {
                        newErrMsgs.add("Line number " + rec.getRecordNumber() + ": InvoiceItem type id: "
                                + invoice.get("invoiceTypeId") + " not found for invoice: " + currentInvoiceId);
                    }
                    GenericValue invoiceType = EntityQuery.use(delegator).from("InvoiceType")
                            .where("invoiceTypeId", invoice.get("invoiceTypeId")).queryOne();
                    if ("PURCHASE_INVOICE".equals(invoiceType.getString("parentTypeId"))
                            && !invoice.get("partyId").equals(organizationPartyId)) {
                        newErrMsgs.add("Line number " + rec.getRecordNumber()
                                + ": A purchase type invoice should have the partyId 'To' being the organizationPartyId(="
                                + organizationPartyId + ")! however is " + invoice.get("partyId")
                                + "! invoice: " + currentInvoiceId);
                    }
                    if ("SALES_INVOICE".equals(invoiceType.getString("parentTypeId"))
                            && !invoice.get("partyIdFrom").equals(organizationPartyId)) {
                        newErrMsgs.add("Line number " + rec.getRecordNumber()
                                + ": A sales type invoice should have the partyId 'from' being the organizationPartyId(="
                                + organizationPartyId + ")! however is " + invoice.get("partyIdFrom")
                                + "! invoice: " + currentInvoiceId);
                    }

                } catch (GenericEntityException e) {
                    Debug.logError("Valication checking problem against database. due to " + e.getMessage(),
                            module);
                }

                if (newErrMsgs.size() > 0) {
                    errMsgs.addAll(newErrMsgs);
                } else {
                    Map<String, Object> invoiceResult = null;
                    try {
                        invoiceResult = dispatcher.runSync("createInvoice", invoice);
                    } catch (GenericServiceException e) {
                        csvReader.close();
                        Debug.logError(e, module);
                        return ServiceUtil.returnError(e.getMessage());
                    }
                    newInvoiceId = (String) invoiceResult.get("invoiceId");
                    invoicesCreated++;
                }
                lastInvoiceId = currentInvoiceId;
            }

            if (newInvoiceId != null) {
                Map<String, Object> invoiceItem = UtilMisc.toMap("invoiceId", newInvoiceId, "invoiceItemSeqId",
                        rec.get("invoiceItemSeqId"), "invoiceItemTypeId", rec.get("invoiceItemTypeId"),
                        "productId", rec.get("productId"), "description", rec.get("itemDescription"), "amount",
                        rec.get("amount"), "quantity", rec.get("quantity"), "userLogin", userLogin);

                if (UtilValidate.isNotEmpty(rec.get("productIdTrans"))) {
                    invoiceItem.put("productId", rec.get("productIdTrans"));
                }
                // invoice item validation
                try {
                    newErrMsgs = FastList.newInstance();
                    if (UtilValidate.isEmpty(invoiceItem.get("invoiceItemSeqId"))) {
                        newErrMsgs.add("Line number " + rec.getRecordNumber()
                                + ": Mandatory item sequence Id missing for invoice: " + currentInvoiceId);
                    }
                    if (UtilValidate.isEmpty(invoiceItem.get("invoiceItemTypeId"))) {
                        newErrMsgs.add("Line number " + rec.getRecordNumber()
                                + ": Mandatory invoice item type missing for invoice: " + currentInvoiceId);
                    } else if (EntityQuery.use(delegator).from("InvoiceItemType")
                            .where("invoiceItemTypeId", invoiceItem.get("invoiceItemTypeId"))
                            .queryOne() == null) {
                        newErrMsgs.add("Line number " + rec.getRecordNumber() + ": InvoiceItem Item type id: "
                                + invoiceItem.get("invoiceItemTypeId") + " not found for invoice: "
                                + currentInvoiceId + " Item seqId:" + invoiceItem.get("invoiceItemSeqId"));
                    }
                    if (UtilValidate.isEmpty(invoiceItem.get("productId"))
                            && UtilValidate.isEmpty(invoiceItem.get("description"))) {
                    }
                    if (UtilValidate.isNotEmpty(invoiceItem.get("productId"))
                            && EntityQuery.use(delegator).from("Product")
                                    .where("productId", invoiceItem.get("productId")).queryOne() == null) {
                        newErrMsgs.add("Line number " + rec.getRecordNumber() + ": Product Id: "
                                + invoiceItem.get("productId") + " not found for invoice: " + currentInvoiceId
                                + " Item seqId:" + invoiceItem.get("invoiceItemSeqId"));
                    }
                    if (UtilValidate.isEmpty(invoiceItem.get("amount"))
                            && UtilValidate.isEmpty(invoiceItem.get("quantity"))) {
                        newErrMsgs.add("Line number " + rec.getRecordNumber()
                                + ": Either or both quantity and amount is required for invoice: "
                                + currentInvoiceId + " Item seqId:" + invoiceItem.get("invoiceItemSeqId"));
                    }
                } catch (GenericEntityException e) {
                    Debug.logError("Validation checking problem against database. due to " + e.getMessage(),
                            module);
                }

                if (newErrMsgs.size() > 0) {
                    errMsgs.addAll(newErrMsgs);
                } else {
                    try {
                        dispatcher.runSync("createInvoiceItem", invoiceItem);
                    } catch (GenericServiceException e) {
                        csvReader.close();
                        Debug.logError(e, module);
                        return ServiceUtil.returnError(e.getMessage());
                    }
                }
            }
        }

    } catch (IOException e) {
        Debug.logError(e, module);
        return ServiceUtil.returnError(e.getMessage());
    }

    if (errMsgs.size() > 0) {
        return ServiceUtil.returnError(errMsgs);
    }

    Map<String, Object> result = ServiceUtil.returnSuccess(invoicesCreated + " new invoice(s) created");
    result.put("organizationPartyId", organizationPartyId);
    return result;
}

From source file:org.ofbiz.magento.CatalogServices.java

public static Map<String, Object> importMagentoProducts(DispatchContext ctx,
        Map<String, ? extends Object> context) {
    Delegator delegator = ctx.getDelegator();
    LocalDispatcher dispatcher = ctx.getDispatcher();
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    Locale locale = (Locale) context.get("locale");
    Map<String, Object> serviceResult = new HashMap<String, Object>();
    int errorRecords = 0;
    int processedRecords = 0;
    try {//from  w w w .  j av  a  2  s  .  c  o m
        serviceResult = dispatcher.runSync("getMagentoProducts", context);
        if (ServiceUtil.isError(serviceResult)) {
            ServiceUtil.returnError(
                    UtilProperties.getMessage(resource, "MagentoErrorInGettingProductsFromMagento", locale));
        }
        serviceResult.clear();

        File csvFile = new File(System.getProperty("ofbiz.home") + "/runtime/tmp/MagentoProductInfo.csv");
        BufferedReader reader = new BufferedReader(new FileReader(csvFile));
        String fieldDelimiter = ",";
        String fieldEncapsulator = "\"";

        CSVFormat csvFormat = CSVFormat.DEFAULT.withDelimiter(fieldDelimiter.charAt(0))
                .withQuote(fieldEncapsulator.charAt(0)).withIgnoreEmptyLines(true)
                .withIgnoreSurroundingSpaces(true);

        CSVParser parser = new CSVParser(reader, csvFormat);
        Boolean isFirstLine = true;
        String[] mappedKeys = null;
        Map<String, Object> processedResult = new HashMap<String, Object>();
        Map<String, Object> serviceResp = new HashMap<String, Object>();

        for (CSVRecord csvRecord : csvFormat.parse(reader)) {
            int csvRecordSize = csvRecord.size();
            if (isFirstLine) {
                mappedKeys = new String[csvRecordSize];
                for (int i = 0; i < csvRecordSize; i++) {
                    mappedKeys[i] = csvRecord.get(i);
                }
                isFirstLine = false;
            } else {
                Map<String, Object> mappedValues = new HashMap<String, Object>();
                for (int i = 0; i < mappedKeys.length; i++) {
                    String csvValue = csvRecord.get(i);
                    String value = (i < csvRecordSize ? csvValue : "");
                    if (UtilValidate.isNotEmpty(value)) {
                        value = value.trim().replaceAll(">|<", "");
                    }
                    mappedValues.put(mappedKeys[i], value);
                }
                Map<String, Object> serviceInMap = UtilMisc.toMap("userLogin", userLogin, "productId",
                        mappedValues.get("Product Id"), "externalId", mappedValues.get("Product Id"), "sku",
                        mappedValues.get("SKU"), "productTypeId", mappedValues.get("Product Type Id"),
                        "productName", mappedValues.get("Product Name"), "bundleParentId",
                        mappedValues.get("Bundle Parent Id"), "configurableParentId",
                        mappedValues.get("Configurable Parent Id"), "groupedParentId",
                        mappedValues.get("Grouped Parent Id"), "description", mappedValues.get("Description"),
                        "longDescription", mappedValues.get("Long Description"), "price",
                        mappedValues.get("Price"), "taxClassId", mappedValues.get("Tax Class Id"),
                        "categoryIds", mappedValues.get("Category Ids"), "webSiteIds",
                        mappedValues.get("Web Site Ids"), "thumbnailImageUrl",
                        mappedValues.get("Thumbnail Image Url"), "smallImageUrl",
                        mappedValues.get("Small Image Url"), "originalImageUrl",
                        mappedValues.get("Original Image Url"), "urlKey", mappedValues.get("Url Key"),
                        "urlPath", mappedValues.get("Url Path"), "metaDescription",
                        mappedValues.get("Meta Description"), "metaKeyword", mappedValues.get("Meta Keyword"),
                        "metaTitle", mappedValues.get("Meta Title"), "status", mappedValues.get("Status"),
                        "specialFromDate", mappedValues.get("Special From Date"), "specialPrice",
                        mappedValues.get("Special Price"), "createdDate", mappedValues.get("Created At"),
                        "lastModifiedDate", mappedValues.get("Updated At"));
                Boolean isError = false;
                if (UtilValidate.isEmpty(serviceInMap.get("productId"))) {
                    isError = true;
                    Debug.logError("Product ID is missing : ", module);
                }
                String productId = (String) serviceInMap.get("productId");
                productId = "MAG-" + productId.trim();
                serviceInMap.put("productId", productId);
                String bundleParentId = ((String) serviceInMap.get("bundleParentId")).trim();
                if (!("NA".equalsIgnoreCase(bundleParentId))) {
                    bundleParentId = "MAG-" + bundleParentId;
                }
                serviceInMap.put("bundleParentId", bundleParentId);
                String configurableParentId = ((String) serviceInMap.get("configurableParentId")).trim();
                if (!("NA".equalsIgnoreCase(configurableParentId))) {
                    configurableParentId = "MAG-" + configurableParentId;
                }
                serviceInMap.put("configurableParentId", configurableParentId);
                String groupedParentId = ((String) serviceInMap.get("groupedParentId")).trim();
                if (!("NA".equalsIgnoreCase(groupedParentId))) {
                    groupedParentId = "MAG-" + groupedParentId;
                }
                serviceInMap.put("groupedParentId", groupedParentId);
                if (UtilValidate.isEmpty(serviceInMap.get("productTypeId"))) {
                    isError = true;
                    Debug.logError("Product Type ID is missing for product id : " + productId, module);
                }
                if (UtilValidate.isEmpty(serviceInMap.get("productName"))) {
                    isError = true;
                    Debug.logError("Name is missing for product id : " + productId, module);
                }
                if (UtilValidate.isEmpty(serviceInMap.get("price"))) {
                    isError = true;
                    Debug.logError("Price is missing for product id : " + productId, module);
                }
                Debug.logInfo("Begin processing for productId [" + productId + "]", module);
                if (!isError) {
                    Debug.logInfo("Create / Update product having productId [" + productId + "]", module);
                    serviceResult = dispatcher.runSync("createMagentoProducts", serviceInMap, 600, true);
                }
                if (ServiceUtil.isError(serviceResult) || isError) {
                    errorRecords++;
                    processedResult.put(productId, "Error");
                    Debug.logInfo("Completed processing for productId [" + productId + "] with ERROR ", module);
                    if (ServiceUtil.isError(serviceResult)) {
                        Debug.logInfo(ServiceUtil.getErrorMessage(serviceResp), module);
                    }
                } else {
                    processedResult.put(productId, "Success");
                    Debug.logInfo("Processing successfully completed for product [" + productId + "]", module);
                }
                processedRecords++;
            }
        }
    } catch (GenericServiceException e) {
        Debug.logError(e, e.getMessage(), module);
        e.printStackTrace();
    } catch (IOException e) {
        Debug.logError(e, e.getMessage(), module);
        e.printStackTrace();
    }
    return ServiceUtil.returnSuccess(UtilProperties.getMessage(resource,
            "MagentoProductsHaveBeenImportedSuccessfully", UtilMisc.toMap("processedRecords", processedRecords,
                    "successRecords", (processedRecords - errorRecords)),
            locale));
}

From source file:org.ofbiz.magento.InventoryServices.java

public static Map<String, Object> loadAndImportWarehouseLocations(DispatchContext dctx,
        Map<String, Object> context) {
    Delegator delegator = dctx.getDelegator();
    Locale locale = (Locale) context.get("locale");
    LocalDispatcher dispatcher = dctx.getDispatcher();
    String fileName = (String) context.get("_uploadedFile_fileName");
    String processData = (String) context.get("processData");
    String contentId = (String) context.get("contentId");
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    List<Map<String, Object>> productFacilityLocations = FastList.newInstance();
    List<Map<String, Object>> processedProductFacilityLocations = FastList.newInstance();
    String facilityId = (String) context.get("facilityId");

    Map<String, Object> serviceResult = FastMap.newInstance();
    try {/* w  w  w  .  j  a  v  a  2  s .c o  m*/
        if (UtilValidate.isEmpty(fileName) && "N".equalsIgnoreCase(processData)) {
            return ServiceUtil.returnError(
                    UtilProperties.getMessage(resource, "MagentoPleaseSelectCSVFileForImport", locale));
        }
        boolean isCsv = false;
        if (UtilValidate.isNotEmpty(fileName) && "N".equalsIgnoreCase(processData)) {
            isCsv = fileName.contains(".csv");
        }
        if (!isCsv && "N".equalsIgnoreCase(processData)) {
            isCsv = fileName.contains(".CSV");
        }
        // If file passed and it is not csv file
        if (!isCsv && "N".equalsIgnoreCase(processData)) {
            return ServiceUtil.returnError(
                    UtilProperties.getMessage(resource, "MagentoPleaseSelectTheFileInCSVFormat", locale));
        }
        if (UtilValidate.isEmpty(contentId) && "Y".equalsIgnoreCase(processData)) {
            return ServiceUtil
                    .returnError(UtilProperties.getMessage(resource, "MagentoNoDataExistsToProcess", locale));
        }
        if (UtilValidate.isNotEmpty(fileName)) {
            Map<String, Object> fileUploadToServerCtx = dctx.getModelService("fileUploadToServer")
                    .makeValid(context, ModelService.IN_PARAM);
            fileUploadToServerCtx.put("contentTypeId", "PROD_FAC_CSV_CNT");
            fileUploadToServerCtx.put("statusId", "PROD_FAC_CSV_INPRGRS");
            fileUploadToServerCtx.put("userLogin", userLogin);
            Map<String, Object> fileUploadToServerResp = dispatcher.runSync("fileUploadToServer",
                    fileUploadToServerCtx);
            if (ServiceUtil.isError(fileUploadToServerResp)) {
                return ServiceUtil.returnError(ServiceUtil.getErrorMessage(fileUploadToServerResp));
            }
            contentId = (String) fileUploadToServerResp.get("contentId");
        }
    } catch (GenericServiceException ex) {
        // TODO Auto-generated catch block
        return ServiceUtil.returnError(ex.getMessage());
    } catch (Exception e) {
        return ServiceUtil.returnError(e.getMessage());
    }
    try {
        String xmlString = ContentWorker.renderContentAsText(dispatcher, delegator, contentId, null, locale,
                "text/plain", false);
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(new ByteArrayInputStream(xmlString.getBytes())));
        String fieldDelimiter = ",";
        String fieldEncapsulator = "\"";
        CSVFormat csvFormat = CSVFormat.DEFAULT.withDelimiter(fieldDelimiter.charAt(0))
                .withQuote(fieldEncapsulator.charAt(0)).withIgnoreEmptyLines(true)
                .withIgnoreSurroundingSpaces(true);

        Boolean isFirstLine = true;
        String[] mappedKeys = null;
        List<String> serviceFields = new ArrayList<String>();
        serviceFields.add("Product Id");
        serviceFields.add("Location Seq Id");
        serviceFields.add("Area Id");
        serviceFields.add("Aisle Id");
        serviceFields.add("Section Id");
        serviceFields.add("Level Id");
        serviceFields.add("Position Id");
        serviceFields.add("Inventory Count");
        for (CSVRecord csvRecord : csvFormat.parse(reader)) {
            int csvRecordSize = csvRecord.size();
            if (isFirstLine) {
                mappedKeys = new String[csvRecordSize];
                for (int i = 0; i < csvRecordSize; i++) {
                    if (serviceFields.contains(csvRecord.get(i).trim())) {
                        mappedKeys[i] = csvRecord.get(i);
                    } else {
                        return ServiceUtil.returnError(
                                UtilProperties.getMessage(resource, "MagentoInvalidColumnFoundInCSV", locale)
                                        + csvRecord.get(i));
                    }
                }
                isFirstLine = false;
            } else {
                Map<String, Object> mappedValues = new HashMap<String, Object>();
                for (int i = 0; i < mappedKeys.length; i++) {
                    String csvValue = csvRecord.get(i);
                    String value = (i < csvRecordSize ? csvValue : "");
                    if (UtilValidate.isNotEmpty(value)) {
                        value = value.trim();
                    }
                    mappedValues.put(mappedKeys[i], value);
                }
                Map<String, Object> serviceInMap = FastMap.newInstance();
                Boolean isError = false;
                StringBuilder errorMessage = new StringBuilder();
                String productId = (String) mappedValues.get("Product Id");
                String locationSeqId = (String) mappedValues.get("Location Seq Id");
                String areaId = (String) mappedValues.get("Area Id");
                String aisleId = (String) mappedValues.get("Aisle Id");
                String sectionId = (String) mappedValues.get("Section Id");
                String levelId = (String) mappedValues.get("Level Id");
                String positionId = (String) mappedValues.get("Position Id");
                String inventoryCount = (String) mappedValues.get("Inventory Count");
                if (UtilValidate.isEmpty(productId)) {
                    errorMessage.append(
                            UtilProperties.getMessage(resource, "MagentoErrorProductIdIsMissing", locale));
                    isError = true;
                }
                if (UtilValidate.isNotEmpty(productId)) {
                    GenericValue goodIdentification = delegator.findOne("GoodIdentification",
                            UtilMisc.toMap("productId", productId, "goodIdentificationTypeId", "MAGENTO_ID"),
                            true);
                    if (UtilValidate.isEmpty(goodIdentification)) {
                        errorMessage.append(UtilProperties.getMessage(resource, "MagentoErrorProductNotFound",
                                UtilMisc.toMap("productId", productId), locale));
                        isError = true;
                    } else {
                        GenericValue product = delegator.findOne("Product",
                                UtilMisc.toMap("productId", productId), true);
                        if (UtilValidate.isNotEmpty(product)
                                && (product.getString("isVirtual").equalsIgnoreCase("Y"))) {
                            errorMessage.append(
                                    UtilProperties.getMessage(resource, "MagentoErrorProductIsAVirtualProduct",
                                            UtilMisc.toMap("productId", productId), locale));
                            isError = true;
                        }
                    }
                }
                if (UtilValidate.isEmpty(locationSeqId)) {
                    if (UtilValidate.isNotEmpty(errorMessage)) {
                        errorMessage.append(", ");
                    } else {
                        errorMessage
                                .append(UtilProperties.getMessage(resource, "MagentoError", locale) + " : ");
                    }
                    errorMessage.append(
                            UtilProperties.getMessage(resource, "MagentoLocationSeqIdIsMissing", locale));
                    isError = true;
                }
                if (UtilValidate.isEmpty(inventoryCount)) {
                    if (UtilValidate.isNotEmpty(errorMessage)) {
                        errorMessage.append(", ");
                    } else {
                        errorMessage
                                .append(UtilProperties.getMessage(resource, "MagentoError", locale) + " : ");
                    }
                    errorMessage
                            .append(UtilProperties.getMessage(resource, "MagentoQuantityIsMissing", locale));
                    isError = true;
                } else {
                    Integer inventory = Integer.valueOf(inventoryCount);
                    if (inventory < 0) {
                        if (UtilValidate.isNotEmpty(errorMessage)) {
                            errorMessage.append(", ");
                        } else {
                            errorMessage.append(
                                    UtilProperties.getMessage(resource, "MagentoError", locale) + " : ");
                        }
                        errorMessage.append(
                                UtilProperties.getMessage(resource, "MagentoQuantityCannotBeNegative", locale));
                        isError = true;
                    }
                }
                serviceInMap.put("productId", productId);
                serviceInMap.put("locationSeqId", locationSeqId);
                serviceInMap.put("areaId", areaId);
                serviceInMap.put("aisleId", aisleId);
                serviceInMap.put("sectionId", sectionId);
                serviceInMap.put("levelId", levelId);
                serviceInMap.put("positionId", positionId);
                serviceInMap.put("inventoryCount", inventoryCount);
                serviceInMap.put("facilityId", facilityId);
                if (isError) {
                    serviceInMap.put("message", errorMessage);
                    serviceResult.put("isError", "Y");
                } else {
                    serviceInMap.put("message", "Success");
                }
                serviceInMap.put("isError", isError);
                productFacilityLocations.add(serviceInMap);
            }
        }
    } catch (IOException e) {
        throw new GeneralRuntimeException(
                UtilProperties.getMessage(resource, "MagentoErrorInResponseWriterOutputStream", locale)
                        + e.toString(),
                e);
    } catch (GeneralException e) {
        throw new GeneralRuntimeException(
                UtilProperties.getMessage(resource, "MagentoErrorRenderingContent", locale) + e.toString(), e);
    }
    Map<String, Object> processedResult = FastMap.newInstance();
    int errorRecords = 0;
    int processedRecords = 0;
    if ("Y".equalsIgnoreCase(processData)) {
        if (!productFacilityLocations.isEmpty()) {
            for (Map<String, Object> productFacilityLocation : productFacilityLocations) {
                String productId = (String) productFacilityLocation.get("productId");
                processedRecords++;
                try {
                    productFacilityLocation.put("userLogin", userLogin);
                    productFacilityLocation.remove("message");
                    productFacilityLocation.remove("isError");
                    Map<String, Object> serviceResp = dispatcher.runSync(
                            "createUpdateProductFacilityAndLocation", productFacilityLocation, 3600, true);
                    if (ServiceUtil.isError(serviceResp)) {
                        processedResult.put(productId, "Error");
                        errorRecords++;
                    } else {
                        processedResult.put(productId, "Success");
                    }
                    processedProductFacilityLocations.add(productFacilityLocation);
                } catch (Exception ex) {
                    return ServiceUtil.returnError(ex.getMessage());
                }
            }
        }
        String jobId = FileUploadHelper.getJobId(delegator, "importWarehouseLocations");
        if (UtilValidate.isNotEmpty(jobId)) {
            String statusId = null;
            if (errorRecords == processedRecords) {
                statusId = "PROD_FAC_CSV_FAIL";
            } else if (errorRecords == 0) {
                statusId = "PROD_FAC_CSV_SUCCESS";
            } else {
                statusId = "PROD_FAC_CSV_PARTIAL";
            }
            try {
                String message = FileUploadHelper.getPlainCustomMessage(processedResult, errorRecords,
                        processedRecords);
                Map<String, Object> updateContentResp = dispatcher.runSync("updateContent",
                        UtilMisc.<String, Object>toMap("contentId", contentId, "statusId", statusId,
                                "userLogin", userLogin));
                if (ServiceUtil.isError(updateContentResp)) {
                    Debug.logError(ServiceUtil.getErrorMessage(updateContentResp), module);
                    return ServiceUtil.returnError(ServiceUtil.getErrorMessage(updateContentResp));
                }
                Map<String, Object> createSimpleTextContentDataResp = dispatcher
                        .runSync("createSimpleTextContentData",
                                UtilMisc.<String, Object>toMap("contentName", "Result_" + jobId + ".txt",
                                        "contentTypeId", "PROD_FAC_CSV_LOG", "text", message, "userLogin",
                                        userLogin));
                if (ServiceUtil.isError(createSimpleTextContentDataResp)) {
                    Debug.logError(ServiceUtil.getErrorMessage(createSimpleTextContentDataResp), module);
                    return ServiceUtil
                            .returnError(ServiceUtil.getErrorMessage(createSimpleTextContentDataResp));
                }
                Map<String, Object> createContentAssocResp = dispatcher.runSync("createContentAssoc",
                        UtilMisc.toMap("contentIdFrom", contentId, "contentIdTo",
                                createSimpleTextContentDataResp.get("contentId"), "contentAssocTypeId",
                                "PROD_FAC_CSV_RESULT", "userLogin", userLogin));
                if (ServiceUtil.isError(createContentAssocResp)) {
                    Debug.logError(ServiceUtil.getErrorMessage(createContentAssocResp), module);
                    return ServiceUtil.returnError(ServiceUtil.getErrorMessage(createContentAssocResp));
                }
            } catch (GenericServiceException ex) {
                return ServiceUtil.returnError(ex.getMessage());
            }
        }
    }
    serviceResult.put("productFacilityLocations", productFacilityLocations);
    if ("Y".equalsIgnoreCase(processData)) {
        serviceResult.put("processedProductFacilityLocations", processedProductFacilityLocations);
    } else {
        serviceResult.put("loadFileContent", contentId);
    }
    return serviceResult;
}

From source file:org.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();
    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 = FastList.newInstance();
    List<String> newErrMsgs = FastList.newInstance();
    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;/*from w ww . j a  v  a  2s  .  c  o 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("Uploaded file data not found");
    }

    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("midleName"), "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("midleName"), "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 = FastList.newInstance();
                }
            }

            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 = FastList.newInstance();
                }
            }

        }

    } 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(partiesCreated + " new parties created");
    return result;
}

From source file:org.opens.referentiel.creator.CodeGeneratorMojo.java

/**
 *
 * @return//from   w  w  w. jav  a2s.c  om
 */
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);
    } catch (IOException ex) {
        Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ex);
        lineIterator = null;
    }
    String[] csvHeaders = lineIterator.next().split(String.valueOf(delimiter));
    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.phenotips.vocabulary.AbstractCSVAnnotationsExtension.java

@Override
public void indexingStarted(@Nonnull final Vocabulary vocabulary) {
    if (this.operationsInProgress.incrementAndGet() == 1) {
        this.data = new HashMap<>();
        try (BufferedReader in = new BufferedReader(new InputStreamReader(
                new URL(getAnnotationSource()).openConnection().getInputStream(), StandardCharsets.UTF_8))) {
            CSVFormat parser = setupCSVParser(vocabulary);
            for (final CSVRecord row : parser.parse(in)) {
                processCSVRecordRow(row, vocabulary);
            }//w  ww . j  a  va2  s  .com
        } catch (final IOException ex) {
            this.logger.error("Failed to load annotation source: {}", ex.getMessage());
        }
    }
}

From source file:org.tanaguru.referentiel.creator.CodeGeneratorMojo.java

/**
 *
 * @return//  www.j  a  v  a2 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.next().split(String.valueOf(delimiter));
    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.tanaguru.rules.doc.utils.updateAw22toRgaa30.CopyFiles.java

private Iterable<CSVRecord> getCsv(ResourceBundle resourceBundle) {
    // we parse the csv file to extract the first line and get the headers 
    LineIterator lineIterator;/*w w  w .j a v  a2  s.  com*/
    try {
        lineIterator = FileUtils.lineIterator(FileUtils.getFile(resourceBundle.getString("export.csvPath")));
    } catch (IOException ex) {
        Logger.getLogger(CopyFiles.class.getName()).log(Level.SEVERE, null, ex);
        lineIterator = null;
    }
    String[] csvHeaders = lineIterator.next().split(String.valueOf(delimiter));

    // 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(CopyFiles.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    } catch (IOException ex) {
        Logger.getLogger(CopyFiles.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}

From source file:org.transitime.custom.sfmta.delayTimes.Intersection.java

public static List<Intersection> readIntersections(String fileName) {
    List<Intersection> intersections = new ArrayList<Intersection>();

    try {// ww w. j a  va  2 s . c o m
        Reader in = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8"));
        CSVFormat formatter = CSVFormat.DEFAULT.withHeader().withCommentMarker('-');

        // Parse the file
        Iterable<CSVRecord> records = formatter.parse(in);
        Iterator<CSVRecord> iterator = records.iterator();
        while (iterator.hasNext()) {
            // Determine the record to process
            CSVRecord record = iterator.next();
            Intersection i = getIntersection(record);
            intersections.add(i);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return intersections;
}