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.impl.provider.repository;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import org.kuali.rice.krms.api.repository.LogicalOperator;
022import org.kuali.rice.krms.api.repository.proposition.PropositionDefinition;
023import org.kuali.rice.krms.api.repository.proposition.PropositionType;
024import org.kuali.rice.krms.framework.engine.CompoundProposition;
025import org.kuali.rice.krms.framework.engine.Proposition;
026import org.kuali.rice.krms.framework.type.PropositionTypeService;
027
028/**
029 * An implementation of {@link PropositionTypeService} which loads a {@link CompoundProposition}
030 * from the given {@link PropositionDefinition}.  A compound proposition contains one
031 * or more propositions which are evaluated in conjunction with a logical operator
032 * such as "AND" or "OR".
033 * 
034 * <p>The proposition given to the {@link #loadProposition(PropositionDefinition)}
035 * method must be of type {@link PropositionType#COMPOUND}.
036 * 
037 * <p>The translation from a {@link PropositionDefinition} to a {@link Proposition}
038 * is performed by the given {@link RepositoryToEngineTranslator}.  This must be
039 * set on this class before it is used.
040 * 
041 * <p>This class is thread-safe and is designed to be wired as a singleton bean in
042 * Spring.
043 * 
044 * @see CompoundProposition
045 * @see RepositoryToEngineTranslator
046 * 
047 * @author Kuali Rice Team (rice.collab@kuali.org)
048 *
049 */
050public class CompoundPropositionTypeService implements PropositionTypeService {
051
052        private RepositoryToEngineTranslator translator;
053        
054        @Override
055        public Proposition loadProposition(PropositionDefinition propositionDefinition) {
056                if (translator == null) {
057                        throw new IllegalStateException("Service not configured properly, no translator available.");
058                }
059                if (propositionDefinition == null) {
060                        throw new IllegalArgumentException("propositionDefinition was null");
061                }
062                if (PropositionType.COMPOUND != PropositionType.fromCode(propositionDefinition.getPropositionTypeCode())) {
063                        throw new IllegalArgumentException("Given proposition definition was not compound, type code was: " + propositionDefinition.getPropositionTypeCode());
064                }
065                List<Proposition> propositions = new ArrayList<Proposition>();
066                for (PropositionDefinition subProp : propositionDefinition.getCompoundComponents()) {
067                        propositions.add(translator.translatePropositionDefinition(subProp));
068                }
069                LogicalOperator operator = LogicalOperator.fromCode(propositionDefinition.getCompoundOpCode());
070                return new CompoundProposition(operator, propositions);
071        }
072        
073        /**
074         * Sets the translator on this service to the given translator.
075         * 
076         * @param translator the translator to set on this service
077         */
078        public void setTranslator(RepositoryToEngineTranslator translator) {
079                this.translator = translator;
080        }
081
082}