package com.mincom.ellipse.myapp.connection;
import java.io.IOException;
import java.io.InterruptedIOException;
import com.mincom.mims.tech.mware.cbr.CBRException;
import com.mincom.mims.tech.mware.common.*;
/**
* The EllipseConnection class is used to establish a connection to Ellipse
*/
public class EllipseConnection {
private IMimsSession mySession;
private String buflib = "";
private String hostName;
private String portNumber;
private String hostUsername;
private String hostPassword;
/**
* EllipseConnection constructor
*/
public EllipseConnection() {
}
/**
* Set the location of the bfl files (for MSO access)
* @param location
*/
public void setBufferLibraryLocation(String location) {
buflib = location;
}
/**
* Connect to the Ellipse server
* @param TP
* @param hostName
* @param portNumber
* @param hostUsername
* @param hostPassword
* @throws EllipseConnectionException
*/
public void connect(String TP, String hostName, int portNumber, String hostUsername, String hostPassword) throws EllipseConnectionException {
MimsSessionFactory sessionFactory = MimsSessionFactory.getMimsSessionFactory();
mySession = sessionFactory.getMimsSession(TP,buflib);
try {
mySession.connect(hostName, portNumber, hostUsername, hostPassword);
} catch (InterruptedIOException e) {
throw new EllipseConnectionException(e.toString());
} catch (IOException e) {
throw new EllipseConnectionException(e.toString());
} catch (CBRException e) {
throw new EllipseConnectionException(e.toString());
}
}
/**
* This method attempts to login to the Ellipse server.
* @param mimsUser
* @param mimsPwd
* @param district
* @param position
* @throws EllipseConnectionException
*/
//login to mims
public void login(String mimsUser,String mimsPwd,String district,String position) throws EllipseConnectionException {
try {
mySession.login(mimsUser,mimsPwd,district,position);
} catch (CBRException e) {
throw new EllipseConnectionException(trimQuotes(e.toString()));
} catch (MwareException e) {
throw new EllipseConnectionException(trimQuotes(e.toString()));
} catch (ServerException e) {
throw new EllipseConnectionException(trimQuotes(e.getLocalizedMessage()));
} catch (IOException e) {
throw new EllipseConnectionException(trimQuotes(e.toString()));
}
}
/**
* This method attempts to disconnect from the Ellipse server.
* @throws EllipseConnectionException
*/
//disconnect from mims server
public void disconnect() throws EllipseConnectionException {
try {
mySession.disconnect();
} catch (IOException e) {
throw new EllipseConnectionException(e.toString());
} catch (CBRException e) {
throw new EllipseConnectionException(e.toString());
}
}
/**
* Get the current connection to Ellipse
* @return IMimsSession: The current Ellipse session
*/
public IMimsSession getSession() {
return mySession;
}
/**
* Trim the error message and remove double quotes
* from the ends of the message.
*/
private String trimQuotes(String errorMessage) {
// Start with initial trim
String trimMessage = errorMessage.trim();
// Strip double quotes (") from error message
if (trimMessage.startsWith("\"") && trimMessage.endsWith("\"")) {
trimMessage = trimMessage.substring(1, trimMessage.length()-1);
}
// Trim again on the way out
return trimMessage.trim();
}
}