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