BrightSide Workbench Full Report + Source Code
AttachCollection.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2015 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.attach.search;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Date;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28 import org.amic.util.date.CheckDate;
29 import org.turro.action.MailAttachment;
30 import org.turro.attach.db.AttachPU;
31 import org.turro.attach.entity.Attachment;
32 import org.turro.elephant.context.ElephantContext;
33 import org.turro.wd.entities.AttachWd;
34 
39 public class AttachCollection {
40 
41  private final ArrayList<MailAttachment> attachments = new ArrayList<>();
42 
43  public Collection<MailAttachment> getAttachments() {
44  return attachments;
45  }
46 
47  public void loadAttachments(String path, boolean publicOnly) {
48  loadAttachments(path, publicOnly, null);
49  }
50 
51  public void loadAttachments(String path, boolean publicOnly, Date from) {
52  AttachResults results = new AttachResults();
53  results.setPublicOnly(publicOnly);
54  results.setAttachPath(path);
55  results.setCkExactPath(true);
56  for(Attachment attach : results.getAttachmentList()) {
57  if(from != null && new CheckDate(attach.getModification()).addMinutes(10).getDate().before(from)) {
58  continue;
59  }
60  attachments.add(new MailAttachment(AttachPU.getObjectPath(attach), attach.getFileName(), attach));
61  }
62  for(MailAttachment ma : attachments) {
63  try {
64  ma.attachment = File.createTempFile("attach_", "_mail");
65  AttachWd.writeToFile((Attachment) ma.entity, ma.attachment);
66  } catch (IOException ex) {
67  Logger.getLogger(AttachCollection.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
68  }
69  }
70  }
71 
72  public void removeAttachments() {
73  for(MailAttachment ma : attachments) {
74  ma.attachment.delete();
75  }
76  }
77 
78  public Collection<Attachment> getAttachmentList(String path, boolean publicOnly, Date from) {
79  ArrayList<Attachment> attlist = new ArrayList<>();
80  AttachResults results = new AttachResults();
81  results.setPublicOnly(publicOnly);
82  results.setAttachPath(path);
83  results.setCkExactPath(true);
84  for(Attachment attach : results.getAttachmentList()) {
85  if(from != null && new CheckDate(attach.getModification()).addMinutes(10).getDate().before(from)) {
86  continue;
87  }
88  attlist.add(attach);
89  }
90  return attlist;
91  }
92 }
static String getObjectPath(Object object)
Definition: AttachPU.java:57
void loadAttachments(String path, boolean publicOnly)
Collection< Attachment > getAttachmentList(String path, boolean publicOnly, Date from)
Collection< MailAttachment > getAttachments()
void loadAttachments(String path, boolean publicOnly, Date from)
void setCkExactPath(boolean ckExactPath)
void setPublicOnly(boolean publicOnly)
java.util.List< Attachment > getAttachmentList()
void setAttachPath(String attachPath)
static void writeToFile(Attachment attachment, File file)
Definition: AttachWd.java:116