View Javadoc

1   /**
2    * Copyright 2005-2013 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.log4j.Logger;
19  import org.kuali.rice.kim.api.KimConstants;
20  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
21  import org.kuali.rice.kim.impl.permission.GenericPermissionBo;
22  import org.kuali.rice.kim.impl.permission.PermissionBo;
23  import org.kuali.rice.kim.impl.permission.PermissionTemplateBo;
24  import org.kuali.rice.kns.document.MaintenanceDocument;
25  import org.kuali.rice.kns.maintenance.KualiMaintainableImpl;
26  import org.kuali.rice.krad.bo.BusinessObject;
27  import org.kuali.rice.krad.bo.PersistableBusinessObject;
28  import org.kuali.rice.krad.service.KRADServiceLocator;
29  import org.kuali.rice.krad.service.SequenceAccessorService;
30  
31  import java.util.Map;
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      protected transient SequenceAccessorService sequenceAccessorService;
44  
45      /**
46       * Saves the responsibility via the responsibility update service
47       *
48       * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#saveBusinessObject()
49       */
50      @Override
51      public void saveDataObject() {
52          if (getDataObject() instanceof PersistableBusinessObject) {
53              GenericPermissionBo genericPermissionBo = (GenericPermissionBo)getDataObject();
54              boolean permissionExists = false;
55              if (genericPermissionBo.getId() != null) {
56                  permissionExists = KimApiServiceLocator.getPermissionService().getPermission(genericPermissionBo.getId()) != null;
57              }
58  
59              if (genericPermissionBo.getTemplateId() != null) {
60                  genericPermissionBo.setTemplate(
61                          PermissionTemplateBo.from(
62                                  KimApiServiceLocator.getPermissionService().getPermissionTemplate(genericPermissionBo.getTemplateId())));
63              }
64              PermissionBo perm = GenericPermissionBo.toPermissionBo(genericPermissionBo);
65              if (permissionExists) {
66                  KimApiServiceLocator.getPermissionService().updatePermission(PermissionBo.to(perm));
67              } else {
68                  KimApiServiceLocator.getPermissionService().createPermission(PermissionBo.to(perm));
69              }
70          } else {
71              throw new RuntimeException(
72                      "Cannot save object of type: " + getDataObjectClass() + " with permission service");
73          }
74      }
75  	
76      /**
77       * Pre-populates the ID field of the new PermissionBo to be created.
78       *
79       * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#saveBusinessObject()
80       */
81      @Override
82      public void processAfterCopy(MaintenanceDocument document, Map<String, String[]> parameters) {
83          super.processAfterCopy(document,parameters);
84          // get id for new permission
85          String newId = getSequenceAccessorService().getNextAvailableSequenceNumber(KimConstants.SequenceNames.KRIM_PERM_ID_S).toString();
86          ((GenericPermissionBo)document.getNewMaintainableObject().getDataObject()).setId(newId);
87      }
88  
89  	/**
90  	 * This overridden method ...
91  	 * 
92  	 * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#getBoClass()
93  	 */
94  	@Override
95  	public Class<? extends PersistableBusinessObject> getBoClass() {
96  		return GenericPermissionBo.class;
97  	}
98  	
99  	/**
100 	 * This overridden method ...
101 	 * 
102 	 * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#isExternalBusinessObject()
103 	 */
104 	@Override
105 	public boolean isExternalBusinessObject() {
106 		return true;
107 	}
108 	
109 	/**
110 	 * This overridden method ...
111 	 * 
112 	 * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#prepareBusinessObject(org.kuali.rice.krad.bo.BusinessObject)
113 	 */
114 	@Override
115 	public void prepareBusinessObject(BusinessObject businessObject) {
116 		try {
117 			if ( businessObject == null ) {
118 				throw new RuntimeException( "Configuration ERROR: GenericPermissionMaintainable.prepareBusinessObject passed a null object." );
119 			}
120 			if ( businessObject instanceof PermissionBo ) {
121 				PermissionBo perm = getBusinessObjectService().findBySinglePrimaryKey(PermissionBo.class, ((PermissionBo)businessObject).getId() );
122 				businessObject = new GenericPermissionBo(perm);
123 			} else if ( businessObject instanceof GenericPermissionBo ) {
124 				// lookup the PermissionBo and convert to a GenericPermissionBo
125 				PermissionBo perm = getBusinessObjectService().findBySinglePrimaryKey(PermissionBo.class, ((GenericPermissionBo)businessObject).getId() );
126 				((GenericPermissionBo)businessObject).loadFromPermission(perm);
127 			} else {
128 				throw new RuntimeException( "Configuration ERROR: GenericPermissionMaintainable passed an unsupported object type: " + businessObject.getClass() );
129 			}
130 			if ( businessObject instanceof PersistableBusinessObject ) {
131 				setBusinessObject( (PersistableBusinessObject)businessObject );
132 			}
133 			super.prepareBusinessObject(businessObject);
134 		} catch ( RuntimeException ex ) {
135 			LOG.error( "Exception in prepareBusinessObject()", ex );
136 			throw ex;
137 		}
138 	}
139 
140     protected SequenceAccessorService getSequenceAccessorService(){
141         if(this.sequenceAccessorService==null){
142             this.sequenceAccessorService = KRADServiceLocator.getSequenceAccessorService();
143         }
144         return this.sequenceAccessorService;
145     }
146 
147 }