1 | |
package org.kuali.student.lum.program.server.transform; |
2 | |
|
3 | |
import java.util.List; |
4 | |
import java.util.Map; |
5 | |
|
6 | |
import org.kuali.student.common.ui.client.mvc.DataModelDefinition; |
7 | |
import org.kuali.student.core.assembly.data.Data; |
8 | |
import org.kuali.student.core.assembly.data.Metadata; |
9 | |
import org.kuali.student.core.assembly.data.QueryPath; |
10 | |
import org.kuali.student.core.assembly.data.Data.DataType; |
11 | |
import org.kuali.student.core.assembly.data.Data.StringKey; |
12 | |
import org.kuali.student.core.assembly.transform.AbstractDataFilter; |
13 | |
import org.kuali.student.core.assembly.transform.MetadataFilter; |
14 | |
import org.kuali.student.lum.program.client.ProgramConstants; |
15 | |
import org.kuali.student.lum.program.dto.MajorDisciplineInfo; |
16 | |
import org.kuali.student.lum.program.dto.ProgramVariationInfo; |
17 | |
import org.kuali.student.lum.program.service.ProgramService; |
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | 0 | public class VersionProgramFilter extends AbstractDataFilter implements MetadataFilter{ |
27 | |
public static final String PREVIOUS_VERSION_DATA = "VersionProgramFilter.PreviousVersionData"; |
28 | |
public static final String PREVIOUS_VERSION_INFO = "previousVersionInfo"; |
29 | |
|
30 | |
private Metadata previousVersionMetadata; |
31 | |
protected ProgramService programService; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
@Override |
37 | |
public void applyInboundDataFilter(Data data, Metadata metadata, |
38 | |
Map<String, Object> properties) throws Exception { |
39 | 0 | Data previousVersionData = data.query(PREVIOUS_VERSION_INFO); |
40 | 0 | data.remove(new StringKey(PREVIOUS_VERSION_INFO)); |
41 | |
|
42 | 0 | if (previousVersionData != null){ |
43 | 0 | properties.put(PREVIOUS_VERSION_DATA, previousVersionData); |
44 | |
} |
45 | 0 | } |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
@Override |
52 | |
public void applyOutboundDataFilter(Data data, Metadata metadata, |
53 | |
Map<String, Object> properties) throws Exception { |
54 | 0 | Data previousVersionData = (Data)properties.get(PREVIOUS_VERSION_DATA); |
55 | 0 | String versionFromId = data.query(ProgramConstants.VERSION_FROM_ID); |
56 | |
|
57 | 0 | if (versionFromId != null){ |
58 | 0 | MajorDisciplineInfo previousVersionMajorInfo = programService.getMajorDiscipline(versionFromId); |
59 | |
|
60 | 0 | if (previousVersionData == null){ |
61 | |
|
62 | 0 | previousVersionData = new Data(); |
63 | 0 | previousVersionData.set(ProgramConstants.ID, previousVersionMajorInfo.getId()); |
64 | 0 | previousVersionData.set(ProgramConstants.END_PROGRAM_ENTRY_TERM, previousVersionMajorInfo.getEndProgramEntryTerm()); |
65 | 0 | previousVersionData.set(ProgramConstants.END_PROGRAM_ENROLL_TERM, previousVersionMajorInfo.getEndTerm()); |
66 | 0 | previousVersionData.set(ProgramConstants.STATE, previousVersionMajorInfo.getState()); |
67 | |
} else { |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | 0 | String state = previousVersionData.get(ProgramConstants.STATE); |
73 | 0 | if (state!= null && !state.equals(previousVersionMajorInfo.getState())){ |
74 | |
|
75 | 0 | String endEntryTerm = previousVersionData.get(ProgramConstants.END_PROGRAM_ENTRY_TERM); |
76 | 0 | String endEnrollTerm = previousVersionData.get(ProgramConstants.END_PROGRAM_ENROLL_TERM); |
77 | |
|
78 | 0 | previousVersionMajorInfo.setState(state); |
79 | 0 | previousVersionMajorInfo.setEndProgramEntryTerm(endEntryTerm); |
80 | 0 | previousVersionMajorInfo.setEndTerm(endEnrollTerm); |
81 | |
|
82 | |
|
83 | 0 | List<ProgramVariationInfo> variationList = previousVersionMajorInfo.getVariations(); |
84 | 0 | for (ProgramVariationInfo variation:variationList){ |
85 | 0 | variation.setState(state); |
86 | |
} |
87 | |
|
88 | 0 | programService.updateMajorDiscipline(previousVersionMajorInfo); |
89 | |
|
90 | |
|
91 | |
|
92 | 0 | String activatedMajorId = data.get(ProgramConstants.ID); |
93 | 0 | programService.setCurrentMajorDisciplineVersion(activatedMajorId, null); |
94 | |
} |
95 | |
|
96 | |
} |
97 | |
|
98 | 0 | data.set(PREVIOUS_VERSION_INFO, previousVersionData); |
99 | |
} |
100 | 0 | } |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
@Override |
106 | |
public void applyMetadataFilter(String dtoName, Metadata metadata, |
107 | |
Map<String, Object> filterProperties) { |
108 | 0 | metadata.getProperties().put(PREVIOUS_VERSION_INFO, getPreviousVersionMetadata(metadata)); |
109 | 0 | } |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
protected Metadata getPreviousVersionMetadata(Metadata metadata){ |
119 | 0 | if (previousVersionMetadata == null){ |
120 | 0 | DataModelDefinition modelDef = new DataModelDefinition(metadata); |
121 | 0 | Metadata programIdMeta = modelDef.getMetadata(QueryPath.parse(ProgramConstants.ID)); |
122 | 0 | Metadata programStateMeta = modelDef.getMetadata(QueryPath.parse(ProgramConstants.STATE)); |
123 | 0 | Metadata endEntryTermMeta = modelDef.getMetadata(QueryPath.parse(ProgramConstants.END_PROGRAM_ENTRY_TERM)); |
124 | 0 | Metadata endEnrollTermMeta = modelDef.getMetadata(QueryPath.parse(ProgramConstants.END_PROGRAM_ENROLL_TERM)); |
125 | |
|
126 | 0 | previousVersionMetadata = new Metadata(); |
127 | 0 | previousVersionMetadata.setDataType(DataType.DATA); |
128 | |
|
129 | 0 | Map<String, Metadata> properties = previousVersionMetadata.getProperties(); |
130 | 0 | properties.put(ProgramConstants.ID, programIdMeta); |
131 | 0 | properties.put(ProgramConstants.STATE, programStateMeta); |
132 | 0 | properties.put(ProgramConstants.END_PROGRAM_ENTRY_TERM, endEntryTermMeta); |
133 | 0 | properties.put(ProgramConstants.END_PROGRAM_ENROLL_TERM, endEnrollTermMeta); |
134 | |
} |
135 | |
|
136 | 0 | return previousVersionMetadata; |
137 | |
} |
138 | |
|
139 | |
public void setProgramService(ProgramService programService) { |
140 | 0 | this.programService = programService; |
141 | 0 | } |
142 | |
} |