*
* @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));
}
Account account = new Account();
try {
String id = row.get(getText("entity.id.label"));
if (!CommonUtil.isNullOrEmpty(id)) {
account.setId(Integer.parseInt(id));
UserUtil.permissionCheck("update_account");
} else {
UserUtil.permissionCheck("create_account");
}
account.setName(CommonUtil.fromNullToEmpty(row
.get(getText("entity.name.label"))));
String typeID = row.get(getText("account.type_id.label"));
if (CommonUtil.isNullOrEmpty(typeID)) {
account.setAccount_type(null);
} else {
AccountType accountType = accountTypeService
.getEntityById(AccountType.class,
Integer.parseInt(typeID));
account.setAccount_type(accountType);
}
String accountLevelID = row
.get(getText("entity.account_level_id.label"));
if (CommonUtil.isNullOrEmpty(accountLevelID)) {
account.setAccount_type(null);
} else {
AccountLevel accountLevel = accountLevelService
.getEntityById(AccountLevel.class,
Integer.parseInt(accountLevelID));
account.setAccount_level(accountLevel);
}
String currencyID = row
.get(getText("entity.currency_id.label"));
if (CommonUtil.isNullOrEmpty(currencyID)) {
account.setCurrency(null);
} else {
Currency currency = currencyService.getEntityById(
Currency.class, Integer.parseInt(currencyID));
account.setCurrency(currency);
}
String capitalID = row
.get(getText("entity.capital_id.label"));
if (CommonUtil.isNullOrEmpty(capitalID)) {
account.setCapital(null);
} else {
Capital capital = capitalService.getEntityById(
Capital.class, Integer.parseInt(capitalID));
account.setCapital(capital);
}
String annualRevenueID = row
.get(getText("entity.annual_revenue_id.label"));
if (CommonUtil.isNullOrEmpty(annualRevenueID)) {
account.setAnnual_revenue(null);
} else {
AnnualRevenue annualRevenue = annualRevenueService
.getEntityById(AnnualRevenue.class,
Integer.parseInt(annualRevenueID));
account.setAnnual_revenue(annualRevenue);
}
String companySizeID = row
.get(getText("entity.company_size_id.label"));
if (CommonUtil.isNullOrEmpty(companySizeID)) {
account.setCompany_size(null);
} else {
CompanySize companySize = companySizeService
.getEntityById(CompanySize.class,
Integer.parseInt(companySizeID));
account.setCompany_size(companySize);
}
String accountTypeID = row
.get(getText("entity.account_type_id.label"));
if (CommonUtil.isNullOrEmpty(accountTypeID)) {
account.setAccount_type(null);
} else {
AccountType accountType = accountTypeService
.getEntityById(AccountType.class,
Integer.parseInt(accountTypeID));
account.setAccount_type(accountType);
}
String industryID = row
.get(getText("entity.industry_id.label"));
if (CommonUtil.isNullOrEmpty(industryID)) {
account.setIndustry(null);
} else {
Industry industry = industryService.getEntityById(
Industry.class, Integer.parseInt(industryID));
account.setIndustry(industry);
}
account.setEmail(CommonUtil.fromNullToEmpty(row
.get(getText("entity.email.label"))));
account.setOffice_phone(CommonUtil.fromNullToEmpty(row
.get(getText("entity.office_phone.label"))));
account.setWebsite(CommonUtil.fromNullToEmpty(row
.get(getText("entity.website.label"))));
account.setFax(CommonUtil.fromNullToEmpty(row
.get(getText("entity.fax.label"))));
account.setBill_street(CommonUtil.fromNullToEmpty(row
.get(getText("entity.billing_street.label"))));
account.setBill_city(CommonUtil.fromNullToEmpty(row
.get(getText("entity.billing_city.label"))));
account.setBill_state(CommonUtil.fromNullToEmpty(row
.get(getText("entity.billing_state.label"))));
account.setBill_postal_code(CommonUtil.fromNullToEmpty(row
.get(getText("entity.billing_postal_code.label"))));
account.setBill_country(CommonUtil.fromNullToEmpty(row
.get(getText("entity.billing_country.label"))));
account.setShip_street(CommonUtil.fromNullToEmpty(row
.get(getText("entity.shipping_street.label"))));
account.setShip_city(CommonUtil.fromNullToEmpty(row
.get(getText("entity.shipping_city.label"))));
account.setShip_state(CommonUtil.fromNullToEmpty(row
.get(getText("entity.shipping_state.label"))));
account.setShip_postal_code(CommonUtil.fromNullToEmpty(row
.get(getText("entity.shipping_postal_code.label"))));
account.setShip_country(CommonUtil.fromNullToEmpty(row
.get(getText("entity.shipping_country.label"))));
String accountNatureID = row
.get(getText("entity.account_nature_id.label"));
if (CommonUtil.isNullOrEmpty(accountNatureID)) {
account.setAccount_nature(null);
} else {
AccountNature accountNature = accountNatureService
.getEntityById(AccountNature.class,
Integer.parseInt(accountNatureID));
account.setAccount_nature(accountNature);
}
account.setLegal_representative(CommonUtil.fromNullToEmpty(row
.get(getText("entity.legal_representative.label"))));
account.setBusiness_scope(CommonUtil.fromNullToEmpty(row
.get(getText("entity.business_scope.label"))));
SimpleDateFormat dateFormat = new SimpleDateFormat(
Constant.DATE_EDIT_FORMAT);
String createDateS = row
.get(getText("entity.create_date.label"));
if (createDateS != null) {
Date createDate = dateFormat.parse(createDateS);
account.setCreate_date(createDate);
} else {
account.setCreate_date(null);
}
account.setCredit(CommonUtil.fromNullToEmpty(row
.get(getText("entity.credit.label"))));
account.setMarket_position(CommonUtil.fromNullToEmpty(row
.get(getText("entity.market_position.label"))));
account.setDevelopment_potential(CommonUtil.fromNullToEmpty(row
.get(getText("entity.development_potential.label"))));
account.setOperational_characteristics(CommonUtil.fromNullToEmpty(row
.get(getText("entity.operational_characteristics.label"))));
account.setOperational_direction(CommonUtil.fromNullToEmpty(row
.get(getText("entity.operational_direction.label"))));
account.setSic_code(CommonUtil.fromNullToEmpty(row
.get(getText("account.sic_code.label"))));
account.setTicket_symbol(CommonUtil.fromNullToEmpty(row
.get(getText("account.ticket_symbol.label"))));
String managerID = row
.get(getText("account.manager_id.label"));
if (CommonUtil.isNullOrEmpty(managerID)) {
account.setManager(null);
} else {
Account manager = baseService.getEntityById(
Account.class, Integer.parseInt(managerID));
account.setManager(manager);
}
String assignedToID = row
.get(getText("entity.assigned_to_id.label"));
if (CommonUtil.isNullOrEmpty(assignedToID)) {
account.setAssigned_to(null);
} else {
User assignedTo = userService.getEntityById(User.class,
Integer.parseInt(assignedToID));
account.setAssigned_to(assignedTo);
}
baseService.makePersistent(account);
successfulNum++;
} catch (Exception e) {
failedNum++;
failedMsg.put(account.getName(), e.getMessage());
}
}
this.setFailedMsg(failedMsg);
this.setFailedNum(failedNum);
this.setSuccessfulNum(successfulNum);
this.setTotalNum(successfulNum + failedNum);
} finally {
reader.close();
}
return SUCCESS;
}