001 /*
002 * This class is part of DocWhatsUp, a bug profiling and -reporting lib.
003 *
004 * Copyright (C)
005 *
006 * This library is free software; you can redistribute it and/or modify it
007 * under the terms of the GNU General Public License as published by the
008 * Free Software Foundation; either version 2 of the License, or (at your
009 * option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful, but
012 * WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
014 * Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License along
017 * with this program; if not, write to the Free Software Foundation, Inc.,
018 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019 *
020 * EXTENSION:
021 * Linking DocWhatsUp statically or dynamically with other modules is making a
022 * combined work based on DocWhatsUp. Thus, the terms and conditions of the
023 * GNU General Public License cover the whole combination.
024 *
025 * As a special exception, the copyright holder of DocWhatsUp give you
026 * permission to link DocWhatsUp with independent modules that communicate with
027 * DocWhatsUp solely through the DocWhatsUp.java interface, regardless of the
028 * license terms of these independent modules, and to copy and distribute the
029 * resulting combined work under terms of your choice, provided that
030 * every copy of the combined work is accompanied by a complete copy of
031 * the source code of DocWhatsUp (the version of DocWhatsUp used to produce the
032 * combined work), being distributed under the terms of the GNU General
033 * Public License plus this exception. An independent module is a module
034 * which is not derived from or based on DocWhatsUp.
035
036 * Note that people who make modified versions of DocWhatsUp are not obligated
037 * to grant this special exception for their modified versions; it is
038 * their choice whether to do so. The GNU General Public License gives
039 * permission to release a modified version without this exception; this
040 * exception also makes it possible to release a modified version which
041 * carries forward this exception.
042 *
043 * Author: Philipp Bartsch; codeshaker@gmx.net
044 */
045
046 package org.shaker.dwu;
047
048 /**
049 * This subclass of MailBody delivers a Bugzilla mail bug sumission compatible
050 * mail body (about 1KB).
051 *
052 * @author <A HREF="mailto:codeshaker@gmx.net">
053 * Philipp Bartsch (codeshaker@gmx.net)</A>,
054 * <A HREF="../../../../gpl.txt">GPL License</A>
055 */
056 final class BugzillaMail extends BugMail {
057
058 /**The corresponding BugProfile*/
059 private final BugProfile bug;
060
061 /**
062 * Creates the MailBody.
063 *
064 * @param bugProfile the calling BugProfile that gets exported.
065 */
066 protected BugzillaMail (final BugProfile bugProfile) {
067 bug = bugProfile;
068 }
069
070 /**
071 * Returns true, if the BugProfile contains all required properties (must
072 * not be null or empty!).
073 * These are:
074 * <UL>
075 * <LI>Product name</LI>
076 * <LI>Product version</LI>
077 * <LI>An error class, that is used as a "component" tag</LI>
078 * <LI>Message, that is used as a "short_desc" tag</LI>
079 * </UL>
080 *
081 * @return true, if all necessary informations are available and valid
082 */
083 protected boolean isSatisfied () {
084 StringBuffer errorString = new StringBuffer();
085
086 if (!bug.containsKey(P_NAME)
087 || bug.getProperty(P_NAME) == null
088 || bug.getProperty(P_NAME).length() == 0)
089 errorString.append(P_NAME + ", ");
090 if (!bug.containsKey(ERROR_CLASS)
091 || bug.getProperty(ERROR_CLASS) == null
092 || bug.getProperty(ERROR_CLASS).length() == 0)
093 errorString.append(ERROR_CLASS + ", ");
094 if (!bug.containsKey(P_VERSION)
095 | bug.getProperty(P_VERSION) == null
096 || bug.getProperty(P_VERSION).length() == 0)
097 errorString.append(P_VERSION + ", ");
098 if (!bug.containsKey(MESSAGE)
099 || bug.getProperty(MESSAGE) == null
100 || bug.getProperty(MESSAGE).length() == 0)
101 errorString.append(MESSAGE + ", ");
102 if (errorString.length() > 0) {
103 bug.setProperty("Intended Format",
104 "Bugzilla needs more info: "
105 + errorString.substring(0,
106 errorString.length()-2));
107 if (DocWhatsUp.SETTINGS.hasAlternativeRecipient())
108 bug.setProperty(BugMail.MAIL_TO,
109 DocWhatsUp.SETTINGS.getAlternativeRecipient());
110 return false;
111 } else
112 return true;
113 }
114
115 /**
116 * Returns text/plain.
117 *
118 * @return MIME type text/plain
119 */
120 protected final String getMimeType() {
121 return "text/plain";
122 }
123
124 /**
125 * Returns an empty string. You could put the short_desc into the subject
126 * line, but dwu saves it in the mail body.
127 *
128 * @return an empty string
129 */
130 protected final String getSubjectLine () {
131 return "";
132 }
133
134 /**
135 * Returns the bug submission mail body.
136 *
137 * @return the bugmail formatted mail body
138 */
139 protected String getMailBody () {
140 final StringBuffer body = new StringBuffer();
141
142 body.append("@product="
143 + bug.getProperty(P_NAME)
144 + NEW_LINE);
145 body.append("@component="
146 + bug.getProperty(ERROR_CLASS)
147 + NEW_LINE);
148 body.append("@version="
149 + bug.getProperty(P_VERSION)
150 + NEW_LINE);
151 body.append("@op_sys="
152 + System.getProperty("os.name")
153 + NEW_LINE);
154 if (bug.containsKey("priority"))
155 body.append("@priority="
156 + bug.getProperty("dwu.priority")
157 + NEW_LINE);
158 if (bug.containsKey("bug_severity"))
159 body.append("@bug_severity="
160 + bug.getProperty("dwu.bug_severity")
161 + NEW_LINE);
162 if (bug.containsKey(MESSAGE))
163 body.append("@shortdescription="
164 + bug.getProperty(MESSAGE)
165 + NEW_LINE);
166
167 body.append(NEW_LINE);
168
169 // bundles the rest of the properties (custom ones)
170 String[] keys = (String[]) bug.keySet().toArray(new String[0]);
171 for (int i = keys.length-1; i >= 0; i--) {
172 if (!isStandardKey(keys[i]))
173 body.append(getTextRow(keys[i],
174 wellform(bug.getProperty(keys[i]))));
175 }
176 body.append(NEW_LINE);
177
178 keys = ToolBox.getSortetSysPropArray();
179 for (int i = 0; i < keys.length; i++) {
180 if (BugProfile.propertyFilter.contains(keys[i]))
181 body.append(getTextRow(keys[i],
182 wellform(System.getProperty(keys[i]))));
183 }
184
185
186 return body.toString();
187 }
188
189 /**
190 * Returns a formatted textrow for a key-value pair
191 *
192 * @param key the label
193 * @param value the content
194 * @return the formatted textrow
195 */
196 private static final String getTextRow (final String key,
197 final String value) {
198 return NEW_LINE
199 + "<" + key + ">" + NEW_LINE
200 + value + NEW_LINE;
201 }
202
203 /**
204 * Returns the given text with replaced linebreaks.
205 *
206 * @param text the text to be wellformed
207 * @return the wellformed text
208 */
209 private static String wellform (String text) {
210 return replace_impl(text,
211 ";",
212 NEW_LINE);
213 }
214 }
|