View Javadoc

1   /*
2    * Copyright 2007 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 1.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl1.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific  language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.student.r2.lum.program.service.assembler;
17  
18  import java.util.List;
19  
20  import org.apache.log4j.Logger;
21  import org.kuali.student.common.conversion.util.R1R2ConverterUtil;
22  import org.kuali.student.r2.lum.service.assembler.CluAssemblerUtils;
23  import org.kuali.student.r1.common.assembly.BOAssembler;
24  import org.kuali.student.r1.common.assembly.BaseDTOAssemblyNode;
25  import org.kuali.student.r1.common.assembly.BaseDTOAssemblyNode.NodeOperation;
26  import org.kuali.student.r2.common.dto.AmountInfo;
27  import org.kuali.student.r2.lum.program.dto.assembly.ProgramAtpAssembly;
28  import org.kuali.student.r2.lum.program.dto.assembly.ProgramBasicOrgAssembly;
29  import org.kuali.student.r2.lum.program.dto.assembly.ProgramCodeAssembly;
30  import org.kuali.student.r2.lum.program.dto.assembly.ProgramFullOrgAssembly;
31  import org.kuali.student.r2.lum.program.dto.assembly.ProgramIdentifierAssembly;
32  import org.kuali.student.r2.lum.program.dto.assembly.ProgramPublicationAssembly;
33  import org.kuali.student.r2.lum.program.dto.assembly.ProgramRequirementAssembly;
34  import org.kuali.student.r2.common.assembler.AssemblyException;
35  import org.kuali.student.r2.common.dto.ContextInfo;
36  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
37  import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
38  import org.kuali.student.r2.lum.clu.dto.CluInfo;
39  import org.kuali.student.r2.lum.clu.service.CluService;
40  import org.kuali.student.r2.lum.course.dto.LoDisplayInfo;
41  import org.kuali.student.r2.lum.program.dto.ProgramVariationInfo;
42  import org.kuali.student.r2.lum.program.dto.assembly.ProgramCommonAssembly;
43  
44  /**
45   * @author KS
46   *
47   */
48  public class ProgramVariationAssembler implements BOAssembler<ProgramVariationInfo, CluInfo> {
49      final static Logger LOG = Logger.getLogger(ProgramVariationAssembler.class);
50  
51      private CluService cluService;
52      private CluAssemblerUtils cluAssemblerUtils;
53      private ProgramAssemblerUtils programAssemblerUtils;
54  
55  
56  
57      @Override
58      public ProgramVariationInfo assemble(CluInfo baseDTO, ProgramVariationInfo businessDTO, boolean shallowBuild, ContextInfo contextInfo) throws AssemblyException, PermissionDeniedException {
59  
60          ProgramVariationInfo pvInfo = (null != businessDTO) ? businessDTO : new ProgramVariationInfo();
61  
62          // Copy all the data from the clu to the programvariation
63          programAssemblerUtils.assembleBasics(baseDTO, (ProgramCommonAssembly) pvInfo, contextInfo);
64          programAssemblerUtils.assembleIdentifiers(baseDTO, (ProgramIdentifierAssembly) pvInfo);
65          programAssemblerUtils.assembleBasicAdminOrgs(baseDTO, (ProgramBasicOrgAssembly) pvInfo);
66          programAssemblerUtils.assembleFullOrgs(baseDTO, (ProgramFullOrgAssembly) pvInfo);
67          programAssemblerUtils.assembleAtps(baseDTO, (ProgramAtpAssembly) pvInfo);
68          programAssemblerUtils.assembleLuCodes(baseDTO, (ProgramCodeAssembly) pvInfo);
69          programAssemblerUtils.assemblePublications(baseDTO, (ProgramPublicationAssembly) pvInfo, contextInfo);
70          
71          if (!shallowBuild) {
72          	programAssemblerUtils.assembleRequirements(baseDTO, (ProgramRequirementAssembly) pvInfo, contextInfo);
73          	pvInfo.setResultOptions(programAssemblerUtils.assembleResultOptions(baseDTO.getId(), contextInfo));
74              pvInfo.setLearningObjectives(cluAssemblerUtils.assembleLos(baseDTO.getId(), shallowBuild,contextInfo));
75          }
76          
77          pvInfo.setIntensity((null != baseDTO.getIntensity()) ? baseDTO.getIntensity().getUnitTypeKey() : null);
78          pvInfo.setStdDuration(baseDTO.getStdDuration());
79          pvInfo.setCampusLocations(baseDTO.getCampusLocations());
80          pvInfo.setEffectiveDate(baseDTO.getEffectiveDate());
81          pvInfo.setDescr(baseDTO.getDescr());
82          pvInfo.setVersion(baseDTO.getVersion());
83  
84          return pvInfo;
85      }
86  
87  
88      @Override
89      public BaseDTOAssemblyNode<ProgramVariationInfo, CluInfo> disassemble(ProgramVariationInfo businessDTO, NodeOperation operation, ContextInfo contextInfo) throws AssemblyException {
90      	BaseDTOAssemblyNode<ProgramVariationInfo, CluInfo> result = new BaseDTOAssemblyNode<ProgramVariationInfo, CluInfo>(this);
91      	
92      	if (businessDTO == null) {
93  			// FIXME Unsure now if this is an exception or just return null or
94  			// empty assemblyNode
95  		    LOG.error("Variation to disassemble is null!");
96  			throw new AssemblyException("Variation can not be null");
97  		}
98  
99  		CluInfo clu;
100 		try {
101 			clu = (NodeOperation.UPDATE == operation) ? cluService.getClu(businessDTO.getId(), contextInfo) : new CluInfo();
102         } catch (Exception e) {
103 			throw new AssemblyException("Error getting existing learning unit during variation update", e);
104         } 
105         
106         boolean stateChanged = NodeOperation.UPDATE == operation && businessDTO.getStateKey() != null && !businessDTO.getStateKey().equals(clu.getStateKey());
107         
108         clu.setStateKey(businessDTO.getStateKey());
109         programAssemblerUtils.disassembleBasics(clu, (ProgramCommonAssembly) businessDTO);
110         if (businessDTO.getId() == null)
111             businessDTO.setId(clu.getId());
112         programAssemblerUtils.disassembleIdentifiers(clu, (ProgramIdentifierAssembly) businessDTO, operation);
113         programAssemblerUtils.disassembleAdminOrgs(clu, (ProgramBasicOrgAssembly) businessDTO, operation);
114         programAssemblerUtils.disassembleAtps(clu, (ProgramAtpAssembly) businessDTO, operation);
115         programAssemblerUtils.disassembleLuCodes(clu, (ProgramCodeAssembly) businessDTO, operation);
116         programAssemblerUtils.disassemblePublications(clu, (ProgramPublicationAssembly) businessDTO, operation, result, contextInfo);
117 
118         if(businessDTO.getProgramRequirements() != null && !businessDTO.getProgramRequirements().isEmpty()) {
119         	programAssemblerUtils.disassembleRequirements(clu, (ProgramRequirementAssembly) businessDTO, operation, result, stateChanged, contextInfo);
120         }
121         
122         if (businessDTO.getResultOptions() != null) {
123             disassembleResultOptions(businessDTO, operation, result, contextInfo);
124         }
125 
126         if (businessDTO.getLearningObjectives() != null) {
127             disassembleLearningObjectives(businessDTO, operation, result,contextInfo);
128         }
129  
130 		AmountInfo intensity = new AmountInfo();
131 		intensity.setUnitTypeKey(businessDTO.getIntensity());
132 		clu.setIntensity(intensity);
133 		clu.setStdDuration(businessDTO.getStdDuration());
134         clu.setCampusLocations(businessDTO.getCampusLocations());
135         clu.setEffectiveDate(businessDTO.getEffectiveDate());
136         clu.setDescr(businessDTO.getDescr());
137         clu.setVersion(businessDTO.getVersion());
138         
139 		// Add the Clu to the result
140 		result.setNodeData(clu);
141 		result.setOperation(operation);
142 		result.setBusinessDTORef(businessDTO);
143 		return result;
144     	
145     }
146 
147     private void disassembleLearningObjectives(ProgramVariationInfo variation, NodeOperation operation, BaseDTOAssemblyNode<ProgramVariationInfo, CluInfo> result,ContextInfo contextInfo) throws AssemblyException {
148     	try {
149             List<BaseDTOAssemblyNode<?, ?>> loResults = cluAssemblerUtils.disassembleLos(variation.getId(), variation.getStateKey(), (List<LoDisplayInfo>) variation.getLearningObjectives(), operation,contextInfo);
150             if (loResults != null) {
151                 result.getChildNodes().addAll(loResults);
152             }
153         } catch (DoesNotExistException e) {
154         } catch (Exception e) {
155             throw new AssemblyException("Error while disassembling los", e);
156         }
157     }
158 
159     private void disassembleResultOptions(ProgramVariationInfo variation, NodeOperation operation, BaseDTOAssemblyNode<ProgramVariationInfo, CluInfo> result, ContextInfo contextInfo) throws AssemblyException {
160         BaseDTOAssemblyNode<?, ?> resultOptions = cluAssemblerUtils.disassembleCluResults(variation.getId(), variation.getStateKey(), variation.getResultOptions(), operation, ProgramAssemblerConstants.DEGREE_RESULTS, "Result options", "Result option", contextInfo);
161         if (resultOptions != null) {
162             result.getChildNodes().add(resultOptions);           
163         }
164     }
165     
166     // Spring setter
167     public void setCluService(CluService cluService) {
168         this.cluService = cluService;
169     }
170     
171     public void setCluAssemblerUtils(CluAssemblerUtils cluAssemblerUtils) {
172         this.cluAssemblerUtils = cluAssemblerUtils;
173     }
174 
175     public void setProgramAssemblerUtils(ProgramAssemblerUtils programAssemblerUtils) {
176         this.programAssemblerUtils = programAssemblerUtils;
177     }
178 
179 
180 }