aboutsummaryrefslogtreecommitdiffstats
path: root/previousAttempts/java/ArchivableFolder.java
blob: 47a5764967795f6dead88c97b6c627f1af6e79a4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.ArrayList;

import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;

/**
 * Class servers as a container for messages to be archived from the
 * particular folder and does the actual archivation.
 * @author matej
 *
 */
@SuppressWarnings("serial")
public class ArchivableFolder extends ArrayList<Message> {
	private Folder sourceFolder;
	private Folder targetFolder;
	
	/**
	 * Constructor for the folder.
	 * @param source
	 * @param year
	 * @throws MessagingException
	 */
	public ArchivableFolder(Folder source, int year) throws MessagingException {
		sourceFolder = source;
		targetFolder = getArchiveFolderName(source,year);
	}
	
	/**
	 * 
	 * @throws MessagingException
	 */
	public void doArchive() throws MessagingException {
		Message[] intArray;
		intArray = (Message[]) this.toArray();
		sourceFolder.copyMessages(intArray, targetFolder);
		for (Message message : intArray) {
			message.setFlag(Flags.Flag.DELETED, true);
		}
		sourceFolder.expunge();
	}
	
	/**
	 * 
	 * @param msg
	 * @return
	 */
	@Override
	public boolean add(Message msg) {
		return this.add(msg);
	}
	
	/**
	 * 
	 * @param folder
	 * @param year
	 * @return
	 * @throws MessagingException
	 */
	private static Folder getArchiveFolderName(Folder folder,int year) throws MessagingException {
		String archFolder = "INBOX/Archiv/";
		int rootLen = "INBOX/".length();
		String baseName = folder.getFullName();
		baseName = baseName.substring(rootLen, baseName.length());
		archFolder = "/" + archFolder + year + "/" + baseName;
		return(folder.getFolder(archFolder));
	}
}