PageRenderTime 26ms CodeModel.GetById 11ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://thoughtsite.googlecode.com/
Java | 266 lines | 163 code | 20 blank | 83 comment | 39 complexity | fb9c0805ad15d5de258a2b58cc6bf788 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.Tag;
 19import com.google.ie.business.domain.User;
 20import com.google.ie.business.service.TagService;
 21import com.google.ie.common.constants.IdeaExchangeConstants;
 22import com.google.ie.common.util.SearchUtility;
 23import com.google.ie.dto.RetrievalInfo;
 24import com.google.ie.dto.SearchResult;
 25import com.google.ie.dto.ViewStatus;
 26
 27import org.apache.log4j.Logger;
 28import org.springframework.beans.factory.annotation.Autowired;
 29import org.springframework.stereotype.Controller;
 30import org.springframework.web.bind.annotation.ModelAttribute;
 31import org.springframework.web.bind.annotation.PathVariable;
 32import org.springframework.web.bind.annotation.RequestMapping;
 33import org.springframework.web.bind.annotation.RequestParam;
 34import org.springframework.web.bind.annotation.SessionAttributes;
 35
 36import java.util.Collections;
 37import java.util.HashMap;
 38import java.util.List;
 39import java.util.Map;
 40
 41import javax.servlet.http.HttpSession;
 42
 43/**
 44 * A controller that handles request for idea tags.
 45 * 
 46 * @author Sachneet
 47 * 
 48 */
 49@Controller
 50@RequestMapping("/tags")
 51@SessionAttributes("user")
 52public class TagController {
 53    private static Logger logger = Logger.getLogger(TagController.class);
 54    @Autowired
 55    private TagService tagService;
 56
 57    @RequestMapping("/list")
 58    public String showTags() {
 59        // List<Tag> tags = tagService.getTagsForAutoSuggestion(initial,
 60        // retrievalInfo);
 61        return "tags/list";
 62    }
 63
 64    /**
 65     * Get tags for auto-suggestion
 66     * 
 67     * @param initial of the tag
 68     * @param retrievalInfo the {@link RetrievalInfo} object containing the
 69     *        query parameters
 70     * @param model the map object
 71     */
 72    @RequestMapping("/suggest/{initial}")
 73    public void showSimilarTags(@PathVariable String initial,
 74                    @ModelAttribute RetrievalInfo retrievalInfo,
 75                    Map<String, Object> model) {
 76        List<Tag> tags = tagService.getTagsForAutoSuggestion(initial,
 77                        retrievalInfo);
 78        ViewStatus viewStatus = ViewStatus
 79                        .createTheViewStatus(tags, WebConstants.TAGS, null);
 80        /* Remove the RetrievalInfo object from model */
 81        if (model.containsKey("retrievalInfo")) {
 82            model.remove("retrievalInfo");
 83        }
 84        /* Put the ViewStatus object containing the tag data into the model */
 85        model.put(WebConstants.VIEW_STATUS, viewStatus);
 86    }
 87
 88    /**
 89     * Get tags starting with a specific string
 90     * 
 91     * @param startString
 92     * @param retrievalInfo the {@link RetrievalInfo} object containing the
 93     *        query parameters
 94     * @param model the map object
 95     */
 96    @RequestMapping("/list/{startString}")
 97    public void showTagsStaringWithSpecificString(@PathVariable String startString,
 98                    @ModelAttribute RetrievalInfo retrievalInfo,
 99                    Map<String, Object> model) {
100        List<Tag> tags = tagService.getTagsWithSpecificStartString(startString.toLowerCase(),
101                        retrievalInfo);
102        Map<String, Object> parameters = new HashMap<String, Object>();
103        if (tags != null && tags.size() > WebConstants.ZERO) {
104            parameters.put(WebConstants.WEIGHTS, getTheMaxAndMinWeights(tags));
105        }
106        ViewStatus viewStatus = ViewStatus.createTheViewStatus(tags, WebConstants.TAGS,
107                        parameters);
108        /* Remove the RetrievalInfo object from model */
109        if (model.containsKey("retrievalInfo")) {
110            model.remove("retrievalInfo");
111        }
112        /* Put the ViewStatus object containing the tag data into the model */
113        model.put(WebConstants.VIEW_STATUS, viewStatus);
114    }
115
116    /**
117     * Displays tag cloud based on the retrieval parameters.
118     * 
119     * @param retrievalInfo the {@link RetrievalInfo} object containing the
120     *        query parameters
121     * 
122     * @param model the map object
123     */
124    @RequestMapping("/tagcloud")
125    public void showTagCloud(@ModelAttribute RetrievalInfo retrievalInfo,
126                    Map<String, Object> model) {
127        List<Tag> tags = tagService.getTagsForTagCloud(retrievalInfo);
128        /*
129         * The second argument is true so that a map containing max/min weight
130         * is also added as data
131         */
132        Map<String, Object> parameters = new HashMap<String, Object>();
133        if (tags != null && tags.size() > WebConstants.ZERO) {
134            parameters.put(WebConstants.WEIGHTS, getTheMaxAndMinWeights(tags));
135        }
136        ViewStatus viewStatus = ViewStatus.createTheViewStatus(tags, WebConstants.TAGS,
137                        parameters);
138        /* Remove the RetrievalInfo object from model */
139        if (model.containsKey("retrievalInfo")) {
140            model.remove("retrievalInfo");
141        }
142        /* Put the ViewStatus object containing the tag data into the model */
143        model.put(WebConstants.VIEW_STATUS, viewStatus);
144    }
145
146    /**
147     * Displays the tag cloud for the specified user
148     * 
149     * @param user the {@link User} whose cloud to be displayed
150     * @param model the map object
151     */
152    @RequestMapping("/my.json")
153    public void showMyTagCloud(HttpSession session, Map<String, Object> model) {
154        User user = (User) session.getAttribute(WebConstants.USER);
155        List<Tag> myTags = tagService.getMyTagCloud(user);
156        Map<String, Object> parameters = null;
157        if (myTags != null && myTags.size() > WebConstants.ZERO) {
158            parameters = new HashMap<String, Object>();
159            parameters.put(WebConstants.WEIGHTS, getTheMaxAndMinWeights(myTags));
160        }
161        ViewStatus viewStatus = ViewStatus.createTheViewStatus(myTags, WebConstants.TAGS,
162                        parameters);
163        model.put(WebConstants.VIEW_STATUS, viewStatus);
164        if (model.containsKey(WebConstants.USER)) {
165            model.remove(WebConstants.USER);
166        }
167    }
168
169    @RequestMapping("/incrementweight")
170    public String incrementWeights(@RequestParam String tagString) {
171        if (tagString == null || tagString.equals("")) {
172            logger.warn("No tag found in query string to increment the weight.");
173        } else {
174            List<Tag> tagList = tagService.incrementWeights(tagString);
175            if (tagList == null || tagList.size() == 0) {
176                logger.warn("No tag weight increment occured.");
177            }
178        }
179        return "queue/queue";
180    }
181
182    /**
183     * Decrement the weight for the tag.
184     * 
185     * @param tagString for which weight to be decremented
186     * @return
187     */
188    @RequestMapping("/decrementweight")
189    public String decrementWeights(@RequestParam String tagString) {
190        if (tagString == null || tagString.equals("")) {
191            logger.warn("No tag found in query string to decrement the weight.");
192        } else {
193            List<Tag> tagList = tagService.decrementWeights(tagString);
194            if (tagList == null || tagList.size() == 0) {
195                logger.warn("No tag weight decrement occured.");
196            }
197        }
198        return "queue/queue";
199    }
200
201    /**
202     * Searches for a tag.
203     * 
204     * @param offset of the search result
205     * @param keyword for which tag to be searched
206     * @param model the map object
207     */
208    @SuppressWarnings("unchecked")
209    @RequestMapping("/search")
210    public void searchTag(@RequestParam(required = false) String offset,
211                    @RequestParam(required = false) String keyword,
212                    Map<String, Object> model) {
213        int offsetToSearch = 0;
214        if (offset != null && offset.trim().length() > 0) {
215            try {
216                offsetToSearch = Integer.parseInt(offset);
217            } catch (Exception e) {
218                // no need to throw number format exception.
219            }
220        }
221        keyword = WebConstants.ASTERISK + keyword + WebConstants.ASTERISK;
222        ViewStatus viewStatus = new ViewStatus();
223        SearchResult searchResult = SearchUtility.search(keyword, true, "title", offsetToSearch,
224                        IdeaExchangeConstants.DEFAULT_PAGE_SIZE, Tag.class.getSimpleName());
225        List<Tag> tags = null;
226        if (searchResult != null && searchResult.getTotalCount() > 0) {
227            tags = (List<Tag>) searchResult.getData();
228            tags = tagService.removeTagWithZeroWeight(tags);
229        }
230        if (tags != null && tags.size() > WebConstants.ZERO) {
231            viewStatus.setStatus(WebConstants.SUCCESS);
232            viewStatus.addData(WebConstants.TAGS, tags);
233        } else {
234            viewStatus.setStatus(WebConstants.ERROR);
235            viewStatus.addMessage(WebConstants.ERROR, WebConstants.NO_TAGS_FOUND);
236        }
237
238        model.put(WebConstants.VIEW_STATUS, viewStatus);
239    }
240
241    public TagService getTagService() {
242        return tagService;
243    }
244
245    public void setTagService(TagService tagService) {
246        this.tagService = tagService;
247    }
248
249    /**
250     * Return a map containing the maximum and minimum weights of the tags in
251     * the tag cloud data received as list
252     * 
253     * @param tags the list of tags to be searched for max and min weights
254     * @return a map containing the maximum and minimum weights
255     */
256    private Map<String, Long> getTheMaxAndMinWeights(List<Tag> tags) {
257        long min = Collections.min(tags).getWeightage();
258        long max = Collections.max(tags).getWeightage();
259        Map<String, Long> mapOfMinMax = new HashMap<String, Long>();
260        mapOfMinMax.put(WebConstants.MAX_WEIGHT, max);
261        mapOfMinMax.put(WebConstants.MIN_WEIGHT, min);
262
263        return mapOfMinMax;
264    }
265}
266