ting.yin 4 ani în urmă
părinte
comite
624db2ba06

+ 25 - 6
stmms-biz/src/main/java/cn/com/qmth/stmms/biz/exam/model/DataSync.java

@@ -56,8 +56,11 @@ public class DataSync implements Serializable {
     @Column(name = "app_id")
     private String appId;
 
-    @Column(name = "secret_key", nullable = false)
-    private String secretKey;
+    @Column(name = "access_key", nullable = true, length = 64)
+    private String accessKey;
+
+    @Column(name = "access_secret", nullable = true, length = 64)
+    private String accessSecret;
 
     @Column(name = "finished", nullable = false)
     private boolean finished;
@@ -167,12 +170,28 @@ public class DataSync implements Serializable {
         this.appId = appId;
     }
 
-    public String getSecretKey() {
-        return secretKey;
+    public ExamSource getSource() {
+        return source;
+    }
+
+    public void setSource(ExamSource source) {
+        this.source = source;
+    }
+
+    public String getAccessKey() {
+        return accessKey;
+    }
+
+    public void setAccessKey(String accessKey) {
+        this.accessKey = accessKey;
+    }
+
+    public String getAccessSecret() {
+        return accessSecret;
     }
 
-    public void setSecretKey(String secretKey) {
-        this.secretKey = secretKey;
+    public void setAccessSecret(String accessSecret) {
+        this.accessSecret = accessSecret;
     }
 
     public String getSubjectPaperUrl() {

+ 4 - 0
stmms-biz/src/main/java/cn/com/qmth/stmms/biz/exam/model/MarkGroupStudent.java

@@ -5,7 +5,10 @@ import cn.com.qmth.stmms.common.enums.SubjectiveStatus;
 import javax.persistence.Column;
 import javax.persistence.EmbeddedId;
 import javax.persistence.Entity;
+import javax.persistence.EnumType;
+import javax.persistence.Enumerated;
 import javax.persistence.Table;
+
 import java.io.Serializable;
 
 /**
@@ -36,6 +39,7 @@ public class MarkGroupStudent implements Serializable {
      * 状态
      */
     @Column(name = "status", length = 16, nullable = false)
+    @Enumerated(EnumType.STRING)
     private SubjectiveStatus status;
 
     public MarkGroupStudent() {

+ 4 - 2
stmms-web/src/main/java/cn/com/qmth/stmms/admin/exam/DataSyncController.java

@@ -9,6 +9,7 @@ import cn.com.qmth.stmms.biz.file.service.FileService;
 import cn.com.qmth.stmms.biz.lock.LockService;
 import cn.com.qmth.stmms.common.annotation.Logging;
 import cn.com.qmth.stmms.common.annotation.RoleRequire;
+import cn.com.qmth.stmms.common.enums.ExamSource;
 import cn.com.qmth.stmms.common.enums.LockType;
 import cn.com.qmth.stmms.common.enums.LogType;
 import cn.com.qmth.stmms.common.enums.Role;
@@ -107,11 +108,12 @@ public class DataSyncController extends BaseExamController {
             dataSync.setSubjectUrl(host + ":" + port + subjectUri);
             dataSync.setSubjectPaperUrl(host + ":" + port + subjectPaperUri);
             dataSync.setAppId(appId);
-            dataSync.setSecretKey(secretKey);
+            dataSync.setAccessKey(secretKey);
         }
         ModelAndView view = new ModelAndView("modules/exam/dataSync");
         view.addObject("running", lockService.isLocked(LockType.DATA_SYNC, examId));
         view.addObject("exam", examService.findById(examId));
+        view.addObject("sourceList", ExamSource.values());
         view.addObject("dataSync", dataSync);
         return view;
     }
@@ -157,7 +159,7 @@ public class DataSyncController extends BaseExamController {
     private List<SubjectiveQuestionDTO> getPaperStruct(DataSync sync) throws Exception {
         List<SubjectiveQuestionDTO> list = new ArrayList<SubjectiveQuestionDTO>();
 
-        HttpUtil subjectHttp = new HttpUtil(sync.getSubjectUrl(), sync.getSecretKey(), sync.getAppId(),
+        HttpUtil subjectHttp = new HttpUtil(sync.getSubjectUrl(), sync.getAccessKey(), sync.getAppId(),
                 sync.getRootOrgId());
         JSONObject datas = new JSONObject();
         datas.accumulate("examId", sync.getCloudExamId());

+ 3 - 3
stmms-web/src/main/java/cn/com/qmth/stmms/admin/thread/DataSyncThread.java

@@ -86,11 +86,11 @@ public class DataSyncThread implements Runnable {
         this.subjectService = subjectService;
         this.fileService = fileService;
         this.pageSize = pageSize;
-        this.subjectHttp = new HttpUtil(dataSync.getSubjectUrl(), dataSync.getSecretKey(), dataSync.getAppId(),
+        this.subjectHttp = new HttpUtil(dataSync.getSubjectUrl(), dataSync.getAccessKey(), dataSync.getAppId(),
                 dataSync.getRootOrgId());
-        this.studentHttp = new HttpUtil(dataSync.getStudentUrl(), dataSync.getSecretKey(), dataSync.getAppId(),
+        this.studentHttp = new HttpUtil(dataSync.getStudentUrl(), dataSync.getAccessKey(), dataSync.getAppId(),
                 dataSync.getRootOrgId());
-        this.subjectPaperHttp = new HttpUtil(dataSync.getSubjectPaperUrl(), dataSync.getSecretKey(),
+        this.subjectPaperHttp = new HttpUtil(dataSync.getSubjectPaperUrl(), dataSync.getAccessKey(),
                 dataSync.getAppId(), dataSync.getRootOrgId());
     }
 

+ 11 - 14
stmms-web/src/main/java/cn/com/qmth/stmms/admin/thread/OnlineExamThread.java

@@ -58,8 +58,6 @@ public class OnlineExamThread implements Runnable {
 
     private DataSyncService dataSyncService;
 
-    private Integer pageSize;
-
     private OnlineExamHttpUtil studentHttp;
 
     private OnlineExamHttpUtil subjectPaperHttp;
@@ -86,10 +84,11 @@ public class OnlineExamThread implements Runnable {
 
     public static final String COUNT = "count";
 
-    public OnlineExamThread(DataSync dataSync, Integer pageSize, LockService lockService,
-            DataSyncService dataSyncService, ExamService examService, ExamStudentService studentService,
-            ExamSubjectService subjectService, ExamQuestionService questionService, MarkGroupService groupService,
-            FileService fileService) {
+    public static final String FILTER = "filter";
+
+    public OnlineExamThread(DataSync dataSync, String host, LockService lockService, DataSyncService dataSyncService,
+            ExamService examService, ExamStudentService studentService, ExamSubjectService subjectService,
+            ExamQuestionService questionService, MarkGroupService groupService, FileService fileService) {
         this.dataSync = dataSync;
         this.lockService = lockService;
         this.dataSyncService = dataSyncService;
@@ -99,16 +98,15 @@ public class OnlineExamThread implements Runnable {
         this.questionService = questionService;
         this.groupService = groupService;
         this.fileService = fileService;
-        this.pageSize = pageSize;
-        this.studentHttp = new OnlineExamHttpUtil(dataSync.getSecretKey(), dataSync.getSecretKey(),
+        this.studentHttp = new OnlineExamHttpUtil(dataSync.getAccessKey(), dataSync.getAccessSecret(), host,
                 dataSync.getStudentUrl());
-        this.subjectPaperHttp = new OnlineExamHttpUtil(dataSync.getSecretKey(), dataSync.getSecretKey(),
+        this.subjectPaperHttp = new OnlineExamHttpUtil(dataSync.getAccessKey(), dataSync.getAccessSecret(), host,
                 dataSync.getSubjectPaperUrl());
     }
 
     @Override
     public void run() {
-        log.info("start data sync for examId=" + dataSync.getExamId());
+        log.info("start online data sync for examId=" + dataSync.getExamId());
         try {
             // 获取考试信息
             Exam exam = examService.findById(dataSync.getExamId());
@@ -129,14 +127,13 @@ public class OnlineExamThread implements Runnable {
             }
             while (startId != null) {
                 datas.put(START_ID, startId);
-                datas.put(COUNT, pageSize);
                 String studentResult = studentHttp.httpAction(null, datas);
                 JSONArray studentArray = JSONArray.fromObject(studentResult);
                 if (studentArray.size() == 0) {
                     startId = null;
                 }
-                for (int k = 0; k < studentArray.size(); k++) {
-                    JSONObject student = studentArray.getJSONObject(k);
+                for (int i = 0; i < studentArray.size(); i++) {
+                    JSONObject student = studentArray.getJSONObject(i);
                     String subjectName = student.getString(COURSE_NAME);
                     JSONArray recordArray = student.getJSONArray(RECORDS);
                     List<ExamStudent> list = new ArrayList<ExamStudent>();
@@ -145,7 +142,7 @@ public class OnlineExamThread implements Runnable {
                         JSONObject record = recordArray.getJSONObject(j);
                         // 保存科目试卷
                         datas.put(ID, record.getString(PAPER_ID));
-                        datas.put("filter", "subjective");
+                        datas.put(FILTER, "subjective");
                         String paperResult = subjectPaperHttp.httpAction(null, datas);
                         JSONObject paperJson = JSONObject.fromObject(paperResult);
                         String subjectCode = paperJson.getString("paperId");

+ 26 - 26
stmms-web/src/main/java/cn/com/qmth/stmms/admin/utils/OnlineExamHttpUtil.java

@@ -1,20 +1,14 @@
 package cn.com.qmth.stmms.admin.utils;
 
 import java.io.BufferedReader;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.net.HttpURLConnection;
 import java.net.URL;
-import java.nio.charset.StandardCharsets;
 import java.security.cert.CertificateException;
 import java.security.cert.X509Certificate;
-import java.util.HashMap;
 import java.util.Map;
 
 import javax.net.ssl.HttpsURLConnection;
@@ -23,7 +17,6 @@ import javax.net.ssl.SSLSocketFactory;
 import javax.net.ssl.TrustManager;
 import javax.net.ssl.X509TrustManager;
 
-import org.apache.commons.io.IOUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -45,15 +38,18 @@ public class OnlineExamHttpUtil {
 
     public static final String AUTH = "Authorization";
 
+    protected String host = null;
+
     protected String uri = null;
 
     protected String accessKey = null;
 
     protected String accessSecret = null;
 
-    public OnlineExamHttpUtil(String accessKey, String accessSecret, String uri) {
+    public OnlineExamHttpUtil(String accessKey, String accessSecret, String host, String uri) {
         this.accessKey = accessKey;
         this.accessSecret = accessSecret;
+        this.host = host;
         this.uri = uri;
     }
 
@@ -74,16 +70,16 @@ public class OnlineExamHttpUtil {
         try {
 
             // 获取链接
-            URL url = new URL(uri);
+            URL url = new URL(host + uri);
             conn = (HttpsURLConnection) url.openConnection();
 
             conn.setRequestMethod(METHOD_POST);
             conn.setRequestProperty(CONTENT_TYPE, APPLICATION_JSON);
             // 设置鉴权
-            String signature = SignatureInfo.build(SignatureType.SECRET, METHOD_POST, uri, System.currentTimeMillis(),
-                    accessKey, accessSecret);
+            long ss = System.currentTimeMillis();
+            String signature = SignatureInfo.build(SignatureType.SECRET, METHOD_POST, uri, ss, accessKey, accessSecret);
             conn.setRequestProperty(AUTH, signature);
-
+            conn.setRequestProperty("time", String.valueOf(ss));
             // ssl
             SSLContext context = SSLContext.getInstance("SSL", "SunJSSE");
             TrustManager[] tm = new TrustManager[] { new X509TrustManager() {
@@ -209,18 +205,23 @@ public class OnlineExamHttpUtil {
     }
 
     public static void main(String[] args) throws IOException {
-        Map<String, Object> datas = new HashMap<String, Object>();
-        datas.put("examId", "62209205889142784");
-        String url = "https://test1.online-exam-test.cn/api/open/exam/course/query";
-        OnlineExamHttpUtil subjectHttp = new OnlineExamHttpUtil("123456", "123", url);
-        String subjectJson = subjectHttp.httpAction(null, datas);
-        System.out.println(subjectJson);
-
-        datas.put("courseCode", "A0001");
-        url = "https://test1.online-exam-test.cn/api/open/exam/record/need_mark";
-        OnlineExamHttpUtil studentHttp = new OnlineExamHttpUtil("123456", "123", url);
-        String studentJson = studentHttp.httpAction(null, datas);
-        System.out.println(studentJson);
+        // Map<String, Object> datas = new HashMap<String, Object>();
+        // datas.put("examId", "62209205889142784");
+        // datas.put("examId", "71613103238311936");
+        // String url =
+        // "https://test1.online-exam-test.cn/api/open/exam/course/query";
+        // OnlineExamHttpUtil subjectHttp = new OnlineExamHttpUtil("123456",
+        // "123", url);
+        // String subjectJson = subjectHttp.httpAction(null, datas);
+        // System.out.println(subjectJson);
+
+        // datas.put("courseCode", "A0001");
+        // url =
+        // "https://test1.online-exam-test.cn/api/open/exam/record/need_mark";
+        // OnlineExamHttpUtil studentHttp = new OnlineExamHttpUtil("123456",
+        // "123", url);
+        // String studentJson = studentHttp.httpAction(null, datas);
+        // System.out.println(studentJson);
 
         // datas.put("id", "62212585906769920");
         // url = "https://test1.online-exam-test.cn/api/open/exam/paper/detail";
@@ -228,10 +229,9 @@ public class OnlineExamHttpUtil {
         // "123", url);
         // String paperJson = paperHttp.httpAction(null, datas);
         // System.out.println(paperJson);
-        //
         // ByteArrayOutputStream buffer = new ByteArrayOutputStream();
         // IOUtils.copy(new
-        // ByteArrayInputStream(paperJson.getBytes(StandardCharsets.UTF_8)),
+        // ByteArrayInputStream(subjectJson.getBytes(StandardCharsets.UTF_8)),
         // buffer);
         // byte[] data = buffer.toByteArray();
         // File target = new File("/Users/ting.yin/Desktop/test123.json");