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