Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/mrbook/mrpostman/another/summary/MailDropSummary.java


1   /*
2    * -*- mode: java; c-basic-indent: 4; indent-tabs-mode: nil -*-
3    * :indentSize=4:noTabs=true:tabSize=4:indentOnTab=true:indentOnEnter=true:mode=java:
4    * ex: set tabstop=4 expandtab:
5    *
6    * MrPostman - webmail <-> email gateway
7    * Copyright (C) 2002-2003 MrPostman Development Group
8    * Projectpage: http://mrbook.org/mrpostman/
9    *
10   *
11   * This program is free software; you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as published by
13   * the Free Software Foundation; either version 2 of the License, or
14   * (at your option) any later version.
15   *
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * In particular, this implies that users are responsible for
21   * using MrPostman after reading the terms and conditions given
22   * by their web-mail provider.
23   *
24   * You should have received a copy of the GNU General Public License
25   * Named LICENSE in the base directory of this distribution,
26   * if not, write to the Free Software
27   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28   */
29  
30  package org.mrbook.mrpostman.another.summary;
31  
32  import java.text.DateFormat;
33  
34  import java.util.Date;
35  import java.util.Iterator;
36  import java.util.LinkedList;
37  
38  
39  /**
40  * Summary class. This will store the mail drop summary info
41  * @author Chris Humphreys
42  */
43  public class MailDropSummary {
44      public static final String CVSID = "$Id: MailDropSummary.java,v 1.5 2003/02/09 23:38:13 lbruand Exp $";
45  
46      /**
47      * Represents the list of message SummaryItems
48      */
49      private LinkedList summaryItems;
50  
51      /**
52      * The total number of TOP commands issued
53      */
54      private int numTopped = 0;
55  
56      /**
57      * The total number of RETR commands issued
58      */
59      private int numRead = 0;
60  
61      /**
62      * The total number of DELE commands issued
63      */
64      private int numDeleted = 0;
65  
66      /**
67      * The total number of errors encountered.
68      */
69      private int numErrors = 0;
70  
71      /**
72      * The total number of messages in the inbox
73      */
74      private int numberOfMessages = 0;
75  
76      /**
77      *  The username of the maildrop
78      */
79      private String username = null;
80  
81      /**
82      * Any general error messages that may have occured (not related to a particular message)
83      * Multiple messages are separated by a newline.
84      */
85      private String errors = null;
86  
87      public MailDropSummary() {
88          summaryItems = new LinkedList();
89      }
90  
91      public void setUsername(String username) {
92          this.username = username;
93      }
94  
95      public void setNumberOfMessages(int numMessages) {
96          numberOfMessages = numMessages;
97  
98          for (int i = 0; i < numberOfMessages; i++) {
99              summaryItems.add(new SummaryItem());
100         }
101     }
102 
103     /**
104     * Record that the issued RETR command on the specified number was a success.
105     */
106     public void retrSuccess(int messageNumber) {
107         getSummaryItemForMsg(messageNumber).getRetr().success();
108         numRead++;
109     }
110 
111     /**
112     * Record that the issued RETR command on the specified number resulted in an error.
113     */
114     public void retrFailure(int messageNumber, String errorMsg) {
115         getSummaryItemForMsg(messageNumber).getRetr().failure(errorMsg);
116         numRead++;
117     }
118 
119     /**
120     * Record that the issued TOP command on the specified number was a success.
121     */
122     public void topSuccess(int messageNumber) {
123         getSummaryItemForMsg(messageNumber).getTop().success();
124         numTopped++;
125     }
126 
127     /**
128     * Record that the issued TOP command on the specified number resulted in an error.
129     */
130     public void topFailure(int messageNumber, String errorMsg) {
131         getSummaryItemForMsg(messageNumber).getTop().failure(errorMsg);
132         numTopped++;
133     }
134 
135     /**
136     * Record that the issued DELE command on the specified number was a success.
137     */
138     public void deleSuccess(int messageNumber) {
139         getSummaryItemForMsg(messageNumber).getDele().success();
140         numDeleted++;
141     }
142 
143     /**
144     * Record that the issued DELE command on the specified number resulted in an error.
145     */
146     public void deleFailure(int messageNumber, String errorMsg) {
147         getSummaryItemForMsg(messageNumber).getDele().failure(errorMsg);
148         numDeleted++;
149     }
150 
151     /**
152     * Create a String representation of all the summary information.
153     */
154     public String toString() {
155         DateFormat df = DateFormat.getDateTimeInstance();
156         String linefeed = System.getProperty("line.separator");
157 
158         StringBuffer buff = new StringBuffer();
159         buff.append("Mail Drop summary for '").append(username);
160         buff.append("' at ").append(df.format(new Date())).append(linefeed);
161         buff.append("Number of messages in inbox: ").append(numberOfMessages).append(linefeed);
162         buff.append("Number of TOP requests: ").append(numTopped).append(linefeed);
163         buff.append("Number of RETR requests: ").append(numRead).append(linefeed);
164         buff.append("Number of DELE requests: ").append(numDeleted).append(linefeed);
165         String subclassMsg = getSessionSpecificSummaryInfo();
166 
167         if (subclassMsg != null) {
168             buff.append(subclassMsg).append(linefeed);
169         }
170         buff.append("Number of ERRORS: ").append(numErrors).append(linefeed);
171         buff.append(linefeed).append(linefeed);
172         buff.append("Message specific breakdown:").append(linefeed);
173 
174         int msgNumber = 1;
175 
176         for (Iterator it = summaryItems.iterator(); it.hasNext();) {
177             SummaryItem summary = (SummaryItem) it.next();
178             buff.append(msgNumber);
179             buff.append("{");
180             buff.append(linefeed);
181             buff.append(summary.toString()).append(linefeed).append("}");
182             buff.append(linefeed);
183             msgNumber++;
184         }
185 
186         return buff.toString();
187     }
188 
189     /**
190     * Override this in subclasses to include any supplementary summary info specific to
191     * subclass in the summary string.
192     */
193     protected String getSessionSpecificSummaryInfo() {
194         return null;
195     }
196 
197     /**
198     * Return the SummaryItem info for a particular message number.
199     * Note: message number starts from 1
200     */
201     public SummaryItem getSummaryItemForMsg(int msgNumber) {
202         return (SummaryItem) summaryItems.get(msgNumber - 1);
203     }
204 
205     /**
206     * Add an error to our list of non-message related errors.
207     */
208     public void addGeneralError(String newError) {
209         if (errors == null) {
210             errors = newError;
211         } else {
212             errors += ("\n" + newError);
213         }
214     }
215 
216     /**
217     * Set the specified message's header info.
218     */
219     public void setMsgHeader(int msgNumber, HeaderInfo header) {
220         getSummaryItemForMsg(msgNumber).setHeader(header);
221     }
222 }