|
@@ -0,0 +1,318 @@
|
|
|
+package edu.nju.service;
|
|
|
+
|
|
|
+import edu.nju.dao.*;
|
|
|
+import edu.nju.entities.ReviewItem;
|
|
|
+import edu.nju.entities.ReviewPaper;
|
|
|
+import edu.nju.entities.ReviewPaperJson;
|
|
|
+import edu.nju.entities.ReviewReport;
|
|
|
+import edu.nju.vo.ReviewPaperVO;
|
|
|
+import org.json.JSONArray;
|
|
|
+import org.json.JSONObject;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class ReviewPaperService {
|
|
|
+ @Autowired
|
|
|
+ ReviewJobDao jobDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ ReviewItemDao itemDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ ReviewAnswerDao answerDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ ReviewService reviewService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ ReviewReportDao reviewReportDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ ReviewPaperJsonDao reviewPaperJsonDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ ReviewPaperDao reviewPaperDao;
|
|
|
+
|
|
|
+ public ReviewPaperJson getReviewPaperJson(String paper_id) {
|
|
|
+ return reviewPaperJsonDao.findJsonByPaper(paper_id);
|
|
|
+ }
|
|
|
+
|
|
|
+ public ReviewPaper getPaper(String paper_id){
|
|
|
+ return reviewPaperDao.findPaper(paper_id);
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<ReviewReport> getReportsByPaperId(String paper_id){
|
|
|
+ return reviewReportDao.getReportsByPaperId(paper_id);
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<ReviewPaper> getPapers(){
|
|
|
+ return reviewPaperDao.findPapers();
|
|
|
+ }
|
|
|
+
|
|
|
+ public JSONObject updatePaper(String paperId,String paperJson){
|
|
|
+ JSONObject object = new JSONObject();
|
|
|
+ String result=checkPaper(paperJson);
|
|
|
+ if(!result.equals("success")){
|
|
|
+ object.put("status","fail");
|
|
|
+ object.put("message",result);
|
|
|
+ return object;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ JSONObject jsonObject = new JSONObject(paperJson);
|
|
|
+ ReviewPaperVO reviewPaperVO = getReviewPaperVO(jsonObject);
|
|
|
+ ReviewPaper reviewPaper = reviewPaperDao.findPaper(paperId);
|
|
|
+ reviewPaper.setReviewPaper(reviewPaperVO);
|
|
|
+ reviewPaperDao.save(reviewPaper); //实际是update
|
|
|
+ reviewPaperJsonDao.update(paperId, paperJson);
|
|
|
+ reviewReportDao.removeByPaper(paperId);
|
|
|
+ itemDao.removeByPaper(paperId);
|
|
|
+ saveReportAndItem(paperId, jsonObject);
|
|
|
+ object.put("status","success");
|
|
|
+ object.put("paperId",paperId);
|
|
|
+ }catch (Exception e){
|
|
|
+ object.put("status","fail");
|
|
|
+ object.put("message",e.getMessage());
|
|
|
+ }
|
|
|
+ return object;
|
|
|
+ }
|
|
|
+
|
|
|
+ public JSONObject savePaper(String paperJson){
|
|
|
+ JSONObject object = new JSONObject();
|
|
|
+ String result=checkPaper(paperJson);
|
|
|
+ if(!result.equals("success")){
|
|
|
+ object.put("status","fail");
|
|
|
+ object.put("message",result);
|
|
|
+ return object;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ JSONObject jsonObject = new JSONObject(paperJson);
|
|
|
+ ReviewPaperVO reviewPaperVO=getReviewPaperVO(jsonObject);
|
|
|
+ String paperId = reviewPaperDao.save(new ReviewPaper(reviewPaperVO));
|
|
|
+ reviewPaperJsonDao.save(new ReviewPaperJson(paperId,paperJson));
|
|
|
+ saveReportAndItem(paperId,jsonObject);
|
|
|
+ object.put("status","success");
|
|
|
+ object.put("paperId",paperId);
|
|
|
+ }catch (Exception e){
|
|
|
+ object.put("status","fail");
|
|
|
+ object.put("message",e.getMessage());
|
|
|
+ //TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
|
|
|
+ }
|
|
|
+ return object;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveReportAndItem(String paperId,JSONObject jsonObject)throws Exception{
|
|
|
+ try {
|
|
|
+ JSONArray reportArray = jsonObject.getJSONArray("report_list");
|
|
|
+ HashMap<Integer, String> reportIndexToId = new HashMap<>();
|
|
|
+ for (int i = 0; i < reportArray.length(); i++) {
|
|
|
+ JSONObject reportObject = reportArray.getJSONObject(i);
|
|
|
+ int reportIndex = reportObject.getInt("index");
|
|
|
+ String id = saveReport(reportObject, paperId);
|
|
|
+ reportIndexToId.put(reportIndex, id);
|
|
|
+ }
|
|
|
+
|
|
|
+ JSONArray itemGroupArray = jsonObject.getJSONArray("item_group_list");
|
|
|
+ //save item
|
|
|
+ //item index-id
|
|
|
+ HashMap<String, Integer> itemIdToOptionNum = new HashMap<>();
|
|
|
+ HashMap<Integer, String> itemIndexToId = new HashMap<>();
|
|
|
+ for (int i = 0; i < itemGroupArray.length(); i++) {
|
|
|
+ JSONObject itemGroupObject = itemGroupArray.getJSONObject(i);
|
|
|
+ saveItem(itemGroupObject, paperId, itemIdToOptionNum, reportIndexToId, itemIndexToId);
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ throw new Exception(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private ReviewPaperVO getReviewPaperVO(JSONObject jsonObject)throws Exception{
|
|
|
+ try {
|
|
|
+ String description = jsonObject.getString("description");
|
|
|
+ String name = jsonObject.getString("name");
|
|
|
+ String time = jsonObject.getString("create_time");
|
|
|
+ String type = jsonObject.getString("type");
|
|
|
+ String applicationLocation = jsonObject.getString("application_url");
|
|
|
+ String requirementLocation = jsonObject.getString("requirement_url");
|
|
|
+ ReviewPaperVO reviewPaperVO=new ReviewPaperVO(description, name, time, type, applicationLocation, requirementLocation);
|
|
|
+ return reviewPaperVO;
|
|
|
+ }catch (Exception e){
|
|
|
+ throw new Exception(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String saveReport(JSONObject reportObject,String paper_id)throws Exception{
|
|
|
+ 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");
|
|
|
+ String original_id=reportObject.getString("original_id");
|
|
|
+ ReviewReport reviewReport=new ReviewReport(original_id,paper_id,name,description,img_urls,file_urls);
|
|
|
+ return reviewReportDao.save(reviewReport);
|
|
|
+ }catch (Exception e){
|
|
|
+ throw new Exception("report 创建失败: "+e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveItem(JSONObject itemGroupObject,String paper_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{
|
|
|
+ //List<String>itemIds=new ArrayList<>();
|
|
|
+ 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(paper_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());
|
|
|
+ }
|
|
|
+// 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是否存在! ");
|
|
|
+// }
|
|
|
+ }
|
|
|
+
|
|
|
+ public String checkPaper(String paperJson){
|
|
|
+ String result="success";
|
|
|
+ try {
|
|
|
+ JSONObject jsonObject=new JSONObject(paperJson);
|
|
|
+ String description=jsonObject.getString("description");
|
|
|
+ String name=jsonObject.getString("name");
|
|
|
+ String time=jsonObject.getString("create_time");
|
|
|
+ String type=jsonObject.getString("type");
|
|
|
+ String application_location=jsonObject.getString("application_url");
|
|
|
+ String requirement_location=jsonObject.getString("requirement_url");
|
|
|
+
|
|
|
+ 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 originalId=report.getString("original_id");
|
|
|
+ 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<Integer>itemReportIndexs=new HashSet<>();
|
|
|
+
|
|
|
+ for(int i=0;i<itemGroupArray.length();i++){
|
|
|
+ Set<Integer>itemIndexs=new HashSet<>();
|
|
|
+ JSONObject itemGroup=itemGroupArray.getJSONObject(i);
|
|
|
+ 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);
|
|
|
+ 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");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+// 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=String.valueOf(workerList.get(j));
|
|
|
+// if(workers.contains(workerId)){
|
|
|
+// throw new Exception("同一个worker不能属于不同的group");
|
|
|
+// }else{
|
|
|
+// workers.add(workerId);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// 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();
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean delPaper(String paperId){
|
|
|
+ try {
|
|
|
+ reviewPaperDao.remove(paperId);
|
|
|
+ reviewPaperJsonDao.remove(paperId);
|
|
|
+ reviewReportDao.removeByPaper(paperId);
|
|
|
+ itemDao.removeByPaper(paperId);
|
|
|
+ return true;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|