/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. package com.google.ie.common.email;
  16. import com.google.appengine.api.labs.taskqueue.Queue;
  17. import com.google.appengine.api.labs.taskqueue.QueueFactory;
  18. import com.google.appengine.api.labs.taskqueue.TaskOptions;
  19. import com.google.ie.common.constants.IdeaExchangeConstants;
  20. import org.apache.log4j.Logger;
  21. import org.springframework.stereotype.Component;
  22. import java.util.Iterator;
  23. import java.util.List;
  24. /**
  25. * A utility class to add mailing task to a mail-queue.
  26. *
  27. *
  28. * @author asirohi
  29. *
  30. */
  31. @Component
  32. public class EmailManager {
  33. /* logger for logging information */
  34. private static Logger log = Logger.getLogger(EmailManager.class);
  35. /* Email URL for sending mail */
  36. private static final String EMAIL_URL = "mail";
  37. /* Queue used for mailing */
  38. private static final String MAIL_QUEUE = "mail-queue";
  39. /* constant used for comma */
  40. private static final String COMMA = ",";
  41. /**
  42. * Create task of sending email based on the given parameters.
  43. *
  44. * @param emailType type of email like invitation to join a project
  45. * @param recepientEmailIdList list containing email ids.
  46. * @param otherInfoList list containing other information
  47. * For eg. sender's name and project name in case of 'createProject'
  48. * type of mail
  49. */
  50. public static void sendMail(String emailType, List<String> recepientEmailIdList,
  51. List<String> otherInfoList) {
  52. Queue queue = QueueFactory.getQueue(MAIL_QUEUE);
  53. String otherInfoString = getStringFromList(otherInfoList);
  54. String recepientEmailIds = getStringFromList(recepientEmailIdList);
  55. TaskOptions taskOptions = TaskOptions.Builder.url(
  56. IdeaExchangeConstants.BACKSLASH + EMAIL_URL
  57. + IdeaExchangeConstants.BACKSLASH + emailType).param(
  58. "recepientEmailIds",
  59. recepientEmailIds).param("otherInfoString", otherInfoString);
  60. queue.add(taskOptions);
  61. log.info("Task for emailing added to queue : "
  62. + MAIL_QUEUE);
  63. }
  64. /**
  65. * Convert list of Strings to one comma separated String.
  66. *
  67. * @param otherInfoList list of strings
  68. * @return comma separated string
  69. */
  70. public static String getStringFromList(List<String> otherInfoList) {
  71. Iterator<String> iterator = otherInfoList.iterator();
  72. StringBuilder stringBuilder = new StringBuilder();
  73. while (iterator.hasNext()) {
  74. String info = iterator.next();
  75. stringBuilder.append(info);
  76. if (iterator.hasNext()) {
  77. stringBuilder.append(COMMA);
  78. }
  79. }
  80. return stringBuilder.toString();
  81. }
  82. }