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