View Javadoc

1   /**
2    * Copyright 2005-2012 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.document;
17  
18  import org.apache.cxf.common.util.StringUtils;
19  import org.apache.log4j.Logger;
20  import org.kuali.rice.kim.api.permission.Permission;
21  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
22  import org.kuali.rice.kim.impl.permission.GenericPermissionBo;
23  import org.kuali.rice.kim.impl.permission.PermissionBo;
24  import org.kuali.rice.kim.impl.permission.PermissionTemplateBo;
25  import org.kuali.rice.kim.impl.responsibility.ReviewResponsibilityBo;
26  import org.kuali.rice.kns.maintenance.KualiMaintainableImpl;
27  import org.kuali.rice.krad.bo.BusinessObject;
28  import org.kuali.rice.krad.bo.PersistableBusinessObject;
29  import org.kuali.rice.krad.util.KRADConstants;
30  
31  import java.util.HashMap;
32  
33  /**
34   * This is a description of what this class does - jonathan don't forget to fill this in. 
35   * 
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   *
38   */
39  public class GenericPermissionMaintainable extends KualiMaintainableImpl {
40  
41  	private static final Logger LOG = Logger.getLogger( GenericPermissionMaintainable.class );	
42  	private static final long serialVersionUID = -8102504656976243468L;
43  
44      /**
45       * Saves the responsibility via the responsibility update service
46       *
47       * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#saveBusinessObject()
48       */
49      @Override
50      public void saveDataObject() {
51          if (getDataObject() instanceof PersistableBusinessObject) {
52              GenericPermissionBo genericPermissionBo = (GenericPermissionBo)getDataObject();
53              boolean permissionExists = false;
54              if (genericPermissionBo.getId() != null) {
55                  permissionExists = KimApiServiceLocator.getPermissionService().getPermission(genericPermissionBo.getId()) != null;
56              }
57  
58              if (genericPermissionBo.getTemplateId() != null) {
59                  genericPermissionBo.setTemplate(
60                          PermissionTemplateBo.from(
61                                  KimApiServiceLocator.getPermissionService().getPermissionTemplate(genericPermissionBo.getTemplateId())));
62              }
63              PermissionBo perm = GenericPermissionBo.toPermissionBo(genericPermissionBo);
64              if (permissionExists) {
65                  KimApiServiceLocator.getPermissionService().updatePermission(PermissionBo.to(perm));
66              } else {
67                  KimApiServiceLocator.getPermissionService().createPermission(PermissionBo.to(perm));
68              }
69          } else {
70              throw new RuntimeException(
71                      "Cannot save object of type: " + getDataObjectClass() + " with permission service");
72          }
73      }
74  	
75  	/**
76  	 * This overridden method ...
77  	 * 
78  	 * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#getBoClass()
79  	 */
80  	@Override
81  	public Class<? extends PersistableBusinessObject> getBoClass() {
82  		return GenericPermissionBo.class;
83  	}
84  	
85  	/**
86  	 * This overridden method ...
87  	 * 
88  	 * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#isExternalBusinessObject()
89  	 */
90  	@Override
91  	public boolean isExternalBusinessObject() {
92  		return true;
93  	}
94  	
95  	/**
96  	 * This overridden method ...
97  	 * 
98  	 * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#prepareBusinessObject(org.kuali.rice.krad.bo.BusinessObject)
99  	 */
100 	@Override
101 	public void prepareBusinessObject(BusinessObject businessObject) {
102 		try {
103 			if ( businessObject == null ) {
104 				throw new RuntimeException( "Configuration ERROR: GenericPermissionMaintainable.prepareBusinessObject passed a null object." );
105 			}
106 			if ( businessObject instanceof PermissionBo ) {
107 				PermissionBo perm = getBusinessObjectService().findBySinglePrimaryKey(PermissionBo.class, ((PermissionBo)businessObject).getId() );
108 				businessObject = new GenericPermissionBo(perm);
109 			} else if ( businessObject instanceof GenericPermissionBo ) {
110 				// lookup the PermissionBo and convert to a GenericPermissionBo
111 				PermissionBo perm = getBusinessObjectService().findBySinglePrimaryKey(PermissionBo.class, ((GenericPermissionBo)businessObject).getId() );
112 				((GenericPermissionBo)businessObject).loadFromPermission(perm);
113 			} else {
114 				throw new RuntimeException( "Configuration ERROR: GenericPermissionMaintainable passed an unsupported object type: " + businessObject.getClass() );
115 			}
116 			if ( businessObject instanceof PersistableBusinessObject ) {
117 				setBusinessObject( (PersistableBusinessObject)businessObject );
118 			}
119 			super.prepareBusinessObject(businessObject);
120 		} catch ( RuntimeException ex ) {
121 			LOG.error( "Exception in prepareBusinessObject()", ex );
122 			throw ex;
123 		}
124 	}
125 	
126 }