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