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.ui;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.util.ConcreteKeyValue;
020import org.kuali.rice.core.api.util.KeyValue;
021import org.kuali.rice.krad.service.KRADServiceLocator;
022import org.kuali.rice.krad.uif.control.UifKeyValuesFinderBase;
023import org.kuali.rice.krad.uif.view.ViewModel;
024import org.kuali.rice.krad.web.form.MaintenanceForm;
025import org.kuali.rice.krms.impl.repository.CategoryBo;
026
027import java.util.ArrayList;
028import java.util.Collection;
029import java.util.Collections;
030import java.util.List;
031import java.util.Map;
032
033/**
034 * Helper class that returns all category types that are valid for the given context.
035 */
036public class CategoryValuesFinder extends UifKeyValuesFinderBase {
037
038    private boolean blankOption;
039
040    /**
041     * @return the blankOption
042     */
043    public boolean isBlankOption() {
044        return this.blankOption;
045    }
046
047    /**
048     * @param blankOption the blankOption to set
049     */
050    public void setBlankOption(boolean blankOption) {
051        this.blankOption = blankOption;
052    }
053
054    @Override
055    public List<KeyValue> getKeyValues(ViewModel model) {
056        List<KeyValue> keyValues = new ArrayList<KeyValue>();
057
058        MaintenanceForm maintenanceForm = (MaintenanceForm) model;
059        AgendaEditor agendaEditor = ((AgendaEditor) maintenanceForm.getDocument().getNewMaintainableObject().getDataObject());
060
061        // if we have an agenda w/ a selected context
062        if (agendaEditor.getAgenda() != null && StringUtils.isNotBlank(agendaEditor.getAgenda().getContextId())) {
063
064            // key off the namespace
065
066            String namespace = agendaEditor.getNamespace();
067
068            if(blankOption){
069                keyValues.add(new ConcreteKeyValue("", ""));
070            }
071
072            Map<String, String> criteria = Collections.singletonMap("namespace", namespace);
073            Collection<CategoryBo> categories =
074                    KRADServiceLocator.getBusinessObjectService().findMatchingOrderBy(
075                            CategoryBo.class, criteria, "name", true
076                    );
077
078            for (CategoryBo category : categories) {
079                keyValues.add(new ConcreteKeyValue(category.getId(), category.getName()));
080            }
081        }
082
083        return keyValues;
084    }
085}