package include.dbcommunication;
import village.webservice.room.RoomInfo;
import javax.naming.*;
import java.util.Collection;
import java.util.ArrayList;
import java.util.Iterator;
import include.auth.Validation;
import village.webservice.VillageServiceInterfaceProxy;
public class RoomInfoGetter {
/**
* Function collects information about rooms on specified server.
*
* @param sid session identifier
* @param server server from which rooms are collected
* @return array with information about rooms
*/
public RoomInfo[] getRoomsInfo (String sid, String server) {
RoomInfo result[] = new RoomInfo[1];
ArrayList<RoomInfo> rooms = new ArrayList<RoomInfo>();
Collection<database.Room> dbRooms;
database.Configuration db = null;
Iterator i = null;
include.net.Server serv = new include.net.Server();
Validation val = new Validation();
RoomInfo tmpRi = null;
database.Room tmpDb = null;
System.out.println("[RoomInfoGetter] Executing function 'getRoomsInfo'. Parameters:");
System.out.println("[RoomInfoGetter] sid: '" + sid + "'");
System.out.println("[RoomInfoGetter] server: '" + server + "'");
try {
InitialContext ctx = new InitialContext();
db = (database.Configuration) ctx.lookup("ear3/CMPConfiguration/local");
} catch (NamingException e) {
e.printStackTrace();
}
if (serv.isMyName(server)) {
System.out.println("[RoomInfoGetter] Local execution");
if (val.validate(sid) != null) {
System.out.println("[RoomInfoGetter] Session is valid.");
System.out.println("[RoomInfoGetter] Gathering rooms.");
dbRooms = db.getRooms();
if (dbRooms == null)
System.out.println("[RoomInfoGetter] No windows found.");
else {
i = dbRooms.iterator();
if (i == null)
System.out.println("[RoomInfoGetter] Error in generating iterator.");
else {
System.out.println("[RoomInfoGetter] Converting rooms.");
while (i.hasNext()) {
tmpDb = (database.Room) i.next();
tmpRi = new RoomInfo(tmpDb.getName() + '@' + serv.giveName(), tmpDb.getDescription());
rooms.add(tmpRi);
}
}
}
result = rooms.toArray(new RoomInfo[0]);
System.out.println("[RoomInfoGetter] result[0].getRid()="+result[0].getRid());
System.out.println("[RoomInfoGetter] End of function 'getRoomsInfo'");
} else {
System.out.println("[RoomInfoGetter] Session is invalid.");
System.out.println("[RoomInfoGetter] End of function 'getRoomsInfo'");
}
} else {
System.out.println("[RoomInfoGetter] Remote execution.");
VillageServiceInterfaceProxy prox = new VillageServiceInterfaceProxy();
village.webservice.VillageServiceInterface iface =
prox.setEndpoint("http://"+server+prox.villagePort+prox.villageSuffix);
try {
result = iface.getRoomList(sid);
} catch(Exception cex){
cex.printStackTrace();
}
}
return result;
}
}