private boolean updateLeader(Division dbDiv, DivisionLdap ldapDiv) {
    String dbObjectSid;
    String ldapObjectSid;
    boolean forceUpdate;
    if (dbDiv.getLeader() != null) {
      dbObjectSid = dbDiv.getLeader().getObjectSid();
      forceUpdate = false;
    } else {
      logger.error("For employee [{}] no objectSid", dbDiv.getLeader());
      forceUpdate = true;
      dbObjectSid = "";
    }

    if (ldapDiv.getLeaderSid() != null) {
      ldapObjectSid = ldapDiv.getLeaderSid();
    } else {
      logger.error("No leader's objectSid for division [{}]", ldapDiv.getLdap_name());
      return false;
    }

    if (forceUpdate || !(dbObjectSid.equals(ldapObjectSid))) {
      Employee employee = employeeService.findByObjectSid(ldapObjectSid);
      dbDiv.setLeader(employee);
      return true;
    }
    return false;
  }
 private boolean updateName(Division dbDiv, DivisionLdap ldapDiv) {
   if ((dbDiv.getLdapName() == null) || !dbDiv.getLdapName().equals(ldapDiv.getLdap_name())) {
     dbDiv.setLdapName(ldapDiv.getLdap_name());
     return true;
   }
   return false;
 }
  public StringBuffer synchronize() {
    List<DivisionLdap> withBadLeader = new ArrayList<DivisionLdap>();
    List<Division> fromDb = divisionDAO.getDivisionsAll();
    List<DivisionLdap> fromLdap = divisionDAO.getDivisionsFromLDAP();
    StringBuffer sb = new StringBuffer("");

    List<Division> forUpdate = new ArrayList<Division>();
    List<DivisionLdap> forAppend = new ArrayList<DivisionLdap>();

    sb.append("Start synchronization divisions.\n\n");
    for (int indexLdap = 0; indexLdap < fromLdap.size(); indexLdap++) {
      DivisionLdap ldapDiv = fromLdap.get(indexLdap);
      if (ldapDiv.isLeaderVerified()) {
        boolean exist = false;
        for (int indexDb = 0; indexDb < fromDb.size(); indexDb++) {
          Division dbDiv = fromDb.get(indexDb);
          String dbSid = dbDiv.getLdap_objectSid();
          String ldapSid = ldapDiv.getLdapObjectSid();
          if (dbSid != null && dbSid.equals(ldapSid)) {
            if (dbDiv.isActive()) {
              boolean updated = false;
              updated = updated | updateName(dbDiv, ldapDiv);
              updated = updated | updateLeader(dbDiv, ldapDiv);
              if (updated) {
                forUpdate.add(dbDiv);
              }
            }
            exist = true;
            break;
          }
        }
        if (!exist) {
          forAppend.add(ldapDiv);
        }
      } else {
        withBadLeader.add(ldapDiv);
      }
    }

    sb.append(add(forAppend));
    sb.append(divisionDAO.setDivision(forUpdate));

    sendMailService.sendAdminAlert(withBadLeader);
    return sb;

    //		testIdGenerator();
    //		return new StringBuffer("Goo");
  }
 private StringBuffer add(List<DivisionLdap> forAppend) {
   StringBuffer sb = new StringBuffer();
   for (DivisionLdap ldapDiv : forAppend) {
     Division division = new Division();
     division.setName(ldapDiv.getLdap_name());
     division.setLdapName(ldapDiv.getLdap_name());
     division.setLdap_objectSid(ldapDiv.getLdapObjectSid());
     Employee leader = employeeService.findByObjectSid(ldapDiv.getLeaderSid());
     if (leader == null) {
       logger.debug("div name {}", ldapDiv.getLdap_name());
       logger.debug("not found leader sid {}", ldapDiv.getLeaderSid());
     }
     division.setLeader(leader);
     Set<Employee> employees = getEmpoyees(ldapDiv.getMembers());
     division.setEmployees(employees);
     division.setActive(true);
     sb.append(divisionDAO.setDivision(division));
   }
   return sb;
 }