| 1 | |
package org.kuali.student.lum.program.client; |
| 2 | |
|
| 3 | |
import com.google.gwt.event.shared.EventHandler; |
| 4 | |
import com.google.gwt.event.shared.GwtEvent; |
| 5 | |
import com.google.gwt.event.shared.HandlerManager; |
| 6 | |
import com.google.gwt.i18n.client.DateTimeFormat; |
| 7 | |
import com.google.gwt.user.client.Window; |
| 8 | |
import org.kuali.student.common.ui.client.configurable.mvc.views.SectionView; |
| 9 | |
import org.kuali.student.common.ui.client.mvc.DataModel; |
| 10 | |
import org.kuali.student.common.ui.client.mvc.View; |
| 11 | |
import org.kuali.student.core.assembly.data.Data; |
| 12 | |
import org.kuali.student.core.assembly.data.ModelDefinition; |
| 13 | |
import org.kuali.student.core.assembly.data.QueryPath; |
| 14 | |
import org.kuali.student.core.validation.dto.ValidationResultInfo; |
| 15 | |
import org.kuali.student.lum.common.client.configuration.AbstractSectionConfiguration; |
| 16 | |
import org.kuali.student.lum.common.client.configuration.Configuration; |
| 17 | |
import org.kuali.student.lum.common.client.configuration.ConfigurationManager; |
| 18 | |
import org.kuali.student.lum.program.client.properties.ProgramProperties; |
| 19 | |
|
| 20 | |
import java.util.*; |
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
public class ProgramUtils { |
| 26 | |
|
| 27 | 0 | public static DateTimeFormat df = DateTimeFormat.getFormat("dd-MMM-yyyy"); |
| 28 | |
|
| 29 | 0 | private ProgramUtils() { |
| 30 | 0 | } |
| 31 | |
|
| 32 | |
public static void addCredentialProgramDataToVariation(Data variationData, DataModel model) { |
| 33 | 0 | Data credentialProgram = new Data(); |
| 34 | 0 | credentialProgram.set(ProgramConstants.INSTITUTION, model.<Data>get(ProgramConstants.CREDENTIAL_PROGRAM + "/" + ProgramConstants.INSTITUTION)); |
| 35 | 0 | credentialProgram.set(ProgramConstants.PROGRAM_LEVEL, model.<String>get(ProgramConstants.CREDENTIAL_PROGRAM + "/" + ProgramConstants.PROGRAM_LEVEL)); |
| 36 | 0 | credentialProgram.set(ProgramConstants.SHORT_TITLE, model.<String>get(ProgramConstants.CREDENTIAL_PROGRAM + "/" + ProgramConstants.SHORT_TITLE)); |
| 37 | |
|
| 38 | 0 | variationData.set(ProgramConstants.CREDENTIAL_PROGRAM, credentialProgram); |
| 39 | 0 | } |
| 40 | |
|
| 41 | |
public static Data createNewSpecializationBasedOnMajor(DataModel programModel) { |
| 42 | 0 | Data newSpecializationData = new Data(); |
| 43 | 0 | newSpecializationData.set(ProgramConstants.STATE, programModel.<String>get(ProgramConstants.STATE)); |
| 44 | 0 | newSpecializationData.set(ProgramConstants.TYPE, ProgramConstants.VARIATION_TYPE_KEY); |
| 45 | 0 | newSpecializationData.set(ProgramConstants.PROGRAM_REQUIREMENTS, new Data()); |
| 46 | 0 | addCredentialProgramDataToVariation(newSpecializationData, programModel); |
| 47 | 0 | return newSpecializationData; |
| 48 | |
} |
| 49 | |
|
| 50 | |
public static void setStatus(DataModel dataModel, String status) { |
| 51 | 0 | QueryPath statePath = new QueryPath(); |
| 52 | 0 | statePath.add(new Data.StringKey(ProgramConstants.STATE)); |
| 53 | 0 | dataModel.set(statePath, status); |
| 54 | 0 | setStatus((Data) dataModel.get(ProgramConstants.VARIATIONS), status); |
| 55 | 0 | } |
| 56 | |
|
| 57 | |
public static void setPreviousStatus(DataModel dataModel, String status) { |
| 58 | 0 | QueryPath statePath = QueryPath.parse(ProgramConstants.PREV_STATE); |
| 59 | 0 | dataModel.set(statePath, status); |
| 60 | 0 | } |
| 61 | |
|
| 62 | |
private static void setStatus(Data inputData, String status) { |
| 63 | 0 | if (inputData != null) { |
| 64 | 0 | for (Data.Property property : inputData) { |
| 65 | 0 | Data data = property.getValue(); |
| 66 | 0 | data.set(new Data.StringKey(ProgramConstants.STATE), status); |
| 67 | 0 | } |
| 68 | |
} |
| 69 | 0 | } |
| 70 | |
|
| 71 | |
public static void retrofitValidationResults(List<ValidationResultInfo> validationResults) { |
| 72 | 0 | for (ValidationResultInfo validationResult : validationResults) { |
| 73 | 0 | String key = validationResult.getElement(); |
| 74 | 0 | if (ProgramConstants.RICH_TEXT_KEYS.contains(key)) { |
| 75 | 0 | key = key + "/plain"; |
| 76 | 0 | validationResult.setElement(key); |
| 77 | |
} |
| 78 | 0 | } |
| 79 | 0 | } |
| 80 | |
|
| 81 | |
public static void handleValidationErrorsForSpecializations(List<ValidationResultInfo> validationResults, DataModel programModel) { |
| 82 | 0 | Set<Integer> failedSpecializations = new TreeSet<Integer>(); |
| 83 | 0 | for (ValidationResultInfo validationResult : validationResults) { |
| 84 | 0 | String element = validationResult.getElement(); |
| 85 | 0 | if (element.contains(ProgramConstants.VARIATIONS)) { |
| 86 | 0 | int specializationIndex = Integer.parseInt(element.split("/")[1]); |
| 87 | 0 | failedSpecializations.add(specializationIndex); |
| 88 | |
} |
| 89 | 0 | } |
| 90 | 0 | if (!failedSpecializations.isEmpty()) { |
| 91 | 0 | final Data variationMap = programModel.get(ProgramConstants.VARIATIONS); |
| 92 | 0 | StringBuilder validationMessage = new StringBuilder(); |
| 93 | 0 | for (Integer failedSpecialization : failedSpecializations) { |
| 94 | 0 | Data data = variationMap.get(failedSpecialization); |
| 95 | 0 | validationMessage.append(data.get(ProgramConstants.LONG_TITLE)).append(", "); |
| 96 | 0 | } |
| 97 | 0 | String resultMessage = validationMessage.toString(); |
| 98 | |
|
| 99 | 0 | resultMessage = resultMessage.substring(0, resultMessage.length() - 2); |
| 100 | 0 | if (failedSpecializations.size() == 1) { |
| 101 | 0 | Window.alert(ProgramProperties.get().major_variationFailed(resultMessage)); |
| 102 | |
} else { |
| 103 | 0 | Window.alert(ProgramProperties.get().major_variationsFailed(resultMessage)); |
| 104 | |
} |
| 105 | |
} |
| 106 | 0 | } |
| 107 | |
|
| 108 | |
|
| 109 | |
public static void syncMetadata(AbstractProgramConfigurer configurer, ModelDefinition modelDefinition) { |
| 110 | 0 | ConfigurationManager configurationManager = configurer.getProgramSectionConfigManager(); |
| 111 | 0 | for (Configuration conf : configurationManager.getConfigurations()) { |
| 112 | 0 | if (conf instanceof AbstractSectionConfiguration) { |
| 113 | 0 | AbstractSectionConfiguration configuration = (AbstractSectionConfiguration) conf; |
| 114 | 0 | View view = configuration.getView(false); |
| 115 | 0 | if (view != null && view instanceof SectionView) { |
| 116 | 0 | SectionView sectionView = (SectionView) view; |
| 117 | 0 | sectionView.updateMetadata(modelDefinition); |
| 118 | |
} |
| 119 | 0 | } |
| 120 | |
} |
| 121 | 0 | } |
| 122 | |
|
| 123 | |
public static void unregisterUnusedHandlers(HandlerManager eventBus) { |
| 124 | 0 | HashMap<GwtEvent.Type, EventHandler> eventsMap = ProgramRegistry.getSpecializationHandlers(); |
| 125 | 0 | if (eventsMap != null) { |
| 126 | 0 | for (Map.Entry<GwtEvent.Type, EventHandler> typeEventHandlerEntry : eventsMap.entrySet()) { |
| 127 | 0 | eventBus.removeHandler(typeEventHandlerEntry.getKey(), typeEventHandlerEntry.getValue()); |
| 128 | |
} |
| 129 | |
} |
| 130 | 0 | } |
| 131 | |
} |