/branches/versions/aipo702/aipo/lib/mail/src/main/java/com/aimluck/eip/mail/file/ALFileLocalFolder.java

http://aipo.googlecode.com/
Java | 443 lines | 281 code | 46 blank | 116 comment | 25 complexity | d8e69684be1f7acd2dd5fcd266a20505 MD5 | raw file
  1. /*
  2. * Aipo is a groupware program developed by Aimluck,Inc.
  3. * Copyright (C) 2004-2011 Aimluck,Inc.
  4. * http://www.aipo.com
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.aimluck.eip.mail.file;
  20. import java.io.BufferedInputStream;
  21. import java.io.ByteArrayInputStream;
  22. import java.io.ByteArrayOutputStream;
  23. import java.io.IOException;
  24. import java.util.Enumeration;
  25. import java.util.List;
  26. import java.util.Properties;
  27. import java.util.jar.Attributes;
  28. import javax.mail.Header;
  29. import javax.mail.Message;
  30. import javax.mail.Session;
  31. import javax.mail.internet.MimeMessage;
  32. import org.apache.cayenne.exp.Expression;
  33. import org.apache.cayenne.exp.ExpressionFactory;
  34. import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
  35. import org.apache.jetspeed.services.logging.JetspeedLogger;
  36. import com.aimluck.eip.cayenne.om.portlet.EipTMail;
  37. import com.aimluck.eip.common.ALPageNotFoundException;
  38. import com.aimluck.eip.mail.ALAbstractFolder;
  39. import com.aimluck.eip.mail.ALLocalMailMessage;
  40. import com.aimluck.eip.mail.ALMailMessage;
  41. import com.aimluck.eip.orm.Database;
  42. import com.aimluck.eip.orm.query.SelectQuery;
  43. import com.aimluck.eip.services.storage.ALStorageService;
  44. import com.aimluck.eip.util.ALCommonUtils;
  45. /**
  46. * ?????????????????????????????????????????????? <br />
  47. *
  48. */
  49. public class ALFileLocalFolder extends ALAbstractFolder {
  50. private static final JetspeedLogger logger = JetspeedLogFactoryService
  51. .getLogger(ALFileLocalFolder.class.getName());
  52. /** ??????????? */
  53. public static final String DEFAULT_MAIL_FILENAME_DATE_FORMAT =
  54. "yyyyMMddHHmmssSSS";
  55. /**
  56. * ???????
  57. *
  58. * @param parentFolder
  59. * ?????
  60. * @param folderName
  61. * ????????
  62. */
  63. public ALFileLocalFolder(int type_mail, String org_id, int user_id,
  64. int account_id) {
  65. super(type_mail, org_id, user_id, account_id);
  66. }
  67. /**
  68. * ??????????
  69. *
  70. * @param index
  71. * @return
  72. */
  73. @Override
  74. public ALLocalMailMessage getMail(int mailid) {
  75. try {
  76. SelectQuery<EipTMail> query = Database.query(EipTMail.class);
  77. Expression exp1 =
  78. ExpressionFactory.matchDbExp(EipTMail.MAIL_ID_PK_COLUMN, Integer
  79. .valueOf(mailid));
  80. Expression exp2 =
  81. ExpressionFactory.matchExp(EipTMail.USER_ID_PROPERTY, user_id);
  82. EipTMail email = query.setQualifier(exp1.andExp(exp2)).fetchSingle();
  83. if (email == null) {
  84. logger.debug("[Mail] Not found ID...");
  85. return null;
  86. }
  87. ALLocalMailMessage msg =
  88. readMail(getFullName()
  89. + ALStorageService.separator()
  90. + email.getFilePath());
  91. // ????????
  92. email.setReadFlg("T");
  93. Database.commit();
  94. return msg;
  95. } catch (Throwable t) {
  96. Database.rollback();
  97. logger.error("[ALFileLocalFolder]", t);
  98. return null;
  99. }
  100. }
  101. /**
  102. * ???????????????mail ???????????
  103. *
  104. * @param fileName
  105. * @return
  106. */
  107. private ALLocalMailMessage readMail(String filepath) {
  108. System.setProperty("mail.mime.charset", "ISO-2022-JP");
  109. System.setProperty("mail.mime.decodetext.strict", "false");
  110. Properties prop = new Properties();
  111. prop.setProperty("mail.mime.address.strict", "false");
  112. ALLocalMailMessage localmsg = null;
  113. BufferedInputStream input = null;
  114. try {
  115. input = new BufferedInputStream(ALStorageService.getFile(filepath));
  116. localmsg =
  117. new ALLocalMailMessage(Session.getDefaultInstance(prop), input);
  118. input.close();
  119. } catch (Exception ex) {
  120. logger.error("Exception", ex);
  121. }
  122. return localmsg;
  123. }
  124. /**
  125. * ?????????
  126. *
  127. * @param messages
  128. * @return
  129. */
  130. @Override
  131. public boolean saveMail(ALMailMessage mail, String orgId) {
  132. boolean res = false;
  133. try {
  134. String tmpFileName = getNewFileName();
  135. res = saveMailToFile(mail, tmpFileName, true);
  136. if (res) {
  137. res = insertMailToDB((MimeMessage) mail, tmpFileName, false, false);
  138. }
  139. } catch (Exception ex) {
  140. logger.error("Exception", ex);
  141. res = false;
  142. }
  143. return res;
  144. }
  145. /**
  146. * ???????????????????????????????<br />
  147. * ????????????????????????????????????
  148. *
  149. * @param localMailMessage
  150. * @return
  151. */
  152. @Override
  153. public boolean saveDefectiveMail(ALMailMessage mail, String orgId) {
  154. boolean res = false;
  155. try {
  156. String tmpFileName = getNewFileName();
  157. res = saveMailToFile(mail, tmpFileName, false);
  158. if (res) {
  159. res = insertMailToDB((MimeMessage) mail, tmpFileName, false, false);
  160. }
  161. } catch (Exception ex) {
  162. logger.error("Exception", ex);
  163. res = false;
  164. }
  165. return res;
  166. }
  167. /**
  168. * ???????????????
  169. *
  170. * @param mail
  171. * @return
  172. */
  173. private boolean saveMailToFile(ALMailMessage mail, String fileName,
  174. boolean savecontents) {
  175. boolean res = false;
  176. ByteArrayOutputStream output = null;
  177. try {
  178. // String pop3MailPath = getFullName() + File.separator + fileName;
  179. // ??????
  180. output = new ByteArrayOutputStream();
  181. if (savecontents) {
  182. mail.writeTo(output);
  183. } else {
  184. MimeMessage mimeMessage = (MimeMessage) mail;
  185. Session session = Session.getDefaultInstance(new Properties());
  186. Message newMsg = new MimeMessage(session);
  187. Enumeration<?> headers = mimeMessage.getAllHeaders();
  188. while (headers.hasMoreElements()) {
  189. Header h = (Header) headers.nextElement();
  190. newMsg.addHeader(h.getName(), h.getValue());
  191. }
  192. newMsg.setText("????????"
  193. + ALCommonUtils.getMaxFileSize()
  194. + "MB??????????????????????????\r\n ??????????????????????????");
  195. newMsg.writeTo(output);
  196. }
  197. ALStorageService.createNewFile(new ByteArrayInputStream(output
  198. .toByteArray()), getFullName(), fileName);
  199. output.flush();
  200. output.close();
  201. mail.clearContents();
  202. res = true;
  203. } catch (Exception ex) {
  204. logger.error("Exception", ex);
  205. try {
  206. if (output != null) {
  207. output.close();
  208. }
  209. } catch (IOException ie) {
  210. logger.error("Exception", ie);
  211. }
  212. res = false;
  213. } finally {
  214. try {
  215. if (output != null) {
  216. output.close();
  217. }
  218. } catch (IOException ie) {
  219. logger.error("Exception", ie);
  220. res = false;
  221. }
  222. }
  223. return res;
  224. }
  225. /**
  226. * ?????????????????????
  227. *
  228. * @return
  229. */
  230. @Override
  231. public boolean deleteMail(int mailid) {
  232. try {
  233. SelectQuery<EipTMail> query = Database.query(EipTMail.class);
  234. Expression exp1 =
  235. ExpressionFactory.matchDbExp(EipTMail.MAIL_ID_PK_COLUMN, Integer
  236. .valueOf(mailid));
  237. Expression exp2 =
  238. ExpressionFactory.matchExp(EipTMail.USER_ID_PROPERTY, user_id);
  239. Expression exp3 =
  240. ExpressionFactory.matchExp(EipTMail.ACCOUNT_ID_PROPERTY, account_id);
  241. List<EipTMail> mail_list =
  242. query
  243. .andQualifier(exp1)
  244. .andQualifier(exp2)
  245. .andQualifier(exp3)
  246. .fetchList();
  247. if (mail_list == null || mail_list.size() == 0) {
  248. logger.debug("[ALDbLocalFolder] Not found ID...");
  249. throw new ALPageNotFoundException();
  250. }
  251. EipTMail record = mail_list.get(0);
  252. String filePath = record.getFilePath();
  253. // ??????
  254. ALStorageService.deleteFile(getFullName()
  255. + ALStorageService.separator()
  256. + filePath);
  257. // ????????
  258. Database.delete(record);
  259. Database.commit();
  260. } catch (Throwable t) {
  261. Database.rollback();
  262. logger.error("[ALFileLocalFolder]", t);
  263. return false;
  264. }
  265. return true;
  266. }
  267. /**
  268. * ?????????????????????
  269. *
  270. * @param msgIndexes
  271. * @return
  272. */
  273. @Override
  274. public boolean deleteMails(List<String> msgIndexes) {
  275. try {
  276. SelectQuery<EipTMail> query = Database.query(EipTMail.class);
  277. Expression exp1 =
  278. ExpressionFactory.inDbExp(EipTMail.MAIL_ID_PK_COLUMN, msgIndexes);
  279. Expression exp2 =
  280. ExpressionFactory.matchExp(EipTMail.USER_ID_PROPERTY, Integer
  281. .valueOf(user_id));
  282. List<EipTMail> mail_list =
  283. query.andQualifier(exp1).andQualifier(exp2).fetchList();
  284. if (mail_list == null || mail_list.size() == 0) {
  285. logger.debug("[ALFileLocalFolder] Not found ID...");
  286. throw new ALPageNotFoundException();
  287. }
  288. for (EipTMail record : mail_list) {
  289. String filePath = record.getFilePath();
  290. ALStorageService.deleteFile(getFullName()
  291. + ALStorageService.separator()
  292. + filePath);
  293. }
  294. Database.deleteAll(mail_list);
  295. Database.commit();
  296. } catch (Throwable t) {
  297. Database.rollback();
  298. logger.error("[ALFileLocalFolder]", t);
  299. return false;
  300. }
  301. return true;
  302. }
  303. /**
  304. *
  305. * @return
  306. */
  307. @Override
  308. protected Attributes getColumnMap() {
  309. Attributes map = new Attributes();
  310. map.putValue("read_flg", EipTMail.READ_FLG_PROPERTY);
  311. map.putValue("subject", EipTMail.SUBJECT_PROPERTY);
  312. map.putValue("person", EipTMail.PERSON_PROPERTY);
  313. map.putValue("date", EipTMail.EVENT_DATE_PROPERTY);
  314. map.putValue("volume", EipTMail.FILE_VOLUME_PROPERTY);
  315. return map;
  316. }
  317. /**
  318. * ????????????
  319. *
  320. * @return
  321. */
  322. @Override
  323. public int getNewMailNum() {
  324. return 0;
  325. }
  326. /**
  327. * ????????????
  328. *
  329. * @param num
  330. */
  331. @Override
  332. public void setNewMailNum(int num) {
  333. }
  334. /**
  335. * ??????????????????????
  336. *
  337. * @param type
  338. * ??????
  339. * @return
  340. */
  341. public int getMailSum() {
  342. return 0;
  343. }
  344. /**
  345. * ??????????????????????
  346. *
  347. * @return
  348. */
  349. @Override
  350. public int getUnreadMailNum() {
  351. return 0;
  352. }
  353. /**
  354. * ?????????????
  355. */
  356. @Override
  357. public void close() {
  358. }
  359. /**
  360. * ??????????????
  361. *
  362. * @return
  363. */
  364. public String getNewFileName() {
  365. return String.valueOf(System.nanoTime());
  366. }
  367. /**
  368. * @param msgIndexes
  369. * @return
  370. */
  371. @Override
  372. public boolean readMails(List<String> msgIndexes) {
  373. try {
  374. SelectQuery<EipTMail> query = Database.query(EipTMail.class);
  375. Expression exp1 =
  376. ExpressionFactory.inDbExp(EipTMail.MAIL_ID_PK_COLUMN, msgIndexes);
  377. Expression exp2 =
  378. ExpressionFactory.matchExp(EipTMail.USER_ID_PROPERTY, Integer
  379. .valueOf(user_id));
  380. List<EipTMail> mail_list =
  381. query.andQualifier(exp1).andQualifier(exp2).fetchList();
  382. if (mail_list == null || mail_list.size() == 0) {
  383. logger.debug("[ALFileLocalFolder] Not found ID...");
  384. throw new ALPageNotFoundException();
  385. }
  386. for (EipTMail record : mail_list) {
  387. record.setReadFlg("T");
  388. }
  389. Database.commit();
  390. } catch (Throwable t) {
  391. Database.rollback();
  392. logger.error("[ALFileLocalFolder]", t);
  393. return false;
  394. }
  395. return true;
  396. }
  397. }