001/**
002 * Copyright 2005-2017 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.ui;
017
018import org.kuali.rice.krad.data.DataObjectService;
019import org.kuali.rice.krad.service.KRADServiceLocator;
020import org.kuali.rice.krms.api.repository.proposition.PropositionParameterType;
021import org.kuali.rice.krms.api.repository.proposition.PropositionType;
022import org.kuali.rice.krms.impl.repository.PropositionBo;
023import org.kuali.rice.krms.impl.repository.PropositionParameterBo;
024import org.kuali.rice.krms.impl.repository.TermBo;
025
026import java.util.List;
027
028/**
029 * abstract data class for the rule tree {@link Node}s
030 * 
031 * @author Kuali Rice Team (rice.collab@kuali.org)
032 *
033 */
034public class SimplePropositionNode extends RuleTreeNode {
035    private static final long serialVersionUID = -629088492909384804L;
036    public static final String NODE_TYPE = "ruleTreeNode simplePropositionNode";
037    protected String parameterDisplayString;
038
039    // needed for inquiry view
040    public SimplePropositionNode() {
041    }
042
043    public SimplePropositionNode(PropositionBo proposition){
044        super(proposition);
045        setupParameterDisplayString();
046    }
047    
048    private void setupParameterDisplayString(){
049        if (proposition != null && proposition.getPropositionTypeCode().equalsIgnoreCase(PropositionType.SIMPLE.getCode())){
050            // Simple Propositions should have 3 parameters ordered in reverse polish notation.
051            // TODO: enhance to get term names for term type parameters.
052            List<PropositionParameterBo> parameters = proposition.getParameters();
053            if (parameters != null && parameters.size() == 3){
054                setParameterDisplayString(getParamValue(parameters.get(0)) 
055                        + " " + getParamValue(parameters.get(2))
056                        + " " + getParamValue(parameters.get(1)));
057            } else {
058                // should not happen
059            }
060        }
061    }
062    
063    private String getParamValue(PropositionParameterBo prop){
064        if (PropositionParameterType.TERM.getCode().equalsIgnoreCase(prop.getParameterType())){
065            //TODO: use termBoService
066            String termId = prop.getValue();
067            TermBo term = null;
068            if (termId!=null) {
069                term = getDataObjectService().find(TermBo.class,termId);
070            }
071            if (term!=null){
072                return term.getSpecification().getName();
073            }
074            return "";
075        } else {
076            return prop.getValue();
077        }
078    }
079    /**
080     * @return the parameterDisplayString
081     */
082    public String getParameterDisplayString() {
083        return this.parameterDisplayString;
084    }
085
086    /**
087     * @param parameterDisplayString the parameterDisplayString to set
088     */
089    public void setParameterDisplayString(String parameterDisplayString) {
090        this.parameterDisplayString = parameterDisplayString;
091    }
092
093    public DataObjectService getDataObjectService() {
094        return KRADServiceLocator.getDataObjectService();
095    }
096    
097}