package include.dbcommunication;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import village.webservice.room.Room;
import village.webservice.room.RoomRequest;
import village.webservice.VillageServiceInterfaceProxy;
public class RoomsGetter {
/**
* Function collects rooms specified in request.
*
* @param sid session identifier
* @param rids array of rids - information which rooms shuld be collected
* @return array of rooms
*/
public Room[] getRooms (String sid, RoomRequest[] rids) {
database.Configuration db = null;
ArrayList<Room> rooms = new ArrayList<Room>();
Room tmpRoom = null;
// database.Window dbWindow = null;
database.Room dbRoom = null;
// include.auth.Validation valid = new include.auth.Validation();
Room[] result = new Room[1];
RoomRequest remoteCall[] = new RoomRequest[1];
Room[] remoteResult = null;
include.net.Server serv = new include.net.Server();
Collection<database.Room> roomColl = null;
utils.Utils u = new utils.Utils();
String server = null;
String rid = null;
String code = null;
String body = null;
Integer i = 0;
System.out.println("[RoomsGetter] Executing function 'getRooms'. Parameters:");
System.out.println("[RoomsGetter] sid: '" + sid + "'");
System.out.println("[RoomsGetter] rid[0]: '" + rids[0].getRid() + "'");
try {
InitialContext ctx = new InitialContext();
db = (database.Configuration) ctx.lookup("ear3/CMPConfiguration/local");
} catch (NamingException e) {
e.printStackTrace();
}
for (i = 0; i < rids.length; i++) {
rid = rids[i].getRid();
if (rid.indexOf('@') == -1){
System.out.println("[RoomsGetter] Local room, rid = "+rid+".");
// dbWindow = db.getWindow(rid, valid.validate(sid));
// dbRoom = dbWindow.getRoom();
roomColl = db.getRooms();
Boolean found = false;
Iterator j = roomColl.iterator();
while ((j.hasNext()) && (!found)) {
dbRoom = (database.Room) j.next();
if ((dbRoom.getName() + '@' + serv.giveName()).equals(rid))
found = true;
}
rid = dbRoom.getName()+"@"+serv.giveName();
tmpRoom = new Room();
tmpRoom.setRid(rid);
tmpRoom.setVersion(dbRoom.getVersion());
body = dbRoom.getBody();
body = body.replace("%safePortletName%", u.getSafeName(rid));
body = body.replace("%portletName%", rid);
tmpRoom.setBody(body);
code = dbRoom.getCode();
code = code.replace("%safePortletName%", u.getSafeName(rid));
code = code.replace("%portletName%", rid);
tmpRoom.setCode(code);
rooms.add(tmpRoom);
} else {
server = rid.substring(rid.indexOf('@') + 1);
if (serv.isMyName(server)) {
System.out.println("[RoomsGetter] Local room, rid = "+rid+".");
// dbWindow = db.getWindow(rid.substring(0, rid.indexOf('@')),
// valid.validate(sid));
// dbRoom = dbWindow.getRoom();
roomColl = db.getRooms();
Boolean found = false;
Iterator j = roomColl.iterator();
while ((j.hasNext()) && (!found)) {
dbRoom = (database.Room) j.next();
if ((dbRoom.getName() + '@' + serv.giveName()).equals(rid))
found = true;
}
rid = dbRoom.getName()+"@"+server;
tmpRoom = new Room();
tmpRoom.setRid(rid);
tmpRoom.setVersion(dbRoom.getVersion());
body = dbRoom.getBody();
body = body.replace("%safePortletName%", u.getSafeName(rid));
body = body.replace("%portletName%", rid);
tmpRoom.setBody(body);
code = dbRoom.getCode();
code = code.replace("%safePortletName%", u.getSafeName(rid));
code = code.replace("%portletName%", rid);
tmpRoom.setCode(code);
rooms.add(tmpRoom);
} else {
System.out.println("[RoomsGetter] Remote room, rid = "+rid+".");
VillageServiceInterfaceProxy prox = new VillageServiceInterfaceProxy();
village.webservice.VillageServiceInterface iface =
prox.setEndpoint("http://"+server+prox.villagePort+prox.villageSuffix);
remoteCall[0] = rids[i];
try {
remoteResult = iface.getRooms(sid, remoteCall);
} catch(Exception cex){
cex.printStackTrace();
}
for (Integer j = 0; j < remoteResult.length; j++) {
rooms.add(remoteResult[j]);
}
}
}
}
System.out.println("[RoomsGetter] Function 'getRooms' returned; value==null: " + (result==null));
result = rooms.toArray(result);
System.out.println("[RoomsGetter] result==null: "+(result==null));
System.out.println("[RoomsGetter] result[0].getRid() = "+(result[0].getRid()));
return result;
}
}