package ch.uzh.csg.reimbursement.ldap;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ldap.CommunicationException;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import ch.uzh.csg.reimbursement.service.UserService;

@Component
public class LdapSynchronizer {

	@Autowired
	private LdapTemplate ldapTemplate;

	@Autowired
	private UserService userService;

	private final Logger logger = LoggerFactory.getLogger(LdapSynchronizer.class);

	@Scheduled(fixedRateString = "${reimbursement.ldap.refreshRate}")
	public void synchronizeDomainWithLdap() {
		List<LdapPerson> ldapPersons = getLdapPersons();
		if(ldapPersons.size() > 0) {
			userService.synchronize(ldapPersons);
		}
		else {
		}
	}

	private List<LdapPerson> getLdapPersons() {
		LdapPersonAttributesMapper mapper = new LdapPersonAttributesMapper();
		List<LdapPerson> list = new ArrayList<LdapPerson>();
		try {
			list = ldapTemplate.search("ou=People", "(&(objectClass=hostObject)(objectClass=inetOrgPerson))", mapper);
			list.removeAll(Collections.singleton(null));
		}
		catch(CommunicationException ex) {
			logger.error("Could not connect to the LDAP server. Check the connection.", ex);
		}
		return list;
	}

}

@@ -841,91 +841,8 @@
 ) %7B%0A
-%09%09List%3CLdapPerson%3E ldapPersons = getLdapPersons();%0A%09%09if(ldapPersons.size() %3E 0) %7B%0A%09
 %09%09us
@@ -863,17 +863,20 @@
 hronize(
-l
+getL
 dapPerso
@@ -881,27 +881,12 @@
 sons
+()
 );
-%0A%09%09%7D%0A%09%09else %7B%0A%09%09%7D
 %0A%09%7D%0A

fix faulty commit