예제 #1
0
  /**
   * Created KC_TASK mapping entry in tk_a2 table If replace is true, first all records are
   * truncated and then mappings inserted If replace is false, records are directly inserted in the
   * table. Duplicate records not allowed.
   *
   * @param tkReader
   * @return
   */
  @Path("/mapkctask")
  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public Response mapKcToTask(TKReader_N_In_A_Row tkReader) {
    Long requestTimestamp = System.currentTimeMillis();
    Session session = null;
    try {
      boolean replace = tkReader.getReplace();
      String external_course_id = tkReader.getExternal_course_id();
      TKAReaderI[] tkReaderArray = tkReader.getTkaReader();

      if (replace) {
        Course course = CourseHandler.getByExternalId(external_course_id);
        session = KCAnalyzerHandler.hqlDeleteByCourse("N_In_A_Row", course, false);
      }
      TaskKC_N_In_A_Row tk2Array[] = new TaskKC_N_In_A_Row[tkReaderArray.length];
      for (int i = 0; i < tkReaderArray.length; i++) {
        TaskKC_N_In_A_Row tk2 = new TaskKC_N_In_A_Row();
        TKAReader_N_In_A_Row tkReader2 = (TKAReader_N_In_A_Row) tkReaderArray[i];
        KnowledgeComponent kc =
            KnowledgeComponentHandler.readByExtId(
                tkReader2.getExternal_kc_id(), external_course_id);
        Task task;
        task = TaskHandler.readByExtId(tkReader2.getExternal_task_id(), external_course_id);
        tk2.setKc(kc);
        tk2.setTask(task);
        tk2Array[i] = tk2;
      }
      session = TaskKCAnalyzerHandler.batchSave(tk2Array, false, session);
      if (session != null) {

        session.getTransaction().commit();
        session.close();
      }

      return Response.status(Status.CREATED)
          .entity(MyResponse.build(MyStatus.SUCCESS, MyMessage.KC_TASK_CREATED))
          .build();
    } catch (CourseException e) {
      if (session != null) {
        session.getTransaction().rollback();
        session.close();
      }

      Response rb =
          Response.status(Status.OK)
              .entity(MyResponse.build(e.getMyStatus(), e.getMyMessage()))
              .build();
      throw new WebApplicationException(rb);
    } catch (TaskException e) {
      if (session != null) {
        session.getTransaction().rollback();
        session.close();
      }
      Response rb =
          Response.status(Status.OK)
              .entity(MyResponse.build(e.getMyStatus(), e.getMyMessage()))
              .build();
      throw new WebApplicationException(rb);
    } catch (KCException e) {
      if (session != null) {
        session.getTransaction().rollback();
        session.close();
      }
      Response rb =
          Response.status(Status.OK)
              .entity(MyResponse.build(e.getMyStatus(), e.getMyMessage()))
              .build();
      throw new WebApplicationException(rb);
    } catch (ConstraintViolationException cve) {
      // kc = KnowledgeComponentHandler.readByExtId(external_kc_id, external_course_id);
      if (session != null) {
        session.getTransaction().rollback();
        session.close();
      }
      Response rb =
          Response.status(Status.OK)
              .entity(MyResponse.build(MyStatus.ERROR, MyMessage.KC_TASK_MAP_ALREADY_PRESENT))
              .build();
      throw new WebApplicationException(rb);
    } catch (Exception e) {

      if (session != null) {
        session.getTransaction().rollback();
        session.close();
      }
      System.out.println(e.getMessage());
      logger.error(e.getStackTrace());
      Response rb =
          Response.status(Status.BAD_REQUEST)
              .entity(MyResponse.build(MyStatus.ERROR, MyMessage.BAD_REQUEST))
              .build();
      throw new WebApplicationException(rb);
    } finally {
      Long responseTimestamp = System.currentTimeMillis();
      Long response = (responseTimestamp - requestTimestamp) / 1000;
      Utilities.writeToGraphite(Constants.METRIC_RESPONSE_TIME, response, requestTimestamp / 1000);
    }
  }
  // create student task - is a transaction API which records every student task request
  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  public Response createStudentTask(STAReader_Required_Optional sta) {
    Long requestTimestamp = System.currentTimeMillis();
    try {
      if (!Utilities.checkExists(sta.getExternal_course_id())) {
        throw new CourseException(MyStatus.ERROR, MyMessage.COURSE_ID_MISSING);
      }
      if (!Utilities.checkExists(sta.getExternal_student_id())) {
        throw new StudentException(MyStatus.ERROR, MyMessage.STUDENT_ID_MISSING);
      }
      if (!Utilities.checkExists(sta.getExternal_task_id())) {
        throw new TaskException(MyStatus.ERROR, MyMessage.TASK_ID_MISSING);
      }
      StudentTask_Required_Optional sta3 = new StudentTask_Required_Optional();

      try {
        StudentTask_Required_Optional sta3Recent =
            (StudentTask_Required_Optional)
                StudentTaskAnalyzerHandler.readOrderByTimestamp(
                        StudentTask_Required_Optional.class,
                        sta.getExternal_student_id(),
                        sta.getExternal_course_id(),
                        sta.getExternal_task_id())
                    .get(0);
        System.out.println(sta3Recent.getId());
        sta3.setD_current_n(sta3Recent.getD_current_n() + 1);

      } catch (StudentTaskException ste) {
        if (ste.getMyMessage() == MyMessage.STUDENT_TASK_NOT_FOUND
            || ste.getMyMessage() == MyMessage.STUDENT_TASK_ANALYZER_NOT_FOUND) {
          sta3.setD_current_n(1);
        }
      }
      sta3.createStudentTask(
          sta.getExternal_student_id(), sta.getExternal_course_id(), sta.getExternal_task_id(), 3);
      // sta3.setD_current_n(sta.getD_current_n());
      // List<StudentTask> stList = StudentTaskHandler.readByExtId(sta.getExternal_student_id(),
      // sta.getExternal_course_id(), sta.getExternal_task_id());

      // sta3.setD_timestsamp(System.currentTimeMillis());
      sta3.setD_is_answered(sta.getD_is_answered());

      StudentTaskAnalyzerHandler.save(sta3);

      // Student student = StudentHandler.getByExternalId(sta.getExternal_student_id(),
      // sta.getExternal_course_id());
      // Task task = TaskHandler.readByExtId(sta.getExternal_task_id(),
      // sta.getExternal_course_id());
      // Course course=CourseHandler.getByExternalId(sta.getExternal_course_id());
      // if(sta.getD_status().equals("done"))
      //	RecommTaskHandler.completeATask(student, course,task);

      return Response.status(Status.CREATED)
          .entity(MyResponse.build(MyStatus.SUCCESS, MyMessage.STUDENT_TASK_CREATED))
          .build();
    } catch (CourseException e) {
      Response rb =
          Response.status(Status.OK)
              .entity(MyResponse.build(e.getMyStatus(), e.getMyMessage()))
              .build();
      throw new WebApplicationException(rb);
    } catch (TaskException e) {
      Response rb =
          Response.status(Status.OK)
              .entity(MyResponse.build(e.getMyStatus(), e.getMyMessage()))
              .build();
      throw new WebApplicationException(rb);
    } catch (StudentException e) {
      Response rb =
          Response.status(Status.OK)
              .entity(MyResponse.build(e.getMyStatus(), e.getMyMessage()))
              .build();
      throw new WebApplicationException(rb);
    } catch (Exception e) {
      logger.error("Exception while creating student", e);
      Response rb =
          Response.status(Status.BAD_REQUEST)
              .entity(MyResponse.build(MyStatus.ERROR, MyMessage.BAD_REQUEST))
              .build();
      throw new WebApplicationException(rb);
    } finally {
      Long responseTimestamp = System.currentTimeMillis();
      Long response = (responseTimestamp - requestTimestamp) / 1000;
      Utilities.writeToGraphite(Constants.METRIC_RESPONSE_TIME, response, requestTimestamp / 1000);
    }
  }