[virtualcommons-svn] SF.net SVN: virtualcommons:[289] mentalmodels/trunk/src/main
Status: Beta
Brought to you by:
alllee
From: <see...@us...> - 2009-10-08 18:29:47
|
Revision: 289 http://virtualcommons.svn.sourceforge.net/virtualcommons/?rev=289&view=rev Author: seematalele Date: 2009-10-08 18:29:40 +0000 (Thu, 08 Oct 2009) Log Message: ----------- Refactrored the services. Assigngroups fucntionality is working but somehow blocks pushing is not working. Modified Paths: -------------- mentalmodels/trunk/src/main/java/edu/asu/commons/mme/entity/Group.java mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/GameService.java mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/RoundService.java mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/StartGame.java mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/StudentService.java mentalmodels/trunk/src/main/webapp/WEB-INF/applicationContext.xml mentalmodels/trunk/src/main/webapp/WEB-INF/classes/log4j.properties Added Paths: ----------- mentalmodels/trunk/src/main/java/edu/asu/commons/mme/utility/ mentalmodels/trunk/src/main/java/edu/asu/commons/mme/utility/MessageHandler.java Modified: mentalmodels/trunk/src/main/java/edu/asu/commons/mme/entity/Group.java =================================================================== --- mentalmodels/trunk/src/main/java/edu/asu/commons/mme/entity/Group.java 2009-10-08 00:29:17 UTC (rev 288) +++ mentalmodels/trunk/src/main/java/edu/asu/commons/mme/entity/Group.java 2009-10-08 18:29:40 UTC (rev 289) @@ -7,6 +7,7 @@ import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; +import javax.persistence.OrderBy; import javax.persistence.Table; @@ -26,6 +27,7 @@ private Long id; @Column(name="grp_number",nullable=false) + @OrderBy("number") private Integer number; /* @ManyToOne Modified: mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/GameService.java =================================================================== --- mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/GameService.java 2009-10-08 00:29:17 UTC (rev 288) +++ mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/GameService.java 2009-10-08 18:29:40 UTC (rev 289) @@ -1,11 +1,1118 @@ package edu.asu.commons.mme.service; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.hibernate.Hibernate; +import org.springframework.transaction.annotation.Transactional; + +import edu.asu.commons.mme.dao.HibernateBlockDao; +import edu.asu.commons.mme.dao.HibernateGameConfigDao; import edu.asu.commons.mme.dao.HibernateGameDao; +import edu.asu.commons.mme.dao.HibernateGameRoundDao; +import edu.asu.commons.mme.dao.HibernateGroupDao; +import edu.asu.commons.mme.dao.HibernateLocationDao; +import edu.asu.commons.mme.dao.HibernateModuleDao; +import edu.asu.commons.mme.dao.HibernateModuleRoundConfigDao; +import edu.asu.commons.mme.dao.HibernateRoundConfigDao; +import edu.asu.commons.mme.dao.HibernateStudentDao; +import edu.asu.commons.mme.entity.Block; +import edu.asu.commons.mme.entity.BlockInformationWindow; +import edu.asu.commons.mme.entity.CategoricalOption; +import edu.asu.commons.mme.entity.CategoricalQuestion; import edu.asu.commons.mme.entity.Game; +import edu.asu.commons.mme.entity.GameConfig; +import edu.asu.commons.mme.entity.GameRound; +import edu.asu.commons.mme.entity.Group; +import edu.asu.commons.mme.entity.Location; +import edu.asu.commons.mme.entity.Module; +import edu.asu.commons.mme.entity.ModuleRoundConfig; +import edu.asu.commons.mme.entity.Question; +import edu.asu.commons.mme.entity.QuestionGroup; +import edu.asu.commons.mme.entity.Round; +import edu.asu.commons.mme.entity.Student; +import edu.asu.commons.mme.utility.MessageHandler; +import flex.messaging.MessageBroker; +import flex.messaging.messages.AsyncMessage; +import flex.messaging.util.UUIDUtils; +@Transactional public class GameService extends Service.Base<Game, HibernateGameDao>{ + + private HibernateModuleDao moduleDao; + private HibernateBlockDao blockDao; + private HibernateRoundConfigDao roundConfigDao; + private HibernateModuleRoundConfigDao moduleRoundDao; + private HibernateGameRoundDao gameRoundDao; + private HibernateGameConfigDao gameConfigDao; + private HibernateGroupDao groupDao; + private HibernateStudentDao studentDao; + private HibernateLocationDao locationDao; + + private int blockSeqNo; - + private RoundService roundService; + public GameConfig createGameConfig(GameConfig gameConfig) + { + GameConfig newGameConfig = null; + try{ + newGameConfig = new GameConfig(); + newGameConfig.setImageLocation(null); + newGameConfig.setTimestamp(gameConfig.getTimestamp()); + newGameConfig.setMaxDays(gameConfig.getMaxDays()); + newGameConfig.setMaxFishHarvest(gameConfig.getMaxFishHarvest()); + newGameConfig.setNumberOfLocations(gameConfig.getNumberOfLocations()); + getGameConfigDao().save(newGameConfig); + initializeGameRounds(newGameConfig); + setAllLocations(newGameConfig); + + }catch(Exception e) + { + e.printStackTrace(); + } + return newGameConfig; + } + + + public Game createGame(Game game) + { + //getLogger().debug("game came from flex is : " + game.getGameCode()); + + Game newGame = new Game(); + //currentGame = game; + try + { + newGame.setGameCode(game.getGameCode()); + newGame.setMoney(game.getMoney()); + newGame.setTimestamp(game.getTimestamp()); + + //FIXME: Currently using default GameConfiguration whose id is 1 + GameConfig gameConfig = gameConfigDao.find(1L); + newGame.setGameConfig(gameConfig); + getDao().save(newGame); + String str = newGame.getGameCode()+newGame.getId(); + + if(getDao().findAllByProperty("gameCode", str).size() != 0) + { + //getLogger().debug("in if loop "); + newGame = null; + } + else + { + newGame.setGameCode(str); + getDao().save(newGame); + getLogger().info("Created the game: " + newGame.getId()); + setAllLocations(gameConfig); + initializeGame(newGame); + } + }catch(Exception e) + { + e.printStackTrace(); + } + getLogger().debug("current game from flex is: " + newGame.getId()); + return newGame; + } + + + /** + * 1)Initially first block will get pushed to all the clients. + * 2)get timer for that block + * 3)check if the block is day by day decision game if yes then start day by day decision service + * 4)if it is communication, start communication service + * 3)start the timer + * 4)Once the timer is over, push the next block + * + */ + public void startGame(Game game) { + + assignGroups(game); +/* getRoundService().setGame(game); + getRoundService().run(); +*/ + Game newGameState = null; + Block block = null; + boolean running = false; + getLogger().info("game start is: " + game.getId()); + newGameState = getDao().find(game.getId()); + MessageHandler msgHandler = new MessageHandler(); + msgHandler.setDestination("mme"); + msgHandler.setSubtopic(getGameCode(newGameState)); + int noOfBlocks = getNumberOfBlock(game); + + int i=0; + try { + while (i<noOfBlocks) { + + i++; + newGameState = getBlock(game); + game = newGameState; + if(newGameState == null) + { + msgHandler.send(null); + } + else + { + block = newGameState.getCurrentBlock(); + msgHandler.send(block); + } + + if(block.getDescription().equalsIgnoreCase("Day-by-day decisions game")) + { + //start day by day decision + //getDayBydayDecisionService(). + + } + else if (block.getDescription().equalsIgnoreCase("Communication")) + { + //start communication round + + } + //get the timer for the block + Integer timer = getTimerforBlock(block); + //start the timer for the block sent + + //for testing purpose 5000 is given as an argument + Thread.sleep(10000); + if(block == null) + { + running = false; + } + } + }catch (InterruptedException e) { + } + } + + private int getNumberOfBlock(Game game) { + // TODO Auto-generated method stub + + //game.getGameConfig() + return 0; + } + + + public void assignGroups(Game currentGame) + { + + List<Student> studentWithGroupsAssigned = new ArrayList<Student>(); + List<Student> students = new ArrayList<Student>(); + Game game = getDao().find(currentGame.getId()); + try{ + //getLogger().debug("In the assigngroups." + game.getId()); + + + students = getStudentDao().findAllByProperty("game", game); + + //getLogger().debug("test student object is : " + students.size()); + if(students.size() > 1) + { + //write an algorithm to form groups + int totalStudents = students.size(); + int noOfGroupsWithSize3=0; + int noOfGroupsWithSize4=0; + int noOfGroupsWithSize5=0; + int x; + + + //no of groups + noOfGroupsWithSize5=((int)(totalStudents/5)); + + + x= totalStudents%5; + + /* if(x==0) + { + totalGroups=groupSize5; + } + else*/ + if(x==1) + { + noOfGroupsWithSize5=noOfGroupsWithSize5 - 1; + noOfGroupsWithSize3=2; + // totalGroups=groupSize5 + groupSize3; + } + + else if(x==2) + { + noOfGroupsWithSize5=noOfGroupsWithSize5-1; + noOfGroupsWithSize4=1; + noOfGroupsWithSize3=1; + // totalGroups=groupSize5 + groupSize3 + groupSize4; + } + else if(x==3) + { + noOfGroupsWithSize3=1; + // totalGroups=groupSize3 + groupSize5; + } + + else if(x==4) + { + noOfGroupsWithSize4=1; + // totalGroups=groupSize4 + groupSize5; + } + + + /*getLogger().debug("size of 4 : " + noOfGroupsWithSize4); + getLogger().debug("size of 3 : " + noOfGroupsWithSize3); + getLogger().debug("size of 5 : " + noOfGroupsWithSize5); + */ + + int groupNo = 0; + + //assign every student a number and group + if(!students.isEmpty()) + { + if(noOfGroupsWithSize5 > 0) + { + + for(Student student5:assignGroupstoStudents(5,noOfGroupsWithSize5,students,groupNo)) + studentWithGroupsAssigned.add(student5); + + //getLogger().debug("student size after 5 is: " + students.size()); + getLogger().debug("student with groups assigned: " + studentWithGroupsAssigned.size()); + } + groupNo = noOfGroupsWithSize5; + if(noOfGroupsWithSize4 > 0) + { + for(Student student4:assignGroupstoStudents(4,noOfGroupsWithSize4,students,groupNo)) + studentWithGroupsAssigned.add(student4); + + //getLogger().debug("student size after 4 is: " + students.size()); + getLogger().debug("student with groups assigned: " + studentWithGroupsAssigned.size()); + } + groupNo = noOfGroupsWithSize5 + noOfGroupsWithSize4; + if(noOfGroupsWithSize3 > 0) + { + for(Student student3:assignGroupstoStudents(3,noOfGroupsWithSize3,students,groupNo)) + studentWithGroupsAssigned.add(student3); + //getLogger().debug("student size after 3 is: " + students.size()); + getLogger().debug("student with groups assigned: " + studentWithGroupsAssigned.size()); + } + } + } + getLogger().debug("Final student with groups assigned: " + studentWithGroupsAssigned.size()); + }catch(Exception e) + { + getLogger().error(e); + } + + if(students != null) + { + /*MessageBroker msgBroker = MessageBroker.getMessageBroker("_messageBroker"); + + String clientID = UUIDUtils.createUUID(); + AsyncMessage msg = new AsyncMessage(); + msg.setDestination("mme"); + String msgDestination = game.getGameCode(); + msg.setHeader("DSSubtopic", msgDestination); + msg.setClientId(clientID); + msg.setMessageId(UUIDUtils.createUUID()); + msg.setTimestamp(System.currentTimeMillis()); + for(Student student: studentWithGroupsAssigned) + { + initializeStudent(student); + } + msg.setBody(studentWithGroupsAssigned); + System.out.println("Message broker is: "+ msgBroker); + System.out.println("Students size is: " + studentWithGroupsAssigned.size()); + + msgBroker.routeMessageToService(msg, null);*/ + MessageHandler msgHandler = new MessageHandler(); + msgHandler.setDestination("mme"); + msgHandler.setSubtopic(game.getGameCode()); + /*for(Student student: studentWithGroupsAssigned) + { + initializeStudent(student); + }*/ + msgHandler.send(studentWithGroupsAssigned); + } + } + + + private List<Student> assignGroupstoStudents(int groupSize, int noOfGroups,List<Student> students,int groupNo) { + // TODO Auto-generated method stub + Student student = new Student(); + List<Student> studentWithGroupsAssigned = new ArrayList<Student>(); + getLogger().debug("group size is : "+groupSize + " and group number is : "+ groupNo); + + for(int counter = 0; counter < noOfGroups; counter++) + { + //create group + Group grp = new Group(); + + grp.setNumber(++groupNo); + groupDao.save(grp); + + getLogger().info("Group is created with id: " + grp.getId()); + int studentNo = 1; + int j = 0; + for(int i = 0; i <= groupSize-1 ; i++) + { + //get a student from a list + student = students.get(0); + getLogger().debug("i is: " + i +"student id is : "+student.getId()); + if(student.getGroup() == null) + { + //assign student no and group to each student + getLogger().debug("group assigned is : " + grp.getId()); + student.setStudentNo(studentNo); + student.setGroup(grp); + getDao().save(student); + studentNo++; + initializeStudent(student); + studentWithGroupsAssigned.add(student); + students.remove(0); + } + j++; + } + } + return studentWithGroupsAssigned; + } + + private void initializeStudent(Student student) { + Hibernate.initialize(student); + Hibernate.initialize(student.getGame()); + Hibernate.initialize(student.getGame().getGameConfig()); + Hibernate.initialize(student.getGame().getGameConfig().getLocations()); + Hibernate.initialize(student.getGroup()); + } + + public Group createGroup(int groupNo) + { + Group grp = new Group(); + grp.setNumber(groupNo); + groupDao.save(grp); + getLogger().info("Group is created with id: " + grp.getId()); + return grp; + } + + private void initializeGameRounds(GameConfig gameConfig) { + // TODO Auto-generated method stub + List<GameRound> gameRounds = new ArrayList<GameRound>(); + for(Round round:getRoundConfigDao().findAll()) + { + GameRound gameRound = new GameRound(); + gameRound.setGameConfig(gameConfig); + gameRound.setRound(round); + gameRoundDao.save(gameRound); + gameRounds.add(gameRound); + } + getGameConfigDao().save(gameConfig); + } + + private List<Location> setAllLocations(GameConfig gameConfig) + { + //FIXME: For the pilot experiment locations are set manually but in future might want to take inputs from user + List<Location> locations = new ArrayList<Location>(); + try{ + Location harbor = new Location(); + harbor.setLocationName("Harbor"); + harbor.setCurrentPopulation(0.0); + harbor.setGrowthRate(0.0); + harbor.setInitialPopulation(0.0); + harbor.setMaxCapacity(0.0); + harbor.setGameConfig(gameConfig); + getLocationDao().save(harbor); + getLogger().info("Saved Location "+harbor.getLocationName()+" with id " + harbor.getId()); + locations.add(harbor); + + Location bay1 = new Location(); + bay1.setLocationName("Bay1"); + bay1.setCurrentPopulation(5.0); + bay1.setInitialPopulation(5.0); + bay1.setGrowthRate(0.5); + bay1.setMaxCapacity(10.0); + bay1.setGameConfig(gameConfig); + getLocationDao().save(bay1); + getLogger().info("Saved Location " + bay1.getLocationName() +" with id "+ bay1.getId()); + locations.add(bay1); + + Location bay2 = new Location(); + bay2.setLocationName("Bay2"); + bay2.setCurrentPopulation(10.0); + bay2.setInitialPopulation(10.0); + bay2.setGrowthRate(0.15); + bay2.setMaxCapacity(20.0); + bay2.setGameConfig(gameConfig); + getLocationDao().save(bay2); + getLogger().info("Saved Location " + bay2.getLocationName() +" with id "+ bay2.getId()); + locations.add(bay2); + + Location bay3 = new Location(); + bay3.setLocationName("Bay3"); + bay3.setCurrentPopulation(15.0); + bay3.setInitialPopulation(15.0); + bay3.setGrowthRate(0.05); + bay3.setMaxCapacity(30.0); + bay3.setGameConfig(gameConfig); + getLocationDao().save(bay3); + getLogger().info("Saved Location " + bay3.getLocationName() +" with id "+ bay3.getId()); + locations.add(bay3); + }catch(Exception e) + { + e.printStackTrace(); + } + + return locations; + } + + public Module getNextModule(int sequenceNo) + { + return getModule(sequenceNo); + } + + public void test() + { + getLogger().debug("test to check if module service is working...." ); + } + + /* + *1) Check if the current module round config is null, this object keeps track of which round + * and which module in that round is going on + *2) Find out the current module number + *3) check if it is finished, if yes find out the current round number + *4) Find out the current block number + *5) Return the next block + */ + + public Game getBlock(Game game) + { + + ModuleRoundConfig currentModuleRoundConfig; + Block currentBlock = null; + Round currentRound = null; + + //check if it is a start of the game + if(game.getCurrentRound() == null && game.getCurrentBlock() == null ) + { + getLogger().info("Current Round is null... "); + getLogger().info("Current block is null... "); + getLogger().info("Starting of the game...." + game.getGameCode()); + + currentRound = getNextRound(game); + getLogger().info("Next Round object is "+ currentRound.getId()); + + currentModuleRoundConfig = getModuleRoundConfig(0, currentRound); + getLogger().info("Next ModuleRound Config object is "+ currentModuleRoundConfig.getId()); + + Module currentModule = getModuleForCurrentRound(currentModuleRoundConfig); + getLogger().info("Next Module object is "+ currentModule.getId()); + currentBlock = getBlock(currentModule,1); + } + else + { + getLogger().debug("Current Round is : " + game.getCurrentRound().getId()); + getLogger().debug("Current block is : " + game.getCurrentBlock().getId()); + currentBlock = game.getCurrentBlock(); + currentRound = game.getCurrentRound(); + if(isModuleFinished(game.getCurrentBlock())) + { + getLogger().debug("current module is finished"); + + //currentModuleRoundConfig = game.getCurrentRound(). + if(isCurrentRoundFinished(game)) + { + getLogger().debug("current round is finished"); + if(isGameFinished(game)) + { + getLogger().debug("Game is finished..." + game.getId()); + return null; + } + else + { + //set the next round + currentRound = getNextRound(game); + getLogger().info("Next Round object is "+ currentRound.getId()); + + currentModuleRoundConfig = getModuleRoundConfig(0, currentRound); + getLogger().info("Next ModuleRound Config object is "+ currentModuleRoundConfig.getId()); + + Module currentModule = getModuleForCurrentRound(currentModuleRoundConfig); + getLogger().info("Next Module object is "+ currentModule.getId()); + + currentBlock = getBlock(currentModule,1); + } + } + else + { + getLogger().debug("current round is not finished"); + //currentRound = getNextRound(game.getCurrentRound()); + + currentModuleRoundConfig = getModuleRoundConfig(getPreviousModuleConfigSequenceNo(game), currentRound); + getLogger().info("Next ModuleRound Config object is "+ currentModuleRoundConfig.getId()); + + Module currentModule = getModuleForCurrentRound(currentModuleRoundConfig); + getLogger().info("Next Module object is "+ currentModule.getId()); + + currentBlock = getBlock(currentModule,1); + + } + } + else + { + //get the next block in the module + //getLogger().debug("currentmodule is " + game.getCurrentBlock().getModule().getId()); + currentBlock = getNextBlock(game.getCurrentBlock()); + getLogger().debug("the next block for the game is: " + game.getCurrentBlock().getDescription()); + } + } + + Game newGame = saveGameState(currentBlock,currentRound,game); + getLogger().debug("question group sizes is : " + newGame.getCurrentBlock().getQuestionGroups().size()); + //initializeCurrentBlock(newGame.getCurrentBlock()); + initializeGame(newGame); + getLogger().debug("Module is " + newGame.getCurrentBlock().getModule().getId() + "Block sent to the server is: " + currentBlock.getDescription()); + return newGame; + } + + private int getPreviousModuleConfigSequenceNo(Game game) { + // TODO Auto-generated method stub + List<ModuleRoundConfig> moduleRoundConfigs = new ArrayList<ModuleRoundConfig> (); + ModuleRoundConfig currentModuleRoundConfig = null; + + Map<String, Object> variables = new HashMap<String, Object>(); + variables.put("module",game.getCurrentBlock().getModule()); + variables.put("round", game.getCurrentRound()); + moduleRoundConfigs = moduleRoundDao.findByEqCriteria(variables); + if(moduleRoundConfigs.size() == 1) + { + currentModuleRoundConfig = moduleRoundConfigs.get(0); + moduleRoundConfigs.clear(); + } + getLogger().debug("the prev Sequence no is : " + currentModuleRoundConfig.getSequenceNo()); + if(currentModuleRoundConfig != null) + return currentModuleRoundConfig.getSequenceNo(); + return 0; + } + + private ModuleRoundConfig getModuleRoundConfig(int prevSeqNo, Round currentRound) { + // TODO Auto-generated method stub + //int prevSeqNo = 0; + List<ModuleRoundConfig> moduleRoundConfigs = new ArrayList<ModuleRoundConfig> (); + ModuleRoundConfig currentModuleRoundConfig = null; + try + { + moduleRoundConfigs = moduleRoundDao.findAllByProperty("round",currentRound); + for(int i = 0; i < moduleRoundConfigs.size(); i++) + { + if(moduleRoundConfigs.get(i).getSequenceNo() > prevSeqNo) + { + currentModuleRoundConfig = moduleRoundConfigs.get(i); + break; + + /*setCurrentModule(moduleRoundConfigs.get(i).getModule()); + getLogger().debug("current module is: " + getCurrentModule().getDescription()); + currentBlock = null; + break;*/ + } + } + }catch(Exception e) + { + e.printStackTrace(); + } + return currentModuleRoundConfig; + + } + + private Module getModuleForCurrentRound(ModuleRoundConfig moduleRoundConfig) { + // TODO Auto-generated method stub + //getLogger().debug("current module is: " + getCurrentModule().getDescription()); + return moduleRoundConfig.getModule(); + } + + + private Round getNextRound(Game game) { + // TODO Auto-generated method stub + + Round returnRound = null; + Round currentRound = game.getCurrentRound(); + GameConfig gameConfig = game.getGameConfig(); + List<GameRound> gameRounds = new ArrayList<GameRound>(); + if(currentRound == null) + { + //get the first round + Map<String, Object> variables = new HashMap<String, Object>(); + variables.put("gameConfig",gameConfig); + variables.put("seqNo", 1); + getGameRoundDao().findByEqCriteria(variables); + gameRounds = gameRoundDao.findByEqCriteria(variables); + if(gameRounds.size() == 1) + { + returnRound = gameRounds.get(0).getRound(); + gameRounds.clear(); + } + } + else + { + Map<String, Object> variables = new HashMap<String, Object>(); + variables.put("gameConfig",gameConfig); + variables.put("round", currentRound); + gameRounds = getGameRoundDao().findByEqCriteria(variables); + if(gameRounds.size() == 1) + { + int currentSeqNo = gameRounds.get(0).getSeqNo(); + gameRounds = getGameRoundDao().findAll(); + for(int i = 0; i < gameRounds.size(); i++) + { + if(currentSeqNo < gameRounds.get(i).getSeqNo()) + { + returnRound = gameRounds.get(i).getRound(); + break; + //getLogger().debug("currentround object is "+ rounds.get(i).getId()); + } + } + } + else + { + getLogger().error("Error Occured.. Game Config is associated with multiple Rounds "); + } + } + + return returnRound; + } + + public void initializeGame(Game game) { + // TODO Auto-generated method stub + try{ + Hibernate.initialize(game); + Hibernate.initialize(game.getCurrentBlock()); + + + GameConfig gameconfig = game.getGameConfig(); + Hibernate.initialize(gameconfig); + + for(Location location:gameconfig.getLocations()) + { + Hibernate.initialize(location); + + } + + Round round = game.getCurrentRound(); + Hibernate.initialize(round); + /*for(GameRound gameRound:round.getGameRounds()) + { + getLogger().debug("gameround in initializing round is " + gameRound.getId()); + Hibernate.initialize(gameRound); + }*/ + + }catch(Exception e ) + { + e.printStackTrace(); + } + //Hibernate.initialize(gameconfig.getGameRounds()); + + + } + + private void initializeModule(Module module) + { + // TODO Auto-generated method stub + Iterator<Block> iterateBlock = module.getBlocks().iterator(); + while(iterateBlock.hasNext()) + { + Block block = iterateBlock.next(); + Hibernate.initialize(block); + List<BlockInformationWindow> infoWindow = block.getInformationWindows(); + for(int j = 0; j < infoWindow.size(); j++) + { + Hibernate.initialize(infoWindow.get(j)); + Hibernate.initialize(infoWindow.get(j).getInfoWindow()); + } + Iterator<QuestionGroup> iteratorquestionGrp = block.getQuestionGroups().iterator(); + { + while(iteratorquestionGrp.hasNext()) + { + QuestionGroup questionGroup = iteratorquestionGrp.next(); + Hibernate.initialize(questionGroup); + // getLogger().debug(questionGroup.getDescription()); + List<Question> questions = questionGroup.getQuestions(); + + for(int j = 0; j < questions.size(); j++) + { + Hibernate.initialize(questions.get(j)); + if(questions.get(j).getClass().getName().equalsIgnoreCase("edu.asu.commons.mme.entity.CategoricalQuestion")) + { + CategoricalQuestion categoricalQ = (CategoricalQuestion)questions.get(j); + Iterator<CategoricalOption> categoricalOption = categoricalQ.getCategoricalOptions().iterator(); + Hibernate.initialize(categoricalOption); + } + } + } + } + + } + } + + private void initializeCurrentBlock(Block block) { + + Hibernate.initialize(block); + Iterator<QuestionGroup> iteratorquestionGrp = block.getQuestionGroups().iterator(); + { + while(iteratorquestionGrp.hasNext()) + { + QuestionGroup questionGroup = iteratorquestionGrp.next(); + Hibernate.initialize(questionGroup); + // getLogger().debug(questionGroup.getDescription()); + List<Question> questions = questionGroup.getQuestions(); + + for(int j = 0; j < questions.size(); j++) + { + Hibernate.initialize(questions.get(j)); + if(questions.get(j).getClass().getName().equalsIgnoreCase("edu.asu.commons.mme.entity.CategoricalQuestion")) + { + CategoricalQuestion categoricalQ = (CategoricalQuestion)questions.get(j); + Iterator<CategoricalOption> categoricalOption = categoricalQ.getCategoricalOptions().iterator(); + Hibernate.initialize(categoricalOption); + } + } + } + } + + Module module = block.getModule(); + Hibernate.initialize(module); + initializeModule(module); + + } + + + private Block getNextBlock(Block currentBlock) { + // TODO Auto-generated method stub + List<Block> blocks = new ArrayList<Block>(); + blocks = currentBlock.getModule().getBlocks(); + Block returnBlock = null; + for(int i = 0; i < blocks.size(); i++) + { + if(currentBlock.getSequenceNo() < blocks.get(i).getSequenceNo()) + { + returnBlock = blocks.get(i); + break; + } + } + return returnBlock; + + } + + public Module getModule(int moduleNumber) + { + // Enter the sequence number in new Integer(Sequence number of module u want to fetch) + + Module module = moduleDao.findByProperty("sequenceNo", moduleNumber); + Hibernate.initialize(module); + getLogger().debug("Module object is " + module.getDescription()); + initializeModule(module); + + return module; + } + + public Block getBlock(Module currentModule, int seqNo) + { + //Module module = new Module(); + // module = currentModule; + Block block = new Block(); + for(int i = 0; i < currentModule.getBlocks().size(); i++) + { + block = currentModule.getBlocks().get(i); + if(block.getSequenceNo() == seqNo) + { + return block; + } + } + + return block; + } + + /* private void setRound(int roundNo) { + // TODO Auto-generated method stub + currentRound = new Round(); + + currentRound = getDao().findByProperty("roundNo", roundNo); + //Hibernate.initialize(currentRound); + getLogger().debug("currentround object is "+ currentRound.getId()); + setCurrentRound(currentRound); + + }*/ + + /*private int getCurrentRoundNo() { + // TODO Auto-generated method stub + //FIXME: Game id 1 is hard coded but needs to make it dynamic + //Game game = gameDao.find(1L); + if(getCurrentGame().getCurrentRound() == null) + return 0; + else + return getCurrentGame().getCurrentRound().getRoundNo(); + + }*/ + public boolean isGameFinished(Game game) { + GameConfig gameConfig = game.getGameConfig(); + List<GameRound> gameRounds = getGameRoundDao().findAllByProperty("gameConfig",game.getGameConfig()); + boolean flag = false; + + Map<String, Object> variables = new HashMap<String, Object>(); + variables.put("gameConfig",gameConfig); + variables.put("round", game.getCurrentRound()); + gameRounds = getGameRoundDao().findByEqCriteria(variables); + if(gameRounds.size() == 1) + { + int currentSeqNo = gameRounds.get(0).getSeqNo(); + gameRounds.clear(); + gameRounds = getGameRoundDao().findAllByProperty("gameConfig", gameConfig); + + if(currentSeqNo == gameRounds.get(gameRounds.size() - 1).getSeqNo()) + { + flag = true; + } + else + { + for(GameRound gameRound:gameRounds) + { + if(currentSeqNo < gameRound.getSeqNo()) + { + flag = false; + } + } + + } + } + return flag; + } + + private boolean isCurrentRoundFinished(Game game) { + boolean flag = false; + List<ModuleRoundConfig> moduleRoundConfigs = new ArrayList<ModuleRoundConfig> (); + Round currentRound = game.getCurrentRound(); + Module currentModule = game.getCurrentBlock().getModule(); + + ModuleRoundConfig currentModuleRoundConfig = new ModuleRoundConfig(); + + Map<String, Object> variables = new HashMap<String, Object>(); + variables.put("module",currentModule); + variables.put("round", currentRound); + moduleRoundConfigs = moduleRoundDao.findByEqCriteria(variables); + if(moduleRoundConfigs.size() == 1) + { + currentModuleRoundConfig = moduleRoundConfigs.get(0); + moduleRoundConfigs.clear(); + } + moduleRoundConfigs = moduleRoundDao.findAllByProperty("round",currentRound); + getLogger().debug("the module round configs size is: " + moduleRoundConfigs.size()); + //check if it contains only one module, if yes then round is over + if(moduleRoundConfigs.size() == 1) + { + flag = true; + } + else + { + getLogger().debug("module round config is "+ moduleRoundConfigs.get(moduleRoundConfigs.size() - 1).getSequenceNo()); + int seqNo = moduleRoundConfigs.get(moduleRoundConfigs.size() - 1).getSequenceNo(); + if((currentModuleRoundConfig.getSequenceNo()) == seqNo) + { + flag = true; + } + else + { + for(int i = 0; i < moduleRoundConfigs.size(); i++) + { + // getLogger().debug("moduleroundconfig seq no is" + moduleRoundConfigs.get(i).getSequenceNo()); + getLogger().debug("Current seq no is" + currentModuleRoundConfig.getSequenceNo() +"and the moduleroundconfig is "+ moduleRoundConfigs.get(i).getSequenceNo()); + + if(currentModuleRoundConfig.getSequenceNo() == moduleRoundConfigs.get(moduleRoundConfigs.size() - 1).getSequenceNo()) + flag = true; + if(currentModuleRoundConfig.getSequenceNo() < moduleRoundConfigs.get(i).getSequenceNo()) + { + flag = false; + } + } + } + } + return flag; + } + + + private boolean isModuleFinished(Block currentBlock) { + // TODO Auto-generated method stub + boolean flag = false; + + List<Block> blocks = new ArrayList<Block>(); + getLogger().debug("in iscurrentmodulefinished"); + if(currentBlock == null) + { + flag = true; + } + else + { + Module currentModule = currentBlock.getModule(); + //getBlocks() return blocks in order of sequence number + blocks = currentModule.getBlocks(); + //select max(b.sequence_no) from block b where b.module_id=2; + getLogger().debug("no of blocks " + blocks.size() +"for module id: " + currentModule.getId()); + //if module contains only one block + if(blocks.size() == 1) + flag = true; + else + { + for(int i = 0; i < blocks.size(); i++) + { + if(currentBlock.getSequenceNo() == blocks.get(blocks.size() - 1).getSequenceNo()) + flag = true; + if(currentBlock.getSequenceNo() < blocks.get(i).getSequenceNo()) + { + flag = false; + } + + } + } + } + return flag; + } + + private Game saveGameState(Block currentBlock, Round currentRound, Game game) { + + //FIXME: Game id 1 is hard coded but needs to make it dynamic + getLogger().info(" Game id to SAVE is: " + game.getId()); + Game newGame = null; + newGame = getDao().find(game.getId()); + getLogger().info("The current block is: " + currentBlock.getId() + " while the current Round is: " + currentRound.getId()); + if(newGame.getCurrentBlock() == null && newGame.getCurrentRound()==null) + getLogger().info("BEFORE GAME SAVE: The current block NULL while the current Round is NULL"); + else + getLogger().info("BEFORE GAME SAVE: The current block is: " + newGame.getCurrentBlock().getId() + " while the current Round is: " + newGame.getCurrentRound().getId()); + newGame.setCurrentBlock(currentBlock); + newGame.setCurrentRound(currentRound); + getRoundConfigDao().save(newGame); + getLogger().info("The game state is saved. The current block : " + newGame.getCurrentBlock().getId() + "while the current Round is: " + newGame.getCurrentRound().getId()); + return newGame; + } + + public Integer getTimerforBlock(Block block) + { + return block.getDuration(); + + } + + /*public Block getCurrentBlock() { + //FIXEME: game id 1 is hard coded, it should be dynamic + // Game game = gameDao.find(1L); + Block block = new Block(); + block = currentGame.getCurrentBlock(); + // Hibernate.initialize(block); + + return block; + }*/ + + + /*public Round getCurrentRound() { + getLogger().debug("in get current round funciton"); + Round round = currentGame.getCurrentRound(); + //Round round = getDao().find(getCurrentRound().getId()); + + initializeRound(round); + getLogger().debug("round no is " + currentRound.getRoundNo() + " " + currentRound.getId()); + return currentRound; + }*/ + + public void setBlockSeqNo(int blockSeqNo) { + this.blockSeqNo = blockSeqNo; + } + + public int getBlockSeqNo() { + return blockSeqNo; + } + public void setModuleRoundDao(HibernateModuleRoundConfigDao moduleRoundDao) { + this.moduleRoundDao = moduleRoundDao; + } + public HibernateModuleRoundConfigDao getModuleRoundDao() { + return moduleRoundDao; + } + public void setBlockDao(HibernateBlockDao blockDao) { + this.blockDao = blockDao; + } + public HibernateBlockDao getBlockDao() { + return blockDao; + } + public void setGameFinished(boolean gameFinished) { + } + + public String getGameCode(Game game) { + // TODO Auto-generated method stub + return game.getGameCode(); + } + + public void setGameRoundDao(HibernateGameRoundDao gameRoundDao) { + this.gameRoundDao = gameRoundDao; + } + + public HibernateGameRoundDao getGameRoundDao() { + return gameRoundDao; + } + + public void setGameConfigDao(HibernateGameConfigDao gameConfigDao) { + this.gameConfigDao = gameConfigDao; + } + + public HibernateGameConfigDao getGameConfigDao() { + return gameConfigDao; + } + + + public void setRoundConfigDao(HibernateRoundConfigDao roundConfigDao) { + this.roundConfigDao = roundConfigDao; + } + + + public HibernateRoundConfigDao getRoundConfigDao() { + return roundConfigDao; + } + + public HibernateModuleDao getModuleDao() { + return moduleDao; + } + + + public void setModuleDao(HibernateModuleDao moduleDao) { + this.moduleDao = moduleDao; + } + + + public void setGroupDao(HibernateGroupDao groupDao) { + this.groupDao = groupDao; + } + + + public HibernateGroupDao getGroupDao() { + return groupDao; + } + + + public void setStudentDao(HibernateStudentDao studentDao) { + this.studentDao = studentDao; + } + + + public HibernateStudentDao getStudentDao() { + return studentDao; + } + + + public void setLocationDao(HibernateLocationDao locationDao) { + this.locationDao = locationDao; + } + + + public HibernateLocationDao getLocationDao() { + return locationDao; + } + + + public void setRoundService(RoundService roundService) { + this.roundService = roundService; + } + + + public RoundService getRoundService() { + return roundService; + } } Modified: mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/RoundService.java =================================================================== --- mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/RoundService.java 2009-10-08 00:29:17 UTC (rev 288) +++ mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/RoundService.java 2009-10-08 18:29:40 UTC (rev 289) @@ -29,9 +29,10 @@ import edu.asu.commons.mme.entity.Question; import edu.asu.commons.mme.entity.QuestionGroup; import edu.asu.commons.mme.entity.Round; +import edu.asu.commons.mme.utility.MessageHandler; @Transactional -public class RoundService extends Service.Base<Round, HibernateRoundConfigDao> { +public class RoundService extends Service.Base<Round, HibernateRoundConfigDao>{ private HibernateModuleDao moduleDao; private HibernateBlockDao blockDao; @@ -43,7 +44,17 @@ private LocationService locationService; private int blockSeqNo; + + private Game game; + public Game getGame() { + return game; + } + + public void setGame(Game game) { + this.game = game; + } + public RoundService() { setBlockSeqNo(0); @@ -136,17 +147,17 @@ try{ Hibernate.initialize(game); Hibernate.initialize(game.getCurrentBlock()); - - + + GameConfig gameconfig = game.getGameConfig(); Hibernate.initialize(gameconfig); - + for(Location location:gameconfig.getLocations()) { Hibernate.initialize(location); } - + Round round = game.getCurrentRound(); Hibernate.initialize(round); /*for(GameRound gameRound:round.getGameRounds()) @@ -154,7 +165,7 @@ getLogger().debug("gameround in initializing round is " + gameRound.getId()); Hibernate.initialize(gameRound); }*/ - + }catch(Exception e ) { e.printStackTrace(); @@ -755,4 +766,63 @@ public HibernateGameConfigDao getGameConfigDao() { return gameConfigDao; } + + + public void run() { + // TODO Auto-generated method stub + Game newGameState = null; + Block block = null; + boolean running = false; + getLogger().info("game start is: " + game.getId()); + newGameState = getGameDao().find(game.getId()); + MessageHandler msgHandler = new MessageHandler(); + msgHandler.setDestination("mme"); + msgHandler.setSubtopic(getGameCode(newGameState)); + + int i=0; + try { + while (running) { + + i++; + newGameState = getBlock(game); + game = newGameState; + if(newGameState == null) + { + msgHandler.send(null); + } + else + { + block = newGameState.getCurrentBlock(); + msgHandler.send(block); + } + + /* if(block.getDescription().equalsIgnoreCase("Day-by-day decisions game")) + { + //start day by day decision + //getDayBydayDecisionService(). + + } + else if (block.getDescription().equalsIgnoreCase("Communication")) + { + //start communication round + + }*/ + //get the timer for the block + Integer timer = getTimerforBlock(block); + //start the timer for the block sent + + //for testing purpose 5000 is given as an argument + Thread.sleep(10000); + if(block == null) + { + running = false; + } + } + }catch (InterruptedException e) { + getLogger().debug(e); + } + + + + } } Modified: mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/StartGame.java =================================================================== --- mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/StartGame.java 2009-10-08 00:29:17 UTC (rev 288) +++ mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/StartGame.java 2009-10-08 18:29:40 UTC (rev 289) @@ -45,6 +45,7 @@ //System.out.println("game came from flex is: " + game.getDescription()); game = roundService.createGame(game); System.out.println("game object return from round service is: " + game); + if(game != null) { setGame(game); @@ -80,79 +81,6 @@ return students; } - /** - * 1)Initially first block will get pushed to all the clients. - * 2)get timer for that block - * 3)check if the block is day by day decision game if yes then start day by day decision service - * 4)if it is communication, start communication service - * 3)start the timer - * 4)Once the timer is over, push the next block - * - */ - public void run() { - - Game newGameState = null; - Block block = null; - //roundService.test(); - - MessageBroker msgBroker = MessageBroker.getMessageBroker("_messageBroker"); - String clientID = UUIDUtils.createUUID(); - newGameState = game; - int i=0; - while (i<17) { - - i++; - String msgDestination = roundService.getGameCode(newGameState); - - //Push the first block to the clients - AsyncMessage msg = new AsyncMessage(); - - msg.setDestination("mme"); - msg.setHeader("DSSubtopic", msgDestination); - msg.setClientId(clientID); - msg.setMessageId(UUIDUtils.createUUID()); - msg.setTimestamp(System.currentTimeMillis()); - - newGameState = roundService.getBlock(game); - setGame(newGameState); - if(newGameState == null) - { - msg.setBody(null); - } - else - { - block = newGameState.getCurrentBlock(); - msg.setBody(block); - } - msgBroker.routeMessageToService(msg, null); - logger.debug("message is sent to the client: " + msg); - - /* if(block.getDescription().equalsIgnoreCase("Day-by-day decisions game")) - { - //start day by day decision - //getDayBydayDecisionService(). - - } - else if (block.getDescription().equalsIgnoreCase("Communication")) - { - //start communication round - - }*/ - //get the timer for the block - Integer timer = roundService.getTimerforBlock(block); - //start the timer for the block sent - try { - //for testing purpose 5000 is given as an argument - Thread.sleep(10000); - if(block == null) - { - running = false; - } - } catch (InterruptedException e) { - } - } - } - private boolean isGameOver() { // TODO Auto-generated method stub return false; @@ -205,5 +133,63 @@ public Game getGame() { return game; } + + + @Override + public void run() { + + /*assignGroups(game); + Game newGameState = null; + Block block = null; + boolean running = false; + MessageBroker msgBroker = MessageBroker.getMessageBroker("_messageBroker"); + String clientID = UUIDUtils.createUUID(); + newGameState = game; + int i=0; + while (running) { + + i++; + String msgDestination = getGameCode(newGameState); + + //Push the first block to the clients + AsyncMessage msg = new AsyncMessage(); + + msg.setDestination("mme"); + msg.setHeader("DSSubtopic", msgDestination); + msg.setClientId(clientID); + msg.setMessageId(UUIDUtils.createUUID()); + msg.setTimestamp(System.currentTimeMillis()); + + newGameState = getBlock(game); + game = newGameState; + if(newGameState == null) + { + msg.setBody(null); + } + else + { + block = newGameState.getCurrentBlock(); + msg.setBody(block); + } + msgBroker.routeMessageToService(msg, null); + getLogger().debug("message is sent to the client: " + msg); + + //get the timer for the block + Integer timer = getTimerforBlock(block); + //start the timer for the block sent + try { + //for testing purpose 5000 is given as an argument + Thread.sleep(10000); + if(block == null) + { + running = false; + } + } catch (InterruptedException e) { + } + }*/ + + + + } } Modified: mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/StudentService.java =================================================================== --- mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/StudentService.java 2009-10-08 00:29:17 UTC (rev 288) +++ mentalmodels/trunk/src/main/java/edu/asu/commons/mme/service/StudentService.java 2009-10-08 18:29:40 UTC (rev 289) @@ -6,6 +6,7 @@ import org.hibernate.Hibernate; import org.springframework.transaction.annotation.Transactional; +import edu.asu.commons.mme.dao.HibernateGameDao; import edu.asu.commons.mme.dao.HibernateGroupDao; import edu.asu.commons.mme.dao.HibernateStudentDao; import edu.asu.commons.mme.entity.Game; @@ -24,37 +25,44 @@ @Transactional public class StudentService extends Service.Base<Student, HibernateStudentDao> { - HibernateGroupDao groupDao; + private HibernateGroupDao groupDao; + private HibernateGameDao gameDao; + public void setGameDao(HibernateGameDao gameDao) { + this.gameDao = gameDao; + } + public Student createStudent(Student student) { Student studentSave = null; if(!checkStudentAlreadyExists(student)) { studentSave = new Student(); - studentSave.setBirthYear(student.getBirthYear()); - studentSave.setGame(student.getGame()); - studentSave.setGameCode(student.getGameCode()); - studentSave.setGender(student.getGender()); - studentSave.setMajor(student.getMajor()); - studentSave.setSemester(student.getSemester()); - studentSave.setTimestamp(student.getTimestamp()); - - //studentSave.setTimestamp(new Timestamp(new Date().getTime())); - getDao(). - save(studentSave); - getLogger().debug("Student id is: " + studentSave.getId()); - + Game game = gameDao.findByProperty("gameCode", student.getGameCode()); + if(game !=null) + { + studentSave.setGame(game); + studentSave.setBirthYear(student.getBirthYear()); + studentSave.setGameCode(student.getGameCode()); + studentSave.setGender(student.getGender()); + studentSave.setMajor(student.getMajor()); + studentSave.setSemester(student.getSemester()); + studentSave.setTimestamp(student.getTimestamp()); + getDao().save(studentSave); + getLogger().info("Created the student..."); + getLogger().debug("Student id is: " + studentSave.getId()); + initializeStudent(studentSave); + } } - - initializeStudent(student); - return studentSave; } private void initializeStudent(Student student) { Hibernate.initialize(student); - Hibernate.initialize(student.getGame()); + Game game = student.getGame(); + Hibernate.initialize(game); + Hibernate.initialize(game.getGameConfig()); + Hibernate.initialize(game.getGameConfig().getLocations()); Hibernate.initialize(student.getGroup()); } @@ -78,87 +86,90 @@ List<Student> studentWithGroupsAssigned = new ArrayList<Student>(); List<Student> students = new ArrayList<Student>(); try{ - getLogger().debug("In the assigngroups." + game.getId()); + getLogger().debug("In the assigngroups." + game.getId()); - students = getDao().findAllByProperty("game", game); - - getLogger().debug("test student object is : " + students.size()); - if(students.size() > 1) - { - //write an algorithm to form groups - int totalStudents = students.size(); - int noOfGroupsWithSize3=0; - int noOfGroupsWithSize4=0; - int noOfGroupsWithSize5=0; - int x; + students = getDao().findAllByProperty("game", game); + //getLogger().debug("test student object is : " + students.size()); + if(students.size() > 1) + { + //write an algorithm to form groups + int totalStudents = students.size(); + int noOfGroupsWithSize3=0; + int noOfGroupsWithSize4=0; + int noOfGroupsWithSize5=0; + int x; - //no of groups - noOfGroupsWithSize5=((int)(totalStudents/5)); + //no of groups + noOfGroupsWithSize5=((int)(totalStudents/5)); - x= totalStudents%5; - /* if(x==0) + x= totalStudents%5; + + /* if(x==0) { totalGroups=groupSize5; } else*/ - if(x==1) - { - noOfGroupsWithSize5=noOfGroupsWithSize5 - 1; - noOfGroupsWithSize3=2; - // totalGroups=groupSize5 + groupSize3; - } + if(x==1) + { + noOfGroupsWithSize5=noOfGroupsWithSize5 - 1; + noOfGroupsWithSize3=2; + // totalGroups=groupSize5 + groupSize3; + } - else if(x==2) - { - noOfGroupsWithSize5=noOfGroupsWithSize5-1; - noOfGroupsWithSize4=1; - noOfGroupsWithSize3=1; - // totalGroups=groupSize5 + groupSize3 + groupSize4; - } - else if(x==3) - { - noOfGroupsWithSize3=1; - // totalGroups=groupSize3 + groupSize5; - } - - else if(x==4) - { - noOfGroupsWithSize4=1; - // totalGroups=groupSize4 + groupSize5; - } - - - getLogger().debug("size of 4 : " + noOfGroupsWithSize4); - getLogger().debug("size of 3 : " + noOfGroupsWithSize3); - getLogger().debug("size of 5 : " + noOfGroupsWithSize5); - - //assign every student a number and group - if(!students.isEmpty()) - { - if(noOfGroupsWithSize5 > 0) + else if(x==2) { - studentWithGroupsAssigned.addAll(assignGroupstoStudents(5,noOfGroupsWithSize5,students)); - getLogger().debug("student size after 5 is: " + students.size()); - getLogger().debug("student with groups assigned: " + studentWithGroupsAssigned.size()); + noOfGroupsWithSize5=noOfGroupsWithSize5-1; + noOfGroupsWithSize4=1; + noOfGroupsWithSize3=1; + // totalGroups=groupSize5 + groupSize3 + groupSize4; } - if(noOfGroupsWithSize4 > 0) + else if(x==3) { - studentWithGroupsAssigned.addAll(assignGroupstoStudents(4,noOfGroupsWithSize4,students)); - getLogger().debug("student size after 4 is: " + students.size()); - getLogger().debug("student with groups assigned: " + studentWithGroupsAssigned.size()); + noOfGroupsWithSize3=1; + // totalGroups=groupSize3 + groupSize5; } - if(noOfGroupsWithSize3 > 0) + + else if(x==4) { - studentWithGroupsAssigned.addAll(assignGroupstoStudents(3,noOfGroupsWithSize3,students)); - getLogger().debug("student size after 3 is: " + students.size()); - getLogger().debug("student with groups assigned: " + studentWithGroupsAssigned.size()); + noOfGroupsWithSize4=1; + // totalGroups=groupSize4 + groupSize5; } + + + //getLogger().debug("size of 4 : " + noOfGroupsWithSize4); + //getLogger().debug("size of 3 : " + noOfGroupsWithSize3); + //getLogger().debug("size of 5 : " + noOfGroupsWithSize5); + + int groupNo = 0; + //assign every student a number and group + if(!students.isEmpty()) + { + if(noOfGroupsWithSize5 > 0) + { + studentWithGroupsAssigned.addAll(assignGroupstoStudents(5,noOfGroupsWithSize5,students,groupNo)); + //getLogger().debug("student size after 5 is: " + students.size()); + //getLogger().debug("student with groups assigned: " + studentWithGroupsAssigned.size()); + } + groupNo = noOfGroupsWithSize5; + if(noOfGroupsWithSize4 > 0) + { + studentWithGroupsAssigned.addAll(assignGroupstoStudents(4,noOfGroupsWithSize4,students,groupNo)); + //getLogger().debug("student size after 4 is: " + students.size()); + //getLogger().debug("student with groups assigned: " + studentWithGroupsAssigned.size()); + } + groupNo = noOfGroupsWithSize5 + noOfGroupsWithSize4; + if(noOfGroupsWithSize3 > 0) + { + studentWithGroupsAssigned.addAll(assignGroupstoStudents(3,noOfGroupsWithSize3,students,groupNo)); + //getLogger().debug("student size after 3 is: " + students.size()); + //getLogger().debug("student with groups assigned: " + studentWithGroupsAssigned.size()); + } + } } - } - getLogger().debug("Final student with groups assigned: " + studentWithGroupsAssigned.size()); + getLogger().debug("Final student with groups assigned: " + studentWithGroupsAssigned.size()); }catch(Exception e) { getLogger().error(e); @@ -167,19 +178,20 @@ } - private List<Student> assignGroupstoStudents(int groupSize, int noOfGroups,List<Student> students) { + private List<Student> assignGroupstoStudents(int groupSize, int noOfGroups,List<Student> students,int groupNo) { // TODO Auto-generated method stub Student student = new Student(); List<Student> studentWithGroupsAssigned = new ArrayList<Student>(); - getLogger().debug("group size is : "+groupSize); - int k = 0; - for(int grpno = 0; grpno < noOfGroups; grpno++) + getLogger().debug("group size is : "+groupSize + " and group number is : "+ groupNo); + + for(int counter = 0; counter < noOfGroups; counter++) { //create group Group grp = new Group(); - k= grpno+1; - grp.setNumber(k); + + grp.setNumber(++groupNo); groupDao.save(grp); + getLogger().info("Group is created with id: " + grp.getId()); int studentNo = 1; int j = 0; @@ -198,7 +210,6 @@ studentNo++; initializeStudent(student); studentWithGroupsAssigned.add(student); - students.remove(0); } j++; Added: mentalmodels/trunk/src/main/java/edu/asu/commons/mme/utility/MessageHandler.java =================================================================== --- mentalmodels/trunk/src/main/java/edu/asu/commons/mme/utility/MessageHandler.java (rev 0) +++ mentalmodels/trunk/src/main/java/edu/asu/commons/mme/utility/MessageHandler.java 2009-10-08 18:29:40 UTC (rev 289) @@ -0,0 +1,78 @@ +package edu.asu.commons.mme.utility; + +import edu.asu.commons.mme.entity.Block; +import edu.asu.commons.mme.service.StartGame; +import flex.messaging.MessageBroker; +import flex.messaging.messages.AcknowledgeMessage; +import flex.messaging.messages.AsyncMessage; +import flex.messaging.util.UUIDUtils; +import org.apache.log4j.Logger; + +public class MessageHandler { + + private static Logger logger = Logger.getLogger(StartGame.class); + + private String destination; + + public String getDestination() { + return destination; + } + + public void setDestination(String destination) { + this.destination = destination; + } + + private String subtopic; + + public String getSubtopic() { + return subtopic; + } + + public void setSubtopic(String subtopic) { + this.subtopic = subtopic; + } + + public void send(Object message) + { + + MessageBroker msgBroker = MessageBroker.getMessageBroker("_messageBroker"); + + String clientID = UUIDUtils.createUUID(); + AsyncMessage msg = new AsyncMessage(); + msg.setDestination(destination); + //String msgDestination = game.getGameCode(); + msg.setHeader("DSSubtopic", subtopic); + msg.setClientId(clientID); + msg.setMessageId(UUIDUtils.createUUID()); + msg.setTimestamp(System.currentTimeMillis()); + + msg.setBody(message); + System.out.println("Message broker is: "+ msgBroker); + System.out.println("Students size is: " + message); + + AcknowledgeMessage ack = msgBroker.routeMessageToService(msg, null); + logger.info("Ack received from client for message " + msg + "is : " + ack); + } + + public void sendBlock(Block block) + { + MessageBroker msgBroker = MessageBroker.getMessageBroker("_messageBroker"); + + String clientID = UUIDUtils.createUUID(); + AsyncMessage msg = new AsyncMessage(); + msg.setDestination(destination); + //String msgDestination = game.getGameCode(); + msg.setHeader("DSSubtopic", subtopic); + msg.setClientId(clientID); + msg.setMessageId(UUIDUtils.createUUID()); + msg.setTimestamp(System.currentTimeMillis()); + + msg.setBody(block); + System.out.println("Message broker is: "+ msgBroker); + //System.out.println("Students size is: " + message); + + AcknowledgeMessage ack = msgBroker.routeMessageToService(msg, null); + logger.info("Ack received from client for message " + msg + "is : " + ack); + + } +} Modified: mentalmodels/trunk/src/main/webapp/WEB-INF/applicationContext.xml =================================================================== --- mentalmodels/trunk/src/main/webapp/WEB-INF/applicationContext.xml 2009-10-08 00:29:17 UTC (rev 288) +++ mentalmodels/trunk/src/main/webapp/WEB-INF/applicationContext.xml 2009-10-08 18:29:40 UTC (rev 289) @@ -111,7 +111,21 @@ <!-- spring managed service layer --> +<bean id='gameService' class='edu.asu.commons.mme.service.GameService'> + <property name='dao' ref='gameDao'/> + <property name='roundConfigDao' ref='roundConfigDao'/> + <property name='moduleDao' ref='moduleDao'/> + <property name='gameConfigDao' ref='gameConfigDao' /> + <property name='moduleRoundDao' ref='moduleRoundDao' /> + <property name='locationDao' ref='locationDao'/> + <property name='gameRoundDao' ref='gameRoundDao'/> + <property name='blockDao' ref='blockDao'/> + <property name='studentDao' ref='studentDao'/> + <property name='groupDao' ref='groupDao'/> + + </bean> + <bean id='dayByDayDecisionsService' class='edu.asu.commons.mme.service.DayByDayDecisionsService'> <property name='dao' ref='dayByDayDecisionsDao'/> </bean> @@ -157,6 +171,7 @@ <bean id='studentService' class='edu.asu.commons.mme.service.StudentService'> <property name='dao' ref='studentDao'/> <property name='groupDao' ref='groupDao'/> + <property name='gameDao' ref='gameDao'/> </bean> <!-- spring managed service layer --> @@ -186,10 +201,11 @@ <flex:remoting-destination ref="moduleService" /> <flex:remoting-destination ref="questionCreatorService" /> <flex:remoting-destination ref="roundService" /> - <flex:remoting-destination ref="startupService" /> + <flex:remoting-destination ref="gameService" /> <flex:remoting-destination ref="answeringService" /> <flex:remoting-destination ref="locationService" /> <flex:remoting-destination ref="feed" /> + <!-- Expose services for Flex/BlazeDS messaging --> <flex:message-destination id="chat" /> Modified: mentalmodels/trunk/src/main/webapp/WEB-INF/classes/log4j.properties =================================================================== --- mentalmodels/trunk/src/main/webapp/WEB-INF/classes/log4j.properties 2009-10-08 00:29:17 UTC (rev 288) +++ mentalmodels/trunk/src/main/webapp/WEB-INF/classes/log4j.properties 2009-10-08 18:29:40 UTC (rev 289) @@ -12,5 +12,5 @@ log4j.logger.edu.asu.commons=DEBUG #log4j.logger.org.springframework=DEBUG -log4j.logger.org.springframework=DEBUG +log4j.logger.org.springframework=ERROR log4j.logger.org.hibernate=ERROR This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |