浏览代码

Merge branch 'xjw' of http://git.mooctest.net/summer/crowdsource-backend into xjw

# Conflicts:
#	src/main/java/edu/nju/service/ItemService.java
xujiawei 6 年之前
父节点
当前提交
fe9a7130b2

+ 13 - 2
src/main/java/edu/nju/controller/ExtraController.java

@@ -449,8 +449,19 @@ public class ExtraController {
 
 	@RequestMapping(value = "/uploadJob", method = RequestMethod.POST)
 	@ResponseBody
-	public String uploadExamUrl(String jobJson) {
-		return null;
+	public void uploadExamUrl(String jobJson, HttpServletResponse response) {
+		try {
+			PrintWriter out = response.getWriter();
+			JSONObject result = new JSONObject();
+			String job_id = extraService.saveJob(jobJson);
+			result.put("id", job_id);
+			out.print(result);
+			out.flush();
+			out.close();
+		} catch (IOException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
 
 	}
 

+ 32 - 17
src/main/java/edu/nju/controller/ItemController.java

@@ -25,6 +25,21 @@ public class ItemController {
     @Autowired
     ItemService iservice;
 
+    @RequestMapping(value = "/getJob")
+    @ResponseBody
+    public void getJobById(String job_id, HttpServletResponse response){
+        try {
+            PrintWriter out = response.getWriter();
+            Job job = iservice.getJob(job_id);
+            out.print(new JSONObject(job));
+            out.flush();
+            out.close();
+        }catch (IOException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+    }
+
     @RequestMapping(value = "/getItemsByJob")
     @ResponseBody
     public void getItemListByJobId(String job_id, HttpServletResponse response){
@@ -41,28 +56,29 @@ public class ItemController {
 
     }
 
-    @RequestMapping(value = "/getJob")
+    @RequestMapping(value = "/getItemsByWorker")
     @ResponseBody
-    public void getJobById(String job_id, HttpServletResponse response){
+    public void getItemListByWorkerJob(String job_id, String worker_id, HttpServletResponse response){
         try {
             PrintWriter out = response.getWriter();
-            Job job = iservice.getJob(job_id);
-            out.print(new JSONObject(job));
+            List<Item> items = iservice.getItemsByWorkerJob(worker_id, job_id);
+            out.print(new JSONArray(items));
             out.flush();
             out.close();
         }catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
+
     }
 
-    @RequestMapping(value = "/getAnswersByWorker")
+    @RequestMapping(value = "/getAnswerByWorkerItem")
     @ResponseBody
-    public void getAnswerByWorkerId(String item_id,String worker_id, HttpServletResponse response){
+    public void getAnswerByWorkerItem(String item_id,String worker_id, HttpServletResponse response){
         try {
             PrintWriter out = response.getWriter();
-            List<Answer> answers = iservice.getAnswerByWorker(item_id, worker_id);
-            out.print(new JSONArray(answers));
+            Answer answer = iservice.getAnswerByItemWorker(item_id, worker_id);
+            out.print(new JSONObject(answer));
             out.flush();
             out.close();
         }catch (IOException e) {
@@ -71,27 +87,26 @@ public class ItemController {
         }
     }
 
-    @RequestMapping(value = "/getItemsByWorker")
+    @RequestMapping(value = "/getAnswersByWorkerJob")
     @ResponseBody
-    public void getItemListByWorkerJob(String job_id, String worker_id, HttpServletResponse response){
+    public void getAnswersByWorkerJob(String job_id,String worker_id, HttpServletResponse response){
         try {
             PrintWriter out = response.getWriter();
-            List<Item> items = iservice.getItemsByWorkerJob(worker_id, job_id);
-            out.print(new JSONArray(items));
+            List<Answer>answers=iservice.getAnswersByWorkerJob(job_id, worker_id);
+            out.print(new JSONArray(answers));
             out.flush();
             out.close();
         }catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
-
     }
 
     @RequestMapping(value = "/saveAnswer")
     @ResponseBody
-    public void saveAnswer(String item_id, String worker_id,List<String>answers,String attachment_location, HttpServletResponse response){
+    public void saveAnswer(String item_id, String worker_id,String job_id,List<String>answers,List<String> attachment_location, HttpServletResponse response){
         JSONObject result = new JSONObject();
-        String id = iservice.saveAnswer(item_id, worker_id, answers, attachment_location);
+        String id = iservice.saveAnswer(item_id, worker_id, job_id, answers, attachment_location);
         if(id.equals("")) {
             result.put("status", "200");
             result.put("id", id);
@@ -112,9 +127,9 @@ public class ItemController {
 
     @RequestMapping(value = "/updateAnswer")
     @ResponseBody
-    public void saveAnswer(String id, String item_id, String worker_id,List<String>answers,String attachment_location, HttpServletResponse response){
+    public void updateAnswer(String id, String item_id, String worker_id, String job_id, List<String>answers,List<String> attachment_location, HttpServletResponse response){
         JSONObject result = new JSONObject();
-        if(iservice.updateAnswer(id, item_id, worker_id, answers, attachment_location)){
+        if(iservice.updateAnswer(id, item_id, worker_id, job_id, answers, attachment_location)){
             result.put("status", "200");
         } else {
             result.put("status", "500");

+ 13 - 2
src/main/java/edu/nju/dao/AnswerDao.java

@@ -15,10 +15,21 @@ public class AnswerDao {
     @Autowired
     private MongoOperations mongoOperations;
 
-    public List<Answer> findAnswersByItemWorker(String item_id, String worker_id){
+    public Answer findAnswerByItemWorker(String item_id, String worker_id){
         Query query = new Query();
         query.addCriteria(Criteria.where("item_id").is(item_id).and("worker_id").is(worker_id));
-        return mongoOperations.find(query, Answer.class);
+        List<Answer>answers= mongoOperations.find(query, Answer.class);
+        if(answers!=null&&answers.size()!=0){
+            return answers.get(0);
+        }
+        return null;
+    }
+
+    public List<Answer> findAnswersByJobWorker(String job_id, String worker_id){
+        Query query = new Query();
+        query.addCriteria(Criteria.where("job_id").is(job_id).and("worker_id").is(worker_id));
+        List<Answer>answers= mongoOperations.find(query, Answer.class);
+        return answers;
     }
 
     //存在则更新,不存在则插入

+ 0 - 1
src/main/java/edu/nju/dao/ItemDao.java

@@ -22,7 +22,6 @@ public class ItemDao {
         return item.getId();
     }
 
-
     public List<Item>findItemsByJob(String job_id){
         Query query = new Query();
         query.addCriteria(Criteria.where("task_id").is(job_id));

+ 6 - 0
src/main/java/edu/nju/dao/UserToItemDao.java

@@ -22,4 +22,10 @@ public class UserToItemDao {
         if(list.size() == 0 || list == null) {return null;}
         return list.get(0).getItem_id();
     }
+
+    //存在则更新,不存在则插入
+    public String save(UserToItem userToItem){
+        mongoOperations.save(userToItem);
+        return userToItem.getId();
+    }
 }

+ 26 - 5
src/main/java/edu/nju/entities/Answer.java

@@ -3,6 +3,7 @@ package edu.nju.entities;
 import org.springframework.data.annotation.Id;
 import org.springframework.data.mongodb.core.mapping.Document;
 
+import java.util.ArrayList;
 import java.util.List;
 
 @Document
@@ -20,15 +21,27 @@ public class Answer implements java.io.Serializable{
 
     private String worker_id;
 
-    private String attachment_location;
+    private String job_id;
 
-    public Answer(String item_id, List<String> answers, String worker_id, String attachment_location) {
+    private List<String> attachment_location;
+
+    public Answer(String item_id, List<String> answers, String worker_id, List<String> attachment_location,String job_id) {
         this.item_id = item_id;
         this.answers = answers;
         this.worker_id = worker_id;
         this.attachment_location = attachment_location;
+        this.job_id=job_id;
+    }
+    public Answer(String item_id,String worker_id,String job_id,int optionNum){
+        this.item_id = item_id;
+        this.worker_id = worker_id;
+        this.attachment_location = new ArrayList<>();
+        this.job_id=job_id;
+        this.answers=new ArrayList<>();
+        for(int i=0;i<optionNum;i++){
+            answers.add("-1");
+        }
     }
-
 
     public String getId() {
         return id;
@@ -62,14 +75,22 @@ public class Answer implements java.io.Serializable{
         this.worker_id = worker_id;
     }
 
-    public String getAttachment_location() {
+    public List<String> getAttachment_location() {
         return attachment_location;
     }
 
-    public void setAttachment_location(String attachment_location) {
+    public void setAttachment_location(List<String> attachment_location) {
         this.attachment_location = attachment_location;
     }
 
+    public String getJob_id() {
+        return job_id;
+    }
+
+    public void setJob_id(String job_id) {
+        this.job_id = job_id;
+    }
+
 
 
 }

+ 1 - 2
src/main/java/edu/nju/entities/UserToItem.java

@@ -19,8 +19,7 @@ public class UserToItem implements java.io.Serializable{
 
     private String job_id;
 
-    public UserToItem(String id, String worker_id, List<String> item_id, String job_id) {
-        this.id = id;
+    public UserToItem(String worker_id, List<String> item_id, String job_id) {
         this.item_id = item_id;
         this.worker_id = worker_id;
         this.job_id = job_id;

+ 17 - 9
src/main/java/edu/nju/service/ExtraService.java

@@ -32,6 +32,9 @@ public class ExtraService {
 
 	@Autowired
 	ItemDao itemDao;
+
+	@Autowired
+	ItemService itemService;
 	
 	//测试用例相关
 	public String saveTestCase(String report_id, String name, String front, String behind, String description) {
@@ -147,22 +150,23 @@ public class ExtraService {
 		//save item
 		//item index-id
 		HashMap<String,String> item_index_to_id=new HashMap<>();
+		HashMap<String,Integer>item_id_to_optionNum=new HashMap<>();
 		for(int i=0;i<itemArray.length();i++) {
 			JSONObject itemObject = itemArray.getJSONObject(i);
-			String item_id=saveItem(itemObject,job_id);
+			String item_id=saveItem(itemObject,job_id,item_id_to_optionNum);
 			String item_index=itemObject.getString("index");
 			item_index_to_id.put(item_index,item_id);
 		}
 
 		//worker_id-item_id_list
 		JSONArray workerArray=jsonObject.getJSONArray("workerList");
-		createWorkerToItem(item_index_to_id,workerArray);
+		createWorkerToItem(item_index_to_id,workerArray,job_id,item_id_to_optionNum);
 
 		return job_id;
 
 	}
 
-	private String saveItem(JSONObject itemObject,String job_id){
+	private String saveItem(JSONObject itemObject,String job_id,HashMap<String,Integer>item_id_to_optionNum){
 
 //			JSONObject itemObject=itemArray.getJSONObject(i);
 			String description=itemObject.getString("description");
@@ -181,7 +185,6 @@ public class ExtraService {
 				JSONObject fileObject=fileArray.getJSONObject(j);
 //				System.out.println(fileObject.getString("url"));
 				file_urls.add(fileObject.getString("url"));
-
 			}
 
 			List<String>  options=new ArrayList<>();
@@ -193,12 +196,13 @@ public class ExtraService {
 				options.add(optionObject.getString("option"));
 			}
 			Item item=new Item(description,img_urls,isRequired,isMultiple,file_urls,options,job_id);
-			return itemDao.saveItem(item);
-
+			String item_id = itemDao.saveItem(item);
+			item_id_to_optionNum.put(item_id,optionArray.length());
+			return item_id;
 
 	}
 
-	private void createWorkerToItem(HashMap<String,String> item_index_to_id,JSONArray workerArray){
+	private void createWorkerToItem(HashMap<String,String> item_index_to_id,JSONArray workerArray,String job_id,HashMap<String,Integer>item_id_to_optionNum){
 		HashMap<String,List<String>> worker_to_item=new HashMap<>();
 
 		for(int i=0;i<workerArray.length();i++){
@@ -211,9 +215,13 @@ public class ExtraService {
 				itemIdList.add(item_index_to_id.get(itemIndexObject.getString("index")));
 			}
 			worker_to_item.put(worker_id,itemIdList);
+			itemService.saveUserToItem(worker_id,itemIdList,job_id);
+			for(int j=0;j<itemIdList.size();j++){
+				String item_id=itemIdList.get(j);
+				int optionNum=item_id_to_optionNum.get(item_id);
+				String answer_id=itemService.saveAnswer(item_id,worker_id,job_id,optionNum);
+			}
 		}
-
-		//todo worker-to-item
 	}
 
 

+ 28 - 9
src/main/java/edu/nju/service/ItemService.java

@@ -1,8 +1,13 @@
 package edu.nju.service;
 
-
-import edu.nju.dao.*;
-import edu.nju.entities.*;
+import edu.nju.dao.AnswerDao;
+import edu.nju.dao.ItemDao;
+import edu.nju.dao.JobDao;
+import edu.nju.dao.UserToItemDao;
+import edu.nju.entities.Answer;
+import edu.nju.entities.Item;
+import edu.nju.entities.Job;
+import edu.nju.entities.UserToItem;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -28,19 +33,28 @@ public class ItemService {
         return jobDao.findJob(job_id);
     }
 
-    public List<Answer> getAnswerByWorker(String item_id, String worker_id){
-        return answerDao.findAnswersByItemWorker(item_id, worker_id);
+    public Answer getAnswerByItemWorker(String item_id, String worker_id){
+        return answerDao.findAnswerByItemWorker(item_id, worker_id);
+    }
+    public List<Answer> getAnswersByWorkerJob(String job_id, String worker_id){
+        return answerDao.findAnswersByJobWorker(job_id, worker_id);
     }
 
-    public String saveAnswer(String item_id, String worker_id,List<String>answers,String attachment_location){
-        Answer answer=new Answer(item_id,answers,worker_id,attachment_location);
+    public String saveAnswer(String item_id, String worker_id,String job_id, List<String>answers,List<String> attachment_location){
+        Answer answer=new Answer(item_id,answers,worker_id,attachment_location,job_id);
         return answerDao.save(answer);
     }
 
-    public boolean updateAnswer(String id, String item_id, String worker_id,List<String>answers,String attachment_location){
+    //对answer进行初始化
+    public String saveAnswer(String item_id, String worker_id,String job_id, int optionNum){
+        Answer answer=new Answer(item_id,worker_id,job_id,optionNum);
+        return answerDao.save(answer);
+    }
+
+    public boolean updateAnswer(String id, String item_id, String worker_id, String job_id, List<String>answers,List<String> attachment_location){
         try {
             if(id == null || id.equals("undefined")) { return false; }
-            Answer answer=new Answer(item_id,answers,worker_id,attachment_location);
+            Answer answer=new Answer(item_id,answers,worker_id,attachment_location,job_id);
             answer.setId(id);
             answerDao.save(answer);
             return true;
@@ -63,4 +77,9 @@ public class ItemService {
         return items;
     }
 
+    public String saveUserToItem(String worker_id, List<String> item_id, String job_id){
+        UserToItem userToItem=new UserToItem(worker_id, item_id, job_id);
+        return userToItemDao.save(userToItem);
+    }
+
 }