PageRenderTime 61ms CodeModel.GetById 25ms app.highlight 27ms RepoModel.GetById 2ms app.codeStats 0ms

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