001 package org.ujmp.mail; 002 003 import java.io.Closeable; 004 import java.io.IOException; 005 import java.util.Collection; 006 import java.util.Iterator; 007 import java.util.List; 008 import java.util.ListIterator; 009 import java.util.Map; 010 011 import javax.mail.Folder; 012 import javax.mail.MessagingException; 013 014 import org.ujmp.core.Matrix; 015 import org.ujmp.core.collections.SoftHashMap; 016 import org.ujmp.core.exceptions.MatrixException; 017 import org.ujmp.core.listmatrix.AbstractListMatrix; 018 019 public class MessagesMatrix extends AbstractListMatrix<Matrix> implements 020 Closeable { 021 private static final long serialVersionUID = -8489199262755536077L; 022 023 private MessagesList list = null; 024 025 public MessagesMatrix(String url, String user, String password, 026 String folderName) throws Exception { 027 ImapUtil util = new ImapUtil(url, user, password); 028 Folder folder = util.getFolder(folderName); 029 this.list = new MessagesList(folder); 030 } 031 032 public MessagesMatrix(Folder folder) { 033 this.list = new MessagesList(folder); 034 } 035 036 037 public List<Matrix> getList() { 038 return list; 039 } 040 041 public void setObject(Matrix value, long row, long column) { 042 } 043 044 public void setObject(Matrix value, int row, int column) { 045 } 046 047 048 public void close() throws IOException { 049 list.close(); 050 } 051 052 } 053 054 class MessagesList implements List<Matrix>, Closeable { 055 056 private Folder folder = null; 057 058 private final Map<Integer, MessageMatrix> messages = new SoftHashMap<Integer, MessageMatrix>(); 059 060 public MessagesList(Folder folder) { 061 this.folder = folder; 062 } 063 064 065 public boolean add(Matrix e) { 066 throw new MatrixException("not implemented"); 067 } 068 069 070 public void add(int index, Matrix element) { 071 throw new MatrixException("not implemented"); 072 } 073 074 075 public boolean addAll(Collection<? extends Matrix> c) { 076 throw new MatrixException("not implemented"); 077 } 078 079 080 public boolean addAll(int index, Collection<? extends Matrix> c) { 081 throw new MatrixException("not implemented"); 082 } 083 084 085 public void clear() { 086 throw new MatrixException("not implemented"); 087 } 088 089 090 public boolean contains(Object o) { 091 throw new MatrixException("not implemented"); 092 } 093 094 095 public boolean containsAll(Collection<?> c) { 096 throw new MatrixException("not implemented"); 097 } 098 099 100 public Matrix get(int index) { 101 MessageMatrix m = messages.get(index); 102 if (m == null) { 103 try { 104 if (!folder.isOpen()) { 105 folder.open(Folder.READ_ONLY); 106 } 107 m = new MessageMatrix(folder.getMessage(index + 1)); 108 } catch (IOException e) { 109 e.printStackTrace(); 110 } catch (MessagingException e) { 111 e.printStackTrace(); 112 } 113 messages.put(index, m); 114 } 115 return m; 116 } 117 118 119 public int indexOf(Object o) { 120 throw new MatrixException("not implemented"); 121 } 122 123 124 public boolean isEmpty() { 125 try { 126 return folder.getMessageCount() == 0; 127 } catch (MessagingException e) { 128 e.printStackTrace(); 129 return true; 130 } 131 } 132 133 134 public Iterator<Matrix> iterator() { 135 return new Iterator<Matrix>() { 136 137 int index = 0; 138 139 140 public boolean hasNext() { 141 return index < size(); 142 } 143 144 145 public Matrix next() { 146 Matrix m = get(index); 147 index++; 148 return m; 149 } 150 151 152 public void remove() { 153 throw new MatrixException("not implemented"); 154 } 155 }; 156 157 } 158 159 160 public int lastIndexOf(Object o) { 161 throw new MatrixException("not implemented"); 162 } 163 164 165 public ListIterator<Matrix> listIterator() { 166 throw new MatrixException("not implemented"); 167 } 168 169 170 public ListIterator<Matrix> listIterator(int index) { 171 throw new MatrixException("not implemented"); 172 } 173 174 175 public boolean remove(Object o) { 176 throw new MatrixException("not implemented"); 177 } 178 179 180 public Matrix remove(int index) { 181 throw new MatrixException("not implemented"); 182 } 183 184 185 public boolean removeAll(Collection<?> c) { 186 throw new MatrixException("not implemented"); 187 } 188 189 190 public boolean retainAll(Collection<?> c) { 191 throw new MatrixException("not implemented"); 192 } 193 194 195 public Matrix set(int index, Matrix element) { 196 throw new MatrixException("not implemented"); 197 } 198 199 200 public int size() { 201 try { 202 return folder.getMessageCount(); 203 } catch (MessagingException e) { 204 e.printStackTrace(); 205 return -1; 206 } 207 } 208 209 210 public List<Matrix> subList(int fromIndex, int toIndex) { 211 throw new MatrixException("not implemented"); 212 } 213 214 215 public Object[] toArray() { 216 throw new MatrixException("not implemented"); 217 } 218 219 220 public <T> T[] toArray(T[] a) { 221 throw new MatrixException("not implemented"); 222 } 223 224 225 public void close() throws IOException { 226 try { 227 folder.close(false); 228 } catch (MessagingException e) { 229 e.printStackTrace(); 230 } 231 } 232 233 }