Example usage for org.apache.commons.csv CSVRecord get

List of usage examples for org.apache.commons.csv CSVRecord get

Introduction

In this page you can find the example usage for org.apache.commons.csv CSVRecord get.

Prototype

public String get(final String name) 

Source Link

Document

Returns a value by name.

Usage

From source file:org.nuxeo.ecm.directory.DirectoryCSVLoader.java

/**
 * Loads the CSV data file based on the provided schema, and creates the corresponding entries using the provided
 * loader./*  w ww .  j  a v a2s. c o m*/
 *
 * @param dataFileName the file name containing CSV data
 * @param delimiter the CSV column separator
 * @param schema the data schema
 * @param loader the actual consumer of loaded rows
 * @since 8.4
 */
public static void loadData(String dataFileName, char delimiter, Schema schema,
        Consumer<Map<String, Object>> loader) throws DirectoryException {
    try (InputStream in = getResource(dataFileName); //
            CSVParser csvParser = new CSVParser(new InputStreamReader(in, "UTF-8"),
                    CSVFormat.DEFAULT.withDelimiter(delimiter).withHeader())) {
        Map<String, Integer> header = csvParser.getHeaderMap();

        List<Field> fields = new ArrayList<>();
        for (String columnName : header.keySet()) {
            Field field = schema.getField(columnName.trim());
            if (field == null) {
                throw new DirectoryException(
                        "Column not found: " + columnName + " in schema: " + schema.getName());
            }
            fields.add(field);
        }

        int lineno = 1; // header was first line
        for (CSVRecord record : csvParser) {
            lineno++;
            if (record.size() == 0 || record.size() == 1 && StringUtils.isBlank(record.get(0))) {
                // NXP-2538: allow columns with only one value but skip empty lines
                continue;
            }
            if (!record.isConsistent()) {
                log.error("Invalid column count while reading CSV file: " + dataFileName + ", line: " + lineno
                        + ", values: " + record);
                continue;
            }

            Map<String, Object> map = new HashMap<String, Object>();
            for (int i = 0; i < header.size(); i++) {
                Field field = fields.get(i);
                String value = record.get(i);
                Object v = CSV_NULL_MARKER.equals(value) ? null : decode(field, value);
                map.put(field.getName().getPrefixedName(), v);
            }
            loader.accept(map);
        }
    } catch (IOException e) {
        throw new DirectoryException("Read error while reading data file: " + dataFileName, e);
    }
}

From source file:org.nuxeo.ecm.platform.filemanager.service.extension.CSVZipImporter.java

@Override
public DocumentModel create(CoreSession documentManager, Blob content, String path, boolean overwrite,
        String filename, TypeManager typeService) throws IOException {
    ZipFile zip = null;/* w w w . jav a  2  s.  c  o  m*/
    try (CloseableFile source = content.getCloseableFile()) {
        zip = getArchiveFileIfValid(source.getFile());
        if (zip == null) {
            return null;
        }

        DocumentModel container = documentManager.getDocument(new PathRef(path));

        ZipEntry index = zip.getEntry(MARKER);
        try (Reader reader = new InputStreamReader(zip.getInputStream(index));
                CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT.withHeader());) {

            Map<String, Integer> header = csvParser.getHeaderMap();
            for (CSVRecord csvRecord : csvParser) {
                String type = null;
                String id = null;
                Map<String, String> stringValues = new HashMap<>();
                for (String headerValue : header.keySet()) {
                    String lineValue = csvRecord.get(headerValue);
                    if ("type".equalsIgnoreCase(headerValue)) {
                        type = lineValue;
                    } else if ("id".equalsIgnoreCase(headerValue)) {
                        id = lineValue;
                    } else {
                        stringValues.put(headerValue, lineValue);
                    }
                }

                boolean updateDoc = false;
                // get doc for update
                DocumentModel targetDoc = null;
                if (id != null) {
                    // update ?
                    String targetPath = new Path(path).append(id).toString();
                    if (documentManager.exists(new PathRef(targetPath))) {
                        targetDoc = documentManager.getDocument(new PathRef(targetPath));
                        updateDoc = true;
                    }
                }

                // create doc if needed
                if (targetDoc == null) {
                    if (type == null) {
                        log.error("Can not create doc without a type, skipping line");
                        continue;
                    }

                    if (id == null) {
                        id = IdUtils.generateStringId();
                    }
                    targetDoc = documentManager.createDocumentModel(path, id, type);
                }

                // update doc properties
                DocumentType targetDocType = targetDoc.getDocumentType();
                for (String fname : stringValues.keySet()) {

                    String stringValue = stringValues.get(fname);
                    Field field = null;
                    boolean usePrefix = false;
                    String schemaName = null;
                    String fieldName = null;

                    if (fname.contains(":")) {
                        if (targetDocType.hasField(fname)) {
                            field = targetDocType.getField(fname);
                            usePrefix = true;
                        }
                    } else if (fname.contains(".")) {
                        String[] parts = fname.split("\\.");
                        schemaName = parts[0];
                        fieldName = parts[1];
                        if (targetDocType.hasSchema(schemaName)) {
                            field = targetDocType.getField(fieldName);
                            usePrefix = false;
                        }
                    } else {
                        if (targetDocType.hasField(fname)) {
                            field = targetDocType.getField(fname);
                            usePrefix = false;
                            schemaName = field.getDeclaringType().getSchemaName();
                        }
                    }

                    if (field != null) {
                        Serializable fieldValue = getFieldValue(field, stringValue, zip);

                        if (fieldValue != null) {
                            if (usePrefix) {
                                targetDoc.setPropertyValue(fname, fieldValue);
                            } else {
                                targetDoc.setProperty(schemaName, fieldName, fieldValue);
                            }
                        }
                    }
                }
                if (updateDoc) {
                    documentManager.saveDocument(targetDoc);
                } else {
                    documentManager.createDocument(targetDoc);
                }
            }
        }
        return container;
    } finally {
        IOUtils.closeQuietly(zip);
    }
}

From source file:org.nuxeo.ecm.user.center.profile.UserProfileImporter.java

/**
 * Import a line from the CSV file.//  w ww. ja va2s.  c  o  m
 *
 * @param userProfileService
 * @param docType
 * @param session
 * @return {@code true} if a document has been created or updated, {@code false} otherwise.
 */
protected boolean importLine(CSVRecord record, final long lineNumber, Integer nameIndex, DocumentType docType,
        CoreSession session, UserProfileService userProfileService, Map<String, Integer> headerValues) {
    final String name = record.get(nameIndex);
    if (StringUtils.isBlank(name)) {
        logImportError(lineNumber, "Missing 'name' value", "label.csv.importer.missingNameValue");
        return false;
    }

    Map<String, Serializable> values = computePropertiesMap(lineNumber, docType, headerValues, record);
    if (values == null) {
        // skip this line
        return false;
    }

    return updateDocument(lineNumber, name, docType, session, userProfileService, values);
}

From source file:org.nuxeo.ecm.user.center.profile.UserProfileImporter.java

protected Map<String, Serializable> computePropertiesMap(long lineNumber, DocumentType docType,
        Map<String, Integer> headerValues, CSVRecord record) {

    Map<String, Serializable> values = new HashMap<String, Serializable>();
    for (String headerValue : headerValues.keySet()) {
        String lineValue = record.get(headerValue);
        lineValue = lineValue.trim();/*  w w w.ja  v a  2 s  . c o  m*/
        String fieldName = headerValue;
        if (!UserProfileImporter.USER_PROFILE_IMPORTER_USERNAME_COL.equals(headerValue)) {
            if (!docType.hasField(fieldName)) {
                fieldName = fieldName.split(":")[1];
            }
            if (docType.hasField(fieldName) && !StringUtils.isBlank(lineValue)) {
                Serializable convertedValue = convertValue(docType, fieldName, headerValue, lineValue,
                        lineNumber);
                if (convertedValue == null) {
                    return null;
                }
                values.put(headerValue, convertedValue);
            }
        }
    }
    return values;
}

From source file:org.nuxeo.theme.presets.PaletteParser.java

public static Map<String, String> parseCsv(String text) {
    Map<String, String> properties = new HashMap<>();
    if (text == null) {
        return properties;
    }//from www .  ja v  a 2 s . c o  m
    try (StringReader sr = new StringReader(text);
            CSVParser reader = new CSVParser(sr, CSVFormat.DEFAULT.withDelimiter('\t'))) {
        for (CSVRecord record : reader) {
            properties.put(record.get(0), record.get(1));
        }
    } catch (IOException e) {
        log.error(e, e);
    }
    return properties;
}

From source file:org.nuxeo.theme.vocabularies.VocabularyManager.java

public List<VocabularyItem> getItems(String name) {
    VocabularyType vocabularyType = (VocabularyType) Manager.getTypeRegistry().lookup(TypeFamily.VOCABULARY,
            name);//from  w  w  w.jav a 2  s  . c  o m
    if (vocabularyType == null) {
        return null;
    }
    final String path = vocabularyType.getPath();
    final String className = vocabularyType.getClassName();

    if (path == null && className == null) {
        log.error("Must specify a class name or a path for vocabulary: " + name);
        return null;
    }
    if (path != null && className != null) {
        log.error("Cannot specify both a class name and a path for vocabulary: " + name);
        return null;
    }

    if (className != null) {
        Vocabulary vocabulary = getInstance(className);
        if (vocabulary == null) {
            log.error("Vocabulary class not found: " + className);
            return null;
        }
        return vocabulary.getItems();
    }

    if (path != null) {
        if (!path.endsWith(".csv")) {
            log.error("Only .csv vocabularies are supported: " + path);
            return null;
        }
        final List<VocabularyItem> items = new ArrayList<>();
        try (InputStream is = getClass().getClassLoader().getResourceAsStream(path)) {
            if (is == null) {
                log.error("Vocabulary file not found: " + path);
                return null;
            }
            try (CSVParser reader = new CSVParser(new InputStreamReader(is, Charsets.UTF_8),
                    CSVFormat.DEFAULT)) {
                for (CSVRecord record : reader) {
                    final String value = record.get(0);
                    String label = value;
                    if (record.size() >= 2) {
                        label = record.get(1);
                    }
                    items.add(new VocabularyItem(value, label));
                }
            }
        } catch (IOException e) {
            log.error("Could not read vocabulary file: " + path, e);
        }
        return items;
    }
    return null;
}

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

public static Map<String, Object> importInvoice(DispatchContext dctx, Map<String, Object> context) {
    Delegator delegator = dctx.getDelegator();
    LocalDispatcher dispatcher = dctx.getDispatcher();
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    ByteBuffer fileBytes = (ByteBuffer) context.get("uploadedFile");
    String organizationPartyId = (String) context.get("organizationPartyId");
    String encoding = System.getProperty("file.encoding");
    String csvString = Charset.forName(encoding).decode(fileBytes).toString();
    final BufferedReader csvReader = new BufferedReader(new StringReader(csvString));
    CSVFormat fmt = CSVFormat.DEFAULT.withHeader();
    List<String> errMsgs = FastList.newInstance();
    List<String> newErrMsgs = FastList.newInstance();
    String lastInvoiceId = null;/*w w w . j  a  v a  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 {/*  w  w  w . ja v 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 {//from   w w  w .  jav  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;//  www .ja  v a  2  s  .  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;
}