View Javadoc

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