1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
35
36
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
47
48
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
78
79
80
81 @Override
82 public void processAfterCopy(MaintenanceDocument document, Map<String, String[]> parameters) {
83 super.processAfterCopy(document,parameters);
84
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
92
93
94
95 @Override
96 public Class<? extends PersistableBusinessObject> getBoClass() {
97 return GenericPermissionBo.class;
98 }
99
100
101
102
103
104
105 @Override
106 public boolean isExternalBusinessObject() {
107 return true;
108 }
109
110
111
112
113
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
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 }