/src/main/java/com/google/ie/web/controller/ObjectionableController.java

http://thoughtsite.googlecode.com/ · Java · 201 lines · 114 code · 20 blank · 67 comment · 18 complexity · b1bb023df1818372919223c8cba719f7 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.web.controller;
  16. import com.google.ie.business.domain.BadWord;
  17. import com.google.ie.business.domain.EntityIndex;
  18. import com.google.ie.business.domain.Idea;
  19. import com.google.ie.business.service.IdeaService;
  20. import com.google.ie.common.objectionable.ObjectionableManager;
  21. import com.google.ie.common.taskqueue.TagWeightUpdationManager;
  22. import com.google.ie.common.util.ClassUtility;
  23. import com.google.ie.common.util.SearchUtility;
  24. import com.google.ie.dto.SearchResult;
  25. import org.apache.commons.lang.StringUtils;
  26. import org.apache.log4j.Logger;
  27. import org.compass.annotations.SearchableProperty;
  28. import org.springframework.beans.factory.annotation.Autowired;
  29. import org.springframework.stereotype.Controller;
  30. import org.springframework.web.bind.annotation.PathVariable;
  31. import org.springframework.web.bind.annotation.RequestMapping;
  32. import java.lang.annotation.Annotation;
  33. import java.lang.reflect.Field;
  34. /**
  35. * A controller that handles request for checking objectionable content.
  36. *
  37. * @author gmaurya
  38. *
  39. */
  40. @RequestMapping("/objectionable")
  41. @Controller
  42. public class ObjectionableController {
  43. private static final Logger LOG = Logger.getLogger(ObjectionableController.class);
  44. @Autowired
  45. private IdeaService ideaService;
  46. @Autowired
  47. private TagWeightUpdationManager weightUpdationManager;
  48. /**
  49. * Handles the request for checking objectionable content.
  50. * This request is initiated by TaskQueue for checking objectionable content
  51. * of ideas and it's started worker TaskQueue for checking objectionable
  52. * content of different idea attribute.
  53. *
  54. * @param key the key of the {@link EntityIndex} entity to be indexed
  55. * @return the name of the resource to which the request should be forwarded
  56. */
  57. @RequestMapping("/check/{key}")
  58. public String checkObjectionable(@PathVariable String key) {
  59. LOG.debug("Checking Objectionable content for idea having key: " + key);
  60. if (key != null && key.trim().length() > WebConstants.ZERO) {
  61. Field[] fields = Idea.class.getDeclaredFields();
  62. for (Field field : fields) {
  63. Annotation[] annotations = field.getDeclaredAnnotations();
  64. for (Annotation annotation : annotations) {
  65. if (annotation.annotationType() == SearchableProperty.class) {
  66. ObjectionableManager.startCheckObjectionableWorker(key,
  67. field.getName());
  68. break;
  69. }
  70. }
  71. }
  72. }
  73. return "queue/queue";
  74. }
  75. /**
  76. * Handles the request for checking objectionable content.
  77. * This is a worker TaskQueue for checking objectionable
  78. * content of different idea attribute.
  79. *
  80. * @param key the key of {@link Idea} object to checked
  81. * @param fieldName the name of the field of the {@link Idea} object to
  82. * checked
  83. * @return the name of the resource to which the request should be forwarded
  84. */
  85. @RequestMapping("/worker/{key}/{fieldName}")
  86. public String objectionableWoker(@PathVariable String key, @PathVariable String fieldName) {
  87. LOG.debug("Checking Objectionable content for idea attribute having key: " + key
  88. + " and for attribute: " + fieldName);
  89. Idea idea = new Idea();
  90. idea.setKey(key);
  91. try {
  92. idea = getIdeaService().getIdeaDetails(idea);
  93. if (!isIdeaObjectionable(idea)) {
  94. String query = getContent(idea, fieldName);
  95. if (query != null) {
  96. SearchResult searchResult = SearchUtility.search(query, true, true, "word",
  97. WebConstants.ZERO,
  98. WebConstants.ONE,
  99. BadWord.class.getSimpleName());
  100. if (searchResult.getTotalCount() > WebConstants.ZERO) {
  101. LOG.debug("Marking Idea as objectionable having key: " + key);
  102. idea.setStatus(Idea.STATUS_OBJECTIONABLE);
  103. ideaService.updateStatus(idea);
  104. SearchUtility.deleteEntityIndex(idea);
  105. /*
  106. * Remove this idea from popular,recently picked and
  107. * recent ideas lists in cache
  108. */
  109. ideaService.removeIdeaFromAllListsInCache(idea.getKey());
  110. /* Decrement Tags weights asynchronously. */
  111. if (!StringUtils.isBlank(idea.getTags())) {
  112. getWeightUpdationManager().decrementWeight(idea.getTags());
  113. }
  114. }
  115. }
  116. } else {
  117. LOG.debug("Idea is already mark as objectionable having key: " + key);
  118. }
  119. } catch (Exception e) {
  120. LOG.error(
  121. "Error occure during checking objectionable content for idea having : "
  122. + key,
  123. e);
  124. }
  125. return "queue/queue";
  126. }
  127. /**
  128. * Check for idea that it's already marked as objectionable.
  129. *
  130. * @param idea the {@link Idea} object to checked for objectionable
  131. * @return boolean whether the {@link Idea} object is already marked
  132. * objectionable
  133. */
  134. private boolean isIdeaObjectionable(Idea idea) {
  135. boolean objectionable = false;
  136. if (idea == null)
  137. return true;
  138. String status = idea.getStatus();
  139. if (status != null && status.equalsIgnoreCase(Idea.STATUS_OBJECTIONABLE)) {
  140. objectionable = true;
  141. }
  142. return objectionable;
  143. }
  144. /**
  145. * This method fetch the value of different attribute of Idea
  146. * object on the basis for fieldType parameter.
  147. *
  148. * @param idea the {@link Idea} object
  149. * @param fieldType fieldType
  150. * @return String attribute content based on field type.
  151. */
  152. private String getContent(Idea idea, String fieldName) {
  153. String content = null;
  154. try {
  155. content = (String) ClassUtility.getObject(idea, fieldName);
  156. } catch (Exception e) {
  157. LOG.error("Error occure during getting value from idea for field : " + fieldName);
  158. }
  159. return content;
  160. }
  161. public TagWeightUpdationManager getWeightUpdationManager() {
  162. return weightUpdationManager;
  163. }
  164. public void setWeightUpdationManager(TagWeightUpdationManager weightUpdationManager) {
  165. this.weightUpdationManager = weightUpdationManager;
  166. }
  167. /**
  168. *
  169. * @return IdeaService
  170. */
  171. public IdeaService getIdeaService() {
  172. return ideaService;
  173. }
  174. /**
  175. *
  176. * @param ideaService IdeaService
  177. */
  178. public void setIdeaService(IdeaService ideaService) {
  179. this.ideaService = ideaService;
  180. }
  181. }