docManage.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. package openapi.restcontroller.wechatapp.system;
  2. import com.obs.services.model.ObjectMetadata;
  3. import net.sf.json.JSONObject;
  4. import openapi.base.Controller;
  5. import openapi.base.SQLFactory;
  6. import openapi.base.data.Row;
  7. import openapi.base.data.Rows;
  8. import openapi.base.data.db.DBConnect;
  9. import openapi.base.parameter.ErrCode;
  10. import openapi.base.parameter.ErrModel;
  11. import openapi.base.parameter.parameter;
  12. import openapi.tools.obs.BucketFile;
  13. import org.apache.commons.lang.StringUtils;
  14. import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
  15. import p2.application.doclink.Docinfo;
  16. import p2.application.doclink.Doclinks;
  17. import p2.p2server.P2Server;
  18. import p2.pao.PaoRemote;
  19. import p2.pao.PaoSetRemote;
  20. import p2.util.P2Exception;
  21. import p2.webclient.system.controller.UploadFile;
  22. import p2.webclient.system.controller.UploadFileOSS;
  23. import p2.webclient.system.controller.Utility;
  24. import javax.ws.rs.POST;
  25. import javax.ws.rs.Path;
  26. import java.io.*;
  27. import java.net.HttpURLConnection;
  28. import java.net.URL;
  29. import java.nio.charset.StandardCharsets;
  30. import java.util.ArrayList;
  31. import java.util.List;
  32. import java.util.Properties;
  33. import java.util.Vector;
  34. public class docManage extends Controller {
  35. public docManage() {
  36. }
  37. public docManage(JSONObject content) {
  38. super(content);
  39. }
  40. public String upLoadDoc(InputStream uploadfileInputStream, FormDataContentDisposition uploadfile, String ownertable, String ownerid,
  41. String hrid, String description, String filenamestr) {
  42. // 获取文件名称
  43. JSONObject resultObject = new JSONObject();
  44. if (uploadfile == null) {
  45. resultObject.put("code", 0);
  46. resultObject.put("errcode", 0);
  47. resultObject.put("msg", "文件为空");
  48. return resultObject.toString();
  49. }
  50. String siteid = getSiteid(hrid);
  51. String doctype = "Attachments";
  52. try {
  53. //获取文件名称
  54. String filename = new String(uploadfile.getFileName().getBytes("ISO8859-1"), StandardCharsets.UTF_8);
  55. /**
  56. * 判断参数合法性
  57. */
  58. if (Utility.isNull(ownertable)) {
  59. resultObject.put("code", 0);
  60. resultObject.put("errcode", 0);
  61. resultObject.put("msg", "参数ownertable错误");
  62. return resultObject.toString();
  63. }
  64. if (Utility.isNull(ownerid)) {
  65. resultObject.put("code", 0);
  66. resultObject.put("errcode", 0);
  67. resultObject.put("msg", "参数ownerid错误");
  68. return resultObject.toString();
  69. }
  70. if (Utility.isNull(filename)) {
  71. resultObject.put("code", 0);
  72. resultObject.put("errcode", 0);
  73. resultObject.put("msg", "参数 uploadfile.getFileName()错误");
  74. return resultObject.toString();
  75. }
  76. if (Utility.isNull(siteid)) {
  77. resultObject.put("code", 0);
  78. resultObject.put("errcode", 0);
  79. resultObject.put("msg", "参数siteid错误");
  80. return resultObject.toString();
  81. }
  82. if (Utility.isNull(doctype)) {
  83. resultObject.put("code", 0);
  84. resultObject.put("errcode", 0);
  85. resultObject.put("msg", "参数doctype错误");
  86. return resultObject.toString();
  87. }
  88. /**
  89. * 将上传文件内容进行缓存操作
  90. */
  91. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  92. try {
  93. byte[] arrayOfByte = new byte[4096];
  94. int i;
  95. while (-1 != (i = uploadfileInputStream.read(arrayOfByte))) {
  96. byteArrayOutputStream.write(arrayOfByte, 0, i);
  97. }
  98. } catch (IOException iOException) {
  99. }
  100. /**
  101. * 开始进行文件写入操作
  102. */
  103. UploadFile uploadFile = new UploadFile(filename, "image\\png", byteArrayOutputStream);
  104. //获取p2设置参数
  105. Properties properties = P2Server.getP2Server().getConfig();
  106. //获取附件文件夹路径
  107. StringBuffer defaultpath = new StringBuffer(properties.getProperty("p2.attachment.defaultpath"));
  108. String ostype = properties.getProperty("p2.attachment.ostype");
  109. //附件文件夹下相对路径
  110. String filepath = "";
  111. /**
  112. * 获取附件存放规则:站点?表?附件类型?
  113. */
  114. PaoSetRemote ps_sysvarsSet = P2Server.getP2Server().getPaoSet("PS_SYSVARS", P2Server.getP2Server().getSystemUserInfo());
  115. ps_sysvarsSet.setWhere("varname='DOCUMENT_DIR_RULE'");
  116. ps_sysvarsSet.reset();
  117. if (!ps_sysvarsSet.isEmpty()) {
  118. String[] document_dir_rule = ps_sysvarsSet.getPao(0).getString("varvalue")
  119. .split("[/]");
  120. defaultpath.append(File.separator + siteid);
  121. /**
  122. * 根据文件存放规则生成相对路径和完整路径
  123. */
  124. filepath = filepath + siteid + "/";
  125. for (byte b = 1; b < document_dir_rule.length; b++) {
  126. if ("DOCTYPE".equalsIgnoreCase(document_dir_rule[b])) {
  127. defaultpath.append(File.separator + doctype);
  128. filepath = filepath + doctype + "/";
  129. } else if ("TABLE".equalsIgnoreCase(document_dir_rule[b])) {
  130. defaultpath.append(File.separator + ownertable);
  131. filepath = filepath + ownertable + "/";
  132. }
  133. }
  134. }
  135. ps_sysvarsSet.close();
  136. /**
  137. * 文件类型
  138. */
  139. String filetype = filename.substring(filename.lastIndexOf(".") + 1);
  140. if (!"".equals(filenamestr)) {
  141. filename = filenamestr;
  142. }
  143. try {
  144. /**
  145. * 创建文件夹,如文件夹不存在则自动创建文件夹
  146. */
  147. uploadFile.setDirectoryName(defaultpath.toString());
  148. /**
  149. * 开始写入文件
  150. */
  151. uploadFile.writeToDisk();
  152. /**
  153. * 获取文件完整路径(加上自动生成的文件流水码)
  154. */
  155. String absolutefilename = uploadFile.getAbsoluteFileName();
  156. File file = new File(absolutefilename);
  157. /**
  158. * 如果上传阿里云,则执行如下代码
  159. */
  160. if (ostype != null && !Utility.isNull(ostype.trim()) && ostype.trim().equalsIgnoreCase("OSS")) {
  161. UploadFileOSS uploadFileOSS = new UploadFileOSS(absolutefilename, uploadFile.getFileName(), "", filepath);
  162. uploadFileOSS.writeOSS();
  163. if (file.exists() && file.isFile()) {
  164. file.delete();
  165. }
  166. }
  167. /**
  168. * 保存文件
  169. */
  170. if (!Utility.isNull(absolutefilename)) {
  171. uploadFile.save();
  172. }
  173. } catch (IOException iOException) {
  174. }
  175. PaoSetRemote docinfoSet = P2Server.getP2Server().getPaoSet("DOCINFO", P2Server.getP2Server().getSystemUserInfo());
  176. PaoRemote docinfo = docinfoSet.add();
  177. docinfo.setValue("document", filename, 11L);
  178. docinfo.setValue("ownertable", ownertable, 11L);
  179. docinfo.setValue("description", description, 11L);
  180. docinfo.setValue("postfix", filetype, 11L);
  181. docinfo.setValue("doctype", doctype, 11L);
  182. docinfo.setValue("serialnumber", uploadFile.getFileName(), 11L);
  183. docinfo.setValue("siteid", siteid, 11L);
  184. docinfoSet.save();
  185. PaoSetRemote doclinksSet = docinfo.getPaoSet("DOCLINKS");
  186. if (doclinksSet.isEmpty()) {
  187. PaoRemote doclinks = doclinksSet.add();
  188. doclinks.setValue("document", filename, 11L);
  189. doclinks.setValue("docinfoid", docinfo.getLong("docinfoid"), 11L);
  190. doclinks.setValue("ownertable", ownertable, 11L);
  191. doclinks.setValue("DOCTYPE", doctype, 11L);
  192. doclinks.setValue("description", description, 11L);
  193. doclinks.setValue("OWNERID", ownerid, 11L);
  194. doclinksSet.save();
  195. doclinksSet = doclinks.getPaoSet("$DOCHISTORY", "DOCHISTORY", "");
  196. ((Doclinks) doclinks).addDochistory(doclinksSet, "Add");
  197. doclinksSet.save();
  198. }
  199. resultObject.put("code", 1);
  200. resultObject.put("msg", "成功");
  201. resultObject.put("data", queryDocRows(ownertable, ownerid));
  202. docinfoSet.close();
  203. } catch (Exception exception) {
  204. resultObject.put("code", 0);
  205. resultObject.put("errcode", 0);
  206. resultObject.put("msg", exception.getMessage());
  207. }
  208. return resultObject.toString();
  209. }
  210. public String upLoadDocUrl(String urlstr, String ownertable, String ownerid, String hrid, String description) {
  211. JSONObject resultObject = new JSONObject();
  212. String siteid = getSiteid(hrid);
  213. String doctype = "Attachments";
  214. try {
  215. String str = urlstr.substring(urlstr.lastIndexOf("/") + 1);
  216. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  217. try {
  218. URL url = new URL(urlstr);
  219. HttpURLConnection httpUrl = (HttpURLConnection) url.openConnection();
  220. httpUrl.connect();
  221. BufferedInputStream bis = new BufferedInputStream(httpUrl.getInputStream());
  222. byte[] arrayOfByte = new byte[4096];
  223. int i;
  224. while (-1 != (i = bis.read(arrayOfByte))) {
  225. byteArrayOutputStream.write(arrayOfByte, 0, i);
  226. }
  227. } catch (IOException iOException) {
  228. }
  229. UploadFile uploadFile = new UploadFile(str, "image\\png", byteArrayOutputStream);
  230. Properties properties = P2Server.getP2Server().getConfig();
  231. StringBuffer stringBuffer = new StringBuffer(properties.getProperty("p2.attachment.defaultpath"));
  232. String str1 = properties.getProperty("p2.attachment.ostype");
  233. String str2 = "";
  234. PaoSetRemote ps_sysvarsSet = P2Server.getP2Server().getPaoSet("PS_SYSVARS", P2Server.getP2Server().getSystemUserInfo());
  235. ps_sysvarsSet.setWhere("varname='DOCUMENT_DIR_RULE'");
  236. ps_sysvarsSet.reset();
  237. if (!ps_sysvarsSet.isEmpty()) {
  238. String[] arrayOfString = ps_sysvarsSet.getPao(0).getString("varvalue")
  239. .split("[/]");
  240. stringBuffer.append(File.separator + siteid);
  241. str2 = str2 + siteid + "/";
  242. for (byte b = 1; b < arrayOfString.length; b++) {
  243. if ("DOCTYPE".equalsIgnoreCase(arrayOfString[b])) {
  244. stringBuffer.append(File.separator + doctype);
  245. str2 = str2 + doctype + "/";
  246. } else if ("TABLE".equalsIgnoreCase(arrayOfString[b])) {
  247. stringBuffer.append(File.separator + ownertable);
  248. str2 = str2 + ownertable + "/";
  249. }
  250. }
  251. }
  252. ps_sysvarsSet.close();
  253. String str3 = stringBuffer.toString();
  254. String filename = uploadFile.getFileName();
  255. String filetype = filename.substring(filename.lastIndexOf(".") + 1);
  256. filename = "url附件";
  257. try {
  258. uploadFile.setDirectoryName(str3);
  259. uploadFile.writeToDisk();
  260. str3 = uploadFile.getAbsoluteFileName();
  261. File file = new File(str3);
  262. if (str1 != null && !Utility.isNull(str1.trim()) && str1.trim().equalsIgnoreCase("OSS")) {
  263. UploadFileOSS uploadFileOSS;
  264. (uploadFileOSS = new UploadFileOSS(str3, uploadFile.getFileName(), "", str2)).writeOSS();
  265. if (file.exists() && file.isFile()) {
  266. file.delete();
  267. }
  268. }
  269. if (!Utility.isNull(str3)) {
  270. uploadFile.save();
  271. }
  272. } catch (IOException iOException) {
  273. }
  274. str3 = uploadFile.getFileName();
  275. PaoRemote paoRemote;
  276. PaoSetRemote paoSetRemote3;
  277. (paoRemote = (paoSetRemote3 = P2Server.getP2Server().getPaoSet("DOCINFO", P2Server.getP2Server().getSystemUserInfo())).add())
  278. .setValue("document", filename, 11L);
  279. paoRemote.setValue("ownertable", ownertable, 11L);
  280. paoRemote.setValue("description", description, 11L);
  281. paoRemote.setValue("postfix", filetype, 11L);
  282. paoRemote.setValue("doctype", doctype, 11L);
  283. paoRemote.setValue("serialnumber", str3, 11L);
  284. paoRemote.setValue("siteid", siteid, 11L);
  285. paoSetRemote3.save();
  286. PaoSetRemote paoSetRemote1;
  287. if ((paoSetRemote1 = paoRemote.getPaoSet("DOCLINKS")).isEmpty()) {
  288. PaoRemote paoRemote1;
  289. (paoRemote1 = paoSetRemote1.add()).setValue("document", filename, 11L);
  290. paoRemote1.setValue("docinfoid", paoRemote.getLong("docinfoid"), 11L);
  291. paoRemote1.setValue("ownertable", ownertable, 11L);
  292. paoRemote1.setValue("DOCTYPE", doctype, 11L);
  293. paoRemote1.setValue("description", description, 11L);
  294. paoRemote1.setValue("OWNERID", ownerid, 11L);
  295. paoSetRemote1.save();
  296. paoSetRemote1 = paoRemote1.getPaoSet("$DOCHISTORY", "DOCHISTORY", "");
  297. ((Doclinks) paoRemote1).addDochistory(paoSetRemote1, "Add");
  298. paoSetRemote1.save();
  299. }
  300. paoSetRemote3.close();
  301. resultObject.put("code", 1);
  302. resultObject.put("msg", "成功");
  303. resultObject.put("data", queryDocRows(ownertable, ownerid));
  304. } catch (Exception exception) {
  305. resultObject.put("code", 0);
  306. resultObject.put("msg", exception.getMessage());
  307. }
  308. return resultObject.toString();
  309. }
  310. public String deleteDoc() throws P2Exception {
  311. String ownertable = content.getString("ownertable");
  312. String ownerid = content.getString("ownerid");
  313. String docinfoid = content.getString("docinfoid");
  314. // 获取文件名称
  315. JSONObject resultObject = new JSONObject();
  316. PaoSetRemote docinfoSet = P2Server.getP2Server().getPaoSet("DOCINFO", P2Server.getP2Server().getSystemUserInfo());
  317. docinfoSet.setWhere("docinfoid ='" + docinfoid + "'");
  318. docinfoSet.reset();
  319. if (docinfoSet.isEmpty()) {
  320. resultObject.put("code", 0);
  321. resultObject.put("msg", "找不到id为" + docinfoid + "附件");
  322. return resultObject.toString();
  323. }
  324. docinfoSet.selectAll();
  325. Vector<?> vector = docinfoSet.getSelection();
  326. String str = P2Server.getP2Server().getConfig().getProperty("p2.attachment.defaultpath");
  327. PaoSetRemote ps_sysvarsSet = P2Server.getP2Server().getPaoSet("PS_SYSVARS", P2Server.getP2Server().getSystemUserInfo());
  328. ps_sysvarsSet.setWhere("varname='DOCUMENT_DIR_RULE'");
  329. ps_sysvarsSet.reset();
  330. boolean bool1 = false;
  331. boolean bool2 = false;
  332. if (!ps_sysvarsSet.isEmpty()) {
  333. String[] arrayOfString = ps_sysvarsSet.getPao(0).getString("varvalue").split("[/]");
  334. for (byte b = 1; b < arrayOfString.length; b++) {
  335. if ("DOCTYPE".equalsIgnoreCase(arrayOfString[b])) {
  336. bool1 = true;
  337. } else if ("TABLE".equalsIgnoreCase(arrayOfString[b])) {
  338. bool2 = true;
  339. }
  340. }
  341. }
  342. PaoSetRemote dochistorySet = P2Server.getP2Server().getPaoSet("DOCHISTORY", P2Server.getP2Server().getSystemUserInfo());
  343. ArrayList<String> arrayList = new ArrayList();
  344. int j = vector.size();
  345. for (byte b2 = 0; b2 < j; b2++) {
  346. PaoRemote paoRemote;
  347. if ((paoRemote = (PaoRemote) vector.elementAt(b2)) != null) {
  348. String str1 = "docinfoid = " + paoRemote.getLong("docinfoid") + " and ownertable='" + ownertable + "' and ownerid = '" + ownerid + "'";
  349. PaoSetRemote paoSetRemote;
  350. PaoRemote paoRemote1 = (paoSetRemote = paoRemote.getPaoSet("$DOCLINKS", "DOCLINKS", str1)).getPao(0);
  351. if (((Docinfo) paoRemote).canBeDelete()) {
  352. String str3 = paoRemote.getString("serialnumber");
  353. StringBuffer stringBuffer = new StringBuffer(str);
  354. String str2 = paoRemote.getString("siteid");
  355. stringBuffer.append(File.separator + str2);
  356. if (bool1) {
  357. stringBuffer.append(File.separator + paoRemote.getString("doctype"));
  358. }
  359. if (bool2) {
  360. stringBuffer.append(File.separator + paoRemote.getString("ownertable"));
  361. }
  362. stringBuffer.append(File.separator + str3);
  363. str2 = stringBuffer.toString();
  364. arrayList.add(str2);
  365. paoRemote.delete();
  366. }
  367. ((Doclinks) paoRemote1).addDochistory(dochistorySet, "Delete");
  368. paoSetRemote.deleteAll(2L);
  369. }
  370. }
  371. dochistorySet.save();
  372. docinfoSet.deleteAll();
  373. docinfoSet.save();
  374. for (byte b1 = 0; b1 < arrayList.size(); b1++) {
  375. File file;
  376. if ((file = new File(arrayList.get(b1))).exists()) {
  377. file.delete();
  378. }
  379. }
  380. dochistorySet.close();
  381. docinfoSet.close();
  382. ps_sysvarsSet.close();
  383. resultObject.put("code", 1);
  384. resultObject.put("msg", "成功");
  385. resultObject.put("data", queryDocRows(ownertable, ownerid));
  386. return resultObject.toString();
  387. }
  388. public String queryDoc() {
  389. String ownertable = content.getString("ownertable");
  390. String ownerid = content.getString("ownerid");
  391. Rows rows = queryDocRows(ownertable, ownerid);
  392. return getReturnObject_suc(rows, true).toString();
  393. }
  394. private Rows queryDocRows(String ownertable, String ownerid) {
  395. DBConnect dbConnect = new DBConnect();
  396. SQLFactory sqlFactory = new SQLFactory(this, "附件查询");
  397. sqlFactory.addParameter("ownertable", ownertable);
  398. sqlFactory.addParameter("ownerid", ownerid);
  399. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
  400. return rows;
  401. }
  402. private String getSiteid(String hrid) {
  403. if (parameter.siteidList.containsKey(hrid)) {
  404. return parameter.siteidList.get(hrid);
  405. }
  406. DBConnect connect = new DBConnect();
  407. Rows rows = connect.runSqlQuery("select defsite from pp_users where hrid='" + hrid + "'");
  408. if (!rows.isEmpty()) {
  409. return rows.get(0).getString("defsite");
  410. }
  411. return "";
  412. }
  413. /**华为云上传测试**/
  414. public String upLoadDocForOBS(InputStream uploadfileInputStream, FormDataContentDisposition uploadfile, String ownertable, String ownerid,
  415. String hrid, String description, String filenamestr,String type) throws IOException, P2Exception {
  416. DBConnect dbConnect=new DBConnect();
  417. Rows rows = dbConnect.runSqlQuery("select defsite from pp_users where hrid='" + hrid + "'");
  418. if(!rows.isEmpty()){
  419. String siteid=rows.get(0).getString("defsite");
  420. /**华为云上传**/
  421. BucketFile bucketFile=new BucketFile(siteid);
  422. String ftype=uploadfile.getFileName().substring(uploadfile.getFileName().lastIndexOf(".")+1,uploadfile.getFileName().length());
  423. String fileName = OBSDocManage.createMD5FileName(uploadfile.getFileName())+"."+ftype;
  424. boolean isimage=ftype.equalsIgnoreCase("png")||
  425. ftype.equalsIgnoreCase("jpg")||
  426. ftype.equalsIgnoreCase("jpeg")?true:false;
  427. bucketFile.upload(fileName,uploadfileInputStream,isimage);
  428. /**将上传信息写入附件信息**/
  429. String obsurl_imageshort = "";
  430. if (isimage) {
  431. obsurl_imageshort = bucketFile.getMinImageUrl(fileName);
  432. }
  433. String obsurl = bucketFile.getFileUrl(fileName);
  434. ObjectMetadata matedata = bucketFile.getObjectMetadata(fileName);
  435. PaoSetRemote tattachmentSet = P2Server.getP2Server().getPaoSet("tattachment", P2Server.getP2Server().getSystemUserInfo());
  436. PaoRemote tattachment = tattachmentSet.addAtEnd();
  437. tattachment.setValue("siteid", siteid);
  438. tattachment.setValue("ownertable", ownertable);
  439. tattachment.setValue("ownerid", ownerid);
  440. tattachment.setValue("ftype", "default");
  441. tattachment.setValue("postfix", ftype);
  442. tattachment.setValue("serialnumber", fileName);
  443. tattachment.setValue("fdocument", uploadfile.getFileName());
  444. tattachment.setValue("fobsurl", obsurl);
  445. if(!StringUtils.isBlank(type)) tattachment.setValue("type", type);
  446. tattachmentSet.save();
  447. Row row=new Row();
  448. row.put("fdocument",uploadfile.getFileName());
  449. row.put("fobsurl",obsurl);
  450. row.put("tattachmentid",tattachment.getUniqueIDValue());
  451. row.put("postfix",ftype);
  452. row.put("type",type);
  453. return getReturnObject_suc(row,false).toString();
  454. // tattachment.setValue("docinfoid", docinfo.getUniqueIDValue());
  455. }
  456. return getReturnObject_err("错误").toString();
  457. }
  458. }