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.core.util.AttributeSet;
 21  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 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.kns.service.KNSServiceLocator;
 29  
 
 30  
 import javax.jws.WebService;
 31  
 import java.util.Iterator;
 32  
 import java.util.List;
 33  
 
 34  
 /**
 35  
  * 
 36  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 37  
  *
 38  
  */
 39  
 @WebService(endpointInterface = KIMWebServiceConstants.PermissionUpdateService.INTERFACE_CLASS, serviceName = KIMWebServiceConstants.PermissionUpdateService.WEB_SERVICE_NAME, portName = KIMWebServiceConstants.PermissionUpdateService.WEB_SERVICE_PORT, targetNamespace = KIMWebServiceConstants.MODULE_TARGET_NAMESPACE)
 40  0
 public class PermissionUpdateServiceImpl extends PermissionServiceBase implements PermissionUpdateService {
 41  0
         private static final Logger LOG = Logger.getLogger( PermissionUpdateServiceImpl.class );
 42  
         /**
 43  
          * This overridden method ...
 44  
          * 
 45  
          * @see org.kuali.rice.kim.service.PermissionUpdateService#savePermission(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, boolean, org.kuali.rice.core.util.AttributeSet)
 46  
          */
 47  
         public void savePermission(String permissionId, String permissionTemplateId,
 48  
                         String namespaceCode, String name, String description, boolean active,
 49  
                         AttributeSet permissionDetails) {
 50  
             // look for an existing permission of the given type
 51  
             try {
 52  0
                     KimPermissionImpl perm = getBusinessObjectService().findBySinglePrimaryKey(KimPermissionImpl.class, permissionId);
 53  0
                     if ( perm == null ) {
 54  0
                             perm = new KimPermissionImpl();
 55  0
                             perm.setPermissionId(permissionId);
 56  
                     }
 57  0
                     perm.setTemplateId(permissionTemplateId);
 58  0
                     perm.refreshReferenceObject( "template" );
 59  0
                     perm.setNamespaceCode(namespaceCode);
 60  0
                     perm.setName(name);
 61  0
                     perm.setDescription(description);
 62  0
                     perm.setActive(active);
 63  0
                     AttributeSet attributesToAdd = new AttributeSet( permissionDetails );
 64  0
                     List<PermissionAttributeDataImpl> details = perm.getDetailObjects();
 65  0
                     Iterator<PermissionAttributeDataImpl> detailIter = details.iterator();
 66  0
                     while ( detailIter.hasNext() ) {
 67  0
                             PermissionAttributeDataImpl detail = detailIter.next();
 68  0
                             String attrName = detail.getKimAttribute().getAttributeName();
 69  0
                             String attrValue = attributesToAdd.get(attrName);
 70  
                             // if not present in the list or is blank, remove from the list
 71  0
                             if ( StringUtils.isBlank(attrValue) ) {
 72  0
                                     detailIter.remove();
 73  
                             } else {
 74  0
                                     detail.setAttributeValue(attrValue);
 75  
                             }
 76  
                             // remove from detail map - used to add new ones later
 77  0
                             attributesToAdd.remove(attrName);
 78  0
                     }
 79  0
                     for ( String attrName : attributesToAdd.keySet() ) {
 80  0
                             KimTypeAttribute attr = perm.getTemplate().getKimType().getAttributeDefinitionByName(attrName);
 81  0
                             if ( attr != null ) {
 82  0
                                     PermissionAttributeDataImpl newDetail = new PermissionAttributeDataImpl();
 83  0
                                     newDetail.setId(getNewAttributeDataId());
 84  0
                                     newDetail.setKimAttributeId(attr.getId());
 85  0
                                     newDetail.setKimTypeId(perm.getTemplate().getKimTypeId());
 86  0
                                     newDetail.setAssignedToId(permissionId);
 87  0
                                     newDetail.setAttributeValue(attributesToAdd.get(attrName));
 88  0
                                     details.add(newDetail);
 89  0
                             } else {
 90  0
                                     LOG.error( "Unknown attribute name saving permission: '" + attrName + "'" );
 91  
                             }
 92  0
                     }
 93  0
                     getBusinessObjectService().save(perm);
 94  0
                     KimApiServiceLocator.getIdentityManagementService().flushPermissionCaches();
 95  0
                     flushPermissionImplCache();
 96  0
             } catch ( RuntimeException ex ) {
 97  0
                     LOG.error( "Exception in savePermission: ", ex );
 98  0
                     throw ex;
 99  0
             }
 100  0
         }
 101  
         
 102  
         public String getNextAvailablePermissionId() throws UnsupportedOperationException {
 103  0
         Long nextSeq = KNSServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber(KimConstants.SequenceNames.KRIM_PERM_ID_S, KimPermissionImpl.class);
 104  
 
 105  0
         if (nextSeq == null) {
 106  0
             LOG.error("Unable to get new permission id from sequence " + KimConstants.SequenceNames.KRIM_PERM_ID_S);
 107  0
             throw new RuntimeException("Unable to get new permission id from sequence " + KimConstants.SequenceNames.KRIM_PERM_ID_S);
 108  
         }
 109  
 
 110  0
         return nextSeq.toString();
 111  
     }
 112  
 
 113  
 }