Java tutorial
/* * Copyright (c) Sergiu Giurgiu 2012. * * This file is part of TVMan. * * TVMan is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * TVMan 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 TVMan. If not, see <http://www.gnu.org/licenses/>. */ package com.zergiu.tvman.shows.search; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.Map; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.quartz.JobBuilder; import org.quartz.JobDataMap; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.Trigger; import org.quartz.TriggerBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.quartz.SchedulerFactoryBean; import org.springframework.stereotype.Service; import org.xml.sax.SAXException; import com.zergiu.tvman.entities.Configuration; import com.zergiu.tvman.entities.TVDBLanguage; import com.zergiu.tvman.entities.TVEpisodeStatus; import com.zergiu.tvman.entities.TVShow; import com.zergiu.tvman.entities.TVShowQuality; import com.zergiu.tvman.entities.TVShowStatus; import com.zergiu.tvman.services.ConfigurationService; import com.zergiu.tvman.services.TVDBLanguageService; import com.zergiu.tvman.services.TVShowService; import com.zergiu.tvman.shows.ShowProvider; import com.zergiu.tvman.shows.ShowsProviderFactoryBean; @Service(value = "showSearchService") public class AddShowFlowService { private static Logger log = LoggerFactory.getLogger(AddShowFlowService.class); public static final String SHOW_SERVICE_KEY = "showService"; public static final String SHOW_KEY = "show"; @Autowired private TVShowService tvShowService; @Autowired private SchedulerFactoryBean schedulerFactory; @Autowired private ShowsProviderFactoryBean showsProvider; @Autowired private TVDBLanguageService tvdbLanguageService; @Autowired private ConfigurationService configurationService; public Map<String, ShowProvider> getProviders() { Map<String, ShowProvider> providers = showsProvider.getProviders(); log.debug("get providers: " + providers); return providers; } public ShowProvider getDefaultProvider() { Configuration conf = configurationService.getConfigurationItem(ConfigurationService.DEFAULT_TVSHOWPROVIDER); String providerId = conf == null ? null : conf.getValue(); ShowProvider defaultProvider = null; if (providerId != null) { Map<String, ShowProvider> providers = showsProvider.getProviders(); return providers.get(providerId); } return defaultProvider; } public List<TVDBLanguage> getLanguages() { log.debug("get languages - enter"); log.debug("get languages - looking in the database to see if we have them "); List<TVDBLanguage> languages = tvdbLanguageService.getLanguages(); if (languages == null || languages.size() <= 0) { fillLanguagesTable(); languages = tvdbLanguageService.getLanguages(); } return languages; } /** * @return */ private void fillLanguagesTable() { log.debug("going to go to TVDB.com to get a list of languages"); List<TVDBLanguage> languages = new ArrayList<TVDBLanguage>(); URL url = null; try { url = new URL("http://www.thetvdb.com/api/CAC7D918A2D6A58F/languages.xml"); InputStream in = url.openStream(); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); SearchLanguagesHandler handler = new SearchLanguagesHandler(languages); parser.parse(in, handler); in.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } tvdbLanguageService.addLanguages(languages); } public List<SearchShowResult> searchShows(SearchCriteria searchCriteria) { String search = searchCriteria.getSearch(); String language = searchCriteria.getLanguage(); String providerId = searchCriteria.getProvider(); log.debug("search:" + search); log.debug("language:" + language); log.debug("provider: " + providerId); if (search == null || search.trim().length() <= 0) { return new ArrayList<SearchShowResult>(); } ShowProvider provider = showsProvider.getProviders().get(providerId); return provider.searchShow(search); } public List<TVEpisodeStatus> getStatuses() { // return Collections.(Enumeration<TVEpisodeStatus>.class); List<TVEpisodeStatus> statuses = new ArrayList<TVEpisodeStatus>(); for (TVEpisodeStatus s : TVEpisodeStatus.values()) { statuses.add(s); } return statuses; } public List<TVShowQuality> getQualities() { // return Collections.(Enumeration<TVEpisodeStatus>.class); List<TVShowQuality> qualities = new ArrayList<TVShowQuality>(); for (TVShowQuality s : TVShowQuality.values()) { qualities.add(s); } return qualities; } public void addShow(AddShowFlowResult result) { log.debug("addShow - result: " + result); if (result == null) { return; } TVShow show = createShow(result); ShowProvider showProvider = getShowProvider(show); show.setFirstAirDate(showProvider.parseFirstAirDate(result.getFirstAirDate())); show = tvShowService.addTVShow(show); scheduleShowLoaderJob(show, showProvider); // http://darthanthony.wordpress.com/2009/07/07/dynamic-scheduling-with-javaspring/ } /** * @param show * @param showProvider * @param result */ private void scheduleShowLoaderJob(TVShow show, ShowProvider showProvider) { // TODO: start the details and episode retrieval job Scheduler scheduler = schedulerFactory.getScheduler(); log.debug("starting job initialTVShowLoader_" + show.getName()); JobDataMap map = new JobDataMap(); map.put(SHOW_KEY, show); final String jobName = "initialTVShowLoader_" + show.getName() + ":" + show.getId(); final String jobGroup = "initialTVShowLoadersGroup"; JobDetail job = JobBuilder.newJob(showProvider.getTVLoadingJob()).withIdentity(jobName, jobGroup) .usingJobData(map).build(); //scheduler it for now Trigger trigger = TriggerBuilder.newTrigger().withIdentity("nowTrigger", "simpleTriggers").startNow() .build(); try { //adding the listener so we can update the show after we have retrieved all the episodes scheduler.getListenerManager().addJobListener(new InitialShowLoaderJobListener(tvShowService), new NameAndGroupJobMatcher(jobName, jobGroup)); scheduler.scheduleJob(job, trigger); } catch (SchedulerException e) { e.printStackTrace(); log.error("Scheduler problem", e); } } /** * @param show * @return */ private ShowProvider getShowProvider(TVShow show) { Map<String, ShowProvider> providers = showsProvider.getProviders(); ShowProvider showProvider = providers.get(show.getProviderId()); return showProvider; } /** * @param result * @return */ private TVShow createShow(AddShowFlowResult result) { TVShow show = new TVShow(); show.setName(result.getSeriesName()); show.setNetwork(null); show.setQuality(result.getQuality()); show.setStatus(TVShowStatus.Unkown); show.setParentFolder(result.getParentFolder()); show.setProviderSeriesId(result.getSeriesId()); show.setProviderId(result.getProviderId()); show.setInitialEpisodesStatus(result.getStatus()); show.setSeasonFolders(result.getSeasonFolders() == null ? false : result.getSeasonFolders()); show.setLanguage(result.getLanguage()); return show; } }