| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.student.common.ui.client.application; |
| 17 | |
|
| 18 | |
import java.util.ArrayList; |
| 19 | |
import java.util.HashMap; |
| 20 | |
import java.util.HashSet; |
| 21 | |
import java.util.Iterator; |
| 22 | |
import java.util.List; |
| 23 | |
import java.util.Map; |
| 24 | |
import java.util.Map.Entry; |
| 25 | |
|
| 26 | |
import org.kuali.student.common.messages.dto.Message; |
| 27 | |
import org.kuali.student.common.ui.client.configurable.mvc.FieldDescriptor; |
| 28 | |
import org.kuali.student.common.ui.client.mvc.HasCrossConstraints; |
| 29 | |
import org.kuali.student.common.ui.client.security.SecurityContext; |
| 30 | |
import org.kuali.student.common.ui.client.service.ServerPropertiesRpcService; |
| 31 | |
import org.kuali.student.common.ui.client.service.ServerPropertiesRpcServiceAsync; |
| 32 | |
import org.kuali.student.common.validation.dto.ValidationResultInfo; |
| 33 | |
import org.kuali.student.common.validation.dto.ValidationResultInfo.ErrorLevel; |
| 34 | |
|
| 35 | |
import com.google.gwt.core.client.GWT; |
| 36 | |
import com.google.gwt.user.client.rpc.AsyncCallback; |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | 0 | public class ApplicationContext { |
| 47 | 0 | private ServerPropertiesRpcServiceAsync serverPropertiesRpcService = GWT.create(ServerPropertiesRpcService.class); |
| 48 | |
|
| 49 | 0 | private boolean loggedIn = true; |
| 50 | 0 | private String userId = "testuser"; |
| 51 | 0 | private String version = "KS"; |
| 52 | 0 | private List<String> roles = new ArrayList<String>(); |
| 53 | |
|
| 54 | 0 | private Map<String, Map<String, String>> messages = new HashMap<String, Map<String,String>>(); |
| 55 | 0 | private Map<String, String> flatMessages = new HashMap<String, String>(); |
| 56 | 0 | private List<Message> messagesList = new ArrayList<Message>(); |
| 57 | |
|
| 58 | |
private SecurityContext securityContext; |
| 59 | |
private String applicationContextUrl; |
| 60 | |
|
| 61 | |
|
| 62 | 0 | private String parentPath = ""; |
| 63 | 0 | private HashMap<String,HashMap<String,FieldDescriptor>> pathToFieldMapping = new HashMap<String,HashMap<String,FieldDescriptor>>(); |
| 64 | 0 | private HashMap<String,HashMap<String,HashSet<HasCrossConstraints>>> crossConstraints = new HashMap<String,HashMap<String,HashSet<HasCrossConstraints>>>(); |
| 65 | 0 | private List<ValidationResultInfo> validationWarnings = new ArrayList<ValidationResultInfo>(); |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | 0 | protected ApplicationContext() { |
| 72 | 0 | roles.add("role1"); |
| 73 | 0 | roles.add("role2"); |
| 74 | |
|
| 75 | 0 | serverPropertiesRpcService.getContextPath(new AsyncCallback<String>(){ |
| 76 | |
|
| 77 | |
@Override |
| 78 | |
public void onFailure(Throwable caught) { |
| 79 | 0 | throw new RuntimeException("Fatal - Unable to initialze application context"); |
| 80 | |
} |
| 81 | |
|
| 82 | |
@Override |
| 83 | |
public void onSuccess(String result) { |
| 84 | 0 | applicationContextUrl = result; |
| 85 | 0 | } |
| 86 | |
}); |
| 87 | 0 | } |
| 88 | |
|
| 89 | |
public void setLoggedIn(boolean loggedIn) { |
| 90 | 0 | this.loggedIn = loggedIn; |
| 91 | 0 | } |
| 92 | |
|
| 93 | |
public void setUserId(String userId) { |
| 94 | 0 | this.userId = userId; |
| 95 | 0 | } |
| 96 | |
|
| 97 | |
public void setRoles(List<String> roles) { |
| 98 | 0 | this.roles = roles; |
| 99 | 0 | } |
| 100 | |
|
| 101 | |
public boolean isLoggedIn() { |
| 102 | 0 | return loggedIn; |
| 103 | |
} |
| 104 | |
|
| 105 | |
public String getUserId() { |
| 106 | 0 | return userId; |
| 107 | |
} |
| 108 | |
|
| 109 | |
public List<String> getRoles() { |
| 110 | 0 | return roles; |
| 111 | |
} |
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
public void addMessages(List<Message> messages) { |
| 118 | 0 | messagesList.addAll(messages); |
| 119 | 0 | for (Message m : messages) { |
| 120 | 0 | String groupName = m.getGroupName(); |
| 121 | 0 | Map<String, String> group = this.messages.get(groupName); |
| 122 | 0 | if (group == null) { |
| 123 | 0 | group = new HashMap<String, String>(); |
| 124 | 0 | this.messages.put(groupName, group); |
| 125 | |
} |
| 126 | 0 | group.put(m.getId(), m.getValue()); |
| 127 | 0 | flatMessages.put(m.getId(), m.getValue()); |
| 128 | 0 | } |
| 129 | 0 | } |
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
public String getMessage(String messageId) { |
| 135 | 0 | return flatMessages.get(messageId); |
| 136 | |
} |
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
public List<Message> getMessages() { |
| 142 | 0 | return messagesList; |
| 143 | |
} |
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
public String getMessage(String groupName, String messageId) { |
| 150 | |
|
| 151 | 0 | String result = null; |
| 152 | |
|
| 153 | 0 | Map<String, String> group = this.messages.get(groupName); |
| 154 | 0 | if (group != null) { |
| 155 | 0 | result = group.get(messageId); |
| 156 | |
} |
| 157 | |
|
| 158 | 0 | return result; |
| 159 | |
} |
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
public String getUILabel(String groupName, String type, String state, String fieldId) { |
| 176 | |
|
| 177 | 0 | String label = getMessage(groupName, type + ":" + state + ":" + fieldId); |
| 178 | |
|
| 179 | 0 | if (label == null) |
| 180 | 0 | label = getMessage(groupName, fieldId); |
| 181 | |
|
| 182 | 0 | if (label == null) |
| 183 | 0 | label = fieldId; |
| 184 | |
|
| 185 | 0 | return label; |
| 186 | |
|
| 187 | |
} |
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
public String getUILabel(String groupName, String fieldId) { |
| 194 | |
|
| 195 | 0 | String label = getMessage(groupName, fieldId); |
| 196 | |
|
| 197 | 0 | if (label == null) |
| 198 | 0 | label = fieldId; |
| 199 | |
|
| 200 | 0 | return label; |
| 201 | |
|
| 202 | |
} |
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
public SecurityContext getSecurityContext() { |
| 209 | 0 | return securityContext; |
| 210 | |
} |
| 211 | |
|
| 212 | |
public void setSecurityContext(SecurityContext securityContext) { |
| 213 | 0 | this.securityContext = securityContext; |
| 214 | 0 | } |
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
public String getApplicationContextUrl() { |
| 220 | 0 | return applicationContextUrl; |
| 221 | |
} |
| 222 | |
|
| 223 | |
public void setVersion(String version) { |
| 224 | 0 | this.version = version; |
| 225 | 0 | } |
| 226 | |
|
| 227 | |
public String getVersion() { |
| 228 | 0 | return version; |
| 229 | |
} |
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
public void putCrossConstraint(String namespace, String path, HasCrossConstraints fd){ |
| 238 | 0 | if(namespace==null){ |
| 239 | 0 | namespace="_default"; |
| 240 | |
} |
| 241 | |
|
| 242 | 0 | HashMap<String,HashSet<HasCrossConstraints>> crossConstraintMap = crossConstraints.get(namespace); |
| 243 | 0 | if(crossConstraintMap==null){ |
| 244 | 0 | crossConstraintMap = new HashMap<String,HashSet<HasCrossConstraints>>(); |
| 245 | 0 | crossConstraints.put(namespace, crossConstraintMap); |
| 246 | |
} |
| 247 | 0 | HashSet<HasCrossConstraints> fieldDescriptors = crossConstraintMap.get(path); |
| 248 | 0 | if(fieldDescriptors == null){ |
| 249 | 0 | fieldDescriptors = new HashSet<HasCrossConstraints>(); |
| 250 | 0 | crossConstraintMap.put(path, fieldDescriptors); |
| 251 | |
} |
| 252 | 0 | fieldDescriptors.add(fd); |
| 253 | 0 | } |
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
public HashSet<HasCrossConstraints> getCrossConstraint(String namespace, String path){ |
| 258 | 0 | if(namespace==null){ |
| 259 | 0 | namespace="_default"; |
| 260 | |
} |
| 261 | 0 | HashMap<String,HashSet<HasCrossConstraints>> crossConstraintMap = crossConstraints.get(namespace); |
| 262 | 0 | if(crossConstraintMap!=null){ |
| 263 | 0 | return crossConstraintMap.get(path); |
| 264 | |
} |
| 265 | 0 | return null; |
| 266 | |
} |
| 267 | |
public void clearCrossConstraintMap(String namespace){ |
| 268 | 0 | if(namespace==null){ |
| 269 | 0 | namespace="_default"; |
| 270 | |
} |
| 271 | 0 | crossConstraints.remove(namespace); |
| 272 | 0 | } |
| 273 | |
public void putPathToFieldMapping(String namespace, String path, FieldDescriptor fd){ |
| 274 | 0 | if(namespace==null){ |
| 275 | 0 | namespace="_default"; |
| 276 | |
} |
| 277 | |
|
| 278 | 0 | HashMap<String,FieldDescriptor> pathToField = pathToFieldMapping.get(namespace); |
| 279 | 0 | if(pathToField==null){ |
| 280 | 0 | pathToField = new HashMap<String,FieldDescriptor>(); |
| 281 | 0 | pathToFieldMapping.put(namespace, pathToField); |
| 282 | |
} |
| 283 | 0 | pathToField.put(path, fd); |
| 284 | 0 | } |
| 285 | |
|
| 286 | |
public FieldDescriptor getPathToFieldMapping(String namespace, String path){ |
| 287 | 0 | if(namespace==null){ |
| 288 | 0 | namespace="_default"; |
| 289 | |
} |
| 290 | |
|
| 291 | 0 | HashMap<String,FieldDescriptor> pathToField = pathToFieldMapping.get(namespace); |
| 292 | 0 | if(pathToField!=null){ |
| 293 | 0 | return pathToField.get(path); |
| 294 | |
} |
| 295 | 0 | return null; |
| 296 | |
} |
| 297 | |
public void clearPathToFieldMapping(String namespace){ |
| 298 | 0 | if(namespace==null){ |
| 299 | 0 | namespace="_default"; |
| 300 | |
} |
| 301 | 0 | pathToFieldMapping.remove(namespace); |
| 302 | 0 | } |
| 303 | |
|
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
public void clearCrossConstraintsWithStartingPath(String namespace, String pathPrefix){ |
| 312 | 0 | if(namespace==null){ |
| 313 | 0 | namespace="_default"; |
| 314 | |
} |
| 315 | |
|
| 316 | 0 | HashMap<String,HashSet<HasCrossConstraints>> crossConstraintMap = crossConstraints.get(namespace); |
| 317 | 0 | if(crossConstraintMap!=null){ |
| 318 | 0 | Iterator<Map.Entry<String,HashSet<HasCrossConstraints>>> constraintMapIter = crossConstraintMap.entrySet().iterator(); |
| 319 | 0 | while(constraintMapIter.hasNext()){ |
| 320 | 0 | Map.Entry<String,HashSet<HasCrossConstraints>> entry = constraintMapIter.next(); |
| 321 | 0 | if(entry.getKey().startsWith(pathPrefix)){ |
| 322 | 0 | constraintMapIter.remove(); |
| 323 | |
} |
| 324 | 0 | } |
| 325 | |
|
| 326 | |
|
| 327 | 0 | HashMap<String,FieldDescriptor> pathToField = pathToFieldMapping.get(namespace); |
| 328 | 0 | if(pathToField!=null){ |
| 329 | 0 | Iterator<Entry<String, FieldDescriptor>> pathMapIter = pathToField.entrySet().iterator(); |
| 330 | 0 | while(pathMapIter.hasNext()){ |
| 331 | 0 | Entry<String, FieldDescriptor> entry = pathMapIter.next(); |
| 332 | 0 | if(entry.getKey().startsWith(pathPrefix)){ |
| 333 | 0 | FieldDescriptor fd = entry.getValue(); |
| 334 | 0 | if(fd.getFieldWidget()!=null && fd.getFieldWidget() instanceof HasCrossConstraints && ((HasCrossConstraints)fd.getFieldWidget()).getCrossConstraints()!=null){ |
| 335 | |
|
| 336 | 0 | for(String path:((HasCrossConstraints)fd.getFieldWidget()).getCrossConstraints()){ |
| 337 | 0 | HashSet<HasCrossConstraints> set = crossConstraintMap.get(path); |
| 338 | 0 | if(set!=null){ |
| 339 | 0 | set.remove(fd.getFieldWidget()); |
| 340 | |
} |
| 341 | 0 | } |
| 342 | |
} |
| 343 | |
} |
| 344 | 0 | } |
| 345 | |
} |
| 346 | |
} |
| 347 | 0 | } |
| 348 | |
|
| 349 | |
public HashSet<HasCrossConstraints> getCrossConstraints(String namespace) { |
| 350 | 0 | if(namespace==null){ |
| 351 | 0 | namespace="_default"; |
| 352 | |
} |
| 353 | 0 | HashSet<HasCrossConstraints> results = new HashSet<HasCrossConstraints>(); |
| 354 | 0 | HashMap<String,HashSet<HasCrossConstraints>> crossConstraintMap = crossConstraints.get(namespace); |
| 355 | 0 | if(crossConstraintMap!=null){ |
| 356 | 0 | for(HashSet<HasCrossConstraints> fds: crossConstraintMap.values()){ |
| 357 | 0 | results.addAll(fds); |
| 358 | |
} |
| 359 | |
} |
| 360 | 0 | return results; |
| 361 | |
} |
| 362 | |
|
| 363 | |
|
| 364 | |
public List<ValidationResultInfo> getValidationWarnings() { |
| 365 | 0 | return validationWarnings; |
| 366 | |
} |
| 367 | |
|
| 368 | |
|
| 369 | |
|
| 370 | |
|
| 371 | |
|
| 372 | |
|
| 373 | |
public void addValidationWarnings(List<ValidationResultInfo> validationResults) { |
| 374 | 0 | if (validationResults != null){ |
| 375 | 0 | for (ValidationResultInfo vr:validationResults){ |
| 376 | 0 | if (vr.getErrorLevel() == ErrorLevel.WARN){ |
| 377 | 0 | this.validationWarnings.add(vr); |
| 378 | |
} |
| 379 | |
} |
| 380 | |
} |
| 381 | 0 | } |
| 382 | |
|
| 383 | |
public void clearValidationWarnings(){ |
| 384 | 0 | validationWarnings.clear(); |
| 385 | 0 | } |
| 386 | |
|
| 387 | |
public String getParentPath() { |
| 388 | 0 | return parentPath; |
| 389 | |
} |
| 390 | |
|
| 391 | |
public void setParentPath(String parentPath) { |
| 392 | 0 | this.parentPath = parentPath; |
| 393 | 0 | } |
| 394 | |
|
| 395 | |
|
| 396 | |
} |