001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krms.api.engine;
017
018import java.util.Collections;
019import java.util.HashMap;
020import java.util.Map;
021
022import org.joda.time.DateTime;
023
024/**
025 * SelectionCritera are used to to select an {@link Agenda} to execute.
026 * @author Kuali Rice Team (rice.collab@kuali.org)
027 */
028public final class SelectionCriteria {
029
030        private final Long effectiveExecutionTime;
031        private final Map<String, String> contextQualifiers;
032        private final Map<String, String> agendaQualifiers;
033
034    /**
035     * Private constructor {@see SelectionCriteria createCriteria}
036     * @param effectiveDate DateTime to use for constructing the SelectionCriteria
037     */
038        private SelectionCriteria(DateTime effectiveDate) {
039                if (effectiveDate != null) {
040                        this.effectiveExecutionTime = effectiveDate.getMillis();
041                } else {
042                        this.effectiveExecutionTime = null;
043                }
044                
045                this.contextQualifiers = new HashMap<String, String>();
046                this.agendaQualifiers = new HashMap<String, String>();
047        }
048
049        /**
050         * This static factory method creates a SelectionCriteria used to select an Agenda to execute.
051         * 
052         * @param effectiveExecutionTime the time that the rule is being executed at.  If null, the time of engine execution will be used.
053         * @param contextQualifiers qualifiers used to select the context
054         * @param agendaQualifiers qualifiers used to select the agenda from the context
055         * @return the {@link SelectionCriteria}
056         */
057        public static SelectionCriteria createCriteria(DateTime effectiveExecutionTime, Map<String, String> contextQualifiers, Map<String, String> agendaQualifiers) {
058                SelectionCriteria criteria = new SelectionCriteria(effectiveExecutionTime);
059        if (contextQualifiers != null) {
060                    criteria.contextQualifiers.putAll(contextQualifiers);
061        }
062        if (agendaQualifiers != null) {
063                    criteria.agendaQualifiers.putAll(agendaQualifiers);
064        }
065                return criteria;
066        }
067
068        /**
069         * This method gets the effective date/time in epoch time, suitable for 
070         * converting to a {@link java.util.Date} via {@link java.util.Date#Date(long)}
071         * @return the epoch time for effective execution, or null 
072         * (which defers to the {@link Engine} but implies that the actual time when execution begins will be used).
073         */
074        public Long getEffectiveExecutionTime() {
075                return effectiveExecutionTime;
076        }
077
078    /**
079     * @return the map of context qualifiers.  May be empty, will never be null.
080     */
081        public Map<String, String> getContextQualifiers() {
082                return Collections.unmodifiableMap(contextQualifiers);
083        }
084
085    /**
086     * @return the map of agenda qualifiers.  May be empty, will never be null.
087     */
088        public Map<String, String> getAgendaQualifiers() {
089                return Collections.unmodifiableMap(agendaQualifiers);
090        }
091
092}