001    package org.kuali.rice.krms.tree;
002    
003    import org.apache.commons.lang.StringEscapeUtils;
004    import org.apache.commons.lang.StringUtils;
005    import org.kuali.rice.krms.api.repository.NaturalLanguageTree;
006    import org.kuali.rice.krms.api.repository.RuleManagementService;
007    import org.kuali.rice.krms.api.repository.language.NaturalLanguageUsage;
008    import org.kuali.rice.krms.api.repository.proposition.PropositionDefinition;
009    import org.kuali.rice.krms.api.repository.proposition.PropositionDefinitionContract;
010    import org.kuali.rice.krms.api.repository.rule.RuleDefinitionContract;
011    import org.kuali.rice.krms.dto.PropositionEditor;
012    import org.kuali.rice.krms.dto.RuleEditor;
013    import org.kuali.student.enrollment.class1.krms.dto.EnrolPropositionEditor;
014    import org.kuali.student.enrollment.class1.krms.dto.EnrolRuleEditor;
015    import org.kuali.student.enrollment.class2.courseoffering.service.decorators.PermissionServiceConstants;
016    import org.kuali.student.krms.naturallanguage.util.KsKrmsConstants;
017    
018    import java.io.Serializable;
019    import java.util.Iterator;
020    import java.util.LinkedList;
021    import java.util.Queue;
022    
023    /**
024     * Created with IntelliJ IDEA.
025     * User: SW
026     * Date: 2013/02/25
027     * Time: 2:12 PM
028     * To change this template use File | Settings | File Templates.
029     */
030    public abstract class AbstractTreeBuilder implements TreeBuilder {
031    
032        private RuleManagementService ruleManagementService;
033    
034        public RuleManagementService getRuleManagementService() {
035            return ruleManagementService;
036        }
037    
038        public void setRuleManagementService(RuleManagementService ruleManagementService) {
039            this.ruleManagementService = ruleManagementService;
040        }
041    
042        protected String buildNodeLabel(RuleDefinitionContract rule, PropositionDefinitionContract prop){
043            return StringEscapeUtils.escapeHtml(this.getDescription(prop));
044        }
045    
046        protected String getDescription(PropositionDefinitionContract proposition) {
047            if (proposition == null) {
048                return StringUtils.EMPTY;
049            }
050    
051            //Return translated nl if exist.
052            if (proposition instanceof PropositionEditor){
053                PropositionEditor editor = (PropositionEditor) proposition;
054                return editor.getNaturalLanguageForUsage(this.getNaturalLanguageUsageKey());
055            }
056    
057            //Return the description
058            if (proposition.getDescription() != null){
059                return proposition.getDescription();
060            }
061    
062            return StringUtils.EMPTY;
063        }
064    
065        protected String getPropositionPrefix(RuleEditor rule, PropositionEditor prop){
066            if (rule == null){
067                return StringUtils.EMPTY;
068            }
069    
070            //Add the proposition with alpha code in the map if it doesn't already exist.
071            if (null == prop.getKey()) {
072                prop.setKey((String) rule.getAlpha().next());
073            }
074    
075            //Build the prefix.
076            return "<b>" + prop.getKey() + ".</b> ";
077        }
078    
079        public String getNaturalLanguageUsageKey(){
080            return  KsKrmsConstants.KRMS_NL_PREVIEW;
081        }
082    
083    }