/* * Copyright 2005 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.wamblee.crawler.kiss.main; import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wamblee.crawler.kiss.guide.Program; import org.wamblee.crawler.kiss.guide.TimeInterval; import org.wamblee.crawler.kiss.guide.Program.RecordingResult; /** * Provides execution of actions for programs. Actions use this class to tell * the executor what to do. The executor then decides on exactly what to do and * in what order and makes decisions in case of conflicts. */ public class ProgramActionExecutor { private static final Log LOG = LogFactory .getLog(ProgramActionExecutor.class); /** * Map of priority to set of programs. */ private Map> _showsToRecord; /** * Report to use. */ private Report _report; /** * Constructs the program action executor. * * @param aReport Report to use. */ public ProgramActionExecutor(Report aReport) { _showsToRecord = new TreeMap>(); _report = aReport; } /** * Called by an action to indicate the desire to record a program. * * @param aPriority * Priority of the program. Used to resolve conflicts. * @param aProgram * Program to record. */ public void recordProgram(int aPriority, Program aProgram) { LOG.info("priority = " + aPriority + ", program: " + aProgram); // Putting -priority into the set makes sure that iteration order // over the priorities will go from higher priority to lower priority. Set programs = _showsToRecord.get(-aPriority); if (programs == null) { programs = new TreeSet(new Program.TimeComparator()); _showsToRecord.put(-aPriority, programs); } programs.add(aProgram); } /** * Called by an action to indicate that a program is interesting. * * @param aCategory * Category of the program. * @param aProgram * Program. */ public void interestingProgram(String aCategory, Program aProgram) { LOG.info("category = '" + aCategory + "', program: " + aProgram); _report.interestingProgram(aCategory, aProgram); } /** * Makes sure that the actions are performed. */ public void commit() { Set previouslyRecorded = new HashSet(); for (Integer priority : _showsToRecord.keySet()) { for (Program program : _showsToRecord.get(priority)) { TimeInterval interval = program.getInterval(); if (recordingConflictExists(previouslyRecorded, interval)) { _report.setRecordingResult(RecordingResult.CONFLICT, program); } else { RecordingResult result = program.record(); _report.setRecordingResult(result, program); previouslyRecorded.add(interval); } } } } /** * Checks an interval for overlap with a previously recorded program. * * @param aPreviouslyRecorded * Previously recorded programs. * @param aInterval * Interval. * @return True iff there is a recording conflict. */ private boolean recordingConflictExists( Set aPreviouslyRecorded, TimeInterval aInterval) { for (TimeInterval recordedInterval : aPreviouslyRecorded) { if (aInterval.overlap(recordedInterval)) { return true; } } return false; } }