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 */
016/*
017 * To change this template, choose Tools | Templates
018 * and open the template in the editor.
019 */
020package org.kuali.rice.krms.impl.repository.mock;
021
022import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
023import org.kuali.rice.krms.api.repository.RuleManagementService;
024import org.kuali.rice.krms.api.repository.language.NaturalLanguageUsage;
025
026/**
027 *
028 * @author nwright
029 */
030public class KrmsNaturalLanguageUsageLoader {
031
032    private RuleManagementService ruleManagementService = null;
033
034    public RuleManagementService getRuleManagementService() {
035        return ruleManagementService;
036    }
037
038    public void setRuleManagementService(RuleManagementService ruleManagementService) {
039        this.ruleManagementService = ruleManagementService;
040    }
041
042    public void loadNlUsage(String id, String name, String nameSpace, String description) {
043        NaturalLanguageUsage.Builder bldr = NaturalLanguageUsage.Builder.create(name, nameSpace);
044        bldr.setId(id);
045        bldr.setActive(true);
046        bldr.setDescription(description);
047        NaturalLanguageUsage existing = this.findExisting(bldr);
048        if (existing == null) {
049            this.getRuleManagementService().createNaturalLanguageUsage(bldr.build());
050        } else {
051            bldr.setVersionNumber(existing.getVersionNumber());
052            this.getRuleManagementService().updateNaturalLanguageUsage(bldr.build());
053        }
054    }
055
056    private NaturalLanguageUsage findExisting(NaturalLanguageUsage.Builder bldr) {
057        if (bldr.getId() != null) {
058            try {
059                return this.getRuleManagementService().getNaturalLanguageUsage(bldr.getId());
060            } catch (RiceIllegalArgumentException ex) {
061                return null;
062            }
063        }
064        return this.getRuleManagementService().getNaturalLanguageUsageByNameAndNamespace(bldr.getName(), bldr.getNamespace());
065    }
066
067    public void load() {
068        loadNlUsage("KS-KRMS-NL-USAGE-1000", "kuali.krms.edit", "KS-SYS", "Kuali Rule Edit");
069        loadNlUsage("KS-KRMS-NL-USAGE-1001", "kuali.krms.composition", "KS-SYS", "Kuali Rule Composition");
070        loadNlUsage("KS-KRMS-NL-USAGE-1002", "kuali.krms.example", "KS-SYS", "Kuali Rule Example");
071        loadNlUsage("KS-KRMS-NL-USAGE-1003", "kuali.krms.preview", "KS-SYS", "Kuali Rule Preview");
072        loadNlUsage("KS-KRMS-NL-USAGE-1004", "kuali.krms.type.description", "KS-SYS", "Kuali Rule Type Description");
073        loadNlUsage("KS-KRMS-NL-USAGE-1005", "kuali.krms.catalog", "KS-SYS", "Kuali Rule Catalog");
074        loadNlUsage("KS-KRMS-NL-USAGE-1006", "kuali.krms.type.instruction", "KS-SYS", "Kuali Rule Type Instructions");
075    }
076}