Search in sources :

Example 81 with CSVRecord

use of org.apache.commons.csv.CSVRecord in project ofbiz-framework by apache.

the class InvoiceServices method importInvoice.

public static Map<String, Object> importInvoice(DispatchContext dctx, Map<String, Object> context) {
    Locale locale = (Locale) context.get("locale");
    Delegator delegator = dctx.getDelegator();
    LocalDispatcher dispatcher = dctx.getDispatcher();
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    ByteBuffer fileBytes = (ByteBuffer) context.get("uploadedFile");
    if (fileBytes == null) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingUploadedFileDataNotFound", locale));
    }
    String organizationPartyId = (String) context.get("organizationPartyId");
    String encoding = System.getProperty("file.encoding");
    String csvString = Charset.forName(encoding).decode(fileBytes).toString();
    final BufferedReader csvReader = new BufferedReader(new StringReader(csvString));
    CSVFormat fmt = CSVFormat.DEFAULT.withHeader();
    List<String> errMsgs = new LinkedList<>();
    List<String> newErrMsgs;
    String lastInvoiceId = null;
    String currentInvoiceId = null;
    String newInvoiceId = null;
    int invoicesCreated = 0;
    try {
        for (final CSVRecord rec : fmt.parse(csvReader)) {
            currentInvoiceId = rec.get("invoiceId");
            if (lastInvoiceId == null || !currentInvoiceId.equals(lastInvoiceId)) {
                newInvoiceId = null;
                Map<String, Object> invoice = UtilMisc.toMap("invoiceTypeId", rec.get("invoiceTypeId"), "partyIdFrom", rec.get("partyIdFrom"), "partyId", rec.get("partyId"), "invoiceDate", rec.get("invoiceDate"), "dueDate", rec.get("dueDate"), "currencyUomId", rec.get("currencyUomId"), "description", rec.get("description"), "referenceNumber", rec.get("referenceNumber") + "   Imported: orginal InvoiceId: " + currentInvoiceId, "userLogin", userLogin);
                // replace values if required
                if (UtilValidate.isNotEmpty(rec.get("partyIdFromTrans"))) {
                    invoice.put("partyIdFrom", rec.get("partyIdFromTrans"));
                }
                if (UtilValidate.isNotEmpty(rec.get("partyIdTrans"))) {
                    invoice.put("partyId", rec.get("partyIdTrans"));
                }
                // invoice validation
                newErrMsgs = new LinkedList<>();
                try {
                    if (UtilValidate.isEmpty(invoice.get("partyIdFrom"))) {
                        newErrMsgs.add("Line number " + rec.getRecordNumber() + ": Mandatory Party Id From and Party Id From Trans missing for invoice: " + currentInvoiceId);
                    } else if (EntityQuery.use(delegator).from("Party").where("partyId", invoice.get("partyIdFrom")).queryOne() == null) {
                        newErrMsgs.add("Line number " + rec.getRecordNumber() + ": partyIdFrom: " + invoice.get("partyIdFrom") + " not found for invoice: " + currentInvoiceId);
                    }
                    if (UtilValidate.isEmpty(invoice.get("partyId"))) {
                        newErrMsgs.add("Line number " + rec.getRecordNumber() + ": Mandatory Party Id and Party Id Trans missing for invoice: " + currentInvoiceId);
                    } else if (EntityQuery.use(delegator).from("Party").where("partyId", invoice.get("partyId")).queryOne() == null) {
                        newErrMsgs.add("Line number " + rec.getRecordNumber() + ": partyId: " + invoice.get("partyId") + " not found for invoice: " + currentInvoiceId);
                    }
                    if (UtilValidate.isEmpty(invoice.get("invoiceTypeId"))) {
                        newErrMsgs.add("Line number " + rec.getRecordNumber() + ": Mandatory Invoice Type missing for invoice: " + currentInvoiceId);
                    } else if (EntityQuery.use(delegator).from("InvoiceType").where("invoiceTypeId", invoice.get("invoiceTypeId")).queryOne() == null) {
                        newErrMsgs.add("Line number " + rec.getRecordNumber() + ": InvoiceItem type id: " + invoice.get("invoiceTypeId") + " not found for invoice: " + currentInvoiceId);
                    }
                    Boolean isPurchaseInvoice = EntityTypeUtil.hasParentType(delegator, "InvoiceType", "invoiceTypeId", (String) invoice.get("invoiceTypeId"), "parentTypeId", "PURCHASE_INVOICE");
                    Boolean isSalesInvoice = EntityTypeUtil.hasParentType(delegator, "InvoiceType", "invoiceTypeId", (String) invoice.get("invoiceTypeId"), "parentTypeId", "SALES_INVOICE");
                    if (isPurchaseInvoice && !invoice.get("partyId").equals(organizationPartyId)) {
                        newErrMsgs.add("Line number " + rec.getRecordNumber() + ": A purchase type invoice should have the partyId 'To' being the organizationPartyId(=" + organizationPartyId + ")! however is " + invoice.get("partyId") + "! invoice: " + currentInvoiceId);
                    }
                    if (isSalesInvoice && !invoice.get("partyIdFrom").equals(organizationPartyId)) {
                        newErrMsgs.add("Line number " + rec.getRecordNumber() + ": A sales type invoice should have the partyId 'from' being the organizationPartyId(=" + organizationPartyId + ")! however is " + invoice.get("partyIdFrom") + "! invoice: " + currentInvoiceId);
                    }
                } catch (GenericEntityException e) {
                    Debug.logError("Valication checking problem against database. due to " + e.getMessage(), module);
                }
                if (newErrMsgs.size() > 0) {
                    errMsgs.addAll(newErrMsgs);
                } else {
                    Map<String, Object> invoiceResult = null;
                    try {
                        invoiceResult = dispatcher.runSync("createInvoice", invoice);
                        if (ServiceUtil.isError(invoiceResult)) {
                            return ServiceUtil.returnError(ServiceUtil.getErrorMessage(invoiceResult));
                        }
                    } catch (GenericServiceException e) {
                        csvReader.close();
                        Debug.logError(e, module);
                        return ServiceUtil.returnError(e.getMessage());
                    }
                    newInvoiceId = (String) invoiceResult.get("invoiceId");
                    invoicesCreated++;
                }
                lastInvoiceId = currentInvoiceId;
            }
            if (newInvoiceId != null) {
                Map<String, Object> invoiceItem = UtilMisc.toMap("invoiceId", newInvoiceId, "invoiceItemSeqId", rec.get("invoiceItemSeqId"), "invoiceItemTypeId", rec.get("invoiceItemTypeId"), "productId", rec.get("productId"), "description", rec.get("itemDescription"), "amount", rec.get("amount"), "quantity", rec.get("quantity"), "userLogin", userLogin);
                if (UtilValidate.isNotEmpty(rec.get("productIdTrans"))) {
                    invoiceItem.put("productId", rec.get("productIdTrans"));
                }
                // invoice item validation
                newErrMsgs = new LinkedList<>();
                try {
                    if (UtilValidate.isEmpty(invoiceItem.get("invoiceItemSeqId"))) {
                        newErrMsgs.add("Line number " + rec.getRecordNumber() + ": Mandatory item sequence Id missing for invoice: " + currentInvoiceId);
                    }
                    if (UtilValidate.isEmpty(invoiceItem.get("invoiceItemTypeId"))) {
                        newErrMsgs.add("Line number " + rec.getRecordNumber() + ": Mandatory invoice item type missing for invoice: " + currentInvoiceId);
                    } else if (EntityQuery.use(delegator).from("InvoiceItemType").where("invoiceItemTypeId", invoiceItem.get("invoiceItemTypeId")).queryOne() == null) {
                        newErrMsgs.add("Line number " + rec.getRecordNumber() + ": InvoiceItem Item type id: " + invoiceItem.get("invoiceItemTypeId") + " not found for invoice: " + currentInvoiceId + " Item seqId:" + invoiceItem.get("invoiceItemSeqId"));
                    }
                    if (UtilValidate.isEmpty(invoiceItem.get("productId")) && UtilValidate.isEmpty(invoiceItem.get("description"))) {
                        newErrMsgs.add("Line number " + rec.getRecordNumber() + ": no Product Id given, no description given");
                    }
                    if (UtilValidate.isNotEmpty(invoiceItem.get("productId")) && EntityQuery.use(delegator).from("Product").where("productId", invoiceItem.get("productId")).queryOne() == null) {
                        newErrMsgs.add("Line number " + rec.getRecordNumber() + ": Product Id: " + invoiceItem.get("productId") + " not found for invoice: " + currentInvoiceId + " Item seqId:" + invoiceItem.get("invoiceItemSeqId"));
                    }
                    if (UtilValidate.isEmpty(invoiceItem.get("amount")) && UtilValidate.isEmpty(invoiceItem.get("quantity"))) {
                        newErrMsgs.add("Line number " + rec.getRecordNumber() + ": Either or both quantity and amount is required for invoice: " + currentInvoiceId + " Item seqId:" + invoiceItem.get("invoiceItemSeqId"));
                    }
                } catch (GenericEntityException e) {
                    Debug.logError("Validation checking problem against database. due to " + e.getMessage(), module);
                }
                if (newErrMsgs.size() > 0) {
                    errMsgs.addAll(newErrMsgs);
                } else {
                    try {
                        Map<String, Object> result = dispatcher.runSync("createInvoiceItem", invoiceItem);
                        if (ServiceUtil.isError(result)) {
                            return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result));
                        }
                    } catch (GenericServiceException e) {
                        csvReader.close();
                        Debug.logError(e, module);
                        return ServiceUtil.returnError(e.getMessage());
                    }
                }
            }
        }
    } catch (IOException e) {
        Debug.logError(e, module);
        return ServiceUtil.returnError(e.getMessage());
    }
    if (errMsgs.size() > 0) {
        return ServiceUtil.returnError(errMsgs);
    }
    Map<String, Object> result = ServiceUtil.returnSuccess(UtilProperties.getMessage(resource, "AccountingNewInvoicesCreated", UtilMisc.toMap("invoicesCreated", invoicesCreated), locale));
    result.put("organizationPartyId", organizationPartyId);
    return result;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) LinkedList(java.util.LinkedList) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) CSVFormat(org.apache.commons.csv.CSVFormat) CSVRecord(org.apache.commons.csv.CSVRecord) GenericServiceException(org.apache.ofbiz.service.GenericServiceException)

Example 82 with CSVRecord

use of org.apache.commons.csv.CSVRecord in project opencast by opencast.

the class EventsLoader method parseCSV.

private List<EventEntry> parseCSV(CSVParser csv) {
    List<EventEntry> arrayList = new ArrayList<>();
    for (CSVRecord record : csv) {
        String title = record.get(0);
        String description = StringUtils.trimToNull(record.get(1));
        String series = StringUtils.trimToNull(record.get(2));
        String seriesName = StringUtils.trimToNull(record.get(3));
        Integer days = Integer.parseInt(record.get(4));
        float signum = Math.signum(days);
        DateTime now = DateTime.now();
        if (signum > 0) {
            now = now.plusDays(days);
        } else if (signum < 0) {
            now = now.minusDays(days * -1);
        }
        Integer duration = Integer.parseInt(record.get(5));
        boolean archive = BooleanUtils.toBoolean(record.get(6));
        String agent = StringUtils.trimToNull(record.get(7));
        String source = StringUtils.trimToNull(record.get(8));
        String contributor = StringUtils.trimToNull(record.get(9));
        List<String> presenters = Arrays.asList(StringUtils.split(StringUtils.trimToEmpty(record.get(10)), ","));
        EventEntry eventEntry = new EventEntry(title, now.toDate(), duration, archive, series, agent, source, contributor, description, seriesName, presenters);
        arrayList.add(eventEntry);
    }
    return arrayList;
}
Also used : ArrayList(java.util.ArrayList) CSVRecord(org.apache.commons.csv.CSVRecord) DateTime(org.joda.time.DateTime)

Example 83 with CSVRecord

use of org.apache.commons.csv.CSVRecord in project sw360portal by sw360.

the class ComponentUploadPortlet method updateReleaseLinks.

@UsedAsLiferayAction
public void updateReleaseLinks(ActionRequest request, ActionResponse response) throws PortletException, IOException, TException {
    List<CSVRecord> releaseLinkRecords = getCSVFromRequest(request, "file");
    FluentIterable<ReleaseLinkCSVRecord> csvRecords = convertCSVRecordsToReleaseLinkCSVRecords(releaseLinkRecords);
    log.trace("read records <" + Joiner.on("\n").join(csvRecords) + ">");
    final ComponentService.Iface componentClient = thriftClients.makeComponentClient();
    User user = UserCacheHolder.getUserFromRequest(request);
    final RequestSummary requestSummary = writeReleaseLinksToDatabase(csvRecords, componentClient, user);
    renderRequestSummary(request, response, requestSummary);
}
Also used : User(org.eclipse.sw360.datahandler.thrift.users.User) CSVRecord(org.apache.commons.csv.CSVRecord) ComponentService(org.eclipse.sw360.datahandler.thrift.components.ComponentService) RequestSummary(org.eclipse.sw360.datahandler.thrift.RequestSummary) UsedAsLiferayAction(org.eclipse.sw360.portal.common.UsedAsLiferayAction)

Example 84 with CSVRecord

use of org.apache.commons.csv.CSVRecord in project sw360portal by sw360.

the class ComponentUploadPortlet method updateLicenses.

@UsedAsLiferayAction
public void updateLicenses(ActionRequest request, ActionResponse response) throws PortletException, IOException, TException {
    final HashMap<String, InputStream> inputMap = new HashMap<>();
    User user = UserCacheHolder.getUserFromRequest(request);
    try {
        fillFilenameInputStreamMap(request, inputMap);
        if (ZipTools.isValidLicenseArchive(inputMap)) {
            final LicenseService.Iface licenseClient = thriftClients.makeLicenseClient();
            log.debug("Parsing risk categories ...");
            Map<Integer, RiskCategory> riskCategoryMap = getIdentifierToTypeMapAndWriteMissingToDatabase(licenseClient, inputMap.get(RISK_CATEGORY_FILE), RiskCategory.class, Integer.class, user);
            log.debug("Parsing risks ...");
            Map<Integer, Risk> riskMap = getIntegerRiskMap(licenseClient, riskCategoryMap, inputMap.get(RISK_FILE), user);
            log.debug("Parsing obligations ...");
            Map<Integer, Obligation> obligationMap = getIdentifierToTypeMapAndWriteMissingToDatabase(licenseClient, inputMap.get(OBLIGATION_FILE), Obligation.class, Integer.class, user);
            log.debug("Parsing obligation todos ...");
            List<CSVRecord> obligationTodoRecords = readAsCSVRecords(inputMap.get(OBLIGATION_TODO_FILE));
            Map<Integer, Set<Integer>> obligationTodoMapping = convertRelationalTableWithIntegerKeys(obligationTodoRecords);
            log.debug("Parsing license types ...");
            Map<Integer, LicenseType> licenseTypeMap = getIdentifierToTypeMapAndWriteMissingToDatabase(licenseClient, inputMap.get(LICENSETYPE_FILE), LicenseType.class, Integer.class, user);
            log.debug("Parsing todos ...");
            Map<Integer, Todo> todoMap = getTodoMapAndWriteMissingToDatabase(licenseClient, obligationMap, obligationTodoMapping, inputMap.get(TODO_FILE), user);
            if (inputMap.containsKey(CUSTOM_PROPERTIES_FILE)) {
                log.debug("Parsing custom properties ...");
                Map<Integer, PropertyWithValue> customPropertiesMap = getCustomPropertiesWithValuesByIdAndWriteMissingToDatabase(licenseClient, inputMap.get(CUSTOM_PROPERTIES_FILE), user);
                log.debug("Parsing todo custom properties relation ...");
                List<CSVRecord> todoPropertiesRecord = readAsCSVRecords(inputMap.get(TODO_CUSTOM_PROPERTIES_FILE));
                Map<Integer, Set<Integer>> todoPropertiesMap = convertRelationalTableWithIntegerKeys(todoPropertiesRecord);
                todoMap = updateTodoMapWithCustomPropertiesAndWriteToDatabase(licenseClient, todoMap, customPropertiesMap, todoPropertiesMap, user);
            }
            log.debug("Parsing license todos ...");
            List<CSVRecord> licenseTodoRecord = readAsCSVRecords(inputMap.get(LICENSE_TODO_FILE));
            Map<String, Set<Integer>> licenseTodoMap = convertRelationalTable(licenseTodoRecord);
            log.debug("Parsing license risks ...");
            List<CSVRecord> licenseRiskRecord = readAsCSVRecords(inputMap.get(LICENSE_RISK_FILE));
            Map<String, Set<Integer>> licenseRiskMap = convertRelationalTable(licenseRiskRecord);
            log.debug("Parsing licenses ...");
            List<CSVRecord> licenseRecord = readAsCSVRecords(inputMap.get(LICENSE_FILE));
            final List<License> licensesToAdd = ConvertRecord.fillLicenses(licenseRecord, licenseTypeMap, todoMap, riskMap, licenseTodoMap, licenseRiskMap);
            addLicenses(licenseClient, licensesToAdd, log, user);
        } else {
            throw new SW360Exception("Invalid file format");
        }
    } finally {
        for (InputStream inputStream : inputMap.values()) {
            inputStream.close();
        }
    }
}
Also used : User(org.eclipse.sw360.datahandler.thrift.users.User) SW360Exception(org.eclipse.sw360.datahandler.thrift.SW360Exception) CSVRecord(org.apache.commons.csv.CSVRecord) UsedAsLiferayAction(org.eclipse.sw360.portal.common.UsedAsLiferayAction)

Example 85 with CSVRecord

use of org.apache.commons.csv.CSVRecord in project sw360portal by sw360.

the class ComponentUploadPortlet method updateComponents.

@UsedAsLiferayAction
public void updateComponents(ActionRequest request, ActionResponse response) throws PortletException, IOException, TException {
    List<CSVRecord> releaseRecords = getCSVFromRequest(request, "file");
    FluentIterable<ComponentCSVRecord> compCSVRecords = convertCSVRecordsToCompCSVRecords(releaseRecords);
    log.trace("read records <" + Joiner.on("\n").join(compCSVRecords) + ">");
    final ComponentService.Iface componentClient = thriftClients.makeComponentClient();
    final VendorService.Iface vendorClient = thriftClients.makeVendorClient();
    final AttachmentService.Iface attachmentClient = thriftClients.makeAttachmentClient();
    User user = UserCacheHolder.getUserFromRequest(request);
    final RequestSummary requestSummary = writeToDatabase(compCSVRecords, componentClient, vendorClient, attachmentClient, user);
    renderRequestSummary(request, response, requestSummary);
}
Also used : VendorService(org.eclipse.sw360.datahandler.thrift.vendors.VendorService) User(org.eclipse.sw360.datahandler.thrift.users.User) CSVRecord(org.apache.commons.csv.CSVRecord) ComponentService(org.eclipse.sw360.datahandler.thrift.components.ComponentService) RequestSummary(org.eclipse.sw360.datahandler.thrift.RequestSummary) AttachmentService(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentService) UsedAsLiferayAction(org.eclipse.sw360.portal.common.UsedAsLiferayAction)

Aggregations

CSVRecord (org.apache.commons.csv.CSVRecord)127 CSVParser (org.apache.commons.csv.CSVParser)71 IOException (java.io.IOException)40 CSVFormat (org.apache.commons.csv.CSVFormat)40 ArrayList (java.util.ArrayList)36 Reader (java.io.Reader)24 StringReader (java.io.StringReader)22 InputStreamReader (java.io.InputStreamReader)18 FileReader (java.io.FileReader)16 Test (org.junit.Test)14 Path (java.nio.file.Path)13 HashMap (java.util.HashMap)11 File (java.io.File)10 PreparedStatement (java.sql.PreparedStatement)10 InputStream (java.io.InputStream)9 ResultSet (java.sql.ResultSet)9 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)9 CSVCommonsLoader (org.apache.phoenix.util.CSVCommonsLoader)9 BufferedReader (java.io.BufferedReader)8 Map (java.util.Map)7