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.kim.rules.ui;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.util.RiceKeyConstants;
020import org.kuali.rice.kim.api.KimConstants;
021import org.kuali.rice.kim.bo.ui.PersonDocumentGroup;
022import org.kuali.rice.kim.document.IdentityManagementPersonDocument;
023import org.kuali.rice.kim.rule.event.ui.AddGroupEvent;
024import org.kuali.rice.kim.rule.ui.AddGroupRule;
025import org.kuali.rice.kns.rules.DocumentRuleBase;
026import org.kuali.rice.krad.util.GlobalVariables;
027
028import java.util.HashMap;
029import java.util.Map;
030
031/**
032 * This is a description of what this class does - shyu don't forget to fill this in. 
033 * 
034 * @author Kuali Rice Team (rice.collab@kuali.org)
035 *
036 */
037public class PersonDocumentGroupRule extends DocumentRuleBase implements AddGroupRule {
038    protected static final String NEW_GROUP = "newGroup";
039    protected static final String GROUP_ID_ERROR_PATH = NEW_GROUP+".groupId";
040
041        public boolean processAddGroup(AddGroupEvent addGroupEvent) {
042                IdentityManagementPersonDocument document = (IdentityManagementPersonDocument)addGroupEvent.getDocument();
043                PersonDocumentGroup newGroup = addGroupEvent.getGroup();
044            boolean rulePassed = true;
045            rulePassed = validAssignGroup(document, newGroup);
046//      List<String> groupIds = KimImplServiceLocator.getUiDocumentService().getPopulatableGroupIds();
047
048        if (newGroup == null || StringUtils.isBlank(newGroup.getGroupId())) {
049            rulePassed = false;
050            GlobalVariables.getMessageMap().putError(GROUP_ID_ERROR_PATH, RiceKeyConstants.ERROR_EMPTY_ENTRY, new String[] {"Group"});
051                
052        } else {
053                    for (PersonDocumentGroup group : document.getGroups()) {
054                        if (group.getGroupId().equals(newGroup.getGroupId())) {
055                            rulePassed = false;
056                            GlobalVariables.getMessageMap().putError(GROUP_ID_ERROR_PATH, RiceKeyConstants.ERROR_DUPLICATE_ENTRY, new String[] {"Group"});
057                                
058                        }
059                    }
060        }
061        
062//        if (rulePassed) {
063//              if (groupIds.isEmpty() || !groupIds.contains(newGroup.getGroupId())) {
064//                errorMap.putError(errorPath+".groupId", RiceKeyConstants.ERROR_POPULATE_GROUP, new String[] {newGroup.getGroupId()});
065//                rulePassed = false;
066//              }   
067//        }
068        // check it before save ??
069        //rulePassed &= validateActiveDate(newGroup.getActiveFromDate(), newGroup.getActiveToDate());
070                return rulePassed;
071        } 
072
073        protected boolean validAssignGroup(IdentityManagementPersonDocument document, PersonDocumentGroup newGroup){
074        boolean rulePassed = true;
075        Map<String,String> additionalPermissionDetails = new HashMap<String,String>();
076        additionalPermissionDetails.put(KimConstants.AttributeConstants.NAMESPACE_CODE, newGroup.getNamespaceCode());
077        additionalPermissionDetails.put(KimConstants.AttributeConstants.GROUP_NAME, newGroup.getGroupName());
078                if(!getDocumentDictionaryService().getDocumentAuthorizer(document).isAuthorizedByTemplate(
079                                document, KimConstants.NAMESPACE_CODE, KimConstants.PermissionTemplateNames.POPULATE_GROUP,
080                                GlobalVariables.getUserSession().getPrincipalId(), additionalPermissionDetails, null)){
081                GlobalVariables.getMessageMap().putError(GROUP_ID_ERROR_PATH, 
082                                RiceKeyConstants.ERROR_ASSIGN_GROUP, 
083                                new String[] {newGroup.getNamespaceCode(), newGroup.getGroupName()});
084            rulePassed = false;
085                }
086                return rulePassed;
087        }
088        
089}