/*
 * RmiMutableStore.java
 *
 * Created on October 14, 2003, 7:23 PM
 */

package hep.aida.ref.remote.testRemote;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Hashtable;

import hep.aida.IManagedObject;

import hep.aida.ref.remote.RemoteClient;
import hep.aida.ref.remote.RemoteManagedObject;
import hep.aida.ref.remote.RemoteMutableStore;
import hep.aida.ref.remote.RemoteServer;
import hep.aida.ref.remote.interfaces.AidaTreeClient;
import hep.aida.ref.remote.interfaces.AidaTreeServer;
import hep.aida.ref.remote.testRemote.converters.RemoteObjectTestConverter;
import hep.aida.ref.remote.testRemote.converters.TestConverter;

import org.freehep.util.FreeHEPLookup;
import org.openide.util.Lookup;

/**
 *
 * @author  serbo
 */
public class RemoteMutableStoreTest extends RemoteMutableStore {
    
    private Map converters;
    private AidaTreeServer testServer;

    /** Creates a new instance of RmiMutableStore */
    public RemoteMutableStoreTest() {
        this(null);
    }
    
    public RemoteMutableStoreTest(AidaTreeServer testServer) {
        super();
        init();
        this.testServer = testServer;
    }
    
    public void init() {
        super.init();
        converters = new Hashtable();        
    }
    
    
    // Abstract methods from RemoteMutableStore
    
    protected RemoteClient createClient(Map options) {
        RemoteClient client = null;  
        
        // Defaults
        String clientType = "AidaTreeClientTest";
        boolean duplex = true;
        long serverUpdateInterval = -1;
        
        // Parse options 
        if (options == null)
            throw new IllegalArgumentException("No information about AidaTreeServer, options NULL");
        
        Iterator it = options.keySet().iterator();
        while (it.hasNext()) {
            String key = ((String) it.next());
            String value = ((String) options.get(key)).trim();
            System.out.println("Key = "+key+" \tValue = "+value);
            try {
            if (key.equalsIgnoreCase("serverUpdateInterval")) { 
                serverUpdateInterval = Long.parseLong(value);
            } else if (key.equalsIgnoreCase("clientType")) { 
                clientType = value;
            } else if (key.equalsIgnoreCase("duplex")) {
                if (value.equalsIgnoreCase("true")) duplex = true;
                else duplex = false;
            }
            } catch (Exception e) {
                System.out.println("Can not parce options: Key="+key+"\tValue = "+value+", will use defaults");
                e.printStackTrace();
            }
        }

        if (clientType.equalsIgnoreCase("RemoteClientTest")) {
            client = new RemoteClientTest(this, duplex, testServer, serverUpdateInterval);
        } 
        
        return client;
    }
    
    public IManagedObject createObject(String name, String aidaType) throws IllegalArgumentException {
        //System.out.println("RemoteMutableStoreTest.createObject:     path="+name+",   type="+aidaType);
        
        // Find Convertor for this AIDA Type
        TestConverter converter = findConverter(aidaType);

        // Create object
        IManagedObject mo = (IManagedObject) converter.createAidaObject(name);
        if (mo instanceof RemoteManagedObject) ((RemoteManagedObject) mo).setStore(this);
        return mo;
    }
    
    public void updateData(String path, String aidaType) throws IllegalArgumentException {
        //System.out.println("RemoteMutableStoreTest.updateData:     path="+path+",   type="+aidaType);
        
        // Find Convertor for this AIDA Type
        TestConverter converter = findConverter(aidaType);

        // Get local AIDA Tree object and remote data
        IManagedObject mo = tree.find(path);
        Object data = client.find(path);
        //System.out.println("RemoteMutableStoreTest.updateData:    mo="+mo+",  data="+data);
        
        converter.updateAidaObject(mo, data);
        if (mo instanceof RemoteManagedObject) ((RemoteManagedObject) mo).setDataValid(true);
    }
    
    

    // IMutableStore methods
    public void close() throws IOException {
        super.close();
        converters.clear();
        converters = null;
    }

    
    // Service methods
    
    /** Find Convertor for this AIDA Type */
    private TestConverter findConverter(String aidaType) {
        TestConverter converter = null;
        if (converters.containsKey(aidaType)) {
            converter = (TestConverter) converters.get(aidaType);
        } else {
            Lookup.Template template = new Lookup.Template(TestConverter.class, aidaType, null);
            Lookup.Item item = FreeHEPLookup.instance().lookupItem(template);
            if (item == null) throw new IllegalArgumentException("No Converter for AIDA Type: "+aidaType);

            converter = (TestConverter) item.getInstance();
            converters.put(aidaType, converter);
        }
        return converter;
     }
}
