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  
          * This overridden method ...
 46  
          * 
 47  
          * @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.Map<String, String>)
 48  
          */
 49  
         public void savePermission(String permissionId, String permissionTemplateId,
 50  
                         String namespaceCode, String name, String description, boolean active,
 51  
                         Map<String, String> permissionDetails) {
 52  
             // look for an existing permission of the given type
 53  
             try {
 54  0
                     KimPermissionImpl perm = getBusinessObjectService().findBySinglePrimaryKey(KimPermissionImpl.class, permissionId);
 55  0
                     if ( perm == null ) {
 56  0
                             perm = new KimPermissionImpl();
 57  0
                             perm.setPermissionId(permissionId);
 58  
                     }
 59  0
                     perm.setTemplateId(permissionTemplateId);
 60  0
                     perm.refreshReferenceObject( "template" );
 61  0
                     perm.setNamespaceCode(namespaceCode);
 62  0
                     perm.setName(name);
 63  0
                     perm.setDescription(description);
 64  0
                     perm.setActive(active);
 65  0
                     Map<String, String> attributesToAdd = new HashMap<String, String>( permissionDetails );
 66  0
                     List<PermissionAttributeDataImpl> details = perm.getDetailObjects();
 67  0
                     Iterator<PermissionAttributeDataImpl> detailIter = details.iterator();
 68  0
                     while ( detailIter.hasNext() ) {
 69  0
                             PermissionAttributeDataImpl detail = detailIter.next();
 70  0
                             String attrName = detail.getKimAttribute().getAttributeName();
 71  0
                             String attrValue = attributesToAdd.get(attrName);
 72  
                             // if not present in the list or is blank, remove from the list
 73  0
                             if ( StringUtils.isBlank(attrValue) ) {
 74  0
                                     detailIter.remove();
 75  
                             } else {
 76  0
                                     detail.setAttributeValue(attrValue);
 77  
                             }
 78  
                             // remove from detail map - used to add new ones later
 79  0
                             attributesToAdd.remove(attrName);
 80  0
                     }
 81  0
                     for ( String attrName : attributesToAdd.keySet() ) {
 82  0
                 KimType type = KimApiServiceLocator.getKimTypeInfoService().getKimType(perm.getTemplate().getKimTypeId());
 83  0
                             KimTypeAttribute attr = type.getAttributeDefinitionByName(attrName);
 84  0
                             if ( attr != null ) {
 85  0
                                     PermissionAttributeDataImpl newDetail = new PermissionAttributeDataImpl();
 86  0
                                     newDetail.setId(getNewAttributeDataId());
 87  0
                                     newDetail.setKimAttributeId(attr.getId());
 88  0
                                     newDetail.setKimTypeId(perm.getTemplate().getKimTypeId());
 89  0
                                     newDetail.setAssignedToId(permissionId);
 90  0
                                     newDetail.setAttributeValue(attributesToAdd.get(attrName));
 91  0
                                     details.add(newDetail);
 92  0
                             } else {
 93  0
                                     LOG.error( "Unknown attribute name saving permission: '" + attrName + "'" );
 94  
                             }
 95  0
                     }
 96  0
                     getBusinessObjectService().save(perm);
 97  
                     //KimApiServiceLocator.getIdentityManagementService().flushPermissionCaches();
 98  0
                     flushPermissionImplCache();
 99  0
             } catch ( RuntimeException ex ) {
 100  0
                     LOG.error( "Exception in savePermission: ", ex );
 101  0
                     throw ex;
 102  0
             }
 103  0
         }
 104  
         
 105  
         public String getNextAvailablePermissionId() throws UnsupportedOperationException {
 106  0
         Long nextSeq = KRADServiceLocator.getSequenceAccessorService().getNextAvailableSequenceNumber(KimConstants.SequenceNames.KRIM_PERM_ID_S, KimPermissionImpl.class);
 107  
 
 108  0
         if (nextSeq == null) {
 109  0
             LOG.error("Unable to get new permission id from sequence " + KimConstants.SequenceNames.KRIM_PERM_ID_S);
 110  0
             throw new RuntimeException("Unable to get new permission id from sequence " + KimConstants.SequenceNames.KRIM_PERM_ID_S);
 111  
         }
 112  
 
 113  0
         return nextSeq.toString();
 114  
     }
 115  
 
 116  
 }