List of usage examples for org.apache.commons.csv CSVFormat DEFAULT
CSVFormat DEFAULT
To view the source code for org.apache.commons.csv CSVFormat DEFAULT.
Click Source Link
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 {/* www . ja v a 2 s . c om*/ 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;/*w ww . j a v a 2 s. co m*/ String lastAddress2 = null; String lastCity = null; String lastCountryGeoId = null; String lastEmailAddress = null; String lastCountryCode = null; String lastAreaCode = null; String lastContactNumber = null; String lastContactMechPurposeTypeId = null; String currentContactMechPurposeTypeId = null; Boolean addParty = false; // when modify party, contact mech not added again if (fileBytes == null) { return ServiceUtil.returnError("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.ohdsi.whiteRabbit.WhiteRabbitMain.java
private DbSettings getTargetDbSettings() { DbSettings dbSettings = new DbSettings(); if (targetType.getSelectedItem().equals("Delimited text files")) { dbSettings.dataType = DbSettings.CSVFILES; switch ((String) targetCSVFormat.getSelectedItem()) { case "Default (comma, CRLF)": dbSettings.csvFormat = CSVFormat.DEFAULT; break; case "RFC4180": dbSettings.csvFormat = CSVFormat.RFC4180; break; case "Excel CSV": dbSettings.csvFormat = CSVFormat.EXCEL; break; case "TDF (tab, CRLF)": dbSettings.csvFormat = CSVFormat.TDF; break; case "MySQL (tab, LF)": dbSettings.csvFormat = CSVFormat.MYSQL; break; default:/*from w w w. j ava2 s . co m*/ dbSettings.csvFormat = CSVFormat.RFC4180; } } else { dbSettings.dataType = DbSettings.DATABASE; dbSettings.user = targetUserField.getText(); dbSettings.password = targetPasswordField.getText(); dbSettings.server = targetServerField.getText(); dbSettings.database = targetDatabaseField.getText(); if (targetType.getSelectedItem().toString().equals("MySQL")) dbSettings.dbType = DbType.MYSQL; else if (targetType.getSelectedItem().toString().equals("Oracle")) dbSettings.dbType = DbType.ORACLE; else if (sourceType.getSelectedItem().toString().equals("PostgreSQL")) dbSettings.dbType = DbType.POSTGRESQL; else if (sourceType.getSelectedItem().toString().equals("SQL Server")) { dbSettings.dbType = DbType.MSSQL; if (sourceUserField.getText().length() != 0) { // Not using windows authentication String[] parts = sourceUserField.getText().split("/"); if (parts.length == 2) { dbSettings.user = parts[1]; dbSettings.domain = parts[0]; } } } else if (sourceType.getSelectedItem().toString().equals("PDW")) { dbSettings.dbType = DbType.PDW; if (sourceUserField.getText().length() != 0) { // Not using windows authentication String[] parts = sourceUserField.getText().split("/"); if (parts.length == 2) { dbSettings.user = parts[1]; dbSettings.domain = parts[0]; } } } if (dbSettings.database.trim().length() == 0) { String message = "Please specify a name for the target database"; JOptionPane.showMessageDialog(frame, StringUtilities.wordWrap(message, 80), "Database error", JOptionPane.ERROR_MESSAGE); return null; } } return dbSettings; }
From source file:org.onebusaway.admin.service.bundle.impl.FixedRouteParserServiceImpl.java
/** * Parses a FixedRouteDataValidation report file into a List of * DataValidationMode objects. /*from w w w . j av a2 s .c o m*/ * * @param fixedRouteReportFile name of the file containing the report * in csv format. * @return a List of DataValidationMode objects containing the report data. */ @Override public List<DataValidationMode> parseFixedRouteReportFile(File fixedRouteReportFile) { List<DataValidationMode> parsedModes = new ArrayList<>(); if (fixedRouteReportFile != null && fixedRouteReportFile.exists()) { DataValidationMode currentMode = null; try { Reader in = new FileReader(fixedRouteReportFile); int i = 0; for (CSVRecord record : CSVFormat.DEFAULT.parse(in)) { if (i == 0) { i++; continue; // Skip the first record, which is just the column headers } // When the record being parsed is for a new mode, the parseRecord // method will add the previous mode to the parsedModes List. currentMode = parseRecord(record, currentMode, parsedModes); i++; } } catch (FileNotFoundException e) { _log.info("Exception parsing csv file " + fixedRouteReportFile, e); e.printStackTrace(); } catch (IOException e) { _log.info("Exception parsing csv file " + fixedRouteReportFile, e); e.printStackTrace(); } parsedModes.add(currentMode); // Add in the last mode processed. } return parsedModes; }
From source file:org.onebusaway.admin.service.impl.BundleCheckParserServiceImpl.java
@Override public BundleValidationParseResults parseBundleChecksFile(Reader csvDataFile) { // Create set of valid tests List<ParsedBundleValidationCheck> parsedChecks = new ArrayList<ParsedBundleValidationCheck>(); List<BundleValidationParseError> parseErrors = new ArrayList<BundleValidationParseError>(); BundleValidationParseResults parseResults = new BundleValidationParseResults(); parseResults.setParsedBundleChecks(parsedChecks); parseResults.setParseErrors(parseErrors); try {/* w w w . ja v a 2s .com*/ int linenum = 0; for (CSVRecord record : CSVFormat.DEFAULT.parse(csvDataFile)) { ++linenum; parseResults = parseRecord(record, parseResults); } } catch (FileNotFoundException e) { _log.info("Exception parsing csv file ", e); e.printStackTrace(); } catch (IOException e) { _log.info("Exception parsing csv file ", e); e.printStackTrace(); } return parseResults; }
From source file:org.onehippo.forge.content.exim.core.impl.AbstractContentMigrationTask.java
@Override public String getSummary() { StringWriter sw = new StringWriter(1024); PrintWriter out = new PrintWriter(sw); CSVPrinter csvPrinter = null;//w ww . j av a2 s . co m try { sw = new StringWriter(1024); out = new PrintWriter(sw); int totalCount = 0; int processedCount = 0; int successCount = 0; for (ContentMigrationRecord record : getContentMigrationRecords()) { ++totalCount; if (record.isProcessed()) { ++processedCount; if (record.isSucceeded()) { ++successCount; } } } out.println( "==============================================================================================================="); out.println("Execution Summary:"); out.println( "---------------------------------------------------------------------------------------------------------------"); out.printf("Total: %d, Processed: %d, Suceeded: %d, Failed: %d, Duration: %dms", totalCount, processedCount, successCount, processedCount - successCount, getStoppedTimeMillis() - getStartedTimeMillis()); out.println(); out.println( "---------------------------------------------------------------------------------------------------------------"); out.println("Details (in CSV format):"); out.println( "---------------------------------------------------------------------------------------------------------------"); try { csvPrinter = CSVFormat.DEFAULT .withHeader("SEQ", "PROCESSED", "SUCCEEDED", "ID", "PATH", "TYPE", "ATTRIBUTES", "ERROR") .print(out); int seq = 0; for (ContentMigrationRecord record : getContentMigrationRecords()) { csvPrinter.printRecord(++seq, record.isProcessed(), record.isSucceeded(), StringUtils.defaultString(record.getContentId()), StringUtils.defaultString(record.getContentPath()), StringUtils.defaultString(record.getContentType()), ObjectUtils.toString(record.getAttributeMap()), StringUtils.defaultString(record.getErrorMessage())); } } catch (IOException e) { e.printStackTrace(out); } out.println( "==============================================================================================================="); out.flush(); return sw.toString(); } finally { IOUtils.closeQuietly(csvPrinter); IOUtils.closeQuietly(out); IOUtils.closeQuietly(sw); } }
From source file:org.onehippo.forge.content.pojo.model.CsvConvertToContentNodesTest.java
@Test public void testReadCsvAndConvertToContentNodes() throws Exception { InputStream input = null;/* ww w. jav a 2 s . c o m*/ InputStreamReader reader = null; try { // 1. Open a reader from a CSV file. input = NEWS_CSV_URL.openStream(); reader = new InputStreamReader(input, "UTF-8"); // 2. Create CSV parser to parse the CSV data with column headers. CSVParser parser = CSVFormat.DEFAULT.withHeader("Title", "Introduction", "Date", "Content") .withSkipHeaderRecord().parse(reader); CSVRecord record; // 3. StringCodec to generate a JCR node name from the title column, // and ObjectMapper instance to log a ContentNode to JSON. final StringCodec codec = new StringCodecFactory.UriEncoding(); final ObjectMapper objectMapper = new ObjectMapper(); String name; String title; String introduction; String date; String content; String translationId; String translationLocale = "en"; String targetDocumentLocation; // 4. Iterate each data record and create a ContentNode for a news article with setting properties and child nodes. for (Iterator<CSVRecord> it = parser.iterator(); it.hasNext();) { record = it.next(); // 4.1. Read each column from a CSV record. title = record.get("Title"); name = codec.encode(title); introduction = record.get("Introduction"); date = record.get("Date"); content = record.get("Content"); // 4.2. Create a ContentNode for a news article and set primitive property values. ContentNode newsNode = new ContentNode(name, "ns1:newsdocument"); newsNode.setProperty("ns1:title", title); newsNode.setProperty("ns1:introduction", introduction); newsNode.setProperty("ns1:date", ContentPropertyType.DATE, date); // 4.3. Create/add a child hippostd:html content node and set the content. ContentNode htmlNode = new ContentNode("ns1:content", HippoStdNodeType.NT_HTML); newsNode.addNode(htmlNode); htmlNode.setProperty(HippoStdNodeType.HIPPOSTD_CONTENT, content); // 4.4. In Hippo CMS, the internal translation UUID and locale string are important in most cases. // So, let's generate a translation UUID and use 'en' for simplicity for now. translationId = UUID.randomUUID().toString(); newsNode.setProperty(HippoTranslationNodeType.ID, translationId); newsNode.setProperty(HippoTranslationNodeType.LOCALE, translationLocale); // 4.5. (Optional) Set kind of meta property for localized document name which is displayed in folder view later. // This meta property is not used by Hippo CMS, but can be read/used by a higher level content importing application // to set a localized (translated) name of the document (e.g, using Hippo TranslationWorkflow). newsNode.setProperty("jcr:localizedName", title); // 4.6. (Optional) Determine the target document location where this content should be generated and // store it in a meta property, jcr:path. // This meta property cannot be used in JCR repository in importing process, but can be read/used by a higher level // content importing application to create a document using Hippo DocumentWorkflow for instance. targetDocumentLocation = "/content/documents/ns1/news/" + name; newsNode.setProperty("jcr:path", targetDocumentLocation); // 4.7. (Optional) Log the JSON-ized string of the news ContentNode instance. StringWriter stringWriter = new StringWriter(256); objectMapper.writerWithDefaultPrettyPrinter().writeValue(stringWriter, newsNode); log.debug("newsNode: \n{}\n", stringWriter.toString()); } } finally { IOUtils.closeQuietly(reader); IOUtils.closeQuietly(input); } }
From source file:org.openlmis.fulfillment.Resource2Db.java
Pair<List<String>, List<Object[]>> resourceCsvToBatchedPair(final Resource resource) throws IOException { XLOGGER.entry(resource.getDescription()); // parse CSV/*from www .j a va2s . c o m*/ try (InputStreamReader isReader = new InputStreamReader( new BOMInputStream(resource.getInputStream(), ByteOrderMark.UTF_8))) { CSVParser parser = CSVFormat.DEFAULT.withHeader().withNullString("").parse(isReader); // read header row MutablePair<List<String>, List<Object[]>> readData = new MutablePair<>(); readData.setLeft(new ArrayList<>(parser.getHeaderMap().keySet())); XLOGGER.info("Read header: " + readData.getLeft()); // read data rows List<Object[]> rows = new ArrayList<>(); for (CSVRecord record : parser.getRecords()) { if (!record.isConsistent()) { throw new IllegalArgumentException("CSV record inconsistent: " + record); } List theRow = IteratorUtils.toList(record.iterator()); rows.add(theRow.toArray()); } readData.setRight(rows); XLOGGER.exit("Records read: " + readData.getRight().size()); return readData; } }
From source file:org.openo.client.cli.fw.output.print.OpenOCommandPrint.java
/** * Print output in csv format.//from w w w. j a v a2s . c o m * * @return string * @throws OpenOCommandOutputPrintingFailed * exception */ public String printCsv() throws OpenOCommandOutputPrintingFailed { StringWriter writer = new StringWriter(); CSVPrinter printer = null; try { CSVFormat formattor = CSVFormat.DEFAULT.withRecordSeparator(System.getProperty("line.separator")); printer = new CSVPrinter(writer, formattor); List<List<Object>> rows = this.formRows(false); for (int i = 0; i < this.findMaxRows(); i++) { printer.printRecord(rows.get(i)); } return writer.toString(); } catch (IOException e) { throw new OpenOCommandOutputPrintingFailed(e); } finally { try { if (printer != null) { printer.close(); } writer.close(); } catch (IOException e) { throw new OpenOCommandOutputPrintingFailed(e); // NOSONAR } } }
From source file:org.ow2.proactive_grid_cloud_portal.scheduler.server.ExportUsageServlet.java
private String csvExport(String sessionId, String user, Date startDate, Date endDate) throws ServiceException, RestServerException, IOException { Object[] header = { "Owner", "Project", "Job Id", "Job Name", "Job Duration", "Task Id", "Task Name", "Task Node Number", "Task Start Time", "Task Finished Time", "Task Duration" }; List<JobUsage> jobUsages = ((SchedulerServiceImpl) Service.get()).getUsage(sessionId, user, startDate, endDate);/*from ww w .j a v a 2 s. co m*/ StringBuilder sb = new StringBuilder(); CSVPrinter csvFilePrinter = null; CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator(LINE_SEPARATOR); csvFilePrinter = new CSVPrinter(sb, csvFileFormat); csvFilePrinter.printRecord(header); for (JobUsage jobUsage : jobUsages) { for (TaskUsage taskUsage : jobUsage.getTaskUsages()) { csvFilePrinter.printRecord(jobUsage.getOwner(), jobUsage.getProject(), jobUsage.getJobId(), jobUsage.getJobName(), jobUsage.getJobDuration(), taskUsage.getTaskId(), taskUsage.getTaskName(), taskUsage.getTaskNodeNumber(), taskUsage.getTaskStartTime(), taskUsage.getTaskFinishedTime(), taskUsage.getTaskExecutionDuration()); } } csvFilePrinter.close(); return sb.toString(); }