package org.csstudio.utility.ldapUpdater;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import org.csstudio.platform.logging.CentralLogger;
import org.csstudio.utility.ldap.reader.ErgebnisListe;
import org.csstudio.utility.ldap.reader.LDAPReader;
import org.csstudio.utility.ldapUpdater.model.DataModel;
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;

public class ReadLdapDatabase implements Observer{

	private DataModel _model;
	private ErgebnisListe _ergebnis;

	public ReadLdapDatabase(DataModel model) {
	 _model=model;
	}

	public void readLdap() {
        _ergebnis = new ErgebnisListe();
        _ergebnis.addObserver(this);

        LDAPReader reader = new LDAPReader("ou=EpicsControls","econ=*",_ergebnis);
        reader.addJobChangeListener(new JobChangeAdapter() {
            public void done(final IJobChangeEvent event) {
                if (event.getResult().isOK()) {
                    _ergebnis.notifyView();
                }
                else {
                    CentralLogger.getInstance().error(this, "LDAP: NOT event.getResult().isOK() ");
                }             
            }
         });
        reader.schedule();
    	}

//  @Override
    public void update(Observable o, Object arg) {
    	CentralLogger.getInstance().info(this, "Start Update");
        ArrayList<String> list = _ergebnis.getAnswer();
        _model.setLdapList(list);
        
        for (String record : list) {
            CentralLogger.getInstance().debug(this, "LDAP: "+record);
            
        }
        
        CentralLogger.getInstance().info(this, "IOC names in LDAP tree : "+list.size());   
        
        _model.setReady(true);
        CentralLogger.getInstance().info(this, "ready="+_model.isReady());
    }
}

@@ -738,16 +738,22 @@
         
+final 
 LDAPRead
@@ -819,16 +819,16 @@
 ebnis);%0A
-
         
@@ -936,24 +936,53 @@
 nt event) %7B%0A
+                int trys =3;%0A
             
@@ -1102,16 +1102,255 @@
  else %7B%0A
+                    if(trys%3C3) %7B%0A                        CentralLogger.getInstance().debug(this, %22No LDAP connection! Try: %22+trys);%0A                        trys++;%0A                        reader.schedule();%0A                    %7Delse %7B%0A    
         
@@ -1437,24 +1437,46 @@
 .isOK() %22);%0A
+                    %7D%0A
             

send only 3 logmsg on no connection