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().getNextAvailableSequenceNumber(KimConstants.SequenceNames.KRIM_PERM_ID_S).toString();
86 ((GenericPermissionBo)document.getNewMaintainableObject().getDataObject()).setId(newId);
87 }
88
89
90
91
92
93
94 @Override
95 public Class<? extends PersistableBusinessObject> getBoClass() {
96 return GenericPermissionBo.class;
97 }
98
99
100
101
102
103
104 @Override
105 public boolean isExternalBusinessObject() {
106 return true;
107 }
108
109
110
111
112
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
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 }