use of org.apache.commons.csv.CSVRecord in project ofbiz-framework by apache.
the class PartyServices method importParty.
public static Map<String, Object> importParty(DispatchContext dctx, Map<String, Object> context) {
Delegator delegator = dctx.getDelegator();
LocalDispatcher dispatcher = dctx.getDispatcher();
Locale locale = (Locale) context.get("locale");
GenericValue userLogin = (GenericValue) context.get("userLogin");
ByteBuffer fileBytes = (ByteBuffer) context.get("uploadedFile");
String encoding = System.getProperty("file.encoding");
String csvString = Charset.forName(encoding).decode(fileBytes).toString();
final BufferedReader csvReader = new BufferedReader(new StringReader(csvString));
CSVFormat fmt = CSVFormat.DEFAULT.withHeader();
List<String> errMsgs = new LinkedList<>();
List<String> newErrMsgs = new LinkedList<>();
// last partyId read from the csv file
String lastPartyId = null;
// current partyId from the csv file
String currentPartyId = null;
// new to create/update partyId in the system
String newPartyId = null;
String newCompanyPartyId = null;
int partiesCreated = 0;
Map<String, Object> result = null;
String newContactMechId = null;
String currentContactMechTypeId = null;
String lastAddress1 = null;
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;
// when modify party, contact mech not added again
Boolean addParty = false;
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);
if (ServiceUtil.isError(result)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result));
}
} else {
// person
Map<String, Object> person = UtilMisc.toMap("partyId", newPartyId, "firstName", rec.get("firstName"), "middleName", rec.get("middleName"), "lastName", rec.get("lastName"), "preferredCurrencyUomId", rec.get("preferredCurrencyUomId"), "userLogin", userLogin);
result = dispatcher.runSync("updatePerson", person);
if (ServiceUtil.isError(result)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result));
}
}
} 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);
if (ServiceUtil.isError(result)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result));
}
} else {
// person
Map<String, Object> person = UtilMisc.toMap("firstName", rec.get("firstName"), "middleName", rec.get("middleName"), "lastName", rec.get("lastName"), "preferredCurrencyUomId", rec.get("preferredCurrencyUomId"), "statusId", "PARTY_ENABLED", "userLogin", userLogin);
result = dispatcher.runSync("createPerson", person);
if (ServiceUtil.isError(result)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result));
}
}
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);
if (ServiceUtil.isError(result)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result));
}
Map<String, Object> partyRole = UtilMisc.toMap("partyId", newPartyId, "roleTypeId", rec.get("roleTypeId"), "userLogin", userLogin);
dispatcher.runSync("createPartyRole", partyRole);
if (ServiceUtil.isError(result)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result));
}
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);
if (ServiceUtil.isError(result)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result));
}
newCompanyPartyId = (String) result.get("partyId");
} else {
newCompanyPartyId = EntityUtil.getFirst(companyCheck).getString("partyId");
}
Map<String, Object> companyRole = UtilMisc.toMap("partyId", newCompanyPartyId, "roleTypeId", "ACCOUNT", "userLogin", userLogin);
Map<String, Object> serviceResult = dispatcher.runSync("createPartyRole", companyRole);
if (ServiceUtil.isError(serviceResult)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(serviceResult));
}
// 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);
if (ServiceUtil.isError(result)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result));
}
}
}
Debug.logInfo(" =========================================================party created id: " + newPartyId, module);
partiesCreated++;
} else {
errMsgs.addAll(newErrMsgs);
newErrMsgs = new LinkedList<>();
}
}
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");
}
// casting is here necessary for some compiler versions
Map<String, Object> postalAddress = UtilMisc.toMap("userLogin", (Object) userLogin);
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");
}
// casting is here necessary for some compiler versions
Map<String, Object> telecomNumber = UtilMisc.toMap("userLogin", (Object) userLogin);
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);
if (ServiceUtil.isError(result)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result));
}
newContactMechId = (String) result.get("contactMechId");
if (currentContactMechPurposeTypeId == null) {
currentContactMechPurposeTypeId = "GENERAL_LOCATION";
}
Map<String, Object> serviceResult = dispatcher.runSync("createPartyContactMech", UtilMisc.toMap("partyId", newPartyId, "contactMechId", newContactMechId, "contactMechPurposeTypeId", currentContactMechPurposeTypeId, "userLogin", userLogin));
if (ServiceUtil.isError(serviceResult)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(serviceResult));
}
}
if (telecomNumberChanged) {
result = dispatcher.runSync("createTelecomNumber", telecomNumber);
if (ServiceUtil.isError(result)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result));
}
newContactMechId = (String) result.get("contactMechId");
if (currentContactMechPurposeTypeId == null) {
currentContactMechPurposeTypeId = "PHONE_WORK";
}
Map<String, Object> resultMap = dispatcher.runSync("createPartyContactMech", UtilMisc.toMap("partyId", newPartyId, "contactMechId", newContactMechId, "contactMechPurposeTypeId", currentContactMechPurposeTypeId, "userLogin", userLogin));
if (ServiceUtil.isError(result)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result));
}
}
if (emailAddressChanged) {
result = dispatcher.runSync("createContactMech", emailAddress);
if (ServiceUtil.isError(result)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result));
}
newContactMechId = (String) result.get("contactMechId");
if (currentContactMechPurposeTypeId == null) {
currentContactMechPurposeTypeId = "PRIMARY_EMAIL";
}
Map<String, Object> resultMap = dispatcher.runSync("createPartyContactMech", UtilMisc.toMap("partyId", newPartyId, "contactMechId", newContactMechId, "contactMechPurposeTypeId", currentContactMechPurposeTypeId, "userLogin", userLogin));
if (ServiceUtil.isError(result)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result));
}
}
if (partyContactMechPurposeChanged) {
partyContactMechPurpose.put("contactMechId", newContactMechId);
result = dispatcher.runSync("createPartyContactMechPurpose", partyContactMechPurpose);
if (ServiceUtil.isError(result)) {
return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result));
}
}
lastPartyId = currentPartyId;
errMsgs.addAll(newErrMsgs);
newErrMsgs = new LinkedList<>();
}
}
}
} catch (GenericServiceException | GenericEntityException | IOException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
if (errMsgs.size() > 0) {
return ServiceUtil.returnError(errMsgs);
}
result = ServiceUtil.returnSuccess(UtilProperties.getMessage(resource, "PartyNewPartiesCreated", UtilMisc.toMap("partiesCreated", partiesCreated), locale));
return result;
}
use of org.apache.commons.csv.CSVRecord in project sw360portal by sw360.
the class UserPortlet method getUsersFromRequest.
private List<UserCSV> getUsersFromRequest(PortletRequest request, String fileUploadFormId) throws IOException, TException {
final UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(request);
final InputStream stream = uploadPortletRequest.getFileAsStream(fileUploadFormId);
Reader reader = new InputStreamReader(stream);
CSVFormat format = CommonUtils.sw360CsvFormat;
CSVParser parser = new CSVParser(reader, format);
List<CSVRecord> records;
records = parser.getRecords();
if (records.size() > 0) {
// Remove header
records.remove(0);
}
return getUsersFromCSV(records);
}
use of org.apache.commons.csv.CSVRecord in project sw360portal by sw360.
the class UserPortlet method getUsersFromCSV.
private List<UserCSV> getUsersFromCSV(List<CSVRecord> records) {
List<UserCSV> users = new ArrayList<>();
for (CSVRecord record : records) {
try {
UserCSV user = new UserCSV(record);
users.add(user);
} catch (IndexOutOfBoundsException e) {
log.error("Broken csv record");
}
}
return users;
}
use of org.apache.commons.csv.CSVRecord in project sw360portal by sw360.
the class ComponentUploadPortlet method updateComponentAttachments.
@UsedAsLiferayAction
public void updateComponentAttachments(ActionRequest request, ActionResponse response) throws PortletException, IOException, TException {
List<CSVRecord> attachmentRecords = getCSVFromRequest(request, "file");
FluentIterable<ComponentAttachmentCSVRecord> compCSVRecords = convertCSVRecordsToComponentAttachmentCSVRecords(attachmentRecords);
log.trace("read records <" + Joiner.on("\n").join(compCSVRecords) + ">");
final ComponentService.Iface componentClient = thriftClients.makeComponentClient();
final AttachmentService.Iface attachmentClient = thriftClients.makeAttachmentClient();
User user = UserCacheHolder.getUserFromRequest(request);
final RequestSummary requestSummary = writeAttachmentsToDatabase(compCSVRecords, user, componentClient, attachmentClient);
renderRequestSummary(request, response, requestSummary);
}
use of org.apache.commons.csv.CSVRecord in project sw360portal by sw360.
the class ImportCSV method readAsCSVRecords.
/**
* reads a CSV file and returns its content as a list of CSVRecord
*
* @param in
* @return list of records
*/
public static List<CSVRecord> readAsCSVRecords(InputStream in) {
List<CSVRecord> records = null;
try (Reader reader = new InputStreamReader(in)) {
CSVParser parser = new CSVParser(reader, CommonUtils.sw360CsvFormat);
records = parser.getRecords();
// Remove header
records.remove(0);
} catch (IOException e) {
log.error("Error parsing CSV File!", e);
}
// To avoid returning null above
if (records == null)
records = Collections.emptyList();
return records;
}
Aggregations