Coverage Report - org.kuali.rice.kim.service.impl.PermissionUpdateServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
PermissionUpdateServiceImpl
0%
0/50
0%
0/12
5.5
 
 1  
 /*
 2  
  * Copyright 2007-2010 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.kim.service.impl;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 19  
 import org.apache.log4j.Logger;
 20  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 21  
 import org.kuali.rice.kim.api.type.KimType;
 22  
 import org.kuali.rice.kim.api.type.KimTypeAttribute;
 23  
 import org.kuali.rice.kim.bo.role.impl.KimPermissionImpl;
 24  
 import org.kuali.rice.kim.bo.role.impl.PermissionAttributeDataImpl;
 25  
 import org.kuali.rice.kim.service.PermissionUpdateService;
 26  
 import org.kuali.rice.kim.util.KIMWebServiceConstants;
 27  
 import org.kuali.rice.kim.util.KimConstants;
 28  
 import org.kuali.rice.krad.service.KRADServiceLocator;
 29  
 
 30  
 import javax.jws.WebService;
 31  
 import java.util.HashMap;
 32  
 import java.util.Iterator;
 33  
 import java.util.List;
 34  
 import java.util.Map;
 35  
 
 36  
 /**
 37  
  * 
 38  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 39  
  *
 40  
  */
 41  
 @WebService(endpointInterface = KIMWebServiceConstants.PermissionUpdateService.INTERFACE_CLASS, serviceName = KIMWebServiceConstants.PermissionUpdateService.WEB_SERVICE_NAME, portName = KIMWebServiceConstants.PermissionUpdateService.WEB_SERVICE_PORT, targetNamespace = KIMWebServiceConstants.MODULE_TARGET_NAMESPACE)
 42  0
 public class PermissionUpdateServiceImpl extends PermissionServiceBase implements PermissionUpdateService {
 43  0
         private static final Logger LOG = Logger.getLogger( PermissionUpdateServiceImpl.class );
 44  
 
 45  
         public void savePermission(String permissionId, String permissionTemplateId,
 46  
                         String namespaceCode, String name, String description, boolean active,
 47  
                         Map<String, String> permissionDetails) {
 48  
             // look for an existing permission of the given type
 49  
             try {
 50  0
                     KimPermissionImpl perm = getBusinessObjectService().findBySinglePrimaryKey(KimPermissionImpl.class, permissionId);
 51  0
                     if ( perm == null ) {
 52  0
                             perm = new KimPermissionImpl();
 53  0
                             perm.setPermissionId(permissionId);
 54  
                     }
 55  0
                     perm.setTemplateId(permissionTemplateId);
 56  0
                     perm.refreshReferenceObject( "template" );
 57  0
                     perm.setNamespaceCode(namespaceCode);
 58  0
                     perm.setName(name);
 59  0
                     perm.setDescription(description);
 60  0
                     perm.setActive(active);
 61  0
                     Map<String, String> attributesToAdd = new HashMap<String, String>( permissionDetails );
 62  0
                     List<PermissionAttributeDataImpl> details = perm.getDetailObjects();
 63  0
                     Iterator<PermissionAttributeDataImpl> detailIter = details.iterator();
 64  0
                     while ( detailIter.hasNext() ) {
 65  0
                             PermissionAttributeDataImpl detail = detailIter.next();
 66  0
                             String attrName = detail.getKimAttribute().getAttributeName();
 67  0
                             String attrValue = attributesToAdd.get(attrName);
 68  
                             // if not present in the list or is blank, remove from the list
 69  0
                             if ( StringUtils.isBlank(attrValue) ) {
 70  0
                                     detailIter.remove();
 71  
                             } else {
 72  0
                                     detail.setAttributeValue(attrValue);
 73  
                             }
 74  
                             // remove from detail map - used to add new ones later
 75  0
                             attributesToAdd.remove(attrName);
 76  0
                     }
 77  0
                     for ( String attrName : attributesToAdd.keySet() ) {
 78  0
                 KimType type = KimApiServiceLocator.getKimTypeInfoService().getKimType(perm.getTemplate().getKimTypeId());
 79  0
                             KimTypeAttribute attr = type.getAttributeDefinitionByName(attrName);
 80  0
                             if ( attr != null ) {
 81  0
                                     PermissionAttributeDataImpl newDetail = new PermissionAttributeDataImpl();
 82  0
                                     newDetail.setId(getNewAttributeDataId());
 83  0
                                     newDetail.setKimAttributeId(attr.getId());
 84  0
                                     newDetail.setKimTypeId(perm.getTemplate().getKimTypeId());
 85  0
                                     newDetail.setAssignedToId(permissionId);
 86  0
                                     newDetail.setAttributeValue(attributesToAdd.get(attrName));
 87  0
                                     details.add(newDetail);
 88  0
                             } else {
 89  0
                                     LOG.error( "Unknown attribute name saving permission: '" + attrName + "'" );
 90  
                             }
 91  0
                     }
 92  0
                     getBusinessObjectService().save(perm);
 93  
                     //KimApiServiceLocator.getIdentityManagementService().flushPermissionCaches();
 94  0
                     flushPermissionImplCache();
 95  0
             } catch ( RuntimeException ex ) {
 96  0
                     LOG.error( "Exception in savePermission: ", ex );
 97  0
                     throw ex;
 98  0
             }
 99  0
         }
 100  
         
 101  
         public String getNextAvailablePermissionId() throws UnsupportedOperationException {
 102  0
         Long nextSeq = KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber(KimConstants.SequenceNames.KRIM_PERM_ID_S, KimPermissionImpl.class);
 103  
 
 104  0
         if (nextSeq == null) {
 105  0
             LOG.error("Unable to get new permission id from sequence " + KimConstants.SequenceNames.KRIM_PERM_ID_S);
 106  0
             throw new RuntimeException("Unable to get new permission id from sequence " + KimConstants.SequenceNames.KRIM_PERM_ID_S);
 107  
         }
 108  
 
 109  0
         return nextSeq.toString();
 110  
     }
 111  
 
 112  
 }