Package org.supercsv.io

Examples of org.supercsv.io.CsvListReader


     *
     * @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;
    }
View Full Code Here


     *
     * @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));
                }

                User user = new User();
                try {
                    String id = row.get(getText("entity.id.label"));
                    if (!CommonUtil.isNullOrEmpty(id)) {
                        user.setId(Integer.parseInt(id));
                    }
                    user.setName(CommonUtil.fromNullToEmpty(row
                            .get(getText("user.name.label"))));
                    user.setFirst_name(CommonUtil.fromNullToEmpty(row
                            .get(getText("entity.first_name.label"))));
                    user.setLast_name(CommonUtil.fromNullToEmpty(row
                            .get(getText("entity.last_name.label"))));
                    String statusID = row
                            .get(getText("entity.status_id.label"));
                    if (CommonUtil.isNullOrEmpty(statusID)) {
                        user.setStatus(null);
                    } else {
                        UserStatus userStatus = userStatusService
                                .getEntityById(UserStatus.class,
                                        Integer.parseInt(statusID));
                        user.setStatus(userStatus);
                    }
                    user.setTitle(CommonUtil.fromNullToEmpty(row
                            .get(getText("entity.title.label"))));
                    user.setEmail(CommonUtil.fromNullToEmpty(row
                            .get(getText("entity.email.label"))));
                    user.setMobile(CommonUtil.fromNullToEmpty(row
                            .get(getText("entity.mobile.label"))));
                    user.setPhone(CommonUtil.fromNullToEmpty(row
                            .get(getText("user.phone.label"))));
                    user.setFax(CommonUtil.fromNullToEmpty(row
                            .get(getText("entity.fax.label"))));
                    user.setDepartment(CommonUtil.fromNullToEmpty(row
                            .get(getText("entity.department.label"))));
                    String reportToID = row
                            .get(getText("user.report_to_id.label"));
                    if (CommonUtil.isNullOrEmpty(reportToID)) {
                        user.setReport_to(null);
                    } else {
                        User reportTo = baseService.getEntityById(User.class,
                                Integer.parseInt(reportToID));
                        user.setReport_to(reportTo);
                    }
                    user.setMail_street(CommonUtil.fromNullToEmpty(row
                            .get(getText("entity.mailing_street.label"))));
                    user.setMail_city(CommonUtil.fromNullToEmpty(row
                            .get(getText("entity.mailing_city.label"))));
                    user.setMail_state(CommonUtil.fromNullToEmpty(row
                            .get(getText("entity.mailing_state.label"))));
                    user.setMail_postal_code(CommonUtil.fromNullToEmpty(row
                            .get(getText("entity.mailing_postal_code.label"))));
                    user.setMail_country(CommonUtil.fromNullToEmpty(row
                            .get(getText("entity.mailing_country.label"))));
                    user.setOther_street(CommonUtil.fromNullToEmpty(row
                            .get(getText("entity.other_street.label"))));
                    user.setOther_city(CommonUtil.fromNullToEmpty(row
                            .get(getText("entity.other_city.label"))));
                    user.setOther_state(CommonUtil.fromNullToEmpty(row
                            .get(getText("entity.other_state.label"))));
                    user.setOther_postal_code(CommonUtil.fromNullToEmpty(row
                            .get(getText("entity.other_postal_code.label"))));
                    user.setOther_country(CommonUtil.fromNullToEmpty(row
                            .get(getText("entity.other_country.label"))));
                    String age = row.get(getText("user.age.label"));
                    if (CommonUtil.isNullOrEmpty(age)) {
                        user.setAge(0);
                    } else {
                        user.setAge(Integer.parseInt(age));
                    }
                    user.setSmtp_username(CommonUtil.fromNullToEmpty(row
                            .get(getText("user.smtp_username.label"))));
                    user.setSmtp_password(CommonUtil.fromNullToEmpty(row
                            .get(getText("user.smtp_password.label"))));
                    user.setDescription(CommonUtil.fromNullToEmpty(row
                            .get(getText("entity.description.label"))));
                    user.setNotes(CommonUtil.fromNullToEmpty(row
                            .get(getText("entity.notes.label"))));
                    baseService.makePersistent(user);
                    successfulNum++;
                } catch (Exception e) {
                    failedNum++;
                    failedMsg.put(user.getName(), e.getMessage());
                }

            }

            this.setFailedMsg(failedMsg);
            this.setFailedNum(failedNum);
            this.setSuccessfulNum(successfulNum);
            this.setTotalNum(successfulNum + failedNum);
        } finally {
            reader.close();
        }
        return SUCCESS;
    }
View Full Code Here

     *
     * @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));
                }

                TargetList targetList = new TargetList();
                try {
                    String id = row.get(getText("entity.id.label"));
                    if (!CommonUtil.isNullOrEmpty(id)) {
                        targetList.setId(Integer.parseInt(id));
                    }
                    targetList.setName(CommonUtil.fromNullToEmpty(row
                            .get(getText("entity.name.label"))));
                    targetList.setNotes(CommonUtil.fromNullToEmpty(row
                            .get(getText("entity.notes.label"))));
                    String assignedToID = row
                            .get(getText("entity.assigned_to_id.label"));
                    if (CommonUtil.isNullOrEmpty(assignedToID)) {
                        targetList.setAssigned_to(null);
                    } else {
                        User assignedTo = userService.getEntityById(User.class,
                                Integer.parseInt(assignedToID));
                        targetList.setAssigned_to(assignedTo);
                    }
                    baseService.makePersistent(targetList);
                    successfulNum++;
                } catch (Exception e) {
                    failedNum++;
                    String Name = CommonUtil.fromNullToEmpty(targetList
                            .getName());
                    failedMsg.put(Name, e.getMessage());
                }

            }

            this.setFailedMsg(failedMsg);
            this.setFailedNum(failedNum);
            this.setSuccessfulNum(successfulNum);
            this.setTotalNum(successfulNum + failedNum);
        } finally {
            reader.close();
        }
        return SUCCESS;
    }
View Full Code Here

     *
     * @return the SUCCESS result
     */
    public String importCSV() throws Exception {
        File file = this.getUpload();
        CsvListReader reader = new CsvListReader(new FileReader(file),
                CsvPreference.EXCEL_PREFERENCE);
        ResourceBundle rb = CommonUtil.getResourceBundle();
        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));
                }

                Role role = new Role();
                try {
                    String id = row.get(rb.getString("entity.id.label"));
                    if (!CommonUtil.isNullOrEmpty(id)) {
                        role.setId(Integer.parseInt(id));
                    }
                    role.setName(CommonUtil.fromNullToEmpty(row.get(rb
                            .getString("entity.name.label"))));
                    role.setDescription(CommonUtil.fromNullToEmpty(row.get(rb
                            .getString("entity.description.label"))));
                    role.setNotes(CommonUtil.fromNullToEmpty(row.get(rb
                            .getString("entity.notes.label"))));
                    baseService.makePersistent(role);
                    successfulNum++;
                } catch (Exception e) {
                    failedNum++;
                    failedMsg.put(role.getName(), e.getMessage());
                }

            }

            this.setFailedMsg(failedMsg);
            this.setFailedNum(failedNum);
            this.setSuccessfulNum(successfulNum);
            this.setTotalNum(successfulNum + failedNum);
        } finally {
            reader.close();
        }
        return SUCCESS;
    }
View Full Code Here

    return builder.build();
  }

  static
  private boolean checkFormat(BufferedReader reader, CsvPreference format) throws IOException {
    CsvListReader parser = new CsvListReader(reader, format);

    int columns = 0;

    // Check the header line and the first ten lines
    for(int line = 0; line < (1 + 10); line++){
      List<String> row = parser.read();
      if(row == null){
        break;
      } // End if

      if(columns == 0 || columns == row.size()){
View Full Code Here

TOP

Related Classes of org.supercsv.io.CsvListReader

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.