PageRenderTime 283ms CodeModel.GetById 141ms app.highlight 5ms RepoModel.GetById 135ms app.codeStats 1ms

/src/main/java/com/google/ie/common/email/EmailManager.java

http://thoughtsite.googlecode.com/
Java | 92 lines | 42 code | 10 blank | 40 comment | 2 complexity | 72442f1a694c252c8c9cbe9445c4c2b9 MD5 | raw file
 1/* Copyright 2010 Google Inc.
 2 * 
 3 * Licensed under the Apache License, Version 2.0 (the "License");
 4 * you may not use this file except in compliance with the License.
 5 * You may obtain a copy of the License at
 6 * 
 7 *      http://www.apache.org/licenses/LICENSE-2.0
 8 * 
 9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS.
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License
14 */
15
16package com.google.ie.common.email;
17
18import com.google.appengine.api.labs.taskqueue.Queue;
19import com.google.appengine.api.labs.taskqueue.QueueFactory;
20import com.google.appengine.api.labs.taskqueue.TaskOptions;
21import com.google.ie.common.constants.IdeaExchangeConstants;
22
23import org.apache.log4j.Logger;
24import org.springframework.stereotype.Component;
25
26import java.util.Iterator;
27import java.util.List;
28
29/**
30 * A utility class to add mailing task to a mail-queue.
31 * 
32 * 
33 * @author asirohi
34 * 
35 */
36@Component
37public class EmailManager {
38    /* logger for logging information */
39    private static Logger log = Logger.getLogger(EmailManager.class);
40    /* Email URL for sending mail */
41    private static final String EMAIL_URL = "mail";
42    /* Queue used for mailing */
43    private static final String MAIL_QUEUE = "mail-queue";
44    /* constant used for comma */
45    private static final String COMMA = ",";
46
47    /**
48     * Create task of sending email based on the given parameters.
49     * 
50     * @param emailType type of email like invitation to join a project
51     * @param recepientEmailIdList list containing email ids.
52     * @param otherInfoList list containing other information
53     *        For eg. sender's name and project name in case of 'createProject'
54     *        type of mail
55     */
56    public static void sendMail(String emailType, List<String> recepientEmailIdList,
57                    List<String> otherInfoList) {
58        Queue queue = QueueFactory.getQueue(MAIL_QUEUE);
59        String otherInfoString = getStringFromList(otherInfoList);
60        String recepientEmailIds = getStringFromList(recepientEmailIdList);
61        TaskOptions taskOptions = TaskOptions.Builder.url(
62                        IdeaExchangeConstants.BACKSLASH + EMAIL_URL
63                        + IdeaExchangeConstants.BACKSLASH + emailType).param(
64                        "recepientEmailIds",
65                        recepientEmailIds).param("otherInfoString", otherInfoString);
66        queue.add(taskOptions);
67        log.info("Task for emailing added to queue : "
68                        + MAIL_QUEUE);
69
70    }
71
72    /**
73     * Convert list of Strings to one comma separated String.
74     * 
75     * @param otherInfoList list of strings
76     * @return comma separated string
77     */
78    public static String getStringFromList(List<String> otherInfoList) {
79        Iterator<String> iterator = otherInfoList.iterator();
80        StringBuilder stringBuilder = new StringBuilder();
81        while (iterator.hasNext()) {
82            String info = iterator.next();
83            stringBuilder.append(info);
84            if (iterator.hasNext()) {
85                stringBuilder.append(COMMA);
86            }
87
88        }
89        return stringBuilder.toString();
90    }
91}
92