View Javadoc

1   package org.kuali.student.lum.program.server;
2   
3   import java.util.HashMap;
4   import java.util.List;
5   import java.util.Map;
6   
7   import org.kuali.student.common.ui.server.gwt.AbstractDataService;
8   import org.kuali.student.core.assembly.transform.ProposalWorkflowFilter;
9   import org.kuali.student.lum.program.client.ProgramClientConstants;
10  import org.kuali.student.lum.program.client.ProgramConstants;
11  import org.kuali.student.r2.common.dto.ContextInfo;
12  import org.kuali.student.r2.common.dto.DtoConstants;
13  import org.kuali.student.r2.common.dto.ValidationResultInfo;
14  import org.kuali.student.r2.common.exceptions.InvalidParameterException;
15  import org.kuali.student.r2.common.exceptions.OperationFailedException;
16  import org.kuali.student.r2.common.util.ContextUtils;
17  import org.kuali.student.r2.common.util.constants.ProgramServiceConstants;
18  import org.kuali.student.r2.core.atp.dto.AtpInfo;
19  import org.kuali.student.r2.core.atp.service.AtpService;
20  import org.kuali.student.r2.core.versionmanagement.dto.VersionDisplayInfo;
21  import org.kuali.student.r2.lum.clu.service.CluService;
22  import org.kuali.student.r2.lum.program.dto.MajorDisciplineInfo;
23  import org.kuali.student.r2.lum.program.dto.ProgramVariationInfo;
24  import org.kuali.student.r2.lum.program.service.ProgramService;
25  
26  
27  /**
28   * @author Igor
29   */
30  public class MajorDisciplineProposalDataService extends AbstractDataService {
31  
32      private static final long serialVersionUID = 1L;
33      
34      private ProgramService programService;
35      private CluService cluService;
36      private AtpService atpService; 
37  
38      @Override
39      protected String getDefaultWorkflowDocumentType() {
40          return null;
41      }
42  
43      @Override
44      protected String getDefaultMetaDataState() {
45          return null;
46      }
47  
48      @Override
49      protected Object get(String id,ContextInfo contextInfo) throws Exception {
50      	//TODO Just Major Discipline for now - need to check for other types later
51          MajorDisciplineInfo returnDTO;
52          if (null == id || id.length() == 0) {
53              returnDTO = new MajorDisciplineInfo();
54              returnDTO.setTypeKey(ProgramClientConstants.MAJOR_PROGRAM);
55              returnDTO.setStateKey(DtoConstants.STATE_DRAFT);
56              returnDTO.setCredentialProgramId(getCredentialId());
57          } else {
58              returnDTO = programService.getMajorDiscipline(id,contextInfo);
59          }
60          return returnDTO;
61      }
62  
63      @Override
64      protected Object save(Object dto, Map<String, Object> properties,ContextInfo contextInfo) throws Exception {
65          if (dto instanceof MajorDisciplineInfo) {
66              MajorDisciplineInfo mdInfo = (MajorDisciplineInfo) dto;
67              if (mdInfo.getId() == null && mdInfo.getVersion() != null) {
68              	
69              	String majorVersionIndId = mdInfo.getVersion().getVersionIndId();
70              	
71              	//Get the current Major Dicipline from the service
72              	VersionDisplayInfo mdVersionInfo = programService.getCurrentVersion(ProgramServiceConstants.PROGRAM_NAMESPACE_MAJOR_DISCIPLINE_URI, majorVersionIndId,ContextUtils.getContextInfo());
73              	mdInfo = programService.getMajorDiscipline(mdVersionInfo.getId(),ContextUtils.getContextInfo());
74              	
75              	//set the prev start term to be the most recent of the major and all variations
76  				AtpInfo latestStartAtp = atpService.getAtp(mdInfo.getStartTerm(),contextInfo);
77  				for (ProgramVariationInfo variation:mdInfo.getVariations()){
78  					AtpInfo variationAtp = atpService.getAtp(variation.getStartTerm(),contextInfo);
79  					if(variationAtp!=null && variationAtp.getStartDate()!=null && variationAtp.getStartDate().compareTo(latestStartAtp.getStartDate())>0){
80  						latestStartAtp = variationAtp;
81  					}
82  				}
83  
84  				//Save the start and end terms from the old version and put into filter properties
85  				String startTerm = latestStartAtp.getId();
86  		    	String endTerm = mdInfo.getEndTerm();
87  		    	String endProgramEntryTerm = mdInfo.getEndProgramEntryTerm();
88  				String endInstAdmitTerm = mdInfo.getAttributeValue(ProgramConstants.END_INSTITUTIONAL_ADMIT_TERM);
89  		    	
90  		    	//
91  		    	Map<String,String> proposalAttributes = new HashMap<String,String>();
92  		    	if(startTerm!=null)
93  		    		proposalAttributes.put("prevStartTerm",startTerm);
94  		    	if(endTerm!=null)
95  		    		proposalAttributes.put("prevEndTerm",endTerm);
96  		    	if(endProgramEntryTerm!=null)
97  		    		proposalAttributes.put("prevEndProgramEntryTerm",endProgramEntryTerm);
98  		    	if(endInstAdmitTerm!=null)
99  		    		proposalAttributes.put("prevEndInstAdmitTerm",endInstAdmitTerm);
100 		    	properties.put(ProposalWorkflowFilter.PROPOSAL_ATTRIBUTES, proposalAttributes);
101             	
102             	mdInfo = programService.createNewMajorDisciplineVersion(majorVersionIndId, "New major discipline version",ContextUtils.getContextInfo());
103             } else if (mdInfo.getId() == null){
104                 mdInfo = programService.createMajorDiscipline(mdInfo.getId(),mdInfo,ContextUtils.getContextInfo());
105             } else {
106                 mdInfo = programService.updateMajorDiscipline(null, mdInfo,ContextUtils.getContextInfo());
107             }
108             return mdInfo;
109         } else {
110             throw new InvalidParameterException("Only persistence of MajorDiscipline is supported by this DataService implementation.");
111         }
112     }
113   
114     @Override
115 	protected List<ValidationResultInfo> validate(Object dto,ContextInfo contextInfo) throws Exception {
116 		return programService.validateMajorDiscipline("OBJECT", (MajorDisciplineInfo)dto,ContextUtils.getContextInfo());
117 	}
118 
119 	@Override
120     protected Class<?> getDtoClass() {
121         return MajorDisciplineInfo.class;
122     }
123 
124     private String getCredentialId() throws Exception {
125 
126             List<String> credIds = cluService.getCluIdsByLuType(ProgramClientConstants.CREDENTIAL_BACCALAUREATE_PROGRAM, DtoConstants.STATE_ACTIVE,ContextUtils.getContextInfo());
127             if (null == credIds || credIds.size() != 1) {
128                 throw new OperationFailedException("A single credential program of type " + ProgramClientConstants.CREDENTIAL_BACCALAUREATE_PROGRAM + " is required; database contains " +
129                                                     (null == credIds ? "0" : credIds.size() +
130                                                     "."));
131             }
132             return credIds.get(0);
133     }
134 
135     public void setProgramService(ProgramService programService) {
136         this.programService = programService;
137     }
138 
139     public void setCluService(CluService cluService) {
140         this.cluService = cluService;
141     }
142 
143 	public void setAtpService(AtpService atpService) {
144 		this.atpService = atpService;
145 	}
146 
147 }