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