1 | |
package org.kuali.student.common.ui.server.gwt; |
2 | |
|
3 | |
import java.util.HashMap; |
4 | |
import java.util.List; |
5 | |
import java.util.Map; |
6 | |
import java.util.UUID; |
7 | |
|
8 | |
import org.apache.commons.lang.StringUtils; |
9 | |
import org.apache.log4j.Logger; |
10 | |
import org.kuali.rice.kim.bo.types.dto.AttributeSet; |
11 | |
import org.kuali.rice.kim.service.IdentityManagementService; |
12 | |
import org.kuali.student.common.assembly.data.Data; |
13 | |
import org.kuali.student.common.assembly.data.Metadata; |
14 | |
import org.kuali.student.common.assembly.transform.AuthorizationFilter; |
15 | |
import org.kuali.student.common.assembly.transform.MetadataFilter; |
16 | |
import org.kuali.student.common.assembly.transform.TransformFilter; |
17 | |
import org.kuali.student.common.assembly.transform.TransformFilter.TransformFilterAction; |
18 | |
import org.kuali.student.common.assembly.transform.TransformationManager; |
19 | |
import org.kuali.student.common.dto.DtoConstants; |
20 | |
import org.kuali.student.common.exceptions.DataValidationErrorException; |
21 | |
import org.kuali.student.common.exceptions.DoesNotExistException; |
22 | |
import org.kuali.student.common.exceptions.OperationFailedException; |
23 | |
import org.kuali.student.common.exceptions.VersionMismatchException; |
24 | |
import org.kuali.student.common.rice.StudentIdentityConstants; |
25 | |
import org.kuali.student.common.rice.authorization.PermissionType; |
26 | |
import org.kuali.student.common.ui.client.service.DataSaveResult; |
27 | |
import org.kuali.student.common.ui.shared.IdAttributes; |
28 | |
import org.kuali.student.common.util.security.SecurityUtils; |
29 | |
import org.kuali.student.common.validation.dto.ValidationResultInfo; |
30 | |
import org.kuali.student.core.assembly.transform.ProposalWorkflowFilter; |
31 | |
import org.kuali.student.core.proposal.dto.ProposalInfo; |
32 | |
import org.kuali.student.core.proposal.service.ProposalService; |
33 | |
import org.springframework.transaction.annotation.Transactional; |
34 | |
|
35 | |
@Transactional(readOnly=true,noRollbackFor={DoesNotExistException.class},rollbackFor={Throwable.class}) |
36 | 0 | public abstract class AbstractDataService implements DataService{ |
37 | |
|
38 | |
private static final long serialVersionUID = 1L; |
39 | |
|
40 | 0 | final Logger LOG = Logger.getLogger(AbstractDataService.class); |
41 | |
|
42 | |
private TransformationManager transformationManager; |
43 | |
|
44 | |
private IdentityManagementService permissionService; |
45 | |
|
46 | |
|
47 | |
private ProposalService proposalService; |
48 | |
|
49 | |
@Override |
50 | |
public Data getData(String id) throws OperationFailedException { |
51 | 0 | Map<String, Object> filterProperties = getDefaultFilterProperties(); |
52 | 0 | filterProperties.put(TransformFilter.FILTER_ACTION, TransformFilterAction.GET); |
53 | 0 | filterProperties.put(MetadataFilter.METADATA_ID_VALUE, id); |
54 | |
|
55 | 0 | String dtoId = id; |
56 | |
|
57 | |
|
58 | |
try{ |
59 | 0 | if (proposalService != null){ |
60 | 0 | ProposalInfo proposalInfo = proposalService.getProposal(dtoId); |
61 | 0 | filterProperties.put(ProposalWorkflowFilter.PROPOSAL_INFO, proposalInfo); |
62 | 0 | dtoId = proposalInfo.getProposalReference().get(0); |
63 | |
} |
64 | |
|
65 | 0 | Object dto = get(dtoId); |
66 | 0 | if (dto != null){ |
67 | 0 | return transformationManager.transform(dto, getDtoClass().getName(), filterProperties); |
68 | |
} |
69 | 0 | } catch(DoesNotExistException e){ |
70 | 0 | return null; |
71 | 0 | } catch (Exception e) { |
72 | 0 | LOG.error("Error getting data",e); |
73 | 0 | throw new OperationFailedException("Error getting data",e); |
74 | 0 | } |
75 | 0 | return null; |
76 | |
} |
77 | |
|
78 | |
@Override |
79 | |
public Metadata getMetadata(String id, Map<String, String> attributes) { |
80 | 0 | Map<String, Object> filterProperties = getDefaultFilterProperties(); |
81 | 0 | filterProperties.put(MetadataFilter.METADATA_ID_VALUE, id); |
82 | |
|
83 | |
|
84 | 0 | String idType = (attributes != null? attributes.get(IdAttributes.ID_TYPE):null); |
85 | 0 | String docType = (attributes != null ? attributes.get(StudentIdentityConstants.DOCUMENT_TYPE_NAME):null); |
86 | 0 | String dtoState = (attributes != null ? attributes.get(DtoConstants.DTO_STATE):null); |
87 | 0 | String dtoNextState = (attributes != null ? attributes.get(DtoConstants.DTO_NEXT_STATE):null); |
88 | 0 | String workflowNode = (attributes != null ? attributes.get(DtoConstants.DTO_WORKFLOW_NODE):null); |
89 | |
|
90 | 0 | if (idType == null){ |
91 | 0 | filterProperties.remove(MetadataFilter.METADATA_ID_TYPE); |
92 | |
} else { |
93 | 0 | filterProperties.put(MetadataFilter.METADATA_ID_TYPE, idType); |
94 | |
} |
95 | |
|
96 | 0 | if (docType == null){ |
97 | 0 | filterProperties.put(ProposalWorkflowFilter.WORKFLOW_DOC_TYPE, getDefaultWorkflowDocumentType()); |
98 | |
} else { |
99 | 0 | filterProperties.put(ProposalWorkflowFilter.WORKFLOW_DOC_TYPE, docType); |
100 | |
} |
101 | |
|
102 | 0 | if (dtoState != null){ |
103 | 0 | filterProperties.put(DtoConstants.DTO_STATE, dtoState); |
104 | |
} |
105 | |
|
106 | 0 | if (dtoNextState != null){ |
107 | 0 | filterProperties.put(DtoConstants.DTO_NEXT_STATE, dtoNextState); |
108 | |
} |
109 | |
|
110 | 0 | if (workflowNode != null){ |
111 | 0 | filterProperties.put(DtoConstants.DTO_WORKFLOW_NODE, workflowNode); |
112 | |
} |
113 | |
|
114 | 0 | if (checkDocumentLevelPermissions()){ |
115 | 0 | filterProperties.put(AuthorizationFilter.DOC_LEVEL_PERM_CHECK, Boolean.TRUE.toString()); |
116 | |
} |
117 | |
|
118 | 0 | Metadata metadata = transformationManager.getMetadata(getDtoClass().getName(), filterProperties); |
119 | 0 | return metadata; |
120 | |
} |
121 | |
|
122 | |
@Override |
123 | |
@Transactional(readOnly=false,noRollbackFor={DoesNotExistException.class},rollbackFor={Throwable.class}) |
124 | |
public DataSaveResult saveData(Data data) throws OperationFailedException, DataValidationErrorException, VersionMismatchException{ |
125 | 0 | Map<String, Object> filterProperties = getDefaultFilterProperties(); |
126 | 0 | filterProperties.put(TransformFilter.FILTER_ACTION, TransformFilterAction.SAVE); |
127 | |
|
128 | 0 | DataSaveResult saveResult = new DataSaveResult(); |
129 | |
try { |
130 | |
|
131 | 0 | Object dto = transformationManager.transform(data, getDtoClass(), filterProperties); |
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | 0 | dto = save(dto, filterProperties); |
137 | |
|
138 | |
|
139 | 0 | List<ValidationResultInfo> validationResults = validate(dto); |
140 | |
|
141 | |
|
142 | 0 | Data persistedData = transformationManager.transform(dto, getDtoClass().getName(), filterProperties); |
143 | |
|
144 | 0 | saveResult.setValue(persistedData); |
145 | 0 | saveResult.setValidationResults(validationResults); |
146 | 0 | }catch (DataValidationErrorException dvee){ |
147 | |
|
148 | 0 | throw dvee; |
149 | 0 | }catch (OperationFailedException ofe){ |
150 | 0 | throw ofe; |
151 | 0 | }catch (VersionMismatchException vme){ |
152 | 0 | throw vme; |
153 | 0 | }catch (Exception e) { |
154 | 0 | LOG.error("Failed to save data",e); |
155 | 0 | throw new OperationFailedException("Failed to save data",e); |
156 | 0 | } |
157 | |
|
158 | 0 | return saveResult; |
159 | |
} |
160 | |
|
161 | |
|
162 | |
|
163 | |
@Override |
164 | |
public List<ValidationResultInfo> validateData(Data data) throws OperationFailedException { |
165 | |
List<ValidationResultInfo> validationResults; |
166 | |
|
167 | |
try { |
168 | 0 | Metadata metadata = transformationManager.getUnfilteredMetadata(getDtoClass().getName()); |
169 | 0 | Object dto = transformationManager.getMapper().convertFromData(data, getDtoClass(), metadata); |
170 | 0 | validationResults = validate(dto); |
171 | 0 | } catch (Exception e) { |
172 | 0 | throw new OperationFailedException("Unable to validate data", e); |
173 | 0 | } |
174 | |
|
175 | 0 | return validationResults; |
176 | |
} |
177 | |
|
178 | |
@Override |
179 | |
public Boolean isAuthorized(PermissionType type, Map<String,String> attributes) { |
180 | 0 | String user = SecurityUtils.getCurrentUserId(); |
181 | 0 | boolean result = false; |
182 | 0 | if (checkDocumentLevelPermissions()) { |
183 | 0 | if (type == null) { |
184 | 0 | return null; |
185 | |
} |
186 | 0 | String namespaceCode = type.getPermissionNamespace(); |
187 | 0 | String permissionTemplateName = type.getPermissionTemplateName(); |
188 | |
|
189 | 0 | AttributeSet roleQuals = new AttributeSet(); |
190 | 0 | if (attributes != null) { |
191 | 0 | if (proposalService != null){ |
192 | 0 | ProposalInfo proposalInfo = null; |
193 | |
try { |
194 | 0 | if (attributes.containsKey(IdAttributes.IdType.KS_KEW_OBJECT_ID.toString())){ |
195 | 0 | proposalInfo = proposalService.getProposal(attributes.get(IdAttributes.IdType.KS_KEW_OBJECT_ID.toString())); |
196 | 0 | } else if (attributes.containsKey(IdAttributes.IdType.DOCUMENT_ID.toString())){ |
197 | 0 | proposalInfo = proposalService.getProposalByWorkflowId(attributes.get(IdAttributes.IdType.DOCUMENT_ID.toString())); |
198 | |
} |
199 | 0 | if (proposalInfo != null){ |
200 | 0 | attributes.put(IdAttributes.IdType.KS_KEW_OBJECT_ID.toString(), proposalInfo.getId()); |
201 | 0 | attributes.put(IdAttributes.IdType.DOCUMENT_ID.toString(), proposalInfo.getWorkflowId()); |
202 | 0 | attributes.put(StudentIdentityConstants.DOCUMENT_TYPE_NAME, proposalInfo.getType()); |
203 | |
} |
204 | 0 | } catch (Exception e){ |
205 | 0 | LOG.error("Could not retrieve proposal to determine permission qualifiers."); |
206 | 0 | } |
207 | |
} |
208 | |
|
209 | 0 | attributes.put("RAND_NO_CACHE", UUID.randomUUID().toString()); |
210 | |
|
211 | 0 | roleQuals.putAll(attributes); |
212 | |
} |
213 | 0 | if (StringUtils.isNotBlank(namespaceCode) && StringUtils.isNotBlank(permissionTemplateName)) { |
214 | 0 | LOG.info("Checking Permission '" + namespaceCode + "/" + permissionTemplateName + "' for user '" + user + "'"); |
215 | 0 | result = getPermissionService().isAuthorizedByTemplateName(user, namespaceCode, permissionTemplateName, null, roleQuals); |
216 | |
} |
217 | |
else { |
218 | 0 | LOG.info("Can not check Permission with namespace '" + namespaceCode + "' and template name '" + permissionTemplateName + "' for user '" + user + "'"); |
219 | 0 | return Boolean.TRUE; |
220 | |
} |
221 | 0 | } |
222 | |
else { |
223 | 0 | LOG.info("Will not check for document level permissions. Defaulting authorization to true."); |
224 | 0 | result = true; |
225 | |
} |
226 | 0 | LOG.info("Result of authorization check for user '" + user + "': " + result); |
227 | 0 | return Boolean.valueOf(result); |
228 | |
} |
229 | |
|
230 | |
public Map<String, Object> getDefaultFilterProperties(){ |
231 | 0 | Map<String, Object> filterProperties = new HashMap<String,Object>(); |
232 | 0 | filterProperties.put(MetadataFilter.METADATA_ID_TYPE, StudentIdentityConstants.QUALIFICATION_KEW_OBJECT_ID); |
233 | 0 | filterProperties.put(ProposalWorkflowFilter.WORKFLOW_USER, SecurityUtils.getCurrentUserId()); |
234 | |
|
235 | 0 | return filterProperties; |
236 | |
} |
237 | |
|
238 | |
protected DataSaveResult _saveData(Data data, Map<String, Object> filterProperties) throws OperationFailedException{ |
239 | |
try { |
240 | 0 | filterProperties.put(MetadataFilter.METADATA_ID_VALUE, (String)data.query("id")); |
241 | |
|
242 | 0 | Object dto = transformationManager.transform(data, getDtoClass(),filterProperties); |
243 | 0 | dto = save(dto, filterProperties); |
244 | |
|
245 | 0 | Data persistedData = transformationManager.transform(dto,getDtoClass().getName(), filterProperties); |
246 | 0 | return new DataSaveResult(null, persistedData); |
247 | 0 | } catch (DataValidationErrorException dvee){ |
248 | 0 | return new DataSaveResult(dvee.getValidationResults(), null); |
249 | 0 | } catch (Exception e) { |
250 | 0 | LOG.error("Unable to save", e); |
251 | 0 | throw new OperationFailedException("Unable to save"); |
252 | |
} |
253 | |
} |
254 | |
|
255 | |
protected boolean checkDocumentLevelPermissions() { |
256 | 0 | return false; |
257 | |
} |
258 | |
|
259 | |
|
260 | |
|
261 | |
public TransformationManager getTransformationManager() { |
262 | 0 | return transformationManager; |
263 | |
} |
264 | |
|
265 | |
public void setTransformationManager(TransformationManager transformationManager) { |
266 | 0 | this.transformationManager = transformationManager; |
267 | 0 | } |
268 | |
|
269 | |
public IdentityManagementService getPermissionService() { |
270 | 0 | return permissionService; |
271 | |
} |
272 | |
|
273 | |
public void setPermissionService(IdentityManagementService permissionService) { |
274 | 0 | this.permissionService = permissionService; |
275 | 0 | } |
276 | |
|
277 | |
public ProposalService getProposalService() { |
278 | 0 | return proposalService; |
279 | |
} |
280 | |
|
281 | |
public void setProposalService(ProposalService proposalService) { |
282 | 0 | this.proposalService = proposalService; |
283 | 0 | } |
284 | |
|
285 | |
protected abstract String getDefaultWorkflowDocumentType(); |
286 | |
|
287 | |
protected abstract String getDefaultMetaDataState(); |
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
protected abstract Object get(String id) throws Exception; |
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
protected abstract Object save(Object dto, Map<String, Object> properties) throws Exception; |
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
protected abstract List<ValidationResultInfo> validate(Object dto) throws Exception; |
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
protected abstract Class<?> getDtoClass(); |
326 | |
} |