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 java.util.HashMap;
19 import java.util.Map;
20
21 import org.apache.commons.lang.StringUtils;
22 import org.apache.log4j.Logger;
23 import org.kuali.rice.kim.api.KimConstants;
24 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
25 import org.kuali.rice.kim.impl.permission.GenericPermissionBo;
26 import org.kuali.rice.kim.impl.permission.PermissionBo;
27 import org.kuali.rice.kim.impl.permission.PermissionTemplateBo;
28 import org.kuali.rice.kim.impl.services.KimImplServiceLocator;
29 import org.kuali.rice.kns.document.MaintenanceDocument;
30 import org.kuali.rice.kns.maintenance.KualiMaintainableImpl;
31 import org.kuali.rice.krad.bo.BusinessObject;
32 import org.kuali.rice.krad.bo.PersistableBusinessObject;
33 import org.kuali.rice.krad.data.KradDataServiceLocator;
34 import org.kuali.rice.krad.data.platform.MaxValueIncrementerFactory;
35 import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
36
37
38
39
40
41
42 public class GenericPermissionMaintainable extends KualiMaintainableImpl {
43
44 private static final Logger LOG = Logger.getLogger( GenericPermissionMaintainable.class );
45 private static final long serialVersionUID = -8102504656976243468L;
46
47 protected static final String DETAIL_OBJECTS_ATTRIBUTE_NAME = "attributeDetails.kimAttribute.attributeName";
48 protected static final String DETAIL_OBJECTS_ATTRIBUTE_VALUE = "attributeDetails.attributeValue";
49
50
51
52
53
54
55 @Override
56 public void saveDataObject() {
57 if (getDataObject() instanceof GenericPermissionBo) {
58 GenericPermissionBo genericPermissionBo = (GenericPermissionBo)getDataObject();
59 boolean permissionExists = false;
60 if (genericPermissionBo.getId() != null) {
61 permissionExists = KimApiServiceLocator.getPermissionService().getPermission(genericPermissionBo.getId()) != null;
62 }
63
64 if (genericPermissionBo.getTemplateId() != null) {
65 genericPermissionBo.setTemplate(
66 PermissionTemplateBo.from(
67 KimApiServiceLocator.getPermissionService().getPermissionTemplate(genericPermissionBo.getTemplateId())));
68 }
69 PermissionBo perm = GenericPermissionBo.toPermissionBo(genericPermissionBo);
70 if (permissionExists) {
71 KimApiServiceLocator.getPermissionService().updatePermission(PermissionBo.to(perm));
72 } else {
73 KimApiServiceLocator.getPermissionService().createPermission(PermissionBo.to(perm));
74 }
75 } else {
76 throw new RuntimeException(
77 "Cannot save object of type: " + getDataObjectClass() + " with permission service");
78 }
79 }
80
81
82
83
84
85
86 @Override
87 public void processAfterNew(MaintenanceDocument document, Map<String, String[]> parameters) {
88 super.processAfterNew(document,parameters);
89
90 GenericPermissionBo permissionBo = (GenericPermissionBo) document.getNewMaintainableObject().getDataObject();
91 initializePermissionId(permissionBo);
92 }
93
94
95
96
97
98
99 @Override
100 public void processAfterCopy(MaintenanceDocument document, Map<String, String[]> parameters) {
101 super.processAfterCopy(document,parameters);
102
103 GenericPermissionBo permissionBo = (GenericPermissionBo) document.getNewMaintainableObject().getDataObject();
104 initializePermissionId(permissionBo);
105 permissionBo.setVersionNumber(null);
106 }
107
108 private void initializePermissionId(Object dataObject) {
109 if (dataObject instanceof GenericPermissionBo) {
110 GenericPermissionBo permissionBo = (GenericPermissionBo) dataObject;
111
112 if (StringUtils.isBlank(permissionBo.getId())) {
113 DataFieldMaxValueIncrementer incrementer = MaxValueIncrementerFactory.getIncrementer(
114 KimImplServiceLocator.getDataSource(), KimConstants.SequenceNames.KRIM_PERM_ID_S);
115 permissionBo.setId(incrementer.nextStringValue());
116 }
117 }
118 }
119
120 @SuppressWarnings("rawtypes")
121 @Override
122 public Class getBoClass() {
123 return GenericPermissionBo.class;
124 }
125
126 @Override
127 public boolean isExternalBusinessObject() {
128 return true;
129 }
130
131 @Override
132 public void prepareBusinessObject(BusinessObject businessObject) {
133 try {
134 if ( businessObject == null ) {
135 throw new RuntimeException( "Configuration ERROR: GenericPermissionMaintainable.prepareBusinessObject passed a null object." );
136 }
137 if ( businessObject instanceof PermissionBo ) {
138 PermissionBo perm = KradDataServiceLocator.getDataObjectService().find(PermissionBo.class, ((PermissionBo)businessObject).getId());
139 businessObject = new GenericPermissionBo(perm);
140 } else if ( businessObject instanceof GenericPermissionBo ) {
141
142 PermissionBo perm = KradDataServiceLocator.getDataObjectService().find(PermissionBo.class, ((GenericPermissionBo)businessObject).getId());
143 ((GenericPermissionBo)businessObject).loadFromPermission(perm);
144 } else {
145 throw new RuntimeException( "Configuration ERROR: GenericPermissionMaintainable passed an unsupported object type: " + businessObject.getClass() );
146 }
147 setDataObject( businessObject );
148 super.prepareBusinessObject(businessObject);
149 } catch ( RuntimeException ex ) {
150 LOG.error( "Exception in prepareBusinessObject()", ex );
151 throw ex;
152 }
153 }
154
155 @Override
156 public void setupNewFromExisting(MaintenanceDocument document, Map<String, String[]> parameters) {
157 String attrName = "";
158 String attrValue = "";
159
160 GenericPermissionBo permissionBo = (GenericPermissionBo) document.getNewMaintainableObject().getDataObject();
161 initializePermissionId(permissionBo);
162 permissionBo.setActive(true);
163
164 for (String paramName : parameters.keySet()) {
165 String[] parameterValues = parameters.get(paramName);
166
167 if (paramName.equals(DETAIL_OBJECTS_ATTRIBUTE_NAME)) {
168 if (parameterValues.length > 0) {
169 attrName = parameterValues[0];
170 }
171 }
172
173 if (paramName.equals(DETAIL_OBJECTS_ATTRIBUTE_VALUE)) {
174 if (parameterValues.length > 0) {
175 attrValue = parameterValues[0];
176 }
177 }
178 }
179
180 if (StringUtils.isNotEmpty(attrName) && StringUtils.isNotEmpty(attrValue)) {
181 Map<String, String> details = new HashMap<String, String>();
182 details.put(attrName, attrValue);
183 permissionBo.setDetails(details);
184 }
185
186 document.getNewMaintainableObject().setDataObject(permissionBo);
187 }
188 }