Ejemplo n.º 1
0
    @Override
    public void load(final Runnable onSuccess, final ICallback<Pair<String, Throwable>> onFailure) {
      String paramName = PageObjectParamNameMap.getInstance().get(CourseSelection.class);
      final Integer courseId = pageParams.getInt(paramName);
      if (courseId == null) {
        onFailure.call(new Pair<String, Throwable>("No course id specified", null));
      } else {
        RPC.getCoursesAndProblemsService.getCourses(
            new AsyncCallback<Course[]>() {
              @Override
              public void onFailure(Throwable caught) {
                onFailure.call(new Pair<String, Throwable>("Could not load course", caught));
              }

              @Override
              public void onSuccess(Course[] result) {
                Course course = null;
                for (Course c : result) {
                  if (c.getId() == courseId.intValue()) {
                    course = c;
                    break;
                  }
                }
                if (course == null) {
                  onFailure.call(
                      new Pair<String, Throwable>("User is not registered for course", null));
                } else {
                  // Add CourseSelection to session, and continue
                  session.add(new CourseSelection(course, null));
                  onSuccess.run();
                }
              }
            });
      }
    }
Ejemplo n.º 2
0
    @Override
    public void load(final Runnable onSuccess, final ICallback<Pair<String, Throwable>> onFailure) {
      String paramName = PageObjectParamNameMap.getInstance().get(Problem.class);
      final Integer problemId = pageParams.getInt(paramName);
      if (problemId == null) {
        onFailure.call(new Pair<String, Throwable>("No problem id specified", null));
      } else {
        CourseSelection courseSelection = session.get(CourseSelection.class);
        RPC.getCoursesAndProblemsService.getProblems(
            courseSelection.getCourse(),
            new AsyncCallback<Problem[]>() {
              @Override
              public void onFailure(Throwable caught) {
                onFailure.call(new Pair<String, Throwable>("Could not get problem", caught));
              }

              public void onSuccess(Problem[] result) {
                Problem problem = null;
                for (Problem p : result) {
                  if (p.getProblemId().equals(problemId)) {
                    problem = p;
                    break;
                  }
                }
                if (problem == null) {
                  onFailure.call(new Pair<String, Throwable>("No such problem", null));
                } else {
                  // Add the Problem to the session and continue
                  session.add(problem);
                  onSuccess.run();
                }
              }
            });
      }
    }
Ejemplo n.º 3
0
    @Override
    public void load(final Runnable onSuccess, final ICallback<Pair<String, Throwable>> onFailure) {
      final Integer userId =
          pageParams.getInt(PageObjectParamNameMap.getInstance().get(UserSelection.class));
      if (userId == null) {
        onFailure.call(new Pair<String, Throwable>("No user id specified", null));
        return;
      }

      // Special case: if the requested user id is the logged-in user's id,
      // then we succeed trivially.  (A user can always access his/her
      // own information, such as a submission history.)
      User loggedInUser = session.get(User.class);
      if (loggedInUser.getId() == userId.intValue()) {
        GWT.log("User self-selection");
        UserSelection userSelection = new UserSelection();
        userSelection.setUser(loggedInUser);
        session.add(userSelection);
        onSuccess.run();
        return;
      }

      // General case: a user is being selected among all users in the course.

      // Users registered in the course should already have been loaded
      User[] regUserList = session.get(User[].class);

      // Check to see if the user is registered in the course
      for (User userInCourse : regUserList) {
        if (userInCourse.getId() == userId.intValue()) {
          // Huzzah!
          UserSelection userSelection = new UserSelection();
          userSelection.setUser(userInCourse);
          session.add(userSelection);
          onSuccess.run();
          return;
        }
      }

      // User is not registered in course
      onFailure.call(
          new Pair<String, Throwable>("User " + userId + " is not registered in the course", null));
    }