| 1 | |
package org.kuali.student.lum.course.service.assembler; |
| 2 | |
|
| 3 | |
import java.util.ArrayList; |
| 4 | |
import java.util.Collections; |
| 5 | |
import java.util.Comparator; |
| 6 | |
import java.util.HashMap; |
| 7 | |
import java.util.HashSet; |
| 8 | |
import java.util.List; |
| 9 | |
import java.util.Map; |
| 10 | |
import java.util.Set; |
| 11 | |
import java.util.Map.Entry; |
| 12 | |
|
| 13 | |
import org.kuali.student.common.util.UUIDHelper; |
| 14 | |
import org.kuali.student.core.assembly.BOAssembler; |
| 15 | |
import org.kuali.student.core.assembly.BaseDTOAssemblyNode; |
| 16 | |
import org.kuali.student.core.assembly.BaseDTOAssemblyNode.NodeOperation; |
| 17 | |
import org.kuali.student.core.assembly.data.AssemblyException; |
| 18 | |
import org.kuali.student.core.exceptions.DoesNotExistException; |
| 19 | |
import org.kuali.student.core.exceptions.InvalidParameterException; |
| 20 | |
import org.kuali.student.core.exceptions.MissingParameterException; |
| 21 | |
import org.kuali.student.core.exceptions.OperationFailedException; |
| 22 | |
import org.kuali.student.lum.course.dto.LoDisplayInfo; |
| 23 | |
import org.kuali.student.lum.lo.dto.LoCategoryInfo; |
| 24 | |
import org.kuali.student.lum.lo.dto.LoInfo; |
| 25 | |
import org.kuali.student.lum.lo.dto.LoLoRelationInfo; |
| 26 | |
import org.kuali.student.lum.lo.service.LearningObjectiveService; |
| 27 | |
|
| 28 | |
|
| 29 | 674 | public class LoAssembler implements BOAssembler<LoDisplayInfo, LoInfo> { |
| 30 | |
|
| 31 | |
private LearningObjectiveService loService; |
| 32 | |
|
| 33 | |
@Override |
| 34 | |
public LoDisplayInfo assemble(LoInfo lo, LoDisplayInfo loDisplayInfo, |
| 35 | |
boolean shallowBuild) throws AssemblyException { |
| 36 | |
|
| 37 | 1059 | LoDisplayInfo loDisplay = (null != loDisplayInfo) ? loDisplayInfo : new LoDisplayInfo(); |
| 38 | |
|
| 39 | 1059 | loDisplay.setLoInfo(lo); |
| 40 | |
|
| 41 | 1059 | if (!shallowBuild) { |
| 42 | 387 | String loId = lo.getId(); |
| 43 | |
try { |
| 44 | 387 | List<LoCategoryInfo> loCategories = loService.getLoCategoriesForLo(loId); |
| 45 | 387 | loDisplay.setLoCategoryInfoList(loCategories); |
| 46 | 0 | } catch (DoesNotExistException e) { |
| 47 | 0 | } catch (Exception e) { |
| 48 | 0 | throw new AssemblyException("Error getting learning objective categories", e); |
| 49 | 387 | } |
| 50 | |
try { |
| 51 | 387 | List<LoInfo> childLos = loService.getRelatedLosByLoId(loId,CourseAssemblerConstants.COURSE_LO_RELATION_INCLUDES); |
| 52 | 387 | for(LoInfo childLo:childLos){ |
| 53 | 304 | LoDisplayInfo childLoDisplay = assemble(childLo, null, shallowBuild); |
| 54 | 304 | childLoDisplay.setParentLoRelationid(lo.getId()); |
| 55 | 304 | childLoDisplay.setParentRelType(CourseAssemblerConstants.COURSE_LO_RELATION_INCLUDES); |
| 56 | 304 | loDisplay.getLoDisplayInfoList().add(childLoDisplay); |
| 57 | 304 | } |
| 58 | 387 | if(loDisplay.getLoDisplayInfoList().size()>1){ |
| 59 | 151 | Collections.sort(loDisplay.getLoDisplayInfoList(), LoDisplayComparator.getInstance()); |
| 60 | |
} |
| 61 | 0 | } catch (DoesNotExistException e) { |
| 62 | 0 | } catch (Exception e) { |
| 63 | 0 | throw new AssemblyException("Error getting learning objective", e); |
| 64 | 387 | } |
| 65 | |
|
| 66 | |
} |
| 67 | 1059 | return loDisplay; |
| 68 | |
} |
| 69 | |
|
| 70 | |
@Override |
| 71 | |
|
| 72 | |
public BaseDTOAssemblyNode<LoDisplayInfo, LoInfo> disassemble( |
| 73 | |
LoDisplayInfo loDisplay, NodeOperation operation) |
| 74 | |
throws AssemblyException { |
| 75 | |
|
| 76 | 703 | BaseDTOAssemblyNode<LoDisplayInfo, LoInfo> result = new BaseDTOAssemblyNode<LoDisplayInfo, LoInfo>(this); |
| 77 | |
|
| 78 | |
|
| 79 | 703 | if (loDisplay == null || loDisplay.getLoInfo() == null) { |
| 80 | 0 | throw new AssemblyException("Lo can not be null"); |
| 81 | |
} |
| 82 | 703 | if (NodeOperation.CREATE != operation && null == loDisplay.getLoInfo().getId()) { |
| 83 | 0 | throw new AssemblyException("Lo id can not be null"); |
| 84 | |
} |
| 85 | |
|
| 86 | |
|
| 87 | 703 | loDisplay.getLoInfo().setId(UUIDHelper.genStringUUID(loDisplay.getLoInfo().getId())); |
| 88 | |
|
| 89 | |
|
| 90 | 703 | loDisplay.getLoInfo().setType(CourseAssemblerConstants.COURSE_LO_TYPE); |
| 91 | 703 | loDisplay.getLoInfo().setLoRepositoryKey(CourseAssemblerConstants.COURSE_LO_REPOSITORY_KEY); |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | 703 | result.setBusinessDTORef(loDisplay); |
| 96 | 703 | result.setNodeData(loDisplay.getLoInfo()); |
| 97 | 703 | result.setOperation(operation); |
| 98 | |
|
| 99 | |
|
| 100 | |
try { |
| 101 | 703 | List<BaseDTOAssemblyNode<?, ?>> childLoNodes = disassembleChildLos(loDisplay, operation); |
| 102 | 703 | result.getChildNodes().addAll(childLoNodes); |
| 103 | 0 | } catch (DoesNotExistException e) { |
| 104 | 0 | } catch (Exception e) { |
| 105 | 0 | throw new AssemblyException("Error disassembling child los", e); |
| 106 | 703 | } |
| 107 | |
|
| 108 | |
|
| 109 | |
try { |
| 110 | 703 | List<BaseDTOAssemblyNode<?, ?>> categoryNodes = disassembleCategories(loDisplay, operation); |
| 111 | 703 | result.getChildNodes().addAll(categoryNodes); |
| 112 | 0 | } catch (Exception e) { |
| 113 | 0 | throw new AssemblyException("Error disassembling categories", e); |
| 114 | 703 | } |
| 115 | |
|
| 116 | 703 | return result; |
| 117 | |
} |
| 118 | |
|
| 119 | |
private List<BaseDTOAssemblyNode<?, ?>> disassembleCategories( |
| 120 | |
LoDisplayInfo loDisplay, NodeOperation operation) throws AssemblyException { |
| 121 | |
|
| 122 | 703 | List<BaseDTOAssemblyNode<?, ?>> results = new ArrayList<BaseDTOAssemblyNode<?, ?>>(); |
| 123 | |
|
| 124 | |
|
| 125 | 703 | Set<String> currentCategoryIds = new HashSet<String>(); |
| 126 | |
|
| 127 | 703 | if (!NodeOperation.CREATE.equals(operation)) { |
| 128 | |
try { |
| 129 | 52 | List<LoCategoryInfo> categories = loService.getLoCategoriesForLo(loDisplay.getLoInfo().getId()); |
| 130 | 52 | for (LoCategoryInfo category : categories) { |
| 131 | 71 | currentCategoryIds.add(category.getId()); |
| 132 | |
} |
| 133 | 0 | } catch (DoesNotExistException e) { |
| 134 | 0 | } catch (Exception e) { |
| 135 | 0 | throw new AssemblyException("Error getting categories", e); |
| 136 | 52 | } |
| 137 | |
} |
| 138 | |
|
| 139 | 703 | for (LoCategoryInfo category : loDisplay.getLoCategoryInfoList()) { |
| 140 | |
|
| 141 | |
|
| 142 | 1360 | if (NodeOperation.CREATE == operation |
| 143 | |
|| (NodeOperation.UPDATE == operation && !currentCategoryIds.contains(category.getId()))) { |
| 144 | |
|
| 145 | 1289 | LoCategoryRelationInfo loCategoryRelation = new LoCategoryRelationInfo(); |
| 146 | 1289 | loCategoryRelation.setCategoryId(category.getId()); |
| 147 | 1289 | loCategoryRelation.setLoId(loDisplay.getLoInfo().getId()); |
| 148 | |
|
| 149 | 1289 | BaseDTOAssemblyNode<LoDisplayInfo, LoCategoryRelationInfo> relationToAddNode = new BaseDTOAssemblyNode<LoDisplayInfo, LoCategoryRelationInfo>(null); |
| 150 | 1289 | relationToAddNode.setNodeData(loCategoryRelation); |
| 151 | 1289 | relationToAddNode.setOperation(NodeOperation.CREATE); |
| 152 | 1289 | results.add(relationToAddNode); |
| 153 | |
|
| 154 | 1289 | currentCategoryIds.remove(category.getId()); |
| 155 | 1289 | } else if (NodeOperation.UPDATE == operation |
| 156 | |
&& currentCategoryIds.contains(category.getId())) { |
| 157 | 29 | currentCategoryIds.remove(category.getId()); |
| 158 | |
} |
| 159 | |
} |
| 160 | |
|
| 161 | 703 | for(String categoryId:currentCategoryIds){ |
| 162 | 42 | LoCategoryRelationInfo loCategoryRelation = new LoCategoryRelationInfo(); |
| 163 | 42 | loCategoryRelation.setCategoryId(categoryId); |
| 164 | 42 | loCategoryRelation.setLoId(loDisplay.getLoInfo().getId()); |
| 165 | |
|
| 166 | 42 | BaseDTOAssemblyNode<LoDisplayInfo, LoCategoryRelationInfo> relationToDeleteNode = new BaseDTOAssemblyNode<LoDisplayInfo, LoCategoryRelationInfo>(null); |
| 167 | 42 | relationToDeleteNode.setNodeData(loCategoryRelation); |
| 168 | 42 | relationToDeleteNode.setOperation(NodeOperation.DELETE); |
| 169 | 42 | results.add(relationToDeleteNode); |
| 170 | 42 | } |
| 171 | |
|
| 172 | 703 | return results; |
| 173 | |
} |
| 174 | |
|
| 175 | |
private List<BaseDTOAssemblyNode<?, ?>> disassembleChildLos(LoDisplayInfo loDisplay, NodeOperation operation) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, AssemblyException{ |
| 176 | 703 | List<BaseDTOAssemblyNode<?, ?>> results = new ArrayList<BaseDTOAssemblyNode<?, ?>>(); |
| 177 | 703 | Map<String,LoLoRelationInfo> currentLoRelations = new HashMap<String,LoLoRelationInfo>(); |
| 178 | |
|
| 179 | 703 | if (!NodeOperation.CREATE.equals(operation)) { |
| 180 | |
try { |
| 181 | 52 | List<LoLoRelationInfo> loRelations = loService.getLoLoRelationsByLoId(loDisplay.getLoInfo().getId()); |
| 182 | 52 | for (LoLoRelationInfo loRelation : loRelations) { |
| 183 | |
|
| 184 | 66 | if(CourseAssemblerConstants.COURSE_LO_RELATION_INCLUDES.equals(loRelation.getType())&& |
| 185 | |
!loDisplay.getLoInfo().getId().equals(loRelation.getRelatedLoId())){ |
| 186 | 30 | currentLoRelations.put(loRelation.getRelatedLoId(), loRelation); |
| 187 | |
} |
| 188 | |
} |
| 189 | 0 | } catch (DoesNotExistException e) { |
| 190 | 0 | } catch (Exception e) { |
| 191 | 0 | throw new AssemblyException("Error getting categories", e); |
| 192 | 52 | } |
| 193 | |
} |
| 194 | |
|
| 195 | |
|
| 196 | 703 | for (LoDisplayInfo childDisplay : loDisplay.getLoDisplayInfoList()) { |
| 197 | |
|
| 198 | |
|
| 199 | 582 | if (NodeOperation.CREATE == operation |
| 200 | |
|| (NodeOperation.UPDATE == operation && !currentLoRelations.containsKey(childDisplay.getLoInfo().getId()))) { |
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | 552 | childDisplay.getLoInfo().setId(null); |
| 205 | 552 | BaseDTOAssemblyNode<LoDisplayInfo, LoInfo> loNode = this |
| 206 | |
.disassemble(childDisplay, NodeOperation.CREATE); |
| 207 | 552 | results.add(loNode); |
| 208 | |
|
| 209 | |
|
| 210 | 552 | LoLoRelationInfo relation = new LoLoRelationInfo(); |
| 211 | 552 | relation.setLoId(loDisplay.getLoInfo().getId()); |
| 212 | 552 | relation.setRelatedLoId(loNode.getNodeData().getId()); |
| 213 | 552 | relation.setType(CourseAssemblerConstants.COURSE_LO_RELATION_INCLUDES); |
| 214 | 552 | relation.setState(loDisplay.getLoInfo().getState()); |
| 215 | |
|
| 216 | |
|
| 217 | 552 | BaseDTOAssemblyNode<LoDisplayInfo, LoLoRelationInfo> relationNode = new BaseDTOAssemblyNode<LoDisplayInfo, LoLoRelationInfo>( |
| 218 | |
null); |
| 219 | 552 | relationNode.setNodeData(relation); |
| 220 | 552 | relationNode.setOperation(NodeOperation.CREATE); |
| 221 | |
|
| 222 | 552 | results.add(relationNode); |
| 223 | 552 | } else if (NodeOperation.UPDATE == operation |
| 224 | |
&& currentLoRelations.containsKey(childDisplay.getLoInfo().getId())) { |
| 225 | |
|
| 226 | |
|
| 227 | 12 | BaseDTOAssemblyNode<LoDisplayInfo, LoInfo> loNode = this |
| 228 | |
.disassemble(childDisplay, NodeOperation.UPDATE); |
| 229 | 12 | results.add(loNode); |
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | 12 | currentLoRelations.remove(childDisplay.getLoInfo().getId()); |
| 234 | 12 | } else if (NodeOperation.DELETE == operation |
| 235 | |
&& currentLoRelations.containsKey(childDisplay.getLoInfo().getId())) { |
| 236 | |
|
| 237 | |
|
| 238 | 18 | LoLoRelationInfo relationToDelete = currentLoRelations.get(childDisplay.getLoInfo().getId()); |
| 239 | 18 | BaseDTOAssemblyNode<LoDisplayInfo, LoLoRelationInfo> relationToDeleteNode = new BaseDTOAssemblyNode<LoDisplayInfo, LoLoRelationInfo>( |
| 240 | |
null); |
| 241 | 18 | relationToDeleteNode.setNodeData(relationToDelete); |
| 242 | 18 | relationToDeleteNode.setOperation(NodeOperation.DELETE); |
| 243 | 18 | results.add(relationToDeleteNode); |
| 244 | |
|
| 245 | 18 | BaseDTOAssemblyNode<LoDisplayInfo, LoInfo> loNode = this |
| 246 | |
.disassemble(childDisplay, NodeOperation.DELETE); |
| 247 | 18 | results.add(loNode); |
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | 18 | currentLoRelations.remove(childDisplay.getLoInfo().getId()); |
| 252 | 582 | } |
| 253 | |
} |
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | 703 | for (Entry<String, LoLoRelationInfo> entry : currentLoRelations.entrySet()) { |
| 258 | |
|
| 259 | |
|
| 260 | 0 | LoLoRelationInfo relationToDelete = entry.getValue(); |
| 261 | 0 | BaseDTOAssemblyNode<LoDisplayInfo, LoLoRelationInfo> relationToDeleteNode = new BaseDTOAssemblyNode<LoDisplayInfo, LoLoRelationInfo>( |
| 262 | |
null); |
| 263 | 0 | relationToDeleteNode.setNodeData(relationToDelete); |
| 264 | 0 | relationToDeleteNode.setOperation(NodeOperation.DELETE); |
| 265 | 0 | results.add(relationToDeleteNode); |
| 266 | |
|
| 267 | 0 | LoInfo loToDelete = loService.getLo(entry.getKey()); |
| 268 | 0 | LoDisplayInfo loDisplayToDelete = this.assemble(loToDelete, null, false); |
| 269 | 0 | BaseDTOAssemblyNode<LoDisplayInfo, LoInfo> loNode = this.disassemble(loDisplayToDelete, NodeOperation.DELETE); |
| 270 | 0 | results.add(loNode); |
| 271 | 0 | } |
| 272 | 703 | return results; |
| 273 | |
|
| 274 | |
} |
| 275 | |
|
| 276 | |
public void setLoService(LearningObjectiveService loService) { |
| 277 | 2 | this.loService = loService; |
| 278 | 2 | } |
| 279 | |
|
| 280 | 2 | public static class LoDisplayComparator implements Comparator<LoDisplayInfo>{ |
| 281 | 1 | private static LoDisplayComparator instance = new LoDisplayComparator(); |
| 282 | |
@Override |
| 283 | |
public int compare(LoDisplayInfo o1, LoDisplayInfo o2) { |
| 284 | 151 | String o1Sequence = o1.getLoInfo().getAttributes().get(CourseAssemblerConstants.COURSE_LO_SEQUENCE); |
| 285 | 151 | String o2Sequence = o1.getLoInfo().getAttributes().get(CourseAssemblerConstants.COURSE_LO_SEQUENCE); |
| 286 | 151 | if(o1Sequence!=null){ |
| 287 | 0 | return o1Sequence.compareTo(o2Sequence); |
| 288 | 151 | }else if(o2Sequence!=null){ |
| 289 | 0 | return -1; |
| 290 | |
} |
| 291 | 151 | return 0; |
| 292 | |
} |
| 293 | |
public static LoDisplayComparator getInstance() { |
| 294 | 151 | return instance; |
| 295 | |
} |
| 296 | |
} |
| 297 | |
} |