Java tutorial
/** * Copyright (C) 2013 Seajas, the Netherlands. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3, as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.seajas.search.profiler.controller; import com.seajas.search.bridge.profiler.model.feed.Feed; import com.seajas.search.profiler.model.command.FeedCommand; import com.seajas.search.profiler.model.command.FeedPagingResult; import com.seajas.search.profiler.service.profiler.ProfilerService; import com.seajas.search.profiler.service.repository.RepositoryService; import com.seajas.search.profiler.service.task.TaskService; import com.seajas.search.utilities.spring.security.model.User; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Collection; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.SortedMap; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.util.StringUtils; import org.springframework.validation.BindingResult; import org.springframework.validation.Validator; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.context.request.WebRequest; /** * Feed controller. * * @author Jasper van Veghel <jasper@seajas.com> */ @RequestMapping("/feeds.html") @Controller public class FeedController { /** * Constants. */ private static final String PARAM_PAGE = "page"; private static final String PARAM_QUERY = "query"; private static final String PARAM_RESULTS_PER_PAGE = "results"; private static final Integer MAX_RESULTS_PER_PAGE = 20; /** * Profiler service. */ @Autowired private ProfilerService profilerService; /** * Task service. */ @Autowired private TaskService taskService; /** * Repository service. */ @Autowired private RepositoryService repositoryService; /** * The validator. */ @Qualifier("feedValidator") @Autowired private Validator validator; /** * User attribute. * * @return User */ @ModelAttribute("user") public User populateUser() { return profilerService.getUser(); } /** * Data attribute. * * @param request * @return FeedPagingResult */ @ModelAttribute("data") public FeedPagingResult populateData(final WebRequest request) { String filterQuery = request.getParameter(PARAM_QUERY); // Retrieve the feed and page numbers Integer page, results; try { page = Integer.valueOf( StringUtils.hasText(request.getParameter(PARAM_PAGE)) ? request.getParameter(PARAM_PAGE) : null); } catch (NumberFormatException e) { page = 0; } try { results = Integer.valueOf(StringUtils.hasText(request.getParameter(PARAM_RESULTS_PER_PAGE)) ? request.getParameter(PARAM_RESULTS_PER_PAGE) : null); } catch (NumberFormatException e) { results = MAX_RESULTS_PER_PAGE; } return retrieveData(filterQuery, page, results); } /** * Retrieve the actual data. * * @param filterQuery * @param page * @param results * @return FeedPagingResult */ public FeedPagingResult retrieveData(final String filterQuery, final Integer page, final Integer results) { Long totalFeeds = profilerService.getFeedCount(filterQuery).longValue(); Integer actualPage = page != -1 ? page : totalFeeds == 0 ? 0 : (totalFeeds.intValue() - 1) / results; // Now retrieve the feeds List<Feed> feeds = profilerService.getPagedFeeds(actualPage * results, results, filterQuery); return new FeedPagingResult(actualPage, results, totalFeeds, feeds); } /** * Available character sets. * * @return SortedMap<String, Charset> */ @ModelAttribute("availableCharsets") public SortedMap<String, Charset> populateAvailableCharsets() { return Charset.availableCharsets(); } /** * Available collections. * * @return Map<String, String> */ @ModelAttribute("collections") public Map<String, String> getCollections() { return profilerService.getJobNames(); } /** * Available queues. * * @return Collection<String> */ @ModelAttribute("queues") public Collection<String> getQueues() { return taskService.getFeedInjectionTriggers().keySet(); } /** * Available strategies. * * @return Collection<String> */ @ModelAttribute("strategies") public Collection<String> getStrategies() { return profilerService.getAuthenticationStrategies().keySet(); } /** * Search engine languages. * * @return List<String> */ @ModelAttribute("searchLanguages") public List<String> getLanguages() { return profilerService.getAvailableSearchLanguages(); } /** * Handle the request. * * @param model * @return String */ @RequestMapping(method = RequestMethod.GET) public String handleRequest(final ModelMap model) { model.put("feedCommand", new FeedCommand()); return "feeds"; } /** * Render the submit action in the same way as a regular page view is rendered. * * @param command * @param result * @param model * @param request * @return String */ @RequestMapping(method = RequestMethod.POST) public String processSubmit(@ModelAttribute("feedCommand") final FeedCommand command, final BindingResult result, final ModelMap model, final WebRequest request) { Collection<String> feedUrls = new ArrayList<String>(); if (request.getParameterValues("feedUrls") != null) for (String feedUrl : request.getParameterValues("feedUrls")) if (StringUtils.hasText(feedUrl)) feedUrls.add(feedUrl); command.setFeedUrls(feedUrls); Map<String, String> feedResultParameters = new LinkedHashMap<String, String>(); if (request.getParameterValues("feedParameterKeys") != null) for (int i = 0; i < request.getParameterValues("feedParameterKeys").length; i++) if (StringUtils.hasText(request.getParameterValues("feedParameterKeys")[i]) || StringUtils.hasText(request.getParameterValues("feedParameterValues")[i])) feedResultParameters.put(request.getParameterValues("feedParameterKeys")[i], request.getParameterValues("feedParameterValues")[i]); command.setFeedParameterKeys(feedResultParameters.keySet()); command.setFeedParameterValues(feedResultParameters.values()); if (request.getParameter("isEnabled") == null) command.setIsEnabled(false); if (request.getParameter("isSummaryBased") == null) command.setIsSummaryBased(false); validator.validate(command, result); if (!result.hasErrors()) { Integer landingPage = 0; if (command.getAction().equals("add")) { Integer feedId = profilerService.addFeed(command.getName(), command.getDescription(), command.getCollection(), command.getFeedEncodingOverride(), command.getResultEncodingOverride(), command.getLanguage(), command.getFeedUrls(), feedResultParameters, command.getIsSummaryBased(), command.getIsEnabled()); if (feedId != null && (StringUtils.hasText(command.getQueue()) || StringUtils.hasText(command.getStrategy()))) profilerService.addFeedConnection(feedId, StringUtils.hasText(command.getQueue()) ? command.getQueue() : null, StringUtils.hasText(command.getStrategy()) ? command.getStrategy() : null); landingPage = -1; } else if (command.getAction().equals("edit")) { profilerService.modifyFeed(command.getId(), command.getName(), command.getDescription(), command.getCollection(), command.getFeedEncodingOverride(), command.getResultEncodingOverride(), command.getLanguage(), command.getFeedUrls(), feedResultParameters, command.getIsSummaryBased(), command.getIsEnabled()); // Always remove the feed connection explicitly, and then re-add it if needed profilerService.deleteFeedConnection(command.getId()); if (StringUtils.hasText(command.getQueue()) || StringUtils.hasText(command.getStrategy())) { profilerService.addFeedConnection(command.getId(), StringUtils.hasText(command.getQueue()) ? command.getQueue() : null, StringUtils.hasText(command.getStrategy()) ? command.getStrategy() : null); } // TODO: Feed anonymization settings try { landingPage = Integer.valueOf( StringUtils.hasText(request.getParameter(PARAM_PAGE)) ? request.getParameter(PARAM_PAGE) : null); } catch (NumberFormatException e) { landingPage = 0; } } else if (command.getAction().equals("delete")) { if (repositoryService.deleteResources(null, command.getId(), null, null, null)) profilerService.deleteFeed(command.getId()); landingPage = 0; } // Re-populate the data String filterQuery = request.getParameter(PARAM_QUERY); Integer results; try { results = Integer.valueOf(StringUtils.hasText(request.getParameter(PARAM_RESULTS_PER_PAGE)) ? request.getParameter(PARAM_RESULTS_PER_PAGE) : null); } catch (NumberFormatException e) { results = MAX_RESULTS_PER_PAGE; } model.put("data", retrieveData(filterQuery, landingPage, results)); command.clear(); } return "feeds"; } }