*
* @return the SUCCESS result
*/
public String importCSV() throws Exception {
File file = this.getUpload();
CsvListReader reader = new CsvListReader(new FileReader(file),
CsvPreference.EXCEL_PREFERENCE);
int failedNum = 0;
int successfulNum = 0;
try {
final String[] header = reader.getCSVHeader(true);
List<String> line = new ArrayList<String>();
Map<String, String> failedMsg = new HashMap<String, String>();
while ((line = reader.read()) != null) {
Map<String, String> row = new HashMap<String, String>();
for (int i = 0; i < line.size(); i++) {
row.put(header[i], line.get(i));
}
Target target = new Target();
try {
String id = row.get(getText("entity.id.label"));
if (!CommonUtil.isNullOrEmpty(id)) {
target.setId(Integer.parseInt(id));
}
String salutationID = row
.get(getText("entity.salutation_id.label"));
if (CommonUtil.isNullOrEmpty(salutationID)) {
target.setSalutation(null);
} else {
Salutation salutation = salutationService
.getEntityById(Salutation.class,
Integer.parseInt(salutationID));
target.setSalutation(salutation);
}
target.setFirst_name(CommonUtil.fromNullToEmpty(row
.get(getText("entity.first_name.label"))));
target.setLast_name(CommonUtil.fromNullToEmpty(row
.get(getText("entity.last_name.label"))));
target.setOffice_phone(CommonUtil.fromNullToEmpty(row
.get(getText("entity.office_phone.label"))));
target.setCompany(CommonUtil.fromNullToEmpty(row
.get(getText("entity.company.label"))));
target.setTitle(CommonUtil.fromNullToEmpty(row
.get(getText("entity.title.label"))));
target.setMobile(CommonUtil.fromNullToEmpty(row
.get(getText("entity.mobile.label"))));
target.setDepartment(CommonUtil.fromNullToEmpty(row
.get(getText("entity.department.label"))));
target.setFax(CommonUtil.fromNullToEmpty(row
.get(getText("entity.fax.label"))));
String accountID = row
.get(getText("entity.account_id.label"));
if (CommonUtil.isNullOrEmpty(accountID)) {
target.setAccount(null);
} else {
Account account = accountService.getEntityById(
Account.class, Integer.parseInt(accountID));
target.setAccount(account);
}
target.setPrimary_street(CommonUtil.fromNullToEmpty(row
.get(getText("entity.primary_street.label"))));
target.setPrimary_city(CommonUtil.fromNullToEmpty(row
.get(getText("entity.primary_city.label"))));
target.setPrimary_state(CommonUtil.fromNullToEmpty(row
.get(getText("entity.primary_state.label"))));
target.setPrimary_postal_code(CommonUtil.fromNullToEmpty(row
.get(getText("entity.primary_postal_code.label"))));
target.setPrimary_country(CommonUtil.fromNullToEmpty(row
.get(getText("entity.primary_country.label"))));
target.setOther_street(CommonUtil.fromNullToEmpty(row
.get(getText("entity.other_street.label"))));
target.setOther_city(CommonUtil.fromNullToEmpty(row
.get(getText("entity.other_city.label"))));
target.setOther_state(CommonUtil.fromNullToEmpty(row
.get(getText("entity.other_state.label"))));
target.setOther_postal_code(CommonUtil.fromNullToEmpty(row
.get(getText("entity.other_postal_code.label"))));
target.setOther_country(CommonUtil.fromNullToEmpty(row
.get(getText("entity.other_country.label"))));
target.setEmail(CommonUtil.fromNullToEmpty(row
.get(getText("entity.email.label"))));
target.setNotes(CommonUtil.fromNullToEmpty(row
.get(getText("entity.notes.label"))));
String doNotCall = row
.get(getText("entity.not_call.label"));
if (CommonUtil.isNullOrEmpty(doNotCall)) {
target.setNot_call(false);
} else {
target.setNot_call(Boolean.parseBoolean(doNotCall));
}
String assignedToID = row
.get(getText("entity.assigned_to_id.label"));
if (CommonUtil.isNullOrEmpty(assignedToID)) {
target.setAssigned_to(null);
} else {
User assignedTo = userService.getEntityById(User.class,
Integer.parseInt(assignedToID));
target.setAssigned_to(assignedTo);
}
baseService.makePersistent(target);
successfulNum++;
} catch (Exception e) {
failedNum++;
String firstName = CommonUtil.fromNullToEmpty(target
.getFirst_name());
String lastName = CommonUtil.fromNullToEmpty(target
.getLast_name());
failedMsg.put(firstName + " " + lastName, e.getMessage());
}
}
this.setFailedMsg(failedMsg);
this.setFailedNum(failedNum);
this.setSuccessfulNum(successfulNum);
this.setTotalNum(successfulNum + failedNum);
} finally {
reader.close();
}
return SUCCESS;
}