//Copyright 2014 Paul Tegelaar


//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
//http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.

package nl.progaia.progress.client;



import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import nl.progaia.progress.exception.AppserverClientException;
import nl.progaia.progress.valueholder.ValueHolder;

import com.progress.open4gl.javaproxy.Connection;
import com.progress.open4gl.javaproxy.OpenAppObject;
import com.progress.open4gl.javaproxy.ParamArray;

public class AppserverClient {
	private static final Logger logger = Logger.getAnonymousLogger();
	
	private String appserverUrl;
	private String username;
	private String password;
	private String appServerInfo;
	private String service;
	private int session;
	private Connection connection;
	private OpenAppObject openAppObject;
	
	   private static AppserverClient instance = null;
	   
	   private AppserverClient() {
	      // Exists only to defeat instantiation.
	   }
	   
	   public static AppserverClient getInstance() {
	      if(instance == null) {
	         instance = new AppserverClient();
	      }
	      return instance;
	   }
	

	
	public synchronized void init(String appserverUrl, String username, String password, String appServerInfo, String service, int session){
		this.appServerInfo = appServerInfo;
		this.appserverUrl = appserverUrl;
		this.username = username;
		this.password = password;
		this.service = service;
		this.session = session;
	}
	
	public synchronized ParamArray callProcedure(ParamArray paramArray, Procedure procedure) throws AppserverClientException{
		try {	
			
			if(openAppObject==null)
				create();
									
			if(openAppObject!=null){
				
				if(logger.isLoggable(Level.FINE)){
					logger.fine("Calling procedure: " + procedure.getName());
					logger.fine(procedure.toString());
				}
				openAppObject.runProc(procedure.getName(), paramArray);	
				return paramArray;
			}else{
				throw new AppserverClientException("Connection to appserver not established.");
			}								
		} catch (Exception e) {
			throw new AppserverClientException(e);
		}
	}
	
	public synchronized Map<Integer, ValueHolder<?>> callProcedure(Map<Integer, ValueHolder<?>> values, Procedure procedure) throws AppserverClientException{		
		ParamArray paramArray = Mapper.from(procedure, values);
		return Mapper.from(procedure,callProcedure(paramArray, procedure));
	}
	
	private void create() throws AppserverClientException{
		
		try {
			if(logger.isLoggable(Level.FINE))
				logger.fine("Connecting to appserver.");
			connection = new Connection(appserverUrl, username, password, appServerInfo);
			connection.setSessionModel(session);
			openAppObject = new OpenAppObject(connection, service);
		} catch (Exception e) {
			logger.severe(e.getMessage());
			throw new AppserverClientException(e);
		}
	}	
	
	
	private void destroy() throws AppserverClientException{
		try {
			if(logger.isLoggable(Level.FINE))
				logger.fine("Disconnecting from appserver.");
			if (openAppObject != null) {
				openAppObject._release();
				openAppObject = null;
			}
			
			if (connection != null) {
				connection.releaseConnection();
				connection = null;
			}
		} catch (Exception e) {
			logger.warning(e.getMessage());
			throw new AppserverClientException(e);
		}
	}	
		
	
}

@@ -2173,16 +2173,121 @@
 t==null)
+%7B%0D%0A%09%09%09%09if(logger.isLoggable(Level.FINE))%0D%0A%09%09%09%09%09logger.fine(%22No active connection with appserver.%22);%0D%0A%09%09%09%09
 %0D%0A%09%09%09%09cr
@@ -2291,24 +2291,36 @@
 %09create();%0D%0A
+%09%09%09%7D%0D%0A%09%09%09%09%0D%0A
 %09%09%09%09%09%09%09%09%09%0D%0A%09
@@ -3228,24 +3228,29 @@
 server.%22);%0D%0A
+%09%09%09%0D%0A
 %09%09%09connectio
@@ -3539,30 +3539,29 @@
 %7D%09%0D%0A%09%0D%0A%09%0D%0A%09p
-rivate
+ublic
  void destro
@@ -3597,32 +3597,40 @@
 eption%7B%0D%0A%09%09try %7B
+%09%09%09%0D%0A%09%09%09
 %0D%0A%09%09%09if(logger.i
@@ -3704,16 +3704,21 @@
 er.%22);%0D%0A
+%09%09%09%0D%0A
 %09%09%09if (o

fix on destroy