| 1 |  |  package org.kuali.student.lum.program.server; | 
  | 2 |  |   | 
  | 3 |  |  import java.util.List; | 
  | 4 |  |   | 
  | 5 |  |  import org.kuali.student.common.dto.DtoConstants; | 
  | 6 |  |  import org.kuali.student.common.exceptions.DoesNotExistException; | 
  | 7 |  |  import org.kuali.student.common.exceptions.InvalidParameterException; | 
  | 8 |  |  import org.kuali.student.common.versionmanagement.dto.VersionDisplayInfo; | 
  | 9 |  |  import org.kuali.student.core.statement.dto.StatementTreeViewInfo; | 
  | 10 |  |  import org.kuali.student.lum.common.server.StatementUtil; | 
  | 11 |  |  import org.kuali.student.lum.program.dto.CoreProgramInfo; | 
  | 12 |  |  import org.kuali.student.lum.program.dto.ProgramRequirementInfo; | 
  | 13 |  |  import org.kuali.student.lum.program.service.ProgramService; | 
  | 14 |  |  import org.kuali.student.lum.program.service.ProgramServiceConstants; | 
  | 15 |  |  import org.springframework.transaction.annotation.Transactional; | 
  | 16 |  |   | 
  | 17 |  |   | 
  | 18 |  |   | 
  | 19 |  |   | 
  | 20 |  |   | 
  | 21 |  |   | 
  | 22 |  |   | 
  | 23 |  |   | 
  | 24 |  |   | 
  | 25 |  |   | 
  | 26 |  |   | 
  | 27 |  |   | 
  | 28 |  |  @Transactional(noRollbackFor = {DoesNotExistException.class}, rollbackFor = {Throwable.class}) | 
  | 29 | 0 |  public class CoreProgramStateChangeServiceImpl  implements StateChangeService { | 
  | 30 |  |   | 
  | 31 |  |       | 
  | 32 |  |      private ProgramService programService; | 
  | 33 |  |   | 
  | 34 |  |       | 
  | 35 |  |   | 
  | 36 |  |   | 
  | 37 |  |   | 
  | 38 |  |   | 
  | 39 |  |   | 
  | 40 |  |   | 
  | 41 |  |   | 
  | 42 |  |      public void changeState(String coreProgramId, String newState) throws Exception { | 
  | 43 |  |           | 
  | 44 |  |           | 
  | 45 | 0 |          changeState(null, null, null, coreProgramId, newState); | 
  | 46 | 0 |      } | 
  | 47 |  |   | 
  | 48 |  |       | 
  | 49 |  |   | 
  | 50 |  |   | 
  | 51 |  |   | 
  | 52 |  |   | 
  | 53 |  |   | 
  | 54 |  |   | 
  | 55 |  |   | 
  | 56 |  |   | 
  | 57 |  |   | 
  | 58 |  |   | 
  | 59 |  |      public void changeState(String endEntryTerm, String endEnrollTerm, String endInstAdmitTerm, String coreProgramId, String newState) throws Exception { | 
  | 60 |  |   | 
  | 61 |  |           | 
  | 62 | 0 |          if (newState == null) | 
  | 63 | 0 |              throw new InvalidParameterException("new state cannot be null"); | 
  | 64 |  |   | 
  | 65 |  |           | 
  | 66 | 0 |          CoreProgramInfo selectedVersion = programService.getCoreProgram(coreProgramId); | 
  | 67 |  |   | 
  | 68 |  |           | 
  | 69 |  |           | 
  | 70 | 0 |          if (newState.equals(DtoConstants.STATE_ACTIVE)) { | 
  | 71 |  |   | 
  | 72 |  |               | 
  | 73 | 0 |                  updatePreviousVersions(selectedVersion, endEntryTerm, endEnrollTerm, endInstAdmitTerm); | 
  | 74 |  |                   | 
  | 75 |  |               | 
  | 76 |  |               | 
  | 77 | 0 |              updateCoreProgramInfoState(selectedVersion, newState); | 
  | 78 |  |   | 
  | 79 |  |               | 
  | 80 | 0 |              makeCurrent(selectedVersion); | 
  | 81 |  |          } else { | 
  | 82 |  |   | 
  | 83 |  |               | 
  | 84 | 0 |              updateCoreProgramInfoState(selectedVersion, newState); | 
  | 85 |  |          } | 
  | 86 |  |   | 
  | 87 | 0 |      } | 
  | 88 |  |   | 
  | 89 |  |       | 
  | 90 |  |   | 
  | 91 |  |   | 
  | 92 |  |   | 
  | 93 |  |   | 
  | 94 |  |   | 
  | 95 |  |   | 
  | 96 |  |   | 
  | 97 |  |   | 
  | 98 |  |      private void updatePreviousVersions (CoreProgramInfo selectedVersion, String endEntryTerm, String endEnrollTerm, String endInstAdmitTerm) throws Exception { | 
  | 99 |  |               | 
  | 100 | 0 |              CoreProgramInfo currentVersion = getCurrentVersion(selectedVersion); | 
  | 101 |  |               | 
  | 102 | 0 |              boolean isSelectedVersionCurrent = selectedVersion.getId().equals(currentVersion.getId()); | 
  | 103 |  |               | 
  | 104 |  |               | 
  | 105 | 0 |              setEndTerms(currentVersion, endEntryTerm, endEnrollTerm); | 
  | 106 | 0 |              updateCoreProgramInfoState(currentVersion, DtoConstants.STATE_SUPERSEDED); | 
  | 107 |  |   | 
  | 108 |  |                   | 
  | 109 |  |                   | 
  | 110 |  |                   | 
  | 111 |  |   | 
  | 112 | 0 |                  List<VersionDisplayInfo> versions = programService.getVersions(ProgramServiceConstants.PROGRAM_NAMESPACE_MAJOR_DISCIPLINE_URI,  | 
  | 113 |  |                                  selectedVersion.getVersionInfo().getVersionIndId()); | 
  | 114 | 0 |                  Long startSeq = new Long(1); | 
  | 115 |  |   | 
  | 116 | 0 |                  if (!isSelectedVersionCurrent) { | 
  | 117 | 0 |                          startSeq = currentVersion.getVersionInfo().getSequenceNumber() + 1; | 
  | 118 |  |                  } | 
  | 119 |  |   | 
  | 120 | 0 |                  for (VersionDisplayInfo versionInfo : versions) { | 
  | 121 | 0 |                          boolean isVersionNewerThanCurrentVersion = versionInfo.getSequenceNumber() >= startSeq; | 
  | 122 | 0 |                          boolean isVersionSelectedVersion = versionInfo.getSequenceNumber().equals(selectedVersion.getVersionInfo().getSequenceNumber());   | 
  | 123 | 0 |                          boolean updateState = isVersionNewerThanCurrentVersion && !isVersionSelectedVersion; | 
  | 124 | 0 |                          if (updateState) { | 
  | 125 | 0 |                                  CoreProgramInfo otherProgram = programService.getCoreProgram(versionInfo.getId()); | 
  | 126 | 0 |                                  if (otherProgram.getState().equals(DtoConstants.STATE_APPROVED) || | 
  | 127 |  |                                          otherProgram.getState().equals(DtoConstants.STATE_ACTIVE)){ | 
  | 128 | 0 |                                  updateCoreProgramInfoState(otherProgram, DtoConstants.STATE_SUPERSEDED); | 
  | 129 |  |                                  }                 | 
  | 130 |  |                          } | 
  | 131 | 0 |                  }             | 
  | 132 |  |   | 
  | 133 | 0 |      } | 
  | 134 |  |   | 
  | 135 |  |           | 
  | 136 |  |   | 
  | 137 |  |   | 
  | 138 |  |   | 
  | 139 |  |   | 
  | 140 |  |          protected CoreProgramInfo getCurrentVersion(CoreProgramInfo coreProgramInfo) | 
  | 141 |  |                          throws Exception { | 
  | 142 |  |                   | 
  | 143 | 0 |                  String verIndId = coreProgramInfo.getVersionInfo().getVersionIndId(); | 
  | 144 |  |   | 
  | 145 |  |                   | 
  | 146 | 0 |                  VersionDisplayInfo curVerDisplayInfo = programService.getCurrentVersion( | 
  | 147 |  |                                  ProgramServiceConstants.PROGRAM_NAMESPACE_MAJOR_DISCIPLINE_URI, verIndId); | 
  | 148 | 0 |                  String curVerId = curVerDisplayInfo.getId(); | 
  | 149 |  |   | 
  | 150 |  |                   | 
  | 151 | 0 |                  CoreProgramInfo currentVersion = programService.getCoreProgram(curVerId); | 
  | 152 |  |   | 
  | 153 | 0 |                  return currentVersion; | 
  | 154 |  |          } | 
  | 155 |  |       | 
  | 156 |  |       | 
  | 157 |  |   | 
  | 158 |  |   | 
  | 159 |  |   | 
  | 160 |  |   | 
  | 161 |  |   | 
  | 162 |  |   | 
  | 163 |  |   | 
  | 164 |  |   | 
  | 165 |  |      private void setEndTerms(CoreProgramInfo coreProgramInfo, String endEntryTerm, String endEnrollTerm) { | 
  | 166 | 0 |          coreProgramInfo.setEndProgramEntryTerm(endEntryTerm); | 
  | 167 | 0 |          coreProgramInfo.setEndTerm(endEnrollTerm); | 
  | 168 | 0 |      } | 
  | 169 |  |   | 
  | 170 |  |       | 
  | 171 |  |   | 
  | 172 |  |   | 
  | 173 |  |   | 
  | 174 |  |   | 
  | 175 |  |   | 
  | 176 |  |   | 
  | 177 |  |   | 
  | 178 |  |      private void updateCoreProgramInfoState(CoreProgramInfo coreProgramInfo, String newState) throws Exception { | 
  | 179 |  |           | 
  | 180 | 0 |          List<String> programRequirementIds = coreProgramInfo.getProgramRequirements(); | 
  | 181 | 0 |          updateRequirementsState(programRequirementIds, newState); | 
  | 182 |  |           | 
  | 183 |  |           | 
  | 184 |  |    | 
  | 185 |  |   | 
  | 186 |  |           | 
  | 187 | 0 |          coreProgramInfo.setState(newState); | 
  | 188 | 0 |          programService.updateCoreProgram(coreProgramInfo); | 
  | 189 | 0 |      } | 
  | 190 |  |   | 
  | 191 |  |       | 
  | 192 |  |   | 
  | 193 |  |   | 
  | 194 |  |   | 
  | 195 |  |   | 
  | 196 |  |      private void makeCurrent(CoreProgramInfo coreProgramInfo) throws Exception { | 
  | 197 |  |   | 
  | 198 |  |           | 
  | 199 |  |           | 
  | 200 | 0 |          VersionDisplayInfo currentVersion = programService.getCurrentVersion(ProgramServiceConstants.PROGRAM_NAMESPACE_MAJOR_DISCIPLINE_URI, coreProgramInfo.getVersionInfo().getVersionIndId()); | 
  | 201 |  |   | 
  | 202 |  |           | 
  | 203 | 0 |          if (!currentVersion.getSequenceNumber().equals(coreProgramInfo.getVersionInfo().getSequenceNumber())) { | 
  | 204 | 0 |              programService.setCurrentCoreProgramVersion(coreProgramInfo.getId(), null); | 
  | 205 |  |          } | 
  | 206 | 0 |      } | 
  | 207 |  |   | 
  | 208 |  |       | 
  | 209 |  |   | 
  | 210 |  |   | 
  | 211 |  |   | 
  | 212 |  |   | 
  | 213 |  |   | 
  | 214 |  |   | 
  | 215 |  |   | 
  | 216 |  |   | 
  | 217 |  |      public void updateRequirementsState(List<String> programRequirementIds, String newState) throws Exception { | 
  | 218 |  |   | 
  | 219 | 0 |          for (String programRequirementId : programRequirementIds) { | 
  | 220 |  |   | 
  | 221 |  |               | 
  | 222 | 0 |              ProgramRequirementInfo programRequirementInfo = programService.getProgramRequirement(programRequirementId, null, null); | 
  | 223 |  |   | 
  | 224 |  |               | 
  | 225 | 0 |              StatementTreeViewInfo statementTree = programRequirementInfo.getStatement(); | 
  | 226 |  |   | 
  | 227 |  |               | 
  | 228 | 0 |              StatementUtil.updateStatementTreeViewInfoState(newState, statementTree); | 
  | 229 |  |   | 
  | 230 |  |               | 
  | 231 | 0 |              programRequirementInfo.setState(newState); | 
  | 232 |  |   | 
  | 233 |  |               | 
  | 234 | 0 |              programService.updateProgramRequirement(programRequirementInfo); | 
  | 235 |  |   | 
  | 236 | 0 |          } | 
  | 237 | 0 |      } | 
  | 238 |  |   | 
  | 239 |  |       | 
  | 240 |  |   | 
  | 241 |  |   | 
  | 242 |  |   | 
  | 243 |  |   | 
  | 244 |  |      public void setProgramService(ProgramService programService) { | 
  | 245 | 0 |          this.programService = programService; | 
  | 246 | 0 |      } | 
  | 247 |  |   | 
  | 248 |  |  } |