| 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.TransformFilter.TransformFilterAction; |
| 17 | |
import org.kuali.student.common.assembly.transform.TransformationManager; |
| 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.exceptions.VersionMismatchException; |
| 23 | |
import org.kuali.student.common.rice.StudentIdentityConstants; |
| 24 | |
import org.kuali.student.common.rice.authorization.PermissionType; |
| 25 | |
import org.kuali.student.common.ui.client.service.DataSaveResult; |
| 26 | |
import org.kuali.student.common.ui.shared.IdAttributes; |
| 27 | |
import org.kuali.student.common.util.security.SecurityUtils; |
| 28 | |
import org.kuali.student.common.validation.dto.ValidationResultInfo; |
| 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,noRollbackFor={DoesNotExistException.class},rollbackFor={Throwable.class}) |
| 123 | |
public DataSaveResult saveData(Data data) throws OperationFailedException, DataValidationErrorException, VersionMismatchException{ |
| 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 (VersionMismatchException vme){ |
| 151 | 0 | throw vme; |
| 152 | 0 | }catch (Exception e) { |
| 153 | 0 | LOG.error("Failed to save data",e); |
| 154 | 0 | throw new OperationFailedException("Failed to save data",e); |
| 155 | 0 | } |
| 156 | |
|
| 157 | 0 | return saveResult; |
| 158 | |
} |
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
@Override |
| 163 | |
public List<ValidationResultInfo> validateData(Data data) throws OperationFailedException { |
| 164 | |
List<ValidationResultInfo> validationResults; |
| 165 | |
|
| 166 | |
try { |
| 167 | 0 | Metadata metadata = transformationManager.getUnfilteredMetadata(getDtoClass().getName()); |
| 168 | 0 | Object dto = transformationManager.getMapper().convertFromData(data, getDtoClass(), metadata); |
| 169 | 0 | validationResults = validate(dto); |
| 170 | 0 | } catch (Exception e) { |
| 171 | 0 | throw new OperationFailedException("Unable to validate data", e); |
| 172 | 0 | } |
| 173 | |
|
| 174 | 0 | return validationResults; |
| 175 | |
} |
| 176 | |
|
| 177 | |
@Override |
| 178 | |
public Boolean isAuthorized(PermissionType type, Map<String,String> attributes) { |
| 179 | 0 | String user = SecurityUtils.getCurrentUserId(); |
| 180 | 0 | boolean result = false; |
| 181 | 0 | if (checkDocumentLevelPermissions()) { |
| 182 | 0 | if (type == null) { |
| 183 | 0 | return null; |
| 184 | |
} |
| 185 | 0 | String namespaceCode = type.getPermissionNamespace(); |
| 186 | 0 | String permissionTemplateName = type.getPermissionTemplateName(); |
| 187 | |
|
| 188 | 0 | AttributeSet roleQuals = new AttributeSet(); |
| 189 | 0 | if (attributes != null) { |
| 190 | 0 | if (proposalService != null){ |
| 191 | 0 | ProposalInfo proposalInfo = null; |
| 192 | |
try { |
| 193 | 0 | if (attributes.containsKey(IdAttributes.IdType.KS_KEW_OBJECT_ID.toString())){ |
| 194 | 0 | proposalInfo = proposalService.getProposal(attributes.get(IdAttributes.IdType.KS_KEW_OBJECT_ID.toString())); |
| 195 | 0 | } else if (attributes.containsKey(IdAttributes.IdType.DOCUMENT_ID.toString())){ |
| 196 | 0 | proposalInfo = proposalService.getProposalByWorkflowId(attributes.get(IdAttributes.IdType.DOCUMENT_ID.toString())); |
| 197 | |
} |
| 198 | 0 | if (proposalInfo != null){ |
| 199 | 0 | attributes.put(IdAttributes.IdType.KS_KEW_OBJECT_ID.toString(), proposalInfo.getId()); |
| 200 | 0 | attributes.put(IdAttributes.IdType.DOCUMENT_ID.toString(), proposalInfo.getWorkflowId()); |
| 201 | 0 | attributes.put(StudentIdentityConstants.DOCUMENT_TYPE_NAME, proposalInfo.getType()); |
| 202 | |
} |
| 203 | 0 | } catch (Exception e){ |
| 204 | 0 | LOG.error("Could not retrieve proposal to determine permission qualifiers."); |
| 205 | 0 | } |
| 206 | |
} |
| 207 | 0 | roleQuals.putAll(attributes); |
| 208 | |
} |
| 209 | 0 | if (StringUtils.isNotBlank(namespaceCode) && StringUtils.isNotBlank(permissionTemplateName)) { |
| 210 | 0 | LOG.info("Checking Permission '" + namespaceCode + "/" + permissionTemplateName + "' for user '" + user + "'"); |
| 211 | 0 | result = getPermissionService().isAuthorizedByTemplateName(user, namespaceCode, permissionTemplateName, null, roleQuals); |
| 212 | |
} |
| 213 | |
else { |
| 214 | 0 | LOG.info("Can not check Permission with namespace '" + namespaceCode + "' and template name '" + permissionTemplateName + "' for user '" + user + "'"); |
| 215 | 0 | return Boolean.TRUE; |
| 216 | |
} |
| 217 | 0 | } |
| 218 | |
else { |
| 219 | 0 | LOG.info("Will not check for document level permissions. Defaulting authorization to true."); |
| 220 | 0 | result = true; |
| 221 | |
} |
| 222 | 0 | LOG.info("Result of authorization check for user '" + user + "': " + result); |
| 223 | 0 | return Boolean.valueOf(result); |
| 224 | |
} |
| 225 | |
|
| 226 | |
public Map<String, Object> getDefaultFilterProperties(){ |
| 227 | 0 | Map<String, Object> filterProperties = new HashMap<String,Object>(); |
| 228 | 0 | filterProperties.put(MetadataFilter.METADATA_ID_TYPE, StudentIdentityConstants.QUALIFICATION_KEW_OBJECT_ID); |
| 229 | 0 | filterProperties.put(ProposalWorkflowFilter.WORKFLOW_USER, SecurityUtils.getCurrentUserId()); |
| 230 | |
|
| 231 | 0 | return filterProperties; |
| 232 | |
} |
| 233 | |
|
| 234 | |
protected DataSaveResult _saveData(Data data, Map<String, Object> filterProperties) throws OperationFailedException{ |
| 235 | |
try { |
| 236 | 0 | filterProperties.put(MetadataFilter.METADATA_ID_VALUE, (String)data.query("id")); |
| 237 | |
|
| 238 | 0 | Object dto = transformationManager.transform(data, getDtoClass(),filterProperties); |
| 239 | 0 | dto = save(dto, filterProperties); |
| 240 | |
|
| 241 | 0 | Data persistedData = transformationManager.transform(dto,getDtoClass().getName(), filterProperties); |
| 242 | 0 | return new DataSaveResult(null, persistedData); |
| 243 | 0 | } catch (DataValidationErrorException dvee){ |
| 244 | 0 | return new DataSaveResult(dvee.getValidationResults(), null); |
| 245 | 0 | } catch (Exception e) { |
| 246 | 0 | LOG.error("Unable to save", e); |
| 247 | 0 | throw new OperationFailedException("Unable to save"); |
| 248 | |
} |
| 249 | |
} |
| 250 | |
|
| 251 | |
protected boolean checkDocumentLevelPermissions() { |
| 252 | 0 | return false; |
| 253 | |
} |
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
public TransformationManager getTransformationManager() { |
| 258 | 0 | return transformationManager; |
| 259 | |
} |
| 260 | |
|
| 261 | |
public void setTransformationManager(TransformationManager transformationManager) { |
| 262 | 0 | this.transformationManager = transformationManager; |
| 263 | 0 | } |
| 264 | |
|
| 265 | |
public IdentityManagementService getPermissionService() { |
| 266 | 0 | return permissionService; |
| 267 | |
} |
| 268 | |
|
| 269 | |
public void setPermissionService(IdentityManagementService permissionService) { |
| 270 | 0 | this.permissionService = permissionService; |
| 271 | 0 | } |
| 272 | |
|
| 273 | |
public ProposalService getProposalService() { |
| 274 | 0 | return proposalService; |
| 275 | |
} |
| 276 | |
|
| 277 | |
public void setProposalService(ProposalService proposalService) { |
| 278 | 0 | this.proposalService = proposalService; |
| 279 | 0 | } |
| 280 | |
|
| 281 | |
protected abstract String getDefaultWorkflowDocumentType(); |
| 282 | |
|
| 283 | |
protected abstract String getDefaultMetaDataState(); |
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
|
| 289 | |
|
| 290 | |
|
| 291 | |
|
| 292 | |
|
| 293 | |
protected abstract Object get(String id) throws Exception; |
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | |
protected abstract Object save(Object dto, Map<String, Object> properties) throws Exception; |
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
|
| 313 | |
|
| 314 | |
protected abstract List<ValidationResultInfo> validate(Object dto) throws Exception; |
| 315 | |
|
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | |
|
| 320 | |
|
| 321 | |
protected abstract Class<?> getDtoClass(); |
| 322 | |
} |