001    package org.kuali.rice.krms.keyvalues;
002    
003    import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
004    import org.kuali.rice.krms.api.KrmsConstants;
005    import org.kuali.rice.krms.api.repository.RuleManagementService;
006    import org.kuali.rice.krms.api.repository.language.NaturalLanguageTemplate;
007    import org.kuali.rice.krms.api.repository.language.NaturalLanguageUsage;
008    import org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService;
009    import org.kuali.rice.krms.api.repository.typerelation.TypeTypeRelation;
010    import org.kuali.rice.krms.impl.repository.KrmsRepositoryServiceLocator;
011    import org.kuali.rice.krms.impl.repository.NaturalLanguageTemplateBoService;
012    import org.kuali.rice.krms.impl.repository.NaturalLanguageUsageBoService;
013    import org.kuali.rice.krms.impl.repository.TypeTypeRelationBoService;
014    import org.kuali.student.enrollment.class2.courseoffering.service.decorators.PermissionServiceConstants;
015    import org.kuali.student.krms.naturallanguage.util.KsKrmsConstants;
016    
017    import javax.xml.namespace.QName;
018    import java.io.Serializable;
019    import java.util.HashMap;
020    import java.util.List;
021    import java.util.Map;
022    import java.util.Set;
023    
024    public class RuleInstructionKeyValues implements Serializable {
025    
026        private static final long serialVersionUID = 1L;
027    
028        private transient RuleManagementService ruleManagementService;
029    
030    
031        public Map<String, String> getKeyValues(Set<String> ruleTypeIds) {
032            Map<String, String> keyValuesInstruction = new HashMap<String, String>();
033            NaturalLanguageUsage usageInstruction = getRuleManagementService().getNaturalLanguageUsageByNameAndNamespace(PermissionServiceConstants.KS_SYS_NAMESPACE, KsKrmsConstants.KRMS_NL_TYPE_INSTRUCTION);
034             //Use Type Type Relation to get Rule Types
035            try {
036                for(String ruleTypeId : ruleTypeIds) {
037                    NaturalLanguageTemplate templateInstruction = null;
038                    try{
039                        templateInstruction = getRuleManagementService().findNaturalLanguageTemplateByLanguageCodeTypeIdAndNluId("en", ruleTypeId, usageInstruction.getId());
040                    }catch (IndexOutOfBoundsException e){
041                        //Ignore, rice error in NaturalLanguageTemplateBoServiceImpl line l
042                    }
043    
044                    if (templateInstruction != null) {
045                        keyValuesInstruction.put(ruleTypeId, templateInstruction.getTemplate());
046                    } else {
047                        keyValuesInstruction.put(ruleTypeId, KsKrmsConstants.KRMS_NL_TYPE_INSTRUCTION);
048                    }
049                };
050            } catch (Exception ex) {}
051            return keyValuesInstruction;
052        }
053    
054        public RuleManagementService getRuleManagementService() {
055            if (ruleManagementService == null) {
056                ruleManagementService = (RuleManagementService) GlobalResourceLoader.getService(new QName(KrmsConstants.Namespaces.KRMS_NAMESPACE_2_0, "ruleManagementService"));
057            }
058            return ruleManagementService;
059        }
060    }