瀏覽代碼

shangchuan

MengyangDuan 5 年之前
父節點
當前提交
94f80dcdb5

+ 18 - 0
src/main/java/edu/nju/controller/ExtraController.java

@@ -465,6 +465,24 @@ public class ExtraController {
 
 	}
 
+//	@RequestMapping(value = "/checkJob", method = RequestMethod.POST)
+//	@ResponseBody
+//	public void checkReviewJob(String jobJson, HttpServletResponse response) {
+//		try {
+////			System.out.println(jobJson);
+//			PrintWriter out = response.getWriter();
+//			String result = extraService.checkJob(jobJson);
+//			out.print(result);
+////			out.print("success");
+//			out.flush();
+//			out.close();
+//		} catch (IOException e) {
+//			// TODO Auto-generated catch block
+//			e.printStackTrace();
+//		}
+//
+//	}
+
 	@RequestMapping(value = "/checkJob", method = RequestMethod.POST)
 	@ResponseBody
 	public void checkReviewJobJson(String jobJson, HttpServletResponse response) {

+ 16 - 4
src/main/java/edu/nju/controller/ReviewController.java

@@ -1,9 +1,6 @@
 package edu.nju.controller;
 
-import edu.nju.entities.ReviewAnswer;
-import edu.nju.entities.ReviewItem;
-import edu.nju.entities.ReviewJob;
-import edu.nju.entities.ReviewReport;
+import edu.nju.entities.*;
 import edu.nju.service.ReviewService;
 import org.json.JSONArray;
 import org.json.JSONObject;
@@ -39,6 +36,21 @@ public class ReviewController {
         }
     }
 
+    @RequestMapping(value = "/getJobJson")
+    @ResponseBody
+    public void getJobJson(String job_id, HttpServletResponse response){
+        try {
+            PrintWriter out = response.getWriter();
+            ReviewJobJson jobJson = iservice.getReviewJobJson(job_id);
+            out.print(new JSONObject(jobJson));
+            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){

+ 31 - 0
src/main/java/edu/nju/dao/ReviewJobJsonDao.java

@@ -0,0 +1,31 @@
+package edu.nju.dao;
+
+import edu.nju.entities.ReviewJobJson;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.mongodb.core.MongoOperations;
+import org.springframework.data.mongodb.core.query.Criteria;
+import org.springframework.data.mongodb.core.query.Query;
+import org.springframework.stereotype.Repository;
+
+import java.util.List;
+
+@Repository
+public class ReviewJobJsonDao {
+
+    @Autowired
+    private MongoOperations mongoOperations;
+
+    //id查询,find查询所有
+    public ReviewJobJson findJsonByJob(String jobId){
+        Query query = new Query();
+        query.addCriteria(Criteria.where("job_id").is(jobId));
+        List<ReviewJobJson> list = mongoOperations.find(query,ReviewJobJson.class);
+        if(list.size() == 0 || list == null) {return null;}
+        return list.get(0);
+    }
+
+    public String save(ReviewJobJson reviewJobJson) {
+        mongoOperations.save(reviewJobJson);
+        return reviewJobJson.getId();
+    }
+}

+ 47 - 0
src/main/java/edu/nju/entities/ReviewJobJson.java

@@ -0,0 +1,47 @@
+package edu.nju.entities;
+
+import org.springframework.data.annotation.Id;
+import org.springframework.data.mongodb.core.mapping.Document;
+
+@Document
+public class ReviewJobJson implements java.io.Serializable{
+
+    private static final long serialVersionUID = 8207574733829202771L;
+
+    @Id
+    private String id;
+
+    private String job_id;
+
+    private String json;
+
+    public ReviewJobJson(String job_id, String json) {
+        this.job_id = job_id;
+        this.json = json;
+    }
+
+
+    public String getId() {
+        return id;
+    }
+
+    public void setId(String id) {
+        this.id = id;
+    }
+
+    public String getJob_id() {
+        return job_id;
+    }
+
+    public void setJob_id(String job_id) {
+        this.job_id = job_id;
+    }
+
+    public String getJson() {
+        return json;
+    }
+
+    public void setJson(String json) {
+        this.json = json;
+    }
+}

+ 152 - 116
src/main/java/edu/nju/service/ExtraService.java

@@ -43,6 +43,9 @@ public class ExtraService {
 
 	@Autowired
 	ReviewReportDao reviewReportDao;
+
+	@Autowired
+	ReviewJobJsonDao reviewJobJsonDao;
 	
 	//测试用例相关
 	public String saveTestCase(String report_id, String name, String front, String behind, String description) {
@@ -149,6 +152,12 @@ public class ExtraService {
 	@Transactional(rollbackFor = Exception.class)
 	public  JSONObject saveJob(String jobJson){
 		JSONObject object = new JSONObject();
+		String result=checkJob(jobJson);
+		if(!result.equals("success")){
+			object.put("status","fail");
+			object.put("message",result);
+			return object;
+		}
 
 		try {
 			JSONObject jsonObject = new JSONObject(jobJson);
@@ -161,6 +170,7 @@ public class ExtraService {
 			int reportNum = jsonObject.getInt("report_num");
 			int workerNum = jsonObject.getInt("worker_num");
 			String jobId = jobDao.save(new ReviewJob(description, name, time, type, applicationLocation, requirementLocation, reportNum, workerNum));
+			reviewJobJsonDao.save(new ReviewJobJson(jobId,jobJson));
 
 
 			JSONArray reportArray = jsonObject.getJSONArray("report_list");
@@ -197,94 +207,112 @@ public class ExtraService {
 	}
 
 	private String saveReport(JSONObject reportObject,String job_id)throws Exception{
-		JSONArray imgArray=reportObject.getJSONArray("img_url");
-		List<String> img_urls=new ArrayList<>();
-		for(int i=0;i<imgArray.length();i++){
-			img_urls.add(String.valueOf(imgArray.get(i)));
-		}
-		JSONArray fileArray=reportObject.getJSONArray("file_url");
-		List<String> file_urls=new ArrayList<>();
-		for(int i=0;i<fileArray.length();i++){
-			file_urls.add(String.valueOf(fileArray.get(i)));
+		try {
+			JSONArray imgArray=reportObject.getJSONArray("img_url");
+			List<String> img_urls=new ArrayList<>();
+			for(int i=0;i<imgArray.length();i++){
+				img_urls.add(String.valueOf(imgArray.get(i)));
+			}
+			JSONArray fileArray=reportObject.getJSONArray("file_url");
+			List<String> file_urls=new ArrayList<>();
+			for(int i=0;i<fileArray.length();i++){
+				file_urls.add(String.valueOf(fileArray.get(i)));
+			}
+			String name=reportObject.getString("name");
+			String description=reportObject.getString("description");
+			ReviewReport reviewReport=new ReviewReport(job_id,name,description,img_urls,file_urls);
+			return reviewReportDao.save(reviewReport);
+		}catch (Exception e){
+			throw new Exception("report 创建失败: "+e.getMessage());
 		}
-		String name=reportObject.getString("name");
-		String description=reportObject.getString("description");
-		ReviewReport reviewReport=new ReviewReport(job_id,name,description,img_urls,file_urls);
-		return reviewReportDao.save(reviewReport);
-
 	}
 
 	private void saveItem(JSONObject itemGroupObject,String job_id,HashMap<String,Integer>item_id_to_option_num,HashMap<Integer,String>report_index_to_id,HashMap<Integer,String>item_index_to_id)throws Exception{
-
-//			JSONObject itemObject=itemArray.getJSONObject(i);
-		List<String> reportIds=new ArrayList<>();
-		JSONArray reportArray=itemGroupObject.getJSONArray("report_list");
-		for (int j=0;j<reportArray.length();j++){
-			int reportIndex= Integer.parseInt(String.valueOf(reportArray.get(j)));
-			reportIds.add(report_index_to_id.get(reportIndex));
-		}
-		JSONArray itemArray=itemGroupObject.getJSONArray("item_list");
 		List<String>itemIds=new ArrayList<>();
-		for (int j=0;j<itemArray.length();j++){
-			JSONObject itemObject=itemArray.getJSONObject(j);
-			int index=itemObject.getInt("index");
-			Boolean isRequired=itemObject.getBoolean("is_required");
-			String description=itemObject.getString("description");
-			String type=itemObject.getString("type");
-			List<String>options=new ArrayList<>();
-			JSONArray optionArray=itemObject.getJSONArray("options");
-			for(int k=0;k<optionArray.length();k++){
-				options.add(String.valueOf(optionArray.get(k)));
+		List<String> reportIds=new ArrayList<>();
+		try {
+			JSONArray reportArray=itemGroupObject.getJSONArray("report_list");
+			for (int j=0;j<reportArray.length();j++){
+				int reportIndex= Integer.parseInt(String.valueOf(reportArray.get(j)));
+				reportIds.add(report_index_to_id.get(reportIndex));
+			}
+			JSONArray itemArray=itemGroupObject.getJSONArray("item_list");
+
+			for (int j=0;j<itemArray.length();j++){
+				JSONObject itemObject=itemArray.getJSONObject(j);
+				int index=itemObject.getInt("index");
+				Boolean isRequired=itemObject.getBoolean("is_required");
+				String description=itemObject.getString("description");
+				String type=itemObject.getString("type");
+				List<String>options=new ArrayList<>();
+				JSONArray optionArray=itemObject.getJSONArray("options");
+				for(int k=0;k<optionArray.length();k++){
+					options.add(String.valueOf(optionArray.get(k)));
+				}
+				ReviewItem item=new ReviewItem(job_id,isRequired,options,description,reportIds,type);
+				String item_id = itemDao.saveItem(item);
+				itemIds.add(item_id);
+				item_id_to_option_num.put(item_id,options.size());
+				item_index_to_id.put(index,item_id);
 			}
-			ReviewItem item=new ReviewItem(job_id,isRequired,options,description,reportIds,type);
-			String item_id = itemDao.saveItem(item);
-			itemIds.add(item_id);
-			item_id_to_option_num.put(item_id,options.size());
-			item_index_to_id.put(index,item_id);
+		}catch (Exception e){
+			throw new Exception("item 创建失败: "+e.getMessage());
 		}
-		for(int j=0;j<reportIds.size();j++){
-			ReviewReport report=reviewReportDao.findReviewReport(reportIds.get(j));
-			report.setItem_id(itemIds);
-			reviewReportDao.save(report);
+		try {
+			for(int j=0;j<reportIds.size();j++){
+				ReviewReport report=reviewReportDao.findReviewReport(reportIds.get(j));
+				report.setItem_id(itemIds);
+				reviewReportDao.save(report);
+			}
+		}catch (Exception e){
+			throw new Exception("item创建失败,请检查配置的report_index是否存在! ");
 		}
-
 	}
 
 	private void createWorkerToItem(JSONArray groupArray,String job_id,HashMap<Integer,String>report_index_to_id,HashMap<String,Integer>item_id_to_option_num)throws Exception{
 		for(int i=0;i<groupArray.length();i++){
-			JSONObject groupObject=groupArray.getJSONObject(i);
-			String description=groupObject.getString("description");
-			String name=groupObject.getString("name");
-			JSONArray workerArray=groupObject.getJSONArray("worker_list");
-			List<String> workerList=new ArrayList<>();
-			for (int j=0;j<workerArray.length();j++){
-				workerList.add(String.valueOf(workerArray.get(j)));
-			}
-			JSONArray reportIndexArray=groupObject.getJSONArray("report_list");
 			List<String> reportIdList=new ArrayList<>();
-			for (int j=0;j<reportIndexArray.length();j++){
-				int reportIndex=Integer.parseInt(String.valueOf(reportIndexArray.get(j)));
-				reportIdList.add(report_index_to_id.get(reportIndex));
+			List<String> workerList=new ArrayList<>();
+			try {
+				JSONObject groupObject=groupArray.getJSONObject(i);
+				String description=groupObject.getString("description");
+				String name=groupObject.getString("name");
+				JSONArray workerArray=groupObject.getJSONArray("worker_list");
+				for (int j=0;j<workerArray.length();j++){
+					workerList.add(String.valueOf(workerArray.get(j)));
+				}
+				JSONArray reportIndexArray=groupObject.getJSONArray("report_list");
+				for (int j=0;j<reportIndexArray.length();j++){
+					int reportIndex=Integer.parseInt(String.valueOf(reportIndexArray.get(j)));
+					reportIdList.add(report_index_to_id.get(reportIndex));
+				}
+				reviewService.saveGroup(name,description,workerList,job_id,reportIdList);
+			}catch (Exception e){
+				throw new Exception("group 创建失败: "+e.getMessage());
 			}
-			reviewService.saveGroup(name,description,workerList,job_id,reportIdList);
-			for(int j=0;j<workerList.size();j++){
-				String worker_id=workerList.get(j);
+			try {
+				for(int j=0;j<workerList.size();j++){
+					String worker_id=workerList.get(j);
 //				reviewService.saveUserToItem(worker_id,itemIdList,job_id,"","","","");
-				for(int k=0;k<reportIdList.size();k++){
-					String report_id=reportIdList.get(k);
-					List<ReviewItem>items= reviewService.getItemsByReport(report_id);
-					for(int l=0;l<items.size();l++) {
-						String item_id=items.get(l).getId();
-						int optionNum=item_id_to_option_num.get(item_id);
-						ReviewAnswer answer=new ReviewAnswer(item_id,report_id,worker_id,job_id,optionNum);
-						answerDao.save(answer);
+					for(int k=0;k<reportIdList.size();k++){
+						String report_id=reportIdList.get(k);
+						List<ReviewItem>items= reviewService.getItemsByReport(report_id);
+						for(int l=0;l<items.size();l++) {
+							String item_id=items.get(l).getId();
+							int optionNum=item_id_to_option_num.get(item_id);
+							ReviewAnswer answer=new ReviewAnswer(item_id,report_id,worker_id,job_id,optionNum);
+							answerDao.save(answer);
+						}
 					}
 				}
+			}catch (Exception e){
+				throw new Exception("group创建失败,请检查配置的report_index是否存在! ");
 			}
 		}
 	}
+
 	public String checkJob(String jobJson){
-		String result="";
+		String result="success";
 		try {
 			JSONObject jsonObject=new JSONObject(jobJson);
 			String description=jsonObject.getString("description");
@@ -293,85 +321,93 @@ public class ExtraService {
 			String type=jsonObject.getString("type");
 			String application_location=jsonObject.getString("application_url");
 			String requirement_location=jsonObject.getString("requirement_url");
-			String report_num=jsonObject.getString("report_num");
-			checkNum(report_num);
-			String worker_num=jsonObject.getString("worker_num");
-			checkNum(worker_num);
+			int report_num=jsonObject.getInt("report_num");
+			int worker_num=jsonObject.getInt("worker_num");
+
+			Set<Integer>reportIndexs=new HashSet<>();
+			JSONArray reportList=jsonObject.getJSONArray("report_list");
+			for(int i=0;i<reportList.length();i++){
+				JSONObject report=reportList.getJSONObject(i);
+				int index=report.getInt("index");
+				if(reportIndexs.contains(index)){
+					throw new Exception("report_list中report的index不能重复");
+				}else{
+					reportIndexs.add(index);
+				}
+				String reportName=report.getString("name");
+				String reportDescription=report.getString("description");
+				JSONArray imgUrls=report.getJSONArray("img_url");
+				JSONArray fileUrls=report.getJSONArray("file_url");
+			}
+
 			JSONArray itemGroupArray=jsonObject.getJSONArray("item_group_list");
-			Set<String>reportIndexs=new HashSet<>();
-			Set<String>itemIndexs=new HashSet<>();
+			Set<Integer>itemReportIndexs=new HashSet<>();
+
 			for(int i=0;i<itemGroupArray.length();i++){
+				Set<Integer>itemIndexs=new HashSet<>();
 				JSONObject itemGroup=itemGroupArray.getJSONObject(i);
-				JSONArray reportList=itemGroup.getJSONArray("report_list");
-				for(int j=0;j<reportList.length();j++){
-					String reportIndex=reportList.getJSONObject(j).getString("report_index");
-					if(reportIndexs.contains(reportIndex)){
-
+				JSONArray itemReportList=itemGroup.getJSONArray("report_list");
+				for(int j=0;j<itemReportList.length();j++){
+					int reportIndex=Integer.parseInt(String.valueOf(itemReportList.get(j)));
+					if(!reportIndexs.contains(reportIndex)){
+						throw new Exception("item_group_list中包含不存在的report ");
 					}else{
-
+						if(itemReportIndexs.contains(reportIndex)){
+							throw new Exception("item_group_list中的report不能重复");
+						}
+						else{
+							itemReportIndexs.add(reportIndex);
+						}
 					}
 				}
 				JSONArray itemList=itemGroup.getJSONArray("item_list");
 				for(int j=0;j<itemList.length();j++){
 					JSONObject item=itemList.getJSONObject(j);
-					String index=item.getString("index");
-					String itemDescription=item.getString("descroption");
-					String isRequired=item.getString("is_required");
+					int index=item.getInt("index");
+					if(itemIndexs.contains(index)){
+						throw new Exception("item_group_list中的item_index不能重复");
+					}
+					else {
+						itemIndexs.add(index);
+					}
+					String itemDescription=item.getString("description");
+					Boolean isRequired=item.getBoolean("is_required");
 					String itemType=item.getString("type");
 					JSONArray options=item.getJSONArray("options");
-					for(int k=0;k<options.length();k++){
-						String option=options.getJSONObject(k).getString("option");
-					}
 				}
 			}
+
+
 			JSONArray groupList=jsonObject.getJSONArray("group_list");
+			Set<String>workers=new HashSet<>();
 			for(int i=0;i<groupList.length();i++){
 				JSONObject groupObject=groupList.getJSONObject(i);
 				String groupName=groupObject.getString("name");
 				String groupDescription=groupObject.getString("description");
 				JSONArray workerList=groupObject.getJSONArray("worker_list");
 				for(int j=0;j<workerList.length();j++){
-					String workerId=workerList.getJSONObject(j).getString("worker_id");
-				}
-				JSONArray reportList=groupObject.getJSONArray("report_list");
-				for(int j=0;j<reportList.length();j++){
-					String reportIndex=reportList.getJSONObject(j).getString("report_index");
-				}
-			}
-			JSONArray reportList=jsonObject.getJSONArray("report_list");
-			for(int i=0;i<reportList.length();i++){
-				JSONObject report=reportList.getJSONObject(i);
-				String index=report.getString("index");
-				String reportName=report.getString("name");
-				String reportDescription=report.getString("description");
-				JSONArray imgUrls=report.getJSONArray("img_url");
-				for(int j=0;j<imgUrls.length();j++){
-					String url=imgUrls.getJSONObject(j).getString("url");
+					String workerId=String.valueOf(workerList.get(j));
+					if(workers.contains(workerId)){
+						throw new Exception("同一个worker不能属于不同的group");
+					}else{
+						workers.add(workerId);
+					}
 				}
-				JSONArray fileUrls=report.getJSONArray("file_url");
-				for(int j=0;j<fileUrls.length();j++){
-					String url=fileUrls.getJSONObject(j).getString("url");
+				JSONArray workerReportList=groupObject.getJSONArray("report_list");
+				for(int j=0;j<workerReportList.length();j++){
+					int reportIndex= Integer.parseInt(String.valueOf(workerReportList.get(j)));
+					if(!reportIndexs.contains(reportIndex)){
+						throw new Exception("group_report_list中包含不存在的report ");
+					}
 				}
 			}
-
 		}catch (Exception e){
 			return e.getMessage();
 		}
-//		String name=jsonObject.getString("name");
-//		if(name==null){
-//			return "标题为空";
-//		}
 
 		return result;
 	}
 
-	private void checkNum(String str) throws Exception {
-		try{
-			int n=Integer.parseInt(str);
-		}catch(NumberFormatException e){
-			throw new Exception("输入的字符串必须能够转化成数字!",e);
-		}
-	}
 
 
 }

+ 6 - 0
src/main/java/edu/nju/service/ReviewService.java

@@ -22,6 +22,8 @@ public class ReviewService {
     ReviewGroupDao groupDao;
     @Autowired
     ReviewReportDao reportDao;
+    @Autowired
+    ReviewJobJsonDao reviewJobJsonDao;
 
 //    public List<ReviewItem>getJobItems(String job_id){
 //        return itemDao.findItemsByJob(job_id);
@@ -31,6 +33,10 @@ public class ReviewService {
         return jobDao.findJob(job_id);
     }
 
+    public ReviewJobJson getReviewJobJson(String job_id) {
+        return reviewJobJsonDao.findJsonByJob(job_id);
+    }
+
     public List<ReviewAnswer> getAnswersByReportWorker(String report_id, String worker_id){
         return answerDao.findAnswersByReportWorker(report_id, worker_id);
     }