ExtraService.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. package edu.nju.service;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.OutputStreamWriter;
  5. import java.io.Writer;
  6. import java.text.SimpleDateFormat;
  7. import java.util.*;
  8. import edu.nju.dao.*;
  9. import edu.nju.entities.*;
  10. import edu.nju.model.*;
  11. import edu.nju.util.*;
  12. import org.apache.commons.lang3.EnumUtils;
  13. import org.json.JSONArray;
  14. import org.json.JSONObject;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.beans.factory.annotation.Value;
  17. import org.springframework.stereotype.Service;
  18. import org.springframework.web.bind.annotation.ResponseBody;
  19. @Service
  20. public class ExtraService {
  21. @Autowired
  22. ReportDao reportDao;
  23. @Autowired
  24. ExamDao examDao;
  25. @Autowired
  26. TestCaseDao testDao;
  27. @Autowired
  28. BugDao bugDao;
  29. @Autowired
  30. TaskDao taskDao;
  31. @Autowired
  32. BugMirrorDao mirrorDao;
  33. @Autowired
  34. ReviewPaperService reviewPaperService;
  35. @Autowired
  36. CrowdSourceToReviewPaperDao crowdSourceToReviewPaperDao;
  37. @Value("${sanjiPageUrl}")
  38. private String pageUrl;
  39. @Value("${main.site}")
  40. private String mainSiteUrl;
  41. //测试用例相关
  42. public String saveTestCase(String report_id, String name, String front, String behind, String description) {
  43. TestCase testCase = new TestCase(name, front, behind, description, report_id, Long.toString(System.currentTimeMillis()));
  44. return testDao.save(testCase);
  45. }
  46. // public String saveTestCase(String report_id, String name, String front, String behind, String description,String if_execute,String if_bug) {
  47. // TestCase testCase = new TestCase(name, front, behind, description, report_id, Long.toString(System.currentTimeMillis()),if_execute,if_bug);
  48. // return testDao.save(testCase);
  49. // }
  50. public boolean updateTestCase(String id, String report_id, String name, String front, String behind, String description) {
  51. try {
  52. testDao.updateTestCase(id, report_id, name, front, behind, description);
  53. return true;
  54. } catch (Exception e) { return false; }
  55. }
  56. public List<TestCase> getCaseList(String report_id) {
  57. return testDao.findByReport(report_id);
  58. }
  59. public TestCase getTestCase(String id) {
  60. return testDao.findById(id);
  61. }
  62. //测试报告相关
  63. public synchronized String saveReport(String case_id, String task_id, String case_take_id, String woker_id, String name, String device_model,
  64. String device_brand, String device_os, String script_location, String report_location, String log_location) {
  65. Report report=reportDao.findByWoker(case_take_id,woker_id);
  66. if(report==null) {
  67. Report report2 = new Report(case_id, task_id, case_take_id, woker_id, name, Long.toString(System.currentTimeMillis()),
  68. device_model, device_brand, device_os, script_location, report_location, log_location);
  69. return reportDao.save(report2);
  70. }
  71. else{
  72. return report.getId();
  73. }
  74. }
  75. public boolean updateReport(String report_id, String case_id, String task_id, String case_take_id, String woker_id, String name, String device_model,
  76. String device_brand, String device_os, String script_location, String report_location, String log_location) {
  77. Report report = new Report(case_id, task_id, case_take_id, woker_id, name, Long.toString(System.currentTimeMillis()),
  78. device_model, device_brand, device_os, script_location, report_location, log_location);
  79. return reportDao.update(report_id, report);
  80. }
  81. public Report getReport(String report_id) {
  82. return reportDao.findById(report_id);
  83. }
  84. public Report findByWorker(String case_take_id, String worker_id) {
  85. return reportDao.findByWoker(case_take_id, worker_id);
  86. }
  87. public List<Bug> getBugList(String report_id, String case_take_id) {
  88. return bugDao.findByReport(report_id, case_take_id);
  89. }
  90. //测试题目相关
  91. public String saveExam(String case_id, String path, String app_name, String paper_type, String test_type, String description, String collaborative_type) {
  92. String json = ExcelToJson.ExcelTranse(path).toString();
  93. Exam exam = new Exam(case_id, json, app_name, paper_type, test_type, description, collaborative_type);
  94. examDao.save(exam);
  95. return json;
  96. }
  97. // public String saveExam1(String case_id, String path, String app_name, String paper_type, String test_type, String description,
  98. // String if_test_case,String if_bug) {
  99. // String json = ExcelToJson.ExcelTranse(path).toString();
  100. //// Exam exam = new Exam(case_id, json, app_name, paper_type, test_type, description);
  101. // Exam exam = new Exam(case_id, json, app_name, paper_type, test_type, description,if_test_case,if_bug);
  102. // examDao.save(exam);
  103. // return json;
  104. // }
  105. public JSONArray getExamList() {
  106. List<Exam> result = examDao.findAll();
  107. JSONArray array = new JSONArray();
  108. // Set<String> ids = bugDao.findAllids();
  109. Map<String, Exam> exams = new HashMap<>();
  110. for(Exam exam: result) {
  111. JSONObject object = new JSONObject();
  112. exams.put(exam.getId(), exam);
  113. object.put("name", exam.getName());
  114. object.put("description", exam.getDescription());
  115. object.put("testType", exam.getTest_type());
  116. object.put("case_id", exam.getId());
  117. object.put("task_id", exam.getId()); //这里是私有云特有的,一次case对应一次task
  118. array.put(object);
  119. }
  120. // for(Exam exam: result) { exams.put(exam.getId(), exam); }
  121. // for(String id: ids) {
  122. // if(id == null || id.length() <= 0) { continue; }
  123. // String[] strs = id.split("-");
  124. // String case_id = strs[0];
  125. // String task_id = strs[1];
  126. // if(exams.containsKey(case_id)) {
  127. // JSONObject object = new JSONObject();
  128. // Exam exam = exams.get(case_id);
  129. // object.put("name", exam.getName());
  130. // object.put("description", exam.getDescription());
  131. // object.put("testType", exam.getTest_type());
  132. // object.put("case_id", case_id);
  133. // object.put("task_id", task_id);
  134. // array.put(object);
  135. // }
  136. // }
  137. return array;
  138. }
  139. public Exam getExam(String id) {
  140. return examDao.findById(id);
  141. }
  142. public Task saveTask(String id){
  143. Task task=taskDao.findById(id);
  144. if(task!=null) {
  145. long oldTime=task.getEnd_time();
  146. //考试时间已过
  147. if(oldTime>System.currentTimeMillis()){
  148. return getAndSaveTaskInfo(id);
  149. }else {
  150. return task;
  151. }
  152. }else{
  153. return getAndSaveTaskInfo(id);
  154. }
  155. }
  156. private Task getAndSaveTaskInfo(String id){
  157. String result = HTTP.sendGet(mainSiteUrl + "/api/exam/" + id + "/info", "");
  158. System.out.println("result " + result);
  159. if (!result.equals("")) {
  160. JSONObject json = new JSONObject(result);
  161. long beginTime = json.getLong("beginTime");
  162. long endTime = json.getLong("endTime");
  163. String name=json.getString("name");
  164. double totalMins = (endTime - beginTime) / 1000 / 60.0;
  165. Task newTask = new Task(id, name,beginTime, endTime, totalMins, totalMins);
  166. taskDao.save(newTask);
  167. return newTask;
  168. }else{
  169. return null;
  170. }
  171. }
  172. public boolean updateTask(String id,double writeMins){
  173. try {
  174. Task task =taskDao.findById(id);
  175. task.setWrite_time(writeMins);
  176. taskDao.save(task);
  177. return true;
  178. }catch (Exception e){
  179. return false;
  180. }
  181. }
  182. public String reportsToReviewPaper(String case_take_id){
  183. int[]options={0,1,2,3,4,5,6,7,8,9,10};
  184. String url="http://mooctest-site.oss-cn-shanghai.aliyuncs.com/paperjson/"+case_take_id+".json";
  185. try {
  186. boolean isExist=OssAliyun.existFile("paperjson/"+case_take_id+".json");
  187. if(isExist){
  188. return url;
  189. }
  190. JSONObject result=new JSONObject();
  191. List<Bug>bugs=bugDao.findByCaseid(case_take_id);
  192. JSONArray reportArray=new JSONArray();
  193. int[]indexArray=new int[bugs.size()];
  194. for(int i=0;i<bugs.size();i++) {
  195. Bug bug=bugs.get(i);
  196. BugMirror mirror=mirrorDao.findById(bug.getId());
  197. JSONObject jsonObject = new JSONObject();
  198. jsonObject.put("index",i);
  199. indexArray[i]=i;
  200. jsonObject.put("name",bug.getTitle());
  201. jsonObject.put("original_id",bug.getId());
  202. String description="";
  203. description+=bug.getDescription()+"\n";
  204. description+="三级页面:"+bug.getBug_page()+"\n";
  205. description+=bug.getBug_category()+"\n";
  206. BugSeverity bugSeverity=BugSeverity.getValue(bug.getSeverity());
  207. description+="严重程度:"+bugSeverity+"\n";
  208. BugRecurrent bugRecurrent=BugRecurrent.getValue(bug.getRecurrent());
  209. description+="复现程度:"+bugRecurrent+"\n";
  210. SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  211. String dateStr = dateformat.format(TimeUtil.StringToDate(bug.getCreate_time_millis()));
  212. description+=dateStr+"\n";
  213. if(mirror==null){
  214. description+="0\n";
  215. description+="0\n";
  216. }
  217. else {
  218. description += "点赞数: "+mirror.getGood().size() + "\n";
  219. description += "点踩数: "+mirror.getBad().size();
  220. }
  221. jsonObject.put("description",description);
  222. String[]urlArray=new String[1];
  223. urlArray[0]=bug.getImg_url();
  224. jsonObject.put("img_url",urlArray);
  225. JSONArray fileArray=new JSONArray();
  226. jsonObject.put("file_url",fileArray);
  227. reportArray.put(jsonObject);
  228. }
  229. result.put("report_list",reportArray);
  230. result.put("description","");
  231. result.put("name","");
  232. result.put("create_time",String.valueOf(System.currentTimeMillis()));
  233. result.put("type","众包测试");
  234. result.put("application_url","");
  235. result.put("requirement_url","");
  236. JSONArray itemGroupArray=new JSONArray();
  237. JSONObject itemGroupObject=new JSONObject();
  238. JSONArray itemArray=new JSONArray();
  239. JSONObject itemObject=new JSONObject();
  240. itemObject.put("index",0);
  241. itemObject.put("description","请评分");
  242. itemObject.put("is_required",true);
  243. itemObject.put("type","Single");
  244. itemObject.put("options",options);
  245. itemArray.put(itemObject);
  246. itemGroupObject.put("report_list",indexArray);
  247. itemGroupObject.put("item_list",itemArray);
  248. itemGroupArray.put(itemGroupObject);
  249. result.put("item_group_list",itemGroupArray);
  250. String fileName=case_take_id+".json";
  251. File file=new File(fileName);
  252. Writer write = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
  253. write.write(result.toString());
  254. write.flush();
  255. write.close();
  256. OssAliyun.uploadFile("paperjson/"+case_take_id+".json",file);
  257. return url;
  258. } catch (Exception e) {
  259. e.printStackTrace();
  260. }
  261. return null;
  262. }
  263. public String getPageUrl(String caseId){
  264. return pageUrl+caseId+".json";
  265. }
  266. public PageExamVO findPageAndExam(long examId, long caseId){
  267. PageExamVO pageExamVO=new PageExamVO(examId,caseId);
  268. Exam examCase=getExam(String.valueOf(caseId));
  269. pageExamVO.setPageContent(examCase.getJson());
  270. String json=examCase.getPaper_type();
  271. int caseTypeId=getPaperType(json);
  272. pageExamVO.setCaseTypeId(caseTypeId);
  273. Task task=taskDao.findById(String.valueOf(examId));
  274. Date startTime=null;
  275. Date endTime=null;
  276. if(task!=null){
  277. startTime=new Date(task.getStart_time());
  278. endTime=new Date(task.getEnd_time());
  279. }
  280. ExamVO examVO=new ExamVO(examId,caseId,0,caseTypeId,startTime,endTime);
  281. pageExamVO.setExamVO(examVO);
  282. return pageExamVO;
  283. }
  284. private int getPaperType(String json){
  285. //json=json.substring(1,json.length()-1);
  286. JSONObject jsonObject = new JSONObject(json);
  287. JSONArray jsonArray=jsonObject.getJSONArray("subTitles");
  288. int caseTypeId=1;//这里只对WEB和APP进行了判断
  289. for(int i=0;i<jsonArray.length();i++) {
  290. JSONObject item = jsonArray.getJSONObject(i);
  291. String label=item.getString("name");
  292. if(label.equals("操作系统")){
  293. JSONArray brandList=item.getJSONArray("value");
  294. String brand=String.valueOf(brandList.get(0));
  295. if(EnumUtils.isValidEnum(WebBrand.class, brand)){
  296. caseTypeId=0;
  297. }
  298. break;
  299. }
  300. }
  301. return caseTypeId;
  302. }
  303. }