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.kew.actionlist;
017
018import java.util.ArrayList;
019import java.util.Collections;
020import java.util.List;
021import org.kuali.rice.core.api.util.ConcreteKeyValue;
022import org.kuali.rice.core.api.util.KeyValue;
023import org.kuali.rice.kew.api.KewApiConstants;
024import org.kuali.rice.kim.api.group.Group;
025import org.kuali.rice.kim.api.services.KimApiServiceLocator;
026import org.kuali.rice.krad.keyvalues.KeyValuesBase;
027import org.kuali.rice.krad.uif.control.UifKeyValuesFinderBase;
028import org.kuali.rice.krad.uif.view.ViewModel;
029import org.kuali.rice.kew.actionlist.ActionListForm;
030
031public class UserWorkgroupsKeyValues extends UifKeyValuesFinderBase {
032
033    private boolean blankOption;
034
035
036    @Override
037    public List<KeyValue> getKeyValues(ViewModel model) {
038        ActionListForm actionListForm = (ActionListForm)model;
039        List<String> userWorkgroups =
040                KimApiServiceLocator.getGroupService().getGroupIdsByPrincipalId(actionListForm.getUser());
041
042        //note that userWorkgroups is unmodifiable so we need to create a new list that we can sort
043        List<String> userGroupsToSort = new ArrayList<String>(userWorkgroups);
044
045        List<KeyValue> sortedUserWorkgroups = new ArrayList<KeyValue>();
046        KeyValue keyValue = null;
047        keyValue = new ConcreteKeyValue(KewApiConstants.NO_FILTERING, KewApiConstants.NO_FILTERING);
048        sortedUserWorkgroups.add(keyValue);
049
050        if (userGroupsToSort != null && userGroupsToSort.size() > 0) {
051            Collections.sort(userGroupsToSort);
052            Group group;
053
054            for (String groupId : userGroupsToSort)
055            {
056                group = KimApiServiceLocator.getGroupService().getGroup(groupId);
057                keyValue = new ConcreteKeyValue(groupId, group.getName());
058                sortedUserWorkgroups.add(keyValue);
059            }
060        }
061
062        return sortedUserWorkgroups;
063    }
064
065    /**
066     * @return the blankOption
067     */
068    public boolean isBlankOption() {
069        return this.blankOption;
070    }
071
072    /**
073     * @param blankOption the blankOption to set
074     */
075    public void setBlankOption(boolean blankOption) {
076        this.blankOption = blankOption;
077    }
078
079//    private List<? extends KeyValue> getUserWorkgroupsDropDownList(String principalId) {
080//        List<String> userWorkgroups =
081//                KimApiServiceLocator.getGroupService().getGroupIdsByPrincipalId(principalId);
082//
083//        //note that userWorkgroups is unmodifiable so we need to create a new list that we can sort
084//        List<String> userGroupsToSort = new ArrayList<String>(userWorkgroups);
085//
086//        List<KeyValue> sortedUserWorkgroups = new ArrayList<KeyValue>();
087//        KeyValue keyValue = null;
088//        keyValue = new ConcreteKeyValue(KewApiConstants.NO_FILTERING, KewApiConstants.NO_FILTERING);
089//        sortedUserWorkgroups.add(keyValue);
090//        if (userGroupsToSort != null && userGroupsToSort.size() > 0) {
091//            Collections.sort(userGroupsToSort);
092//
093//            Group group;
094//            for (String groupId : userGroupsToSort)
095//            {
096//                group = KimApiServiceLocator.getGroupService().getGroup(groupId);
097//                keyValue = new ConcreteKeyValue(groupId, group.getName());
098//                sortedUserWorkgroups.add(keyValue);
099//            }
100//        }
101//        return sortedUserWorkgroups;
102//    }
103
104
105}