001/*
002 * Copyright 2008 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 1.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl1.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.student.r2.lum.course.service.assembler;
017
018import org.kuali.student.common.util.UUIDHelper;
019import org.kuali.student.r1.common.assembly.BOAssembler;
020import org.kuali.student.r1.common.assembly.BaseDTOAssemblyNode;
021import org.kuali.student.r1.common.assembly.BaseDTOAssemblyNode.NodeOperation;
022import org.kuali.student.r2.common.assembler.AssemblyException;
023import org.kuali.student.r2.common.dto.ContextInfo;
024import org.kuali.student.r2.lum.clu.dto.CluCluRelationInfo;
025import org.kuali.student.r2.lum.clu.dto.CluInfo;
026import org.kuali.student.r2.lum.clu.service.CluService;
027import org.kuali.student.r2.lum.course.dto.CourseJointInfo;
028/**
029 * Assembles/Disassembles CourseJointInfo DTO from/to CluCluRelationInfo 
030 * 
031 * @author Kuali Student Team
032 *
033 */
034public class CourseJointAssembler implements BOAssembler<CourseJointInfo, CluCluRelationInfo> {
035                
036        CluService cluService;          
037        
038        /**
039         * @return the cluService
040         */
041        public CluService getCluService() {
042                return cluService;
043        }
044
045        /**
046         * @param cluService the cluService to set
047         */
048        public void setCluService(CluService cluService) {
049                this.cluService = cluService;
050        }
051
052        @Override
053        public CourseJointInfo assemble(CluCluRelationInfo cluRel, CourseJointInfo jointInfo, boolean shallowBuild, ContextInfo contextInfo) throws AssemblyException {
054                if(null == cluRel) {
055                        return null;
056                }
057                
058                CourseJointInfo joint = (jointInfo != null) ? jointInfo : new CourseJointInfo();
059
060                CluInfo clu = null;
061                try {
062                        clu = cluService.getClu(cluRel.getRelatedCluId() , contextInfo);
063
064                        joint.setCourseId(clu.getId());
065
066                        joint.setTypeKey(clu.getTypeKey());//FIXME is this ever used?
067                        joint.setSubjectArea(clu.getOfficialIdentifier().getDivision());
068                        joint.setCourseTitle(clu.getOfficialIdentifier().getLongName());
069                        joint.setCourseNumberSuffix(clu.getOfficialIdentifier().getSuffixCode());
070                        joint.setRelationId(cluRel.getId());
071
072                } catch (Exception e) {
073                        throw new AssemblyException("Error getting related clus", e);
074                } 
075                
076                return joint;
077        }
078
079        
080        public CourseJointInfo assemble(CluCluRelationInfo cluRel, String cluId, CourseJointInfo jointInfo, boolean shallowBuild, ContextInfo contextInfo) throws AssemblyException {
081                if(null == cluRel) {
082                        return null;
083                }
084                
085                CourseJointInfo joint = (jointInfo != null) ? jointInfo : new CourseJointInfo();
086
087                CluInfo clu = null;
088                try {
089                        clu = cluService.getClu(cluId , contextInfo);
090                        
091                        joint.setCourseId(clu.getId());
092            joint.setTypeKey(clu.getTypeKey());
093            joint.setSubjectArea(clu.getOfficialIdentifier().getDivision());
094            joint.setCourseTitle(clu.getOfficialIdentifier().getLongName());
095            joint.setCourseNumberSuffix(clu.getOfficialIdentifier().getSuffixCode());
096            joint.setRelationId(cluRel.getId());
097                        
098                } catch (Exception e) {
099                        throw new AssemblyException("Error getting related clus", e);
100                } 
101                
102                return joint;
103        }
104
105        @Override
106        public BaseDTOAssemblyNode<CourseJointInfo, CluCluRelationInfo> disassemble(
107                        CourseJointInfo joint, NodeOperation operation,ContextInfo contextInfo) throws AssemblyException {
108                
109                if(null == joint){
110                        //FIXME Unsure now if this is an exception or just return null or empty assemblyNode 
111                        throw new AssemblyException("Activity can not be null");
112                }
113                                
114                BaseDTOAssemblyNode<CourseJointInfo, CluCluRelationInfo> result = new BaseDTOAssemblyNode<CourseJointInfo, CluCluRelationInfo>(this);
115                result.setBusinessDTORef(joint);
116                result.setOperation(operation);
117                
118                CluCluRelationInfo cluRel = new CluCluRelationInfo();
119                cluRel.setId(UUIDHelper.genStringUUID(joint.getRelationId()));
120                cluRel.setRelatedCluId(joint.getCourseId());
121                                
122                cluRel.setTypeKey(CourseAssemblerConstants.JOINT_RELATION_TYPE);
123                result.setNodeData(cluRel);
124                // The caller is required to set the CluId on the cluCluRelation
125                
126                return result;
127        }
128}