}
else {
// Set up your database Tools
DatabaseTools myTools = new DatabaseTools();
// Confirm user exists and has the correct password before letting them pass.
boolean userFound = myTools.dataSearch("User", "id", userId, "password", password);
// Return false if a user is not found or password incorrect!
if(!userFound) {
throw new Exception ("User not found");
}
else {
//check if group exists first
boolean groupFound = myTools.dataSearch("Group", "id", groupId);
//group not found
if(!groupFound) {
return GroupFactory.validGroup(false);
}
//group found
else {
//check to see if user has permission to delete group (admin)
//set up retrieval tools
ObjectRetrievalTools<UserRoleGroup> objTool = new ObjectRetrievalTools<UserRoleGroup>();
//return the UserRoleGroup
UserRoleGroup urg = (UserRoleGroup)objTool.getObject(Table.UserRoleGroup, "userId", userId, "groupId", groupId);
//get role id
int admin = urg.getRoleId();
//check for admin
if (admin == 0) {
throw new Exception ("User does not have admin privledges");
}
//admin confirmed
else {
boolean answer;
//first check to see if group has any questions first
//if there are questions, there will be answers as well
if (answer = myTools.dataSearch("Question", "groupId", groupId)){
//get all the questions associated with the group id
ObjectRetrievalTools<Question> objTool2 = new ObjectRetrievalTools<Question>();
List<Question> questionList = objTool2.getObjectList(Table.Question, "groupId", groupId);
//delete the questions from the db
for (Iterator<Question> questIterator = questionList.iterator(); questIterator.hasNext();) {
Question questionDelete = questIterator.next();
//get questionId for answer deletion
int questionId = questionDelete.getId();
myTools.dataRemoval(questionDelete);
//get all the answers associated with the question id
ObjectRetrievalTools<Answer> objTool3 = new ObjectRetrievalTools<Answer>();
List<Answer> answerList = objTool3.getObjectList(Table.Answer, "questionId", questionId);
//delete all the answers from the db
for (Iterator<Answer> ansIterator = answerList.iterator(); ansIterator.hasNext();) {
Answer answerDelete = ansIterator.next();
myTools.dataRemoval(answerDelete);
}
}
}
//group has no questions/answers
else {}
//get all the UserRoleGroup associated with the group id
//there should be at least 1 for the group creator
ObjectRetrievalTools<UserRoleGroup> objTool4 = new ObjectRetrievalTools<UserRoleGroup>();
List<UserRoleGroup> urgList = objTool4.getObjectList(Table.UserRoleGroup, "groupId", groupId);
//delete all the UserRoleGroup from db
for (Iterator<UserRoleGroup> iterator = urgList.iterator(); iterator.hasNext();) {
UserRoleGroup urgDelete = iterator.next();
myTools.dataRemoval(urgDelete);
}
//lastly, retrieve the actual group for deletion
ObjectRetrievalTools<Group> objTool5 = new ObjectRetrievalTools<Group>();
Group remGroup = (Group) objTool5.getObject(Table.Group, "id", groupId);
//call the tools to remove the group from the db
myTools.dataRemoval(remGroup);
//return true when group is deleted
return GroupFactory.validGroup(!myTools.dataSearch("Group", "id", groupId));
}
}
}
}
}