/*
    Account Id
    Account Value
    Account Date
    Account Label
    External Family Account Id
 */
 @Override
 public Map<String, Integer> importDepositAccounts(
     Long externalApplicationId, String broker, byte[] csvDatas)
     throws CvqModelException, CvqObjectNotFoundException {
   ExternalDepositAccountItem edai;
   ExternalHomeFolder ehf;
   ExternalApplication ea = getExternalApplicationById(externalApplicationId);
   Map<String, Integer> report = new HashMap<String, Integer>();
   report.put("created", 0);
   report.put("updated", 0);
   report.put("ignored", 0);
   try {
     CSVReader csvReader = new CSVReader(new StringReader(new String(csvDatas)), ',', '"', 1);
     for (Object o : csvReader.readAll()) {
       boolean updated = false;
       String[] line = (String[]) o;
       ehf =
           genericDAO
               .simpleSelect(ExternalHomeFolder.class)
               .and("externalId", line[4])
               .and("externalApplication", ea)
               .unique();
       if (ehf == null) {
         report.put("ignored", report.get("ignored") + 1);
         continue;
       }
       edai =
           genericDAO
               .simpleSelect(ExternalDepositAccountItem.class)
               .and("externalItemId", line[0])
               .and("externalApplicationId", String.valueOf(externalApplicationId))
               .unique();
       if (edai != null) {
         genericDAO.delete(edai);
         report.put("updated", report.get("updated") + 1);
         updated = true;
       }
       edai =
           new ExternalDepositAccountItem(
               line[3],
               Double.valueOf(line[1]),
               EXTERNAL_APPLICATION_LABEL,
               line[0],
               DateUtils.parseIso(line[2]),
               Double.valueOf(line[1]),
               broker);
       edai.setExternalApplicationId(ea.getId().toString());
       edai.setExternalHomeFolderId(ehf.getExternalId());
       genericDAO.create(edai);
       if (!updated) report.put("created", report.get("created") + 1);
     }
   } catch (Exception e) {
     logger.error(e.getMessage());
     e.printStackTrace();
     HibernateUtil.getSession().getTransaction().rollback();
     throw new CvqModelException("external.error.csvImport");
   }
   return report;
 }
 @Override
 public void deleteExternalApplication(Long id)
     throws CvqModelException, CvqObjectNotFoundException {
   ExternalApplication ea = getExternalApplicationById(id);
   if (ea.getExternalHomeFolders() != null) {
     //            List<ExternalAccountItem> externalAccountItems =
     // paymentService.getAllExternalAccountItems();
     //            for (ExternalAccountItem eai : externalAccountItems) {
     //                if
     // (eai.getExternalServiceSpecificDataByKey(ExternalServiceUtils.EXTERNAL_APPLICATION_ID_KEY).equals(String.valueOf(ea.getId()))) {
     //                    externalAccountItems.remove(eai);
     //                    eai.setExternalServiceSpecificData(null);
     //                    genericDAO.delete(eai);
     //                }
     //            }
     for (ExternalHomeFolder externalHomeFolder : ea.getExternalHomeFolders()) {
       externalHomeFolderService.deleteHomeFolderMappings(
           ea.getLabel(),
           externalHomeFolderService
               .getHomeFolderMapping(ea.getLabel(), externalHomeFolder.getExternalId())
               .getHomeFolderId());
       genericDAO.delete(externalHomeFolder);
     }
     ea.setExternalHomeFolders(null);
   }
   genericDAO.delete(ea);
 }
 /*
     External Individual Id
     Last Name
     First Name
     External Family Account Id
     Address
     Is Responsible
     Email
     Phone
 */
 @Override
 public Map<String, Integer> importHomeFolders(Long externalApplicationId, byte[] csvDatas)
     throws CvqModelException, CvqObjectNotFoundException {
   ExternalHomeFolder ehf;
   ExternalIndividual ei;
   ExternalApplication ea = getExternalApplicationById(externalApplicationId);
   Boolean created = false;
   Map<String, Integer> report = new HashMap<String, Integer>();
   report.put("created", 0);
   report.put("updated", 0);
   try {
     CSVReader csvReader = new CSVReader(new StringReader(new String(csvDatas)), ',', '"', 1);
     for (Object o : csvReader.readAll()) {
       String[] line = (String[]) o;
       ehf =
           genericDAO
               .simpleSelect(ExternalHomeFolder.class)
               .and("externalId", line[3])
               .and("externalApplication", ea)
               .unique();
       if (ehf == null) {
         ehf = new ExternalHomeFolder(line[3], ea, line[4]);
         ea.getExternalHomeFolders().add(ehf);
         genericDAO.create(ehf);
         modifyExternalApplication(ea);
         report.put("created", report.get("created") + 1);
         created = true;
       }
       ei =
           genericDAO
               .simpleSelect(ExternalIndividual.class)
               .and("externalId", line[0])
               .and("externalHomeFolder", ehf)
               .unique();
       if (ei == null) {
         ei =
             new ExternalIndividual(
                 line[0], line[1], line[2], line[6], line[7], "1".equals(line[5]) ? true : false);
         ei.setExternalHomeFolder(ehf);
         ehf.getIndividuals().add(ei);
         if (ei.isResponsible()) ehf.setAddress(line[4]);
         genericDAO.create(ei);
         genericDAO.update(ehf);
         if (!created) report.put("updated", report.get("updated") + 1);
       } else if (!ei.getLastName().equals(line[1])
           || !ei.getFirstName().equals(line[2])
           || (!ehf.getAddress().equals(StringUtils.upperCase(line[4])) && ei.isResponsible())) {
         ei.setLastName(line[1]);
         ei.setFirstName(line[2]);
         if (ei.isResponsible()) ehf.setAddress(line[4]);
         genericDAO.update(ei);
         genericDAO.update(ehf);
         report.put("updated", report.get("updated") + 1);
       }
       HibernateUtil.getSession().flush();
     }
   } catch (Exception e) {
     logger.error(e.getMessage());
     e.printStackTrace();
     HibernateUtil.getSession().getTransaction().rollback();
     throw new CvqModelException("external.error.csvImport");
   }
   return report;
 }