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.type;
017
018import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
019import org.kuali.rice.krms.api.KrmsApiServiceLocator;
020import org.kuali.rice.krms.api.repository.agenda.AgendaDefinition;
021import org.kuali.rice.krms.framework.engine.Agenda;
022import org.kuali.rice.krms.framework.engine.BasicAgenda;
023import org.kuali.rice.krms.framework.type.AgendaTypeService;
024import org.kuali.rice.krms.impl.provider.repository.LazyAgendaTree;
025import org.kuali.rice.krms.impl.provider.repository.RepositoryToEngineTranslator;
026import org.kuali.rice.krms.impl.provider.repository.RepositoryToEngineTranslatorImpl;
027import org.kuali.rice.krms.impl.util.KRMSServiceLocatorInternal;
028
029import java.util.HashMap;
030import java.util.Map;
031
032/**
033 * Base class for {@link org.kuali.rice.krms.framework.type.AgendaTypeService} implementations, providing
034 * boilerplate for attribute building and merging from various sources.
035 */
036public class AgendaTypeServiceBase extends KrmsTypeServiceBase implements AgendaTypeService {
037
038    public static final AgendaTypeService defaultAgendaTypeService = new AgendaTypeServiceBase();
039    private static final String NAME_ATTRIBUTE = "name";
040
041    @Override
042    public Agenda loadAgenda(AgendaDefinition agendaDefinition) {
043
044        if (agendaDefinition == null) { throw new RiceIllegalArgumentException("agendaDefinition must not be null"); }
045        if (getRepositoryToEngineTranslator() == null) {
046            return null;
047        }
048
049        // pass the name as a built-in attribute so that it can be used during selection
050        Map<String, String> existingAttributes = new HashMap<String, String>(agendaDefinition.getAttributes());
051        existingAttributes.put(NAME_ATTRIBUTE, agendaDefinition.getName());
052
053        return new BasicAgenda(existingAttributes, new LazyAgendaTree(agendaDefinition, getRepositoryToEngineTranslator()));
054    }
055
056    private RepositoryToEngineTranslator getRepositoryToEngineTranslator() {
057        return RepositoryToEngineTranslatorHolder.instance;
058    }
059
060    // Lazy initialization holder class, see Effective Java item #71
061    private static class RepositoryToEngineTranslatorHolder {
062        static final RepositoryToEngineTranslator instance = KRMSServiceLocatorInternal.getRepositoryToEngineTranslator();
063    }
064}