private void initProblems() { // Get the list of existing problems from our XML data file string problemsDataFile = Path.Combine(dataDir, "Problems.xml"); string problemsSchemaFile = Path.Combine(schemaDir, "Problems.xsd"); ProblemCollection problems = new ProblemCollection(problemsDataFile, problemsSchemaFile); // Persist the problems in the database ISession session = null; ITransaction transaction = null; try { session = factory.OpenSession(); transaction = session.BeginTransaction(); foreach (Problem p in problems) { P_Problem p_prob = new P_Problem(p.Name); p_prob.Name = p.Name; session.Save(p_prob); } transaction.Commit(); } catch { if (transaction != null) { transaction.Rollback(); } throw; } finally { if (session != null) { session.Close(); } } }
public P_Problem[] ListAllProblems() { ISession session = factory.OpenSession(); ICriteria criterion = session.CreateCriteria(typeof(P_Problem)); System.Collections.IList problemList = criterion.List(); P_Problem[] answer = new P_Problem[problemList.Count]; for (int i = 0; i < problemList.Count; i++) { answer[i] = (P_Problem)problemList[i]; } return(answer); }
public int SetProblem(int userId, string problemName) { int answer = 0; ISession session = null; ITransaction transaction = null; try { session = factory.OpenSession(); transaction = session.BeginTransaction(); P_User user = session.Load <P_User>(userId); // If the user has no open Episode, or if the open Episode is for a different problem, we need // to generate a new Episode if (user.OpenEpisode == null || user.OpenEpisode.Problem.Name != problemName) { ICriteria criterion = session.CreateCriteria(typeof(P_Problem)); criterion.Add(Expression.Eq("Name", problemName)); P_Problem problem = criterion.UniqueResult <P_Problem>(); P_Episode episode = new P_Episode(problem); user.AddEpisode(episode); session.Save(problem); session.Save(episode); answer = problem.Id; } else { // requested problem is already associate with the user's open episode, do nothing answer = user.OpenEpisode.Problem.Id; } transaction.Commit(); } catch { if (transaction != null) { transaction.Rollback(); throw; } } finally { if (session != null) { session.Close(); } } return(answer); }
public P_Episode(P_Problem problem) { this.problem = problem; this.isOpen = true; }