MyConsumer.java 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. package cn.com.qmth.export;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.io.UnsupportedEncodingException;
  5. import java.net.URLDecoder;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.util.ArrayList;
  12. import java.util.HashMap;
  13. import java.util.HashSet;
  14. import java.util.List;
  15. import java.util.Map;
  16. import java.util.Set;
  17. import java.util.regex.Matcher;
  18. import java.util.regex.Pattern;
  19. import org.apache.commons.collections4.CollectionUtils;
  20. import org.apache.commons.io.FileUtils;
  21. import org.apache.commons.lang3.StringUtils;
  22. import org.jsoup.Jsoup;
  23. import org.jsoup.nodes.Document;
  24. import org.jsoup.nodes.Element;
  25. import org.jsoup.nodes.Node;
  26. import com.alibaba.fastjson.JSONObject;
  27. public class MyConsumer extends Consumer<String> {
  28. private String paperSuff = "(211)";
  29. private int maxqc = 100;
  30. private String excelDir = "D:\\kd_export\\";
  31. private String picFiles = "D:\\files\\";
  32. private Pattern mp3Pat = Pattern.compile("<a[^<]+href=\"([^<\"]+\\.mp3)\"[^<]*>[^<]*</a>");
  33. private Pattern imgPat = Pattern.compile("<img[^<]+src=\"([^<\"]+)\"[^<]*>");
  34. private Pattern notmp3Pat = Pattern.compile("<a[^<]+href=\"[^<\"]+(?!\\.mp3)\"[^<]*>([^<]*)</a>");
  35. private List<String> qdb = new ArrayList<>();
  36. private List<String> qzip = new ArrayList<>();
  37. private List<String> adb = new ArrayList<>();
  38. private List<String> azip = new ArrayList<>();
  39. private List<String> http = new ArrayList<>();
  40. private List<String> file = new ArrayList<>();
  41. private Set<String> noques = new HashSet<>();
  42. private Set<Long> invalidAnswerCode = new HashSet<>();
  43. @Override
  44. public void consume(String code) {
  45. Connection connect = null;
  46. try {
  47. Class.forName("com.mysql.cj.jdbc.Driver");
  48. String url = "jdbc:mysql://localhost:3306/moodle_question?serverTimezone=GMT%2B8";
  49. String user = "root";
  50. String password = "123456";
  51. connect = DriverManager.getConnection(url, user, password);
  52. exportPaper(connect, code);
  53. ExportByCourseCode.addDisposeCount();
  54. } catch (Exception e) {
  55. throw new RuntimeException(e);
  56. } finally {
  57. if (connect != null) {
  58. try {
  59. connect.close();
  60. } catch (SQLException e) {
  61. }
  62. }
  63. }
  64. }
  65. private Course getCourse(Connection connect, String code) throws SQLException, IOException {
  66. Course c = new Course();
  67. PreparedStatement preState = null;
  68. ResultSet resultSet = null;
  69. try {
  70. String sql = "select * from mdl_course where idnumber like '" + code + "_%';";
  71. preState = connect.prepareStatement(sql);
  72. resultSet = preState.executeQuery();
  73. int count = 0;
  74. while (resultSet.next()) {
  75. count++;
  76. if (count > 1) {
  77. return null;
  78. }
  79. c.setId(resultSet.getLong("id"));
  80. c.setName(resultSet.getString("shortname"));
  81. c.setIdnumber(resultSet.getString("idnumber"));
  82. c.setCode(code);
  83. }
  84. return c;
  85. } finally {
  86. if (resultSet != null) {
  87. resultSet.close();
  88. }
  89. if (preState != null) {
  90. preState.close();
  91. }
  92. }
  93. }
  94. private List<Long> getContextIds(Connection connect, Long courseId) throws SQLException, IOException {
  95. List<Long> ids = new ArrayList<>();
  96. PreparedStatement preState = null;
  97. ResultSet resultSet = null;
  98. try {
  99. String sql = "select * from mdl_context where instanceid =" + courseId
  100. + " and contextlevel=50 order by id ";
  101. preState = connect.prepareStatement(sql);
  102. resultSet = preState.executeQuery();
  103. while (resultSet.next()) {
  104. ids.add(resultSet.getLong("id"));
  105. }
  106. return ids;
  107. } finally {
  108. if (resultSet != null) {
  109. resultSet.close();
  110. }
  111. if (preState != null) {
  112. preState.close();
  113. }
  114. }
  115. }
  116. private List<Long> getQuestionCategorie(Connection connect, List<Long> cids) throws SQLException, IOException {
  117. List<Long> ids = new ArrayList<>();
  118. PreparedStatement preState = null;
  119. ResultSet resultSet = null;
  120. try {
  121. String sql = "select * from mdl_question_categories where contextid in (" + getInStr(cids)
  122. + ") order by id ";
  123. preState = connect.prepareStatement(sql);
  124. resultSet = preState.executeQuery();
  125. while (resultSet.next()) {
  126. ids.add(resultSet.getLong("id"));
  127. }
  128. return ids;
  129. } finally {
  130. if (resultSet != null) {
  131. resultSet.close();
  132. }
  133. if (preState != null) {
  134. preState.close();
  135. }
  136. }
  137. }
  138. private List<KdQuestion> getQuestion(Connection connect, List<Long> qcids) throws SQLException, IOException {
  139. List<KdQuestion> qs = new ArrayList<>();
  140. PreparedStatement preState = null;
  141. ResultSet resultSet = null;
  142. try {
  143. String sql = "SELECT f.* FROM mdl_question f WHERE category IN (" + getInStr(qcids)
  144. + ") AND f.qtype IN ('multichoice','truefalse') ";
  145. preState = connect.prepareStatement(sql);
  146. resultSet = preState.executeQuery();
  147. while (resultSet.next()) {
  148. KdQuestion q = new KdQuestion();
  149. q.setId(resultSet.getLong("id"));
  150. q.setBody(disBody(resultSet.getString("questiontext")));
  151. q.setQtype(resultSet.getString("qtype"));
  152. q.setObjective(true);
  153. qs.add(q);
  154. }
  155. return qs;
  156. } finally {
  157. if (resultSet != null) {
  158. resultSet.close();
  159. }
  160. if (preState != null) {
  161. preState.close();
  162. }
  163. }
  164. }
  165. private List<Answer> getAnswer(Connection connect, KdQuestion q) throws SQLException, IOException {
  166. List<Answer> as = new ArrayList<>();
  167. PreparedStatement preState = null;
  168. ResultSet resultSet = null;
  169. try {
  170. String sql = "select * from mdl_question_answers where question =" + q.getId();
  171. preState = connect.prepareStatement(sql);
  172. resultSet = preState.executeQuery();
  173. while (resultSet.next()) {
  174. Answer a = new Answer();
  175. a.setId(resultSet.getLong("id"));
  176. a.setBody(resultSet.getString("answer"));
  177. a.setScore(resultSet.getDouble("fraction"));
  178. as.add(a);
  179. }
  180. return as;
  181. } finally {
  182. if (resultSet != null) {
  183. resultSet.close();
  184. }
  185. if (preState != null) {
  186. preState.close();
  187. }
  188. }
  189. }
  190. private void exportPaper(Connection connect, String code) throws Exception {
  191. Course c = getCourse(connect, code);
  192. if (c == null) {
  193. noques.add(code);
  194. return;
  195. }
  196. List<Long> ctids = getContextIds(connect, c.getId());
  197. if (ctids.size() == 0) {
  198. noques.add(code);
  199. return;
  200. }
  201. List<Long> qcids = getQuestionCategorie(connect, ctids);
  202. if (qcids.size() == 0) {
  203. noques.add(code);
  204. return;
  205. }
  206. List<KdQuestion> qs = getQuestion(connect, qcids);
  207. if (qs.size() == 0) {
  208. noques.add(code);
  209. return;
  210. }
  211. for (KdQuestion q : qs) {
  212. List<Answer> as = getAnswer(connect, q);
  213. disposeAnswer(q, as);
  214. }
  215. qs = removeInvalidQuestion(qs, code);
  216. if (qs.size() == 0) {
  217. noques.add(code);
  218. return;
  219. }
  220. File courseCode = new File(excelDir + c.getCode() + "\\");
  221. courseCode.mkdirs();
  222. File att = new File(excelDir + c.getCode() + "\\att\\");
  223. att.mkdirs();
  224. for (KdQuestion q : qs) {
  225. try {
  226. disposeMedia(connect, c.getIdnumber(), q, att);
  227. q.setValid(true);
  228. } catch (MediaNotFoundException e) {
  229. q.setValid(false);
  230. }
  231. }
  232. List<KdQuestion> tem = new ArrayList<>();
  233. for (KdQuestion q : qs) {
  234. if (q.getValid()) {
  235. tem.add(q);
  236. }
  237. }
  238. qs = tem;
  239. if (qs.size() == 0) {
  240. noques.add(code);
  241. return;
  242. }
  243. List<KdQuestion> single = new ArrayList<>();
  244. List<KdQuestion> muti = new ArrayList<>();
  245. List<KdQuestion> boo = new ArrayList<>();
  246. for (KdQuestion q : qs) {
  247. if (q.getStructType() == 1) {
  248. single.add(q);
  249. } else if (q.getStructType() == 2) {
  250. muti.add(q);
  251. } else if (q.getStructType() == 3) {
  252. boo.add(q);
  253. }
  254. }
  255. createPapers(single, c, 1);
  256. createPapers(muti, c, 2);
  257. createPapers(boo, c, 3);
  258. // int detailIndx = 0;
  259. // List<KdDetail> des = new ArrayList<>();
  260. // if (single.size() > 0) {
  261. // detailIndx++;
  262. // KdDetail d = new KdDetail();
  263. // d.setName("单选题");
  264. // d.setNumber(detailIndx);
  265. // d.setQuestionCount(single.size());
  266. // d.setTotalScore((double) single.size());
  267. // d.setQuestions(single);
  268. // des.add(d);
  269. // }
  270. // if (muti.size() > 0) {
  271. // detailIndx++;
  272. // KdDetail d = new KdDetail();
  273. // d.setName("多选题");
  274. // d.setNumber(detailIndx);
  275. // d.setQuestionCount(muti.size());
  276. // d.setTotalScore((double) muti.size());
  277. // d.setQuestions(muti);
  278. // des.add(d);
  279. // }
  280. // if (boo.size() > 0) {
  281. // detailIndx++;
  282. // KdDetail d = new KdDetail();
  283. // d.setName("判断题");
  284. // d.setNumber(detailIndx);
  285. // d.setQuestionCount(boo.size());
  286. // d.setTotalScore((double) boo.size());
  287. // d.setQuestions(boo);
  288. // des.add(d);
  289. // }
  290. // paper.setDetails(des);
  291. // paper.setDetailCount(detailIndx);
  292. //
  293. // FileUtil.writeFile(courseCode.getAbsolutePath(), "\\paper.json", JSONObject.toJSONString(paper));
  294. }
  295. private void createPapers(List<KdQuestion> qs, Course c, int structType) throws IOException {
  296. if (qs == null || qs.size() == 0) {
  297. return;
  298. }
  299. if (qs.size() <= maxqc) {
  300. createPaper(qs, c, structType, 1);
  301. } else {
  302. int size = qs.size();
  303. int len = maxqc;
  304. int count = (size + len - 1) / len;
  305. for (int i = 0; i < count; i++) {
  306. List<KdQuestion> subList = qs.subList(i * len, ((i + 1) * len > size ? size : len * (i + 1)));
  307. createPaper(subList, c, structType, i + 1);
  308. }
  309. }
  310. }
  311. private void createPaper(List<KdQuestion> qs, Course c, int structType, int indx) throws IOException {
  312. if (qs.size() == 0) {
  313. return;
  314. }
  315. String detailName="";
  316. if (structType == 1) {
  317. detailName="单选题";
  318. } else if (structType == 2) {
  319. detailName="多选题";
  320. } else if (structType == 3) {
  321. detailName="判断题";
  322. }
  323. KdPaper paper = new KdPaper();
  324. paper.setTotalScore((double) qs.size());
  325. paper.setName(c.getName()+paperSuff+detailName+"_"+indx);
  326. paper.setCourseCode(c.getCode());
  327. List<KdDetail> des = new ArrayList<>();
  328. KdDetail d = new KdDetail();
  329. d.setName(detailName);
  330. d.setNumber(1);
  331. d.setQuestionCount(qs.size());
  332. d.setTotalScore((double) qs.size());
  333. d.setQuestions(qs);
  334. des.add(d);
  335. paper.setDetails(des);
  336. paper.setDetailCount(1);
  337. File paperdir = new File(excelDir + c.getCode() + "\\paper"+structType+"_"+indx+"\\");
  338. paperdir.mkdirs();
  339. FileUtil.writeFile(paperdir.getAbsolutePath(), "\\paper.json", JSONObject.toJSONString(paper));
  340. }
  341. private List<KdQuestion> removeInvalidQuestion(List<KdQuestion> qs, String courseCode) {
  342. List<KdQuestion> ret = new ArrayList<>();
  343. for (KdQuestion q : qs) {
  344. if (StringUtils.isBlank(q.getAnswer())) {
  345. invalidAnswerCode.add(q.getId());
  346. continue;
  347. }
  348. if (isEmpty(q.getBody())) {
  349. invalidAnswerCode.add(q.getId());
  350. continue;
  351. }
  352. if (q.getStructType() == 1 || q.getStructType() == 2) {
  353. if (!checkQuestionAndRemoveInvalidOption(q)) {
  354. invalidAnswerCode.add(q.getId());
  355. continue;
  356. }
  357. }
  358. ret.add(q);
  359. }
  360. return ret;
  361. }
  362. private boolean checkQuestionAndRemoveInvalidOption(KdQuestion q) {
  363. List<KdQuesOption> ret = new ArrayList<>();
  364. for (KdQuesOption op : q.getOptions()) {
  365. if (isEmpty(op.getBody())) {
  366. if (op.getSelect()) {
  367. return false;
  368. }
  369. } else {
  370. ret.add(op);
  371. }
  372. }
  373. if (ret.size() < 2) {
  374. return false;
  375. } else {
  376. q.setOptions(ret);
  377. }
  378. return true;
  379. }
  380. private void disposeAnswer(KdQuestion q, List<Answer> as) {
  381. if (CollectionUtils.isEmpty(as)) {
  382. return;
  383. }
  384. if ("truefalse".equals(q.getQtype())) {
  385. q.setStructType(3);
  386. for (Answer a : as) {
  387. if (a.getScore() > 0) {
  388. q.setAnswer("对".equals(a.getBody().trim()) ? "true" : "false");
  389. return;
  390. }
  391. }
  392. } else {
  393. int index = 0;
  394. List<KdQuesOption> ops = new ArrayList<>();
  395. StringBuilder sb = new StringBuilder();
  396. for (Answer a : as) {
  397. index++;
  398. KdQuesOption op = new KdQuesOption();
  399. op.setAnswerId(a.getId());
  400. op.setNumber(index);
  401. op.setBody(disBody(a.getBody()));
  402. if (a.getScore() > 0) {
  403. op.setSelect(true);
  404. sb.append(index).append(",");
  405. } else {
  406. op.setSelect(false);
  407. }
  408. ops.add(op);
  409. }
  410. q.setOptions(ops);
  411. if (sb.length() > 0) {
  412. sb.deleteCharAt(sb.length() - 1);
  413. }
  414. if (sb.indexOf(",") > 0) {
  415. q.setStructType(2);
  416. } else {
  417. q.setStructType(1);
  418. }
  419. q.setAnswer(sb.toString());
  420. }
  421. }
  422. private String disBody(String html) {
  423. Document doc = Jsoup.parse(html);
  424. Element b = doc.body();
  425. if (b.childrenSize() == 1 && "p".equals(b.child(0).nodeName())) {
  426. b.child(0).tagName("span");
  427. }
  428. if (b.childrenSize() > 0) {
  429. for (Element n : b.children()) {
  430. if ("p".equals(n.nodeName())) {
  431. boolean[] find = new boolean[] { false };
  432. findImg(n, find);
  433. if (!find[0] && StringUtils.isBlank(n.text())) {
  434. n.tagName("span");
  435. }
  436. }
  437. disNode(n);
  438. }
  439. }
  440. return b.html();
  441. }
  442. private void findImg(Element e, boolean[] find) {
  443. if (find[0]) {
  444. return;
  445. } else {
  446. if ("img".equals(e.nodeName())) {
  447. find[0] = true;
  448. return;
  449. } else {
  450. if (e.childrenSize() > 0) {
  451. for (Element ce : e.children()) {
  452. findImg(ce, find);
  453. }
  454. } else {
  455. return;
  456. }
  457. }
  458. }
  459. }
  460. private static void disNode(Node n) {
  461. if (n instanceof Element) {
  462. if (!"img".equals(n.nodeName())) {
  463. n.removeAttr("style");
  464. }
  465. if ("h1".equals(n.nodeName()) || "h2".equals(n.nodeName()) || "h3".equals(n.nodeName())
  466. || "h4".equals(n.nodeName()) || "h5".equals(n.nodeName())) {
  467. ((Element) n).tagName("h6");
  468. }
  469. if ("strong".equals(n.nodeName()) || "a".equals(n.nodeName()) || "u".equals(n.nodeName())
  470. || "em".equals(n.nodeName())) {
  471. ((Element) n).tagName("span");
  472. }
  473. if (n.childNodeSize() > 0) {
  474. for (Node cn : n.childNodes()) {
  475. disNode(cn);
  476. }
  477. }
  478. }
  479. }
  480. private String getInStr(List<Long> cids) {
  481. StringBuilder sb = new StringBuilder();
  482. for (Long id : cids) {
  483. sb.append(id).append(",");
  484. }
  485. sb.deleteCharAt(sb.length() - 1);
  486. return sb.toString();
  487. }
  488. // private Map<String, String> findAtag(String body) {
  489. // Map<String, String> set = new HashMap<>();
  490. // if (StringUtils.isBlank(body)) {
  491. // return set;
  492. // }
  493. // Matcher m = notmp3Pat.matcher(body);
  494. // while (m.find()) {
  495. // String f = m.group(1);
  496. // set.put(m.group(), f);
  497. // }
  498. // return set;
  499. // }
  500. //
  501. // private void removeATag(KdQuestion q) throws Exception {
  502. // String body = q.getBody();
  503. //
  504. // Map<String, String> as = findAtag(body);
  505. // if (as.size() > 0) {
  506. // for (String k : as.keySet()) {
  507. // String val = as.get(k);
  508. // body = body.replaceAll(k, val);
  509. // }
  510. // q.setBody(body);
  511. // }
  512. //
  513. // if (CollectionUtils.isNotEmpty(q.getOptions())) {
  514. // for (KdQuesOption o : q.getOptions()) {
  515. // String obody = o.getBody();
  516. //
  517. // Map<String, String> oas = findAtag(obody);
  518. // if (oas.size() > 0) {
  519. // for (String k : oas.keySet()) {
  520. // String val = oas.get(k);
  521. // obody = obody.replaceAll(k, val);
  522. // }
  523. // o.setBody(obody);
  524. // }
  525. // }
  526. // }
  527. // }
  528. private void disposeMedia(Connection connect, String courseidnumber, KdQuestion q, File att) throws Exception {
  529. String body = q.getBody();
  530. Map<String, String> imgs = findAllImg(body, q.getId(), null, courseidnumber);
  531. if (imgs.size() > 0) {
  532. for (String k : imgs.keySet()) {
  533. String img = imgs.get(k);
  534. String fileName = getFileName(img);
  535. File file = getQuestionFile(connect, q.getId(), courseidnumber, fileName);
  536. if (file == null) {
  537. ExportByCourseCode.addNotFd();
  538. // body = body.replaceAll(k, "[未找到图片文件]");
  539. throw new MediaNotFoundException();
  540. } else {
  541. ExportByCourseCode.addFd();
  542. body = body.replaceAll(img.replaceAll("\\?", "\\\\?"), FileUtil.fileToBase64Src(file));
  543. }
  544. }
  545. q.setBody(body);
  546. }
  547. Map<String, String> audios = findAllAudio(body, q.getId(), null, courseidnumber);
  548. if (audios.size() > 0) {
  549. q.setHaveAudio(true);
  550. for (String k : audios.keySet()) {
  551. String val = audios.get(k);
  552. String fileName = getFileName(val);
  553. File file = getQuestionFile(connect, q.getId(), courseidnumber, fileName);
  554. if (file == null) {
  555. ExportByCourseCode.addNotFd();
  556. // body = body.replaceAll(k, "[未找到音频文件]");
  557. throw new MediaNotFoundException();
  558. } else {
  559. ExportByCourseCode.addFd();
  560. File newAudio = new File(att.getAbsoluteFile() + "\\" + file.getName());
  561. newAudio.createNewFile();
  562. FileUtils.copyFile(file, newAudio);
  563. body = body.replaceAll(k.replaceAll("\\?", "\\\\?"), "<a id=\"" + file.getName() + "\" name=\"" + file.getName() + "\"></a>");
  564. }
  565. }
  566. q.setBody(body);
  567. }
  568. if (CollectionUtils.isNotEmpty(q.getOptions())) {
  569. for (KdQuesOption o : q.getOptions()) {
  570. String obody = o.getBody();
  571. Map<String, String> oimgs = findAllImg(obody, null, o.getAnswerId(), courseidnumber);
  572. if (oimgs.size() > 0) {
  573. for (String k : oimgs.keySet()) {
  574. String img = oimgs.get(k);
  575. String fileName = getFileName(img);
  576. File file = getAnswerFile(connect, o.getAnswerId(), courseidnumber, fileName);
  577. if (file == null) {
  578. ExportByCourseCode.addNotFd();
  579. // obody = obody.replaceAll(k, "[未找到图片文件]");
  580. throw new MediaNotFoundException();
  581. } else {
  582. ExportByCourseCode.addFd();
  583. obody = obody.replaceAll(img.replaceAll("\\?", "\\\\?"), FileUtil.fileToBase64Src(file));
  584. }
  585. }
  586. o.setBody(obody);
  587. }
  588. Map<String, String> oaudios = findAllAudio(obody, null, o.getAnswerId(), courseidnumber);
  589. if (oaudios.size() > 0) {
  590. q.setHaveAudio(true);
  591. for (String k : oaudios.keySet()) {
  592. String val = oaudios.get(k);
  593. String fileName = getFileName(val);
  594. File file = getAnswerFile(connect, o.getAnswerId(), courseidnumber, fileName);
  595. if (file == null) {
  596. ExportByCourseCode.addNotFd();
  597. // obody = obody.replaceAll(k, "[未找到音频文件]");
  598. throw new MediaNotFoundException();
  599. } else {
  600. ExportByCourseCode.addFd();
  601. File newAudio = new File(att.getAbsoluteFile() + "\\" + file.getName());
  602. newAudio.createNewFile();
  603. FileUtils.copyFile(file, newAudio);
  604. obody = obody.replaceAll(k.replaceAll("\\?", "\\\\?"),
  605. "<a id=\"" + file.getName() + "\" name=\"" + file.getName() + "\"></a>");
  606. }
  607. }
  608. o.setBody(obody);
  609. }
  610. }
  611. }
  612. }
  613. private Map<String, String> findAllImg(String body, Long qid, Long aid, String courseidnumber) {
  614. Map<String, String> set = new HashMap<>();
  615. Matcher m = imgPat.matcher(body);
  616. while (m.find()) {
  617. String f = m.group(1);
  618. if (f.startsWith("file:") || f.startsWith("FILE:")) {
  619. if (qid != null)
  620. file.add("questionid:" + qid + " " + courseidnumber + " " + f);
  621. if (aid != null)
  622. file.add("answerId:" + aid + " " + courseidnumber + " " + f);
  623. continue;
  624. }
  625. if (f.startsWith("http:") || f.startsWith("HTTP:")) {
  626. if (qid != null)
  627. http.add("questionid:" + qid + " " + courseidnumber + " " + f);
  628. if (aid != null)
  629. http.add("answerId:" + aid + " " + courseidnumber + " " + f);
  630. continue;
  631. }
  632. if (!f.startsWith("data:image")) {
  633. set.put(m.group(), f);
  634. }
  635. }
  636. return set;
  637. }
  638. private Map<String, String> findAllAudio(String body, Long qid, Long aid, String courseidnumber) {
  639. Map<String, String> set = new HashMap<>();
  640. Matcher m = mp3Pat.matcher(body);
  641. while (m.find()) {
  642. String f = m.group(1);
  643. if (f.startsWith("file:") || f.startsWith("FILE:")) {
  644. if (qid != null)
  645. file.add("questionid:" + qid + " " + courseidnumber + " " + f);
  646. if (aid != null)
  647. file.add("answerId:" + aid + " " + courseidnumber + " " + f);
  648. continue;
  649. }
  650. if (f.startsWith("http:") || f.startsWith("HTTP:")) {
  651. if (qid != null)
  652. http.add("questionid:" + qid + " " + courseidnumber + " " + f);
  653. if (aid != null)
  654. http.add("answerId:" + aid + " " + courseidnumber + " " + f);
  655. continue;
  656. }
  657. set.put(m.group(), f);
  658. }
  659. return set;
  660. }
  661. private boolean hasImg(String body) {
  662. Matcher m = imgPat.matcher(body);
  663. while (m.find()) {
  664. return true;
  665. }
  666. return false;
  667. }
  668. private boolean hasAudio(String body) {
  669. Matcher m = mp3Pat.matcher(body);
  670. while (m.find()) {
  671. return true;
  672. }
  673. return false;
  674. }
  675. private boolean isEmpty(String html) {
  676. if (StringUtils.isBlank(html)) {
  677. return true;
  678. }
  679. if (hasImg(html) || hasAudio(html)) {
  680. return false;
  681. }
  682. Document doc = Jsoup.parse(html);
  683. Element b = doc.body();
  684. if (StringUtils.isBlank(b.wholeText())) {
  685. return true;
  686. } else {
  687. return false;
  688. }
  689. }
  690. private String getFileName(String s) throws UnsupportedEncodingException {
  691. int end = s.length();
  692. int f = s.indexOf("?");
  693. if (f != -1) {
  694. end = f;
  695. }
  696. return URLDecoder.decode(s.substring(s.lastIndexOf("/") + 1, end), "utf-8");
  697. }
  698. private File getQuestionFile(Connection connect, Long quesId, String courseidnumber, String fileName)
  699. throws Exception {
  700. PreparedStatement preState = null;
  701. ResultSet resultSet = null;
  702. try {
  703. String hash = null;
  704. String sql = " select * from mdl_files where itemid = " + quesId + " and source='" + fileName
  705. + "' and component = 'question' and filearea = 'questiontext' ";
  706. preState = connect.prepareStatement(sql);
  707. resultSet = preState.executeQuery();
  708. while (resultSet.next()) {
  709. hash = resultSet.getString("contenthash");
  710. break;
  711. }
  712. if (hash == null) {
  713. qdb.add(quesId + " " + courseidnumber + " " + fileName);
  714. return null;
  715. }
  716. String suff = fileName.substring(fileName.lastIndexOf("."));
  717. File file = new File(picFiles + courseidnumber + "\\" + hash + suff);
  718. if (!file.exists()) {
  719. qzip.add(quesId + " " + courseidnumber + " " + fileName);
  720. return null;
  721. }
  722. return file;
  723. } finally {
  724. if (resultSet != null) {
  725. resultSet.close();
  726. }
  727. if (preState != null) {
  728. preState.close();
  729. }
  730. }
  731. }
  732. private File getAnswerFile(Connection connect, Long answerId, String courseidnumber, String fileName)
  733. throws Exception {
  734. PreparedStatement preState = null;
  735. ResultSet resultSet = null;
  736. try {
  737. String hash = null;
  738. String sql = " select * from mdl_files where itemid = " + answerId + " and source='" + fileName
  739. + "' and component = 'question' and filearea = 'answer' ";
  740. preState = connect.prepareStatement(sql);
  741. resultSet = preState.executeQuery();
  742. while (resultSet.next()) {
  743. hash = resultSet.getString("contenthash");
  744. break;
  745. }
  746. if (hash == null) {
  747. adb.add(answerId + " " + courseidnumber + " " + fileName);
  748. return null;
  749. }
  750. String suff = fileName.substring(fileName.lastIndexOf("."));
  751. File file = new File(picFiles + courseidnumber + "\\" + hash + suff);
  752. if (!file.exists()) {
  753. azip.add(answerId + " " + courseidnumber + " " + fileName);
  754. return null;
  755. }
  756. return file;
  757. } finally {
  758. if (resultSet != null) {
  759. resultSet.close();
  760. }
  761. if (preState != null) {
  762. preState.close();
  763. }
  764. }
  765. }
  766. @Override
  767. public void initResult() {
  768. setResult(new HashMap<>());
  769. getResult().put("qdb", qdb);
  770. getResult().put("qzip", qzip);
  771. getResult().put("adb", adb);
  772. getResult().put("azip", azip);
  773. getResult().put("invalidAnswerCode", invalidAnswerCode);
  774. getResult().put("file", file);
  775. getResult().put("http", http);
  776. getResult().put("noques", noques);
  777. }
  778. }