View Javadoc

1   /**
2    * Copyright 2005-2014 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()
86                  .getNextAvailableSequenceNumber(KimConstants.SequenceNames.KRIM_PERM_ID_S, PermissionBo.class).toString();
87          ((GenericPermissionBo)document.getNewMaintainableObject().getDataObject()).setId(newId);
88      }
89  
90  	/**
91  	 * This overridden method ...
92  	 * 
93  	 * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#getBoClass()
94  	 */
95  	@Override
96  	public Class<? extends PersistableBusinessObject> getBoClass() {
97  		return GenericPermissionBo.class;
98  	}
99  	
100 	/**
101 	 * This overridden method ...
102 	 * 
103 	 * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#isExternalBusinessObject()
104 	 */
105 	@Override
106 	public boolean isExternalBusinessObject() {
107 		return true;
108 	}
109 	
110 	/**
111 	 * This overridden method ...
112 	 * 
113 	 * @see org.kuali.rice.kns.maintenance.KualiMaintainableImpl#prepareBusinessObject(org.kuali.rice.krad.bo.BusinessObject)
114 	 */
115 	@Override
116 	public void prepareBusinessObject(BusinessObject businessObject) {
117 		try {
118 			if ( businessObject == null ) {
119 				throw new RuntimeException( "Configuration ERROR: GenericPermissionMaintainable.prepareBusinessObject passed a null object." );
120 			}
121 			if ( businessObject instanceof PermissionBo ) {
122 				PermissionBo perm = getBusinessObjectService().findBySinglePrimaryKey(PermissionBo.class, ((PermissionBo)businessObject).getId() );
123 				businessObject = new GenericPermissionBo(perm);
124 			} else if ( businessObject instanceof GenericPermissionBo ) {
125 				// lookup the PermissionBo and convert to a GenericPermissionBo
126 				PermissionBo perm = getBusinessObjectService().findBySinglePrimaryKey(PermissionBo.class, ((GenericPermissionBo)businessObject).getId() );
127 				((GenericPermissionBo)businessObject).loadFromPermission(perm);
128 			} else {
129 				throw new RuntimeException( "Configuration ERROR: GenericPermissionMaintainable passed an unsupported object type: " + businessObject.getClass() );
130 			}
131 			if ( businessObject instanceof PersistableBusinessObject ) {
132 				setBusinessObject( (PersistableBusinessObject)businessObject );
133 			}
134 			super.prepareBusinessObject(businessObject);
135 		} catch ( RuntimeException ex ) {
136 			LOG.error( "Exception in prepareBusinessObject()", ex );
137 			throw ex;
138 		}
139 	}
140 
141     protected SequenceAccessorService getSequenceAccessorService(){
142         if(this.sequenceAccessorService==null){
143             this.sequenceAccessorService = KRADServiceLocator.getSequenceAccessorService();
144         }
145         return this.sequenceAccessorService;
146     }
147 
148 }