Coverage Report - org.kuali.student.lum.program.service.assembler.ProgramAssemblerUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ProgramAssemblerUtils
77%
374/484
69%
198/284
5.919
 
 1  
 /*
 2  
  * Copyright 2010 The Kuali Foundation Licensed under the
 3  
  * Educational Community License, Version 2.0 (the "License"); you may
 4  
  * not use this file except in compliance with the License. You may
 5  
  * obtain a copy of the License at
 6  
  *
 7  
  * http://www.osedu.org/licenses/ECL-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing,
 10  
  * software distributed under the License is distributed on an "AS IS"
 11  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 12  
  * or implied. See the License for the specific language governing
 13  
  * permissions and limitations under the License.
 14  
  */
 15  
 
 16  
 package org.kuali.student.lum.program.service.assembler;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.HashMap;
 20  
 import java.util.Iterator;
 21  
 import java.util.List;
 22  
 import java.util.Map;
 23  
 
 24  
 import org.kuali.student.common.util.UUIDHelper;
 25  
 import org.kuali.student.core.assembly.BaseDTOAssemblyNode;
 26  
 import org.kuali.student.core.assembly.BaseDTOAssemblyNode.NodeOperation;
 27  
 import org.kuali.student.core.assembly.data.AssemblyException;
 28  
 import org.kuali.student.core.dto.RichTextInfo;
 29  
 import org.kuali.student.core.exceptions.DoesNotExistException;
 30  
 import org.kuali.student.core.exceptions.InvalidParameterException;
 31  
 import org.kuali.student.core.exceptions.MissingParameterException;
 32  
 import org.kuali.student.core.exceptions.OperationFailedException;
 33  
 import org.kuali.student.lum.lu.dto.AdminOrgInfo;
 34  
 import org.kuali.student.lum.lu.dto.CluCluRelationInfo;
 35  
 import org.kuali.student.lum.lu.dto.CluIdentifierInfo;
 36  
 import org.kuali.student.lum.lu.dto.CluInfo;
 37  
 import org.kuali.student.lum.lu.dto.CluPublicationInfo;
 38  
 import org.kuali.student.lum.lu.dto.CluResultInfo;
 39  
 import org.kuali.student.lum.lu.dto.FieldInfo;
 40  
 import org.kuali.student.lum.lu.dto.LuCodeInfo;
 41  
 import org.kuali.student.lum.lu.service.LuService;
 42  
 import org.kuali.student.lum.program.dto.CredentialProgramInfo;
 43  
 import org.kuali.student.lum.program.dto.assembly.ProgramAtpAssembly;
 44  
 import org.kuali.student.lum.program.dto.assembly.ProgramBasicOrgAssembly;
 45  
 import org.kuali.student.lum.program.dto.assembly.ProgramCodeAssembly;
 46  
 import org.kuali.student.lum.program.dto.assembly.ProgramCommonAssembly;
 47  
 import org.kuali.student.lum.program.dto.assembly.ProgramCredentialAssembly;
 48  
 import org.kuali.student.lum.program.dto.assembly.ProgramFullOrgAssembly;
 49  
 import org.kuali.student.lum.program.dto.assembly.ProgramIdentifierAssembly;
 50  
 import org.kuali.student.lum.program.dto.assembly.ProgramPublicationAssembly;
 51  
 import org.kuali.student.lum.program.dto.assembly.ProgramRequirementAssembly;
 52  
 import org.kuali.student.lum.service.assembler.CluAssemblerUtils;
 53  
 
 54  1
 public class ProgramAssemblerUtils {
 55  
 
 56  
     private LuService luService;
 57  
     private CluAssemblerUtils cluAssemblerUtils;
 58  
 
 59  
      /**
 60  
      * Copy basic values from clu to program
 61  
      *
 62  
      * @param clu
 63  
      * @param program
 64  
      * @return
 65  
      * @throws AssemblyException
 66  
      */
 67  
      public ProgramCommonAssembly assembleBasics(CluInfo clu, ProgramCommonAssembly program) throws AssemblyException {
 68  
 
 69  135
          if (program instanceof CredentialProgramInfo) {
 70  6
              ((CredentialProgramInfo)program).setCredentialProgramType(clu.getType());
 71  
          }
 72  
          else {
 73  129
              program.setType(clu.getType());
 74  
          }
 75  135
          program.setState(clu.getState());
 76  135
          program.setMetaInfo(clu.getMetaInfo());
 77  135
          program.setAttributes(clu.getAttributes());
 78  135
          program.setId(clu.getId());
 79  
 
 80  135
          return program;
 81  
      }
 82  
 
 83  
     /**
 84  
      * Copy basic values from program to clu
 85  
      *
 86  
      * @param clu
 87  
      * @param program
 88  
      * @param operation
 89  
      * @return
 90  
      * @throws AssemblyException
 91  
      */
 92  
     public CluInfo disassembleBasics(CluInfo clu, ProgramCommonAssembly program) throws AssemblyException {
 93  
 
 94  61
         if (program instanceof CredentialProgramInfo) {
 95  2
             clu.setType (((CredentialProgramInfo)program).getCredentialProgramType());
 96  
         }
 97  
         else {
 98  59
             clu.setType(program.getType());
 99  
         }
 100  61
         clu.setId(UUIDHelper.genStringUUID(program.getId()));
 101  61
         clu.setState(program.getState());
 102  61
         clu.setMetaInfo(program.getMetaInfo());
 103  61
         clu.setAttributes(program.getAttributes());
 104  61
         return clu;
 105  
 
 106  
     }
 107  
 
 108  
     //TODO maybe this should be in CluAssemblerUtils??
 109  
     public ProgramRequirementAssembly assembleRequirements(CluInfo clu, ProgramRequirementAssembly program) throws AssemblyException {
 110  
 
 111  
         try {
 112  78
             List<String> requirements = luService.getRelatedCluIdsByCluId(clu.getId(), ProgramAssemblerConstants.HAS_PROGRAM_REQUIREMENT);
 113  78
             if (requirements != null && requirements.size() > 0) {
 114  7
                 program.setProgramRequirements(requirements);
 115  
             }
 116  
         }
 117  0
         catch (Exception e)
 118  
         {
 119  0
             throw new AssemblyException("Error assembling program requirements", e);
 120  78
         }
 121  
 
 122  78
         return program;
 123  
     }
 124  
 
 125  
     //TODO  maybe this should be in CluAssemblerUtils??
 126  
     public CluInfo disassembleRequirements(CluInfo clu, ProgramRequirementAssembly program, NodeOperation operation, BaseDTOAssemblyNode<?, ?> result, boolean stateChanged) throws AssemblyException {
 127  
         try {
 128  14
             List<String> requirements = program.getProgramRequirements ();
 129  
 
 130  14
             if (requirements != null && !requirements.isEmpty()) {
 131  14
                     if (stateChanged){
 132  0
                             addUpdateRequirementStateNodes(requirements, clu.getState(), result);
 133  
                     }
 134  
                     
 135  14
                        Map<String, String> currentRelations = null;
 136  
 
 137  14
                 if (!NodeOperation.CREATE.equals(operation)) {
 138  2
                         currentRelations = getCluCluRelations(clu.getId(), ProgramAssemblerConstants.HAS_PROGRAM_REQUIREMENT);
 139  
                 }
 140  
                 
 141  14
                         for (String requirementId : requirements){
 142  27
                                 List<BaseDTOAssemblyNode<?, ?>> reqResults = addAllRelationNodes(clu.getId(), requirementId, ProgramAssemblerConstants.HAS_PROGRAM_REQUIREMENT, operation, currentRelations);
 143  27
                         if (reqResults != null && reqResults.size()> 0) {
 144  26
                             result.getChildNodes().addAll(reqResults);
 145  
                         }
 146  27
                         }
 147  
                         
 148  14
                     if(currentRelations != null && currentRelations.size() > 0){
 149  1
                             for (Map.Entry<String, String> entry : currentRelations.entrySet()) {
 150  
                                 // Create a new relation with the id of the relation we want to
 151  
                                 // delete
 152  2
                                 CluCluRelationInfo relationToDelete = new CluCluRelationInfo();
 153  2
                                 relationToDelete.setId( entry.getValue() );
 154  2
                                 BaseDTOAssemblyNode<Object, CluCluRelationInfo> relationToDeleteNode = new BaseDTOAssemblyNode<Object, CluCluRelationInfo>(
 155  
                                         null);
 156  2
                                 relationToDeleteNode.setNodeData(relationToDelete);
 157  2
                                 relationToDeleteNode.setOperation(NodeOperation.DELETE);
 158  2
                                 result.getChildNodes().add(relationToDeleteNode);
 159  2
                             }
 160  
                     }
 161  
             }
 162  0
             } catch (Exception e) {
 163  0
                 throw new AssemblyException("Error while disassembling program requirements", e);
 164  14
             }
 165  
             
 166  14
         return clu;
 167  
 
 168  
     }
 169  
 
 170  
         
 171  
     /**
 172  
      * This adds nodes to update the state for the requirement clu
 173  
      * @param state
 174  
      * @param result
 175  
      * @throws OperationFailedException 
 176  
      * @throws MissingParameterException 
 177  
      * @throws InvalidParameterException 
 178  
      */
 179  
     private void addUpdateRequirementStateNodes(List<String> requirements, String state, BaseDTOAssemblyNode<?, ?> result) throws InvalidParameterException, MissingParameterException, OperationFailedException {
 180  0
             for (String requirementId:requirements){
 181  
                     try {
 182  0
                             CluInfo requirementClu = luService.getClu(requirementId);
 183  0
                     requirementClu.setState(state);
 184  0
                     BaseDTOAssemblyNode<Object, CluInfo> reqCluNode = new BaseDTOAssemblyNode<Object, CluInfo>(null);
 185  0
                     reqCluNode.setNodeData(requirementClu);
 186  0
                     reqCluNode.setOperation(NodeOperation.UPDATE);
 187  0
                     result.getChildNodes().add(reqCluNode);
 188  0
                     } catch (DoesNotExistException e){
 189  
                             //Don't need to update what doesn't exist
 190  0
                     }
 191  
             }
 192  0
         }
 193  
 
 194  
         /**
 195  
      * Copy identifier values from clu to program
 196  
      *
 197  
      * @param clu
 198  
      * @param o
 199  
      * @return
 200  
      * @throws AssemblyException
 201  
      */
 202  
     public ProgramIdentifierAssembly assembleIdentifiers(CluInfo clu, ProgramIdentifierAssembly program) throws AssemblyException {
 203  
 
 204  135
         if (clu.getOfficialIdentifier() != null) {
 205  135
             if (clu.getOfficialIdentifier().getShortName() != null) {
 206  135
                 program.setShortTitle(clu.getOfficialIdentifier().getShortName());
 207  
             }
 208  135
             if (clu.getOfficialIdentifier().getLongName() != null) {
 209  135
                 program.setLongTitle(clu.getOfficialIdentifier().getLongName());
 210  
             }
 211  135
             if (clu.getOfficialIdentifier().getCode() != null) {
 212  135
                 program.setCode(clu.getOfficialIdentifier().getCode());
 213  
             }
 214  
         }
 215  135
         if (clu.getAlternateIdentifiers() != null) {
 216  135
             for (CluIdentifierInfo cluIdInfo : clu.getAlternateIdentifiers()) {
 217  224
                 String idInfoType = cluIdInfo.getType();
 218  224
                 if (ProgramAssemblerConstants.TRANSCRIPT.equals(idInfoType)) {
 219  130
                     program.setTranscriptTitle(cluIdInfo.getShortName());
 220  94
                 } else if (ProgramAssemblerConstants.DIPLOMA.equals(idInfoType)) {
 221  72
                     program.setDiplomaTitle(cluIdInfo.getShortName());
 222  
                 }
 223  224
             }
 224  
         }
 225  135
         return program;
 226  
     }
 227  
 
 228  
 
 229  
     /**
 230  
      * Copy identifier values from program to clu
 231  
      *
 232  
      * @param clu
 233  
      * @param program
 234  
      * @param operation
 235  
      * @return
 236  
      * @throws AssemblyException
 237  
      */
 238  
     public CluInfo disassembleIdentifiers(CluInfo clu, ProgramIdentifierAssembly program, NodeOperation operation) throws AssemblyException {
 239  
 
 240  52
         CluIdentifierInfo official = null != clu.getOfficialIdentifier() ? clu.getOfficialIdentifier() : new CluIdentifierInfo();
 241  
 
 242  52
         official.setCode(program.getCode());
 243  52
         official.setLongName(program.getLongTitle());
 244  52
         official.setShortName(program.getShortTitle());
 245  52
         String existingState = program.getState();
 246  52
         official.setState((null != existingState && existingState.length() > 0) ? existingState : ProgramAssemblerConstants.ACTIVE);
 247  
         // gotta be this type
 248  52
         official.setType(ProgramAssemblerConstants.OFFICIAL);
 249  
 
 250  52
         if (program instanceof CredentialProgramInfo) {
 251  2
             CredentialProgramInfo cred = (CredentialProgramInfo)program;
 252  2
             official.setLevel(cred.getProgramLevel());
 253  
         }
 254  
 
 255  52
         clu.setOfficialIdentifier(official);
 256  
 
 257  
         //Remove any existing diploma or transcript alt identifiers
 258  52
         CluIdentifierInfo diplomaInfo = null;
 259  52
         CluIdentifierInfo transcriptInfo = null;
 260  52
         for(Iterator<CluIdentifierInfo> iter = clu.getAlternateIdentifiers().iterator();iter.hasNext();){
 261  59
             CluIdentifierInfo cluIdentifier = iter.next();
 262  59
             if (ProgramAssemblerConstants.DIPLOMA.equals(cluIdentifier.getType())) {
 263  19
                 diplomaInfo = cluIdentifier;
 264  40
             } else if (ProgramAssemblerConstants.TRANSCRIPT.equals(cluIdentifier.getType())) {
 265  34
                 transcriptInfo = cluIdentifier;
 266  
             }
 267  59
         }
 268  
 
 269  52
         if (program.getDiplomaTitle() != null) {
 270  29
             if (diplomaInfo == null) {
 271  10
                 diplomaInfo = new CluIdentifierInfo();
 272  10
                 diplomaInfo.setState(ProgramAssemblerConstants.ACTIVE);
 273  10
                 clu.getAlternateIdentifiers().add(diplomaInfo);
 274  
             }
 275  29
             diplomaInfo.setCode(official.getCode());
 276  29
             diplomaInfo.setShortName(program.getDiplomaTitle());
 277  29
             diplomaInfo.setType(ProgramAssemblerConstants.DIPLOMA);
 278  
         }
 279  
 
 280  52
         if (program.getTranscriptTitle() != null) {
 281  51
             if (transcriptInfo == null) {
 282  17
                 transcriptInfo = new CluIdentifierInfo();
 283  17
                 transcriptInfo.setState(ProgramAssemblerConstants.ACTIVE);
 284  17
                 clu.getAlternateIdentifiers().add(transcriptInfo);
 285  
             }
 286  51
             transcriptInfo.setCode(official.getCode());
 287  51
             transcriptInfo.setShortName(program.getTranscriptTitle());
 288  51
             transcriptInfo.setType(ProgramAssemblerConstants.TRANSCRIPT);
 289  
         }
 290  52
         return clu;
 291  
     }
 292  
 
 293  
     /**
 294  
      * Copy Lu Codes from clu to program
 295  
      *
 296  
      * @param clu
 297  
      * @param program
 298  
      * @return
 299  
      * @throws AssemblyException
 300  
      */
 301  
     public ProgramCodeAssembly assembleLuCodes(CluInfo clu, ProgramCodeAssembly program) throws AssemblyException {
 302  
 
 303  135
         if (clu.getLuCodes() != null) {
 304  135
             for (LuCodeInfo codeInfo : clu.getLuCodes()) {
 305  506
                 if (ProgramAssemblerConstants.CIP_2000.equals(codeInfo.getType())) {
 306  94
                     program.setCip2000Code(codeInfo.getValue());
 307  412
                 } else if (ProgramAssemblerConstants.CIP_2010.equals(codeInfo.getType())) {
 308  94
                     program.setCip2010Code(codeInfo.getValue());
 309  318
                 } else if (ProgramAssemblerConstants.HEGIS.equals(codeInfo.getType())) {
 310  94
                     program.setHegisCode(codeInfo.getValue());
 311  224
                 } else if (ProgramAssemblerConstants.UNIVERSITY_CLASSIFICATION.equals(codeInfo.getType())) {
 312  130
                     program.setUniversityClassification(codeInfo.getValue());
 313  94
                 } else if (ProgramAssemblerConstants.SELECTIVE_ENROLLMENT.equals(codeInfo.getType())) {
 314  94
                     program.setSelectiveEnrollmentCode(codeInfo.getValue());
 315  
                 }
 316  
             }
 317  
         }
 318  
 
 319  135
         return program;
 320  
     }
 321  
 
 322  
 
 323  
     /**
 324  
      * Copy Lu Codes from program to clu
 325  
      *
 326  
      * @param clu
 327  
      * @param program
 328  
      * @param operation
 329  
      * @throws AssemblyException
 330  
      */
 331  
     public CluInfo disassembleLuCodes(CluInfo clu, ProgramCodeAssembly program, NodeOperation operation) throws AssemblyException {
 332  
 
 333  52
         clu.setLuCodes(new ArrayList<LuCodeInfo>());
 334  
 
 335  52
         addLuCodeFromProgram(ProgramAssemblerConstants.CIP_2000, program.getCip2000Code(), clu.getLuCodes());
 336  52
         addLuCodeFromProgram(ProgramAssemblerConstants.CIP_2010, program.getCip2010Code(), clu.getLuCodes());
 337  52
         addLuCodeFromProgram(ProgramAssemblerConstants.HEGIS, program.getHegisCode(), clu.getLuCodes());
 338  52
         addLuCodeFromProgram(ProgramAssemblerConstants.UNIVERSITY_CLASSIFICATION, program.getUniversityClassification(), clu.getLuCodes());
 339  52
         addLuCodeFromProgram(ProgramAssemblerConstants.SELECTIVE_ENROLLMENT, program.getSelectiveEnrollmentCode(), clu.getLuCodes());
 340  
 
 341  52
         return clu;
 342  
 
 343  
     }
 344  
 
 345  
     /**
 346  
      * Copy AdminOrg id's from clu's AdminOrgInfo's to program
 347  
      *
 348  
      * @param clu
 349  
      * @param program
 350  
      * @return
 351  
      * @throws AssemblyException
 352  
      */
 353  
     public ProgramBasicOrgAssembly assembleBasicAdminOrgs(CluInfo clu, ProgramBasicOrgAssembly program) throws AssemblyException {
 354  
 
 355  135
         if (clu.getAdminOrgs() != null) {
 356  135
             clearProgramAdminOrgs(program);
 357  135
             for (AdminOrgInfo cluOrg : clu.getAdminOrgs()) {
 358  1465
                 if (cluOrg.getType().equals(ProgramAssemblerConstants.CURRICULUM_OVERSIGHT_DIVISION)) {
 359  169
                     program.getDivisionsContentOwner().add(cluOrg.getOrgId());
 360  
                 }
 361  1296
                 else if (cluOrg.getType().equals(ProgramAssemblerConstants.STUDENT_OVERSIGHT_DIVISION)) {
 362  169
                     program.getDivisionsStudentOversight().add(cluOrg.getOrgId())  ;
 363  
                 }
 364  1127
                 else if (cluOrg.getType().equals(ProgramAssemblerConstants.CURRICULUM_OVERSIGHT_UNIT)) {
 365  169
                     program.getUnitsContentOwner().add(cluOrg.getOrgId())  ;
 366  
                 }
 367  958
                 else if (cluOrg.getType().equals(ProgramAssemblerConstants.STUDENT_OVERSIGHT_UNIT)) {
 368  220
                     program.getUnitsStudentOversight().add(cluOrg.getOrgId())  ;
 369  
                 }
 370  
             }
 371  
         }
 372  135
         return program;
 373  
     }
 374  
 
 375  
     public ProgramFullOrgAssembly assembleFullOrgs(CluInfo clu, ProgramFullOrgAssembly program) throws AssemblyException {
 376  
 
 377  94
         clearFullAdminOrgs(program);
 378  94
         for (AdminOrgInfo cluOrg : clu.getAdminOrgs()) {
 379  1259
             if (cluOrg.getType().equals(ProgramAssemblerConstants.DEPLOYMENT_DIVISION)) {
 380  119
                 program.getDivisionsDeployment().add(cluOrg.getOrgId())  ;
 381  
             }
 382  1140
             else if (cluOrg.getType().equals(ProgramAssemblerConstants.FINANCIAL_RESOURCES_DIVISION)) {
 383  119
                 program.getDivisionsFinancialResources().add(cluOrg.getOrgId())  ;
 384  
             }
 385  1021
             else if (cluOrg.getType().equals(ProgramAssemblerConstants.FINANCIAL_CONTROL_DIVISION)) {
 386  119
                 program.getDivisionsFinancialControl().add(cluOrg.getOrgId())  ;
 387  
             }
 388  902
             else if (cluOrg.getType().equals(ProgramAssemblerConstants.DEPLOYMENT_UNIT)) {
 389  119
                 program.getUnitsDeployment().add(cluOrg.getOrgId())  ;
 390  
             }
 391  783
             else if (cluOrg.getType().equals(ProgramAssemblerConstants.FINANCIAL_RESOURCES_UNIT)) {
 392  119
                 program.getUnitsFinancialResources().add(cluOrg.getOrgId())  ;
 393  
             }
 394  664
             else if (cluOrg.getType().equals(ProgramAssemblerConstants.FINANCIAL_CONTROL_UNIT)) {
 395  137
                 program.getUnitsFinancialControl().add(cluOrg.getOrgId())  ;
 396  
             }
 397  
         }
 398  94
         return program;
 399  
     }
 400  
 
 401  
     private void clearProgramAdminOrgs(ProgramBasicOrgAssembly program) {
 402  135
         program.setDivisionsContentOwner(new ArrayList<String>());
 403  135
         program.setDivisionsStudentOversight(new ArrayList<String>());
 404  135
         program.setUnitsContentOwner(new ArrayList<String>());
 405  135
         program.setUnitsStudentOversight(new ArrayList<String>());
 406  135
     }
 407  
 
 408  
     private void clearFullAdminOrgs(ProgramFullOrgAssembly program) {
 409  94
         program.setDivisionsDeployment(new ArrayList<String>());
 410  94
         program.setDivisionsFinancialResources(new ArrayList<String>());
 411  94
         program.setDivisionsFinancialControl(new ArrayList<String>());
 412  94
         program.setUnitsDeployment(new ArrayList<String>());
 413  94
         program.setUnitsFinancialResources(new ArrayList<String>());
 414  94
         program.setUnitsFinancialControl(new ArrayList<String>());
 415  94
     }
 416  
 
 417  
     /**
 418  
      * Copy AdminOrg values from program to clu
 419  
      *
 420  
      * @param clu
 421  
      * @param o
 422  
      * @param operation
 423  
      */
 424  
     public CluInfo disassembleAdminOrgs(CluInfo clu, ProgramBasicOrgAssembly program, NodeOperation operation){
 425  
 
 426  
         // clear out all old admin orgs
 427  52
         clu.setAdminOrgs(new ArrayList<AdminOrgInfo>());
 428  
 
 429  52
         newBuildAdminOrgs(clu,  program.getDivisionsContentOwner(), ProgramAssemblerConstants.CURRICULUM_OVERSIGHT_DIVISION);
 430  52
         newBuildAdminOrgs(clu, program.getDivisionsStudentOversight(), ProgramAssemblerConstants.STUDENT_OVERSIGHT_DIVISION );
 431  52
         newBuildAdminOrgs(clu,  program.getUnitsContentOwner(), ProgramAssemblerConstants.CURRICULUM_OVERSIGHT_UNIT);
 432  52
         newBuildAdminOrgs(clu, program.getUnitsStudentOversight(), ProgramAssemblerConstants.STUDENT_OVERSIGHT_UNIT );
 433  52
         if (program instanceof CredentialProgramInfo) {
 434  2
             List<String> institutionOrgs = new ArrayList<String>();
 435  2
             institutionOrgs.add(((CredentialProgramInfo)program).getInstitution().getOrgId());
 436  2
             newBuildAdminOrgs(clu, institutionOrgs , ProgramAssemblerConstants.INSTITUTION) ;
 437  
         }
 438  52
         if (program instanceof ProgramFullOrgAssembly) {
 439  35
             ProgramFullOrgAssembly fullOrg = (ProgramFullOrgAssembly) program;
 440  35
             newBuildAdminOrgs(clu, fullOrg.getDivisionsDeployment(), ProgramAssemblerConstants.DEPLOYMENT_DIVISION);
 441  35
             newBuildAdminOrgs(clu, fullOrg.getDivisionsFinancialResources(), ProgramAssemblerConstants.FINANCIAL_RESOURCES_DIVISION);
 442  35
             newBuildAdminOrgs(clu, fullOrg.getDivisionsFinancialControl(), ProgramAssemblerConstants.FINANCIAL_CONTROL_DIVISION);
 443  35
             newBuildAdminOrgs(clu, fullOrg.getUnitsDeployment(), ProgramAssemblerConstants.DEPLOYMENT_UNIT);
 444  35
             newBuildAdminOrgs(clu, fullOrg.getUnitsFinancialResources(), ProgramAssemblerConstants.FINANCIAL_RESOURCES_UNIT);
 445  35
             newBuildAdminOrgs(clu, fullOrg.getUnitsFinancialControl(), ProgramAssemblerConstants.FINANCIAL_CONTROL_UNIT);
 446  
 
 447  
         }
 448  52
         return clu;
 449  
 
 450  
     }
 451  
 
 452  
     private CluInfo newBuildAdminOrgs(CluInfo clu,  List<String> orgIds, String type) {
 453  
 
 454  420
         if (null != orgIds) {
 455  420
             for (String orgId : orgIds) {
 456  622
                 AdminOrgInfo subjectOrg = new AdminOrgInfo();
 457  622
                 subjectOrg.setType(type);
 458  622
                 subjectOrg.setOrgId(orgId);
 459  622
                 clu.getAdminOrgs().add(subjectOrg);
 460  622
             }
 461  
         }
 462  420
         return clu;
 463  
     }
 464  
 
 465  
     /**
 466  
      * Copy result option values from clu to program
 467  
      *
 468  
      * @param cluId
 469  
      * @param resultType
 470  
      * @return
 471  
      * @throws AssemblyException
 472  
      */
 473  
     public List<String> assembleResultOptions(String cluId) throws AssemblyException {
 474  60
         List<String> resultOptions = null;
 475  
         try{
 476  60
             List<CluResultInfo> cluResults = luService.getCluResultByClu(cluId);
 477  
 
 478  60
             List<String> resultTypes = new ArrayList<String>();
 479  60
             resultTypes.add(ProgramAssemblerConstants.DEGREE_RESULTS);
 480  60
             resultTypes.add(ProgramAssemblerConstants.CERTIFICATE_RESULTS);
 481  
 
 482  60
             resultOptions = cluAssemblerUtils.assembleCluResults(resultTypes, cluResults);
 483  
 
 484  0
         } catch (DoesNotExistException e){
 485  0
         } catch (Exception e) {
 486  0
             throw new AssemblyException("Error getting major results", e);
 487  60
         }
 488  60
         return resultOptions;
 489  
     }
 490  
 
 491  
     /**
 492  
      * Copy atp values  from clu to program
 493  
      *
 494  
      * @param clu
 495  
      * @param program
 496  
      * @return
 497  
      * @throws AssemblyException
 498  
      */
 499  
     public ProgramAtpAssembly assembleAtps(CluInfo clu, ProgramAtpAssembly program) throws AssemblyException {
 500  
 
 501  135
         if (clu.getExpectedFirstAtp() != null) {
 502  130
             program.setStartTerm(clu.getExpectedFirstAtp());
 503  
         }
 504  135
         if (clu.getLastAtp() != null) {
 505  130
             program.setEndTerm(clu.getLastAtp());
 506  
         }
 507  135
         if (clu.getLastAdmitAtp() != null) {
 508  130
             program.setEndProgramEntryTerm(clu.getLastAdmitAtp());
 509  
         }
 510  135
         return program;
 511  
     }
 512  
 
 513  
 
 514  
     /**
 515  
      * Copy atp values from Program to clu
 516  
      *
 517  
      * @param clu
 518  
      * @param program
 519  
      * @return
 520  
      * @throws AssemblyException
 521  
      */
 522  
     public CluInfo disassembleAtps(CluInfo clu, ProgramAtpAssembly program, NodeOperation operation) throws AssemblyException {
 523  
 
 524  52
         clu.setExpectedFirstAtp(program.getStartTerm());
 525  52
         clu.setLastAtp(program.getEndTerm());
 526  52
         clu.setLastAdmitAtp(program.getEndProgramEntryTerm());
 527  
 
 528  52
         return clu;
 529  
 
 530  
     }
 531  
 
 532  
     /**
 533  
      * Copy publication values from clu to program
 534  
      *
 535  
      * @param clu
 536  
      * @param program
 537  
      * @return
 538  
      * @throws AssemblyException
 539  
      */
 540  
     public ProgramPublicationAssembly assemblePublications(CluInfo clu, ProgramPublicationAssembly program) throws AssemblyException {
 541  
 
 542  
 
 543  116
         if (clu.getReferenceURL() != null) {
 544  116
             program.setReferenceURL(clu.getReferenceURL());
 545  
         }
 546  
 
 547  
         try {
 548  116
             List<CluPublicationInfo> cluPublications = luService.getCluPublicationsByCluId(clu.getId());
 549  
 
 550  116
             List<String> targets = new ArrayList<String>();
 551  
 
 552  116
             for (CluPublicationInfo cluPublication : cluPublications) {
 553  104
                 if (cluPublication.getType().equals(ProgramAssemblerConstants.CATALOG)) {
 554  83
                     assembleCatalogDescr(program, cluPublication);
 555  
                 }
 556  
                 else {
 557  21
                     targets.add(cluPublication.getType());
 558  
                 }
 559  
             }
 560  
 
 561  116
             if (targets != null && !targets.isEmpty()) {
 562  12
                 program.setCatalogPublicationTargets(targets);
 563  
             }
 564  0
         } catch (DoesNotExistException e) {
 565  0
         } catch (InvalidParameterException e) {
 566  0
         } catch (MissingParameterException e) {
 567  0
         } catch (OperationFailedException e) {
 568  0
             throw new AssemblyException("Error getting publication targets", e);
 569  116
         }
 570  116
         return program;
 571  
     }
 572  
 
 573  
     private void assembleCatalogDescr(ProgramPublicationAssembly program, CluPublicationInfo cluPublication)  {
 574  
 
 575  83
         for (FieldInfo fieldInfo : cluPublication.getVariants()) {
 576  73
             if (fieldInfo.getId().equals(ProgramAssemblerConstants.CATALOG_DESCR)) {
 577  73
                 RichTextInfo desc = new RichTextInfo();
 578  73
                 desc.setPlain(fieldInfo.getValue());
 579  73
                 desc.setFormatted(fieldInfo.getValue());
 580  73
                 program.setCatalogDescr(desc);
 581  73
                 break;
 582  
             }
 583  
         }
 584  83
     }
 585  
 
 586  
      private List<BaseDTOAssemblyNode<?, ?>> disassembleCatalogDescr(ProgramPublicationAssembly program,  NodeOperation operation) throws AssemblyException {
 587  
 
 588  50
          List<BaseDTOAssemblyNode<?, ?>> results = new ArrayList<BaseDTOAssemblyNode<?, ?>>();
 589  
 
 590  50
          CluPublicationInfo currentPubInfo = null;
 591  
 
 592  
          try {
 593  
 
 594  
              // if not create get current catalog descr
 595  50
              if (!NodeOperation.CREATE.equals(operation)) {
 596  34
                  List<CluPublicationInfo> pubs = luService.getCluPublicationsByCluId(program.getId());
 597  34
                  for (CluPublicationInfo pubInfo : pubs) {
 598  30
                      if (pubInfo.getType().equals(ProgramAssemblerConstants.CATALOG)) {
 599  19
                          currentPubInfo = pubInfo;
 600  
                      }
 601  
                  }
 602  
              }
 603  
 
 604  50
              if (program.getCatalogDescr() != null) {
 605  
                  //  If this is a create then create new catalog descr
 606  43
                  if (NodeOperation.CREATE == operation
 607  
                          || (NodeOperation.UPDATE == operation && currentPubInfo == null )) {
 608  
                      // the description does not exist, so create
 609  24
                      CluPublicationInfo pubInfo = buildCluPublicationInfo(program.getId(), ProgramAssemblerConstants.CATALOG);
 610  
 
 611  24
                      FieldInfo variant = new FieldInfo();
 612  24
                      variant.setId(ProgramAssemblerConstants.CATALOG_DESCR);
 613  24
                      variant.setValue(program.getCatalogDescr() .getPlain());
 614  24
                      pubInfo.getVariants().add(variant);
 615  
 
 616  24
                      BaseDTOAssemblyNode<Object, CluPublicationInfo> pubNode = new BaseDTOAssemblyNode<Object, CluPublicationInfo>(
 617  
                              null);
 618  24
                      pubNode.setNodeData(pubInfo);
 619  24
                      pubNode.setOperation(NodeOperation.CREATE);
 620  
 
 621  24
                      results.add(pubNode);
 622  24
                  } else if (NodeOperation.UPDATE == operation
 623  
                          && currentPubInfo != null) {
 624  
 
 625  19
                      CluPublicationInfo pubInfo = currentPubInfo;
 626  
 
 627  19
                      for (FieldInfo fieldInfo : pubInfo.getVariants()) {
 628  19
                          if (fieldInfo.getId().equals(ProgramAssemblerConstants.CATALOG_DESCR)) {
 629  19
                              fieldInfo.setValue(program.getCatalogDescr() .getPlain());
 630  19
                              break;
 631  
                          }
 632  
                      }
 633  
 
 634  19
                      BaseDTOAssemblyNode<Object, CluPublicationInfo> pubNode = new BaseDTOAssemblyNode<Object, CluPublicationInfo>(
 635  
                              null);
 636  19
                      pubNode.setNodeData(pubInfo);
 637  19
                      pubNode.setOperation(NodeOperation.UPDATE);
 638  
 
 639  19
                      results.add(pubNode);
 640  
                      
 641  19
                  }
 642  0
                  else if (NodeOperation.DELETE == operation  )  {
 643  
 
 644  0
                      deletePublicationInfo(results, currentPubInfo);
 645  
                  }
 646  
              }
 647  0
          } catch (Exception e) {
 648  0
              throw new AssemblyException(e);
 649  50
          }
 650  50
          return results;
 651  
     }
 652  
 
 653  
     private void deletePublicationInfo(List<BaseDTOAssemblyNode<?, ?>> results, CluPublicationInfo currentPubInfo) {
 654  0
         CluPublicationInfo descrToDelete = new CluPublicationInfo();
 655  0
         descrToDelete.setId(currentPubInfo.getId());
 656  0
         BaseDTOAssemblyNode<Object, CluPublicationInfo> pubToDeleteNode = new BaseDTOAssemblyNode<Object, CluPublicationInfo>(
 657  
                 null);
 658  0
         pubToDeleteNode.setNodeData(descrToDelete);
 659  0
         pubToDeleteNode.setOperation(NodeOperation.DELETE);
 660  0
         results.add(pubToDeleteNode);
 661  0
     }
 662  
 
 663  
     /**
 664  
      * Copy publication values from program to clu
 665  
      *
 666  
      * @param clu
 667  
      * @param program
 668  
      * @param operation
 669  
      * @return
 670  
      * @throws AssemblyException
 671  
      */
 672  
     public CluInfo disassemblePublications(CluInfo clu, ProgramPublicationAssembly program, NodeOperation operation, BaseDTOAssemblyNode<?, ?> result) throws AssemblyException {
 673  
 
 674  50
         clu.setReferenceURL(program.getReferenceURL());
 675  
 
 676  50
         List<BaseDTOAssemblyNode<?, ?>> targetResults = disassemblePublicationTargets(program, operation);
 677  50
         if (targetResults != null && targetResults.size()> 0) {
 678  16
             result.getChildNodes().addAll(targetResults);
 679  
         }
 680  
 
 681  50
         List<BaseDTOAssemblyNode<?, ?>> descrResults = disassembleCatalogDescr(program, operation) ;
 682  50
         if (descrResults != null && descrResults.size()> 0) {
 683  43
             result.getChildNodes().addAll(descrResults);
 684  
         }
 685  
 
 686  50
         return clu;
 687  
 
 688  
     }
 689  
 
 690  
     /**
 691  
      * Copy credential program id value from program to clu
 692  
      *
 693  
      * @param clu
 694  
      * @param o
 695  
      * @param operation
 696  
      * @return
 697  
      * @throws AssemblyException
 698  
      */
 699  
     public List<BaseDTOAssemblyNode<?,?>>  disassembleCredentialProgram(ProgramCredentialAssembly program, NodeOperation operation, String relationType) throws AssemblyException {
 700  
 
 701  11
         List<BaseDTOAssemblyNode<?, ?>> results = new ArrayList<BaseDTOAssemblyNode<?, ?>>();
 702  
 
 703  
         try {
 704  11
             CluInfo credentialClu = luService.getClu(program.getCredentialProgramId());
 705  0
         } catch (DoesNotExistException e) {
 706  0
         } catch (Exception e) {
 707  0
             throw new AssemblyException("Credential Clu does not exist for " + program.getCredentialProgramId());
 708  11
         }
 709  
 
 710  11
         Map<String, String> currentRelations = new HashMap<String, String>();
 711  
 
 712  11
         if (!NodeOperation.CREATE.equals(operation)) {
 713  
             try {
 714  8
                 List<CluCluRelationInfo> cluRelations = luService.getCluCluRelationsByClu(program.getId());
 715  8
                 for (CluCluRelationInfo cluRelation : cluRelations) {
 716  32
                     if (relationType.equals(cluRelation.getType()) ) {
 717  6
                         currentRelations.put(cluRelation.getRelatedCluId(), cluRelation.getId());
 718  
                     }
 719  
                 }
 720  0
             } catch (DoesNotExistException e) {
 721  0
             } catch (InvalidParameterException e) {
 722  0
             } catch (MissingParameterException e) {
 723  0
             } catch (OperationFailedException e) {
 724  0
                 throw new AssemblyException("Error getting related clus", e);
 725  8
             }
 726  
         }
 727  
 
 728  
 
 729  
         //  If this is a create then vreate new relation
 730  11
         if (NodeOperation.CREATE == operation
 731  
                 || (NodeOperation.UPDATE == operation && !currentRelations.containsKey(program.getCredentialProgramId()) )) {
 732  
             // the relation does not exist, so create
 733  11
             CluCluRelationInfo relation = new CluCluRelationInfo();
 734  11
             relation.setCluId(program.getCredentialProgramId());
 735  11
             relation.setRelatedCluId(program.getId());
 736  11
             relation.setType(relationType);
 737  11
             relation.setState(ProgramAssemblerConstants.ACTIVE);
 738  
 
 739  11
             BaseDTOAssemblyNode<Object, CluCluRelationInfo> relationNode = new BaseDTOAssemblyNode<Object, CluCluRelationInfo>(
 740  
                     null);
 741  11
             relationNode.setNodeData(relation);
 742  11
             relationNode.setOperation(NodeOperation.CREATE);
 743  
 
 744  11
             results.add(relationNode);
 745  11
         } else if (NodeOperation.UPDATE == operation
 746  
                 && currentRelations.containsKey(program.getCredentialProgramId())) {
 747  
             // If the relationship already exists update it
 748  
 
 749  
             // remove this entry from the map so we can tell what needs to
 750  
             // be deleted at the end
 751  0
             currentRelations.remove(program.getCredentialProgramId());
 752  0
         } else if (NodeOperation.DELETE == operation
 753  
                 && currentRelations.containsKey(program.getId()))  {
 754  
             // Delete the Format and its relation
 755  0
             CluCluRelationInfo relationToDelete = new CluCluRelationInfo();
 756  0
             relationToDelete.setId( currentRelations.get(program.getId()) );
 757  0
             BaseDTOAssemblyNode<Object, CluCluRelationInfo> relationToDeleteNode = new BaseDTOAssemblyNode<Object, CluCluRelationInfo>(
 758  
                     null);
 759  0
             relationToDeleteNode.setNodeData(relationToDelete);
 760  0
             relationToDeleteNode.setOperation(NodeOperation.DELETE);
 761  0
             results.add(relationToDeleteNode);
 762  
 
 763  
             // remove this entry from the map so we can tell what needs to
 764  
             // be deleted at the end
 765  0
             currentRelations.remove(program.getId());
 766  
         }
 767  
 
 768  11
         if(currentRelations != null && currentRelations.size() > 0){
 769  6
                 for (Map.Entry<String, String> entry : currentRelations.entrySet()) {
 770  
                     // Create a new relation with the id of the relation we want to
 771  
                     // delete
 772  6
                     CluCluRelationInfo relationToDelete = new CluCluRelationInfo();
 773  6
                     relationToDelete.setId( entry.getValue() );
 774  6
                     BaseDTOAssemblyNode<Object, CluCluRelationInfo> relationToDeleteNode = new BaseDTOAssemblyNode<Object, CluCluRelationInfo>(
 775  
                             null);
 776  6
                     relationToDeleteNode.setNodeData(relationToDelete);
 777  6
                     relationToDeleteNode.setOperation(NodeOperation.DELETE);
 778  6
                     results.add(relationToDeleteNode);
 779  6
                 }
 780  
         }
 781  11
         return results;
 782  
     }
 783  
 
 784  
     public List<BaseDTOAssemblyNode<?, ?>> addRelationNodes(String cluId, String relatedCluId, String relationType, NodeOperation operation)throws AssemblyException{
 785  0
             Map<String, String> currentRelations = null;
 786  0
             List<BaseDTOAssemblyNode<?, ?>> results = new ArrayList<BaseDTOAssemblyNode<?, ?>>();
 787  
 
 788  0
         if (!NodeOperation.CREATE.equals(operation)) {
 789  0
                 currentRelations = getCluCluRelations(cluId, relationType);
 790  
         }
 791  
 
 792  
         //  If this is a create then vreate new relation
 793  0
         if (NodeOperation.CREATE == operation
 794  
                 || (NodeOperation.UPDATE == operation && !currentRelations.containsKey(relatedCluId) )) {
 795  
             // the relation does not exist, so create
 796  0
                 addCreateRelationNode(cluId, relatedCluId, relationType, results);
 797  0
         } else if (NodeOperation.UPDATE == operation
 798  
                 && currentRelations.containsKey(relatedCluId)) {
 799  
             // If the relationship already exists update it
 800  
 
 801  
             // remove this entry from the map so we can tell what needs to
 802  
             // be deleted at the end
 803  0
             currentRelations.remove(relatedCluId);
 804  0
         } else if (NodeOperation.DELETE == operation
 805  
                 && currentRelations.containsKey(relatedCluId))  {
 806  
             // Delete the Format and its relation
 807  0
                 addDeleteRelationNodes(currentRelations, results);
 808  
 
 809  
             // remove this entry from the map so we can tell what needs to
 810  
             // be deleted at the end
 811  0
             currentRelations.remove(relatedCluId);
 812  
         }
 813  
         
 814  0
         if(currentRelations != null && currentRelations.size() > 0){
 815  0
                 for (Map.Entry<String, String> entry : currentRelations.entrySet()) {
 816  
                     // Create a new relation with the id of the relation we want to
 817  
                     // delete
 818  0
                     CluCluRelationInfo relationToDelete = new CluCluRelationInfo();
 819  0
                     relationToDelete.setId( entry.getValue() );
 820  0
                     BaseDTOAssemblyNode<Object, CluCluRelationInfo> relationToDeleteNode = new BaseDTOAssemblyNode<Object, CluCluRelationInfo>(
 821  
                             null);
 822  0
                     relationToDeleteNode.setNodeData(relationToDelete);
 823  0
                     relationToDeleteNode.setOperation(NodeOperation.DELETE);
 824  0
                     results.add(relationToDeleteNode);
 825  0
                 }
 826  
         }
 827  0
         return results;
 828  
     }
 829  
     public List<BaseDTOAssemblyNode<?, ?>> addAllRelationNodes(String cluId, String relatedCluId, String relationType, NodeOperation operation, Map<String, String> currentRelations)throws AssemblyException{
 830  29
             List<BaseDTOAssemblyNode<?, ?>> results = new ArrayList<BaseDTOAssemblyNode<?, ?>>();
 831  
 
 832  29
                 if (NodeOperation.CREATE == operation
 833  
                         || (NodeOperation.UPDATE == operation && !currentRelations.containsKey(relatedCluId) )) {
 834  
                     // the relation does not exist, so create
 835  27
                         addCreateRelationNode(cluId, relatedCluId, relationType, results);
 836  2
                 } else if (NodeOperation.UPDATE == operation
 837  
                         && currentRelations.containsKey(relatedCluId)) {
 838  
                     // If the relationship already exists update it
 839  
                 
 840  
                     // remove this entry from the map so we can tell what needs to
 841  
                     // be deleted at the end
 842  2
                     currentRelations.remove(relatedCluId);
 843  0
                 } else if (NodeOperation.DELETE == operation
 844  
                         && currentRelations.containsKey(relatedCluId))  {
 845  
                     // Delete the Format and its relation
 846  0
                         addDeleteRelationNodes(currentRelations, results);
 847  
                 
 848  
                     // remove this entry from the map so we can tell what needs to
 849  
                     // be deleted at the end
 850  0
                     currentRelations.remove(relatedCluId);
 851  
                 }
 852  
         
 853  29
         return results;
 854  
     }
 855  
     public Map<String, String> getCluCluRelations(String cluId, String relationType) throws AssemblyException{
 856  3
         Map<String, String> currentRelations = new HashMap<String, String>();
 857  
 
 858  
             try {
 859  3
                 List<CluCluRelationInfo> cluRelations = luService.getCluCluRelationsByClu(cluId);
 860  
                
 861  3
                 for (CluCluRelationInfo cluRelation : cluRelations) {
 862  13
                     if (relationType.equals(cluRelation.getType())) {
 863  4
                         currentRelations.put(cluRelation.getRelatedCluId(), cluRelation.getId());
 864  
                     }
 865  
                 }
 866  0
             } catch (DoesNotExistException e) {
 867  0
             } catch (InvalidParameterException e) {
 868  0
             } catch (MissingParameterException e) {
 869  0
             } catch (OperationFailedException e) {
 870  0
                 throw new AssemblyException("Error getting related clus", e);
 871  3
             }
 872  
 
 873  3
             return currentRelations;
 874  
     }
 875  
     
 876  
     public Map<String, CluCluRelationInfo> getCluCluActiveRelations(String cluId, String relationType) throws AssemblyException{
 877  22
         Map<String, CluCluRelationInfo> currentRelations = new HashMap<String, CluCluRelationInfo>();
 878  
 
 879  
             try {
 880  22
                 List<CluCluRelationInfo> cluRelations = luService.getCluCluRelationsByClu(cluId);
 881  
 
 882  22
                 for (CluCluRelationInfo cluRelation : cluRelations) {
 883  97
                     if (relationType.equals(cluRelation.getType()) && (!cluRelation.getState().isEmpty() && cluRelation.getState().equalsIgnoreCase(ProgramAssemblerConstants.ACTIVE))) {
 884  47
                         currentRelations.put(cluRelation.getRelatedCluId(), cluRelation);
 885  
                     }
 886  
                 }
 887  0
             } catch (DoesNotExistException e) {
 888  0
             } catch (InvalidParameterException e) {
 889  0
             } catch (MissingParameterException e) {
 890  0
             } catch (OperationFailedException e) {
 891  0
                 throw new AssemblyException("Error getting related clus", e);
 892  22
             }
 893  
 
 894  22
             return currentRelations;
 895  
     }
 896  
 
 897  
     public void addCreateRelationNode(String cluId, String relatedCluId, String relationType, List<BaseDTOAssemblyNode<?, ?>> results){
 898  34
         CluCluRelationInfo relation = new CluCluRelationInfo();
 899  34
         relation.setCluId(cluId);
 900  34
         relation.setRelatedCluId(relatedCluId);
 901  34
         relation.setType(relationType);
 902  34
         relation.setState(ProgramAssemblerConstants.ACTIVE);
 903  
 
 904  34
         BaseDTOAssemblyNode<Object, CluCluRelationInfo> relationNode = new BaseDTOAssemblyNode<Object, CluCluRelationInfo>(
 905  
                 null);
 906  34
         relationNode.setNodeData(relation);
 907  34
         relationNode.setOperation(NodeOperation.CREATE);
 908  
 
 909  34
         results.add(relationNode);
 910  
 
 911  34
     }
 912  
 
 913  
     public void addDeleteRelationNodes(Map<String, String> currentRelations, List<BaseDTOAssemblyNode<?, ?>> results){
 914  0
         for (Map.Entry<String, String> entry : currentRelations.entrySet()) {
 915  
             // Create a new relation with the id of the relation we want to
 916  
             // delete
 917  0
             CluCluRelationInfo relationToDelete = new CluCluRelationInfo();
 918  0
             relationToDelete.setId( entry.getValue() );
 919  0
             BaseDTOAssemblyNode<Object, CluCluRelationInfo> relationToDeleteNode = new BaseDTOAssemblyNode<Object, CluCluRelationInfo>(
 920  
                     null);
 921  0
             relationToDeleteNode.setNodeData(relationToDelete);
 922  0
             relationToDeleteNode.setOperation(NodeOperation.DELETE);
 923  0
             results.add(relationToDeleteNode);
 924  0
         }
 925  0
     }
 926  
     
 927  
     public void addInactiveRelationNodes(Map<String, CluCluRelationInfo> currentRelations, List<BaseDTOAssemblyNode<?, ?>> results){
 928  1
         for (Map.Entry<String, CluCluRelationInfo> entry : currentRelations.entrySet()) {
 929  1
             CluCluRelationInfo inactiveRelation = new CluCluRelationInfo();
 930  1
             inactiveRelation = entry.getValue();
 931  1
             inactiveRelation.setState(ProgramAssemblerConstants.INACTIVE);
 932  1
             BaseDTOAssemblyNode<Object, CluCluRelationInfo> inactiveRelationNode = new BaseDTOAssemblyNode<Object, CluCluRelationInfo>(
 933  
                     null);
 934  1
             inactiveRelationNode.setNodeData(inactiveRelation);
 935  1
             inactiveRelationNode.setOperation(NodeOperation.UPDATE);
 936  1
             results.add(inactiveRelationNode);
 937  1
         }
 938  1
     }
 939  
 
 940  
     private void addLuCodeFromProgram(String type, String value, List<LuCodeInfo> list) throws AssemblyException {
 941  
 
 942  260
         if (value != null && !value.isEmpty()) {
 943  191
             LuCodeInfo code = new LuCodeInfo();
 944  191
             code.setType(type);
 945  191
             code.setValue(value);
 946  191
             code.setAttributes(new HashMap<String, String>());
 947  191
             list.add(code);
 948  
         }
 949  260
     }
 950  
 
 951  
     /**
 952  
      * Copy publications from program to clu
 953  
      *
 954  
      * @param clu
 955  
      * @param o
 956  
      * @param operation
 957  
      * @return
 958  
      * @throws AssemblyException
 959  
           */
 960  
     private List<BaseDTOAssemblyNode<?, ?>> disassemblePublicationTargets(ProgramPublicationAssembly program,  NodeOperation operation) throws AssemblyException {
 961  
 
 962  50
         List<BaseDTOAssemblyNode<?, ?>> results = new ArrayList<BaseDTOAssemblyNode<?, ?>>();
 963  
 
 964  50
         Map<String, CluPublicationInfo> currentPubs = new HashMap<String, CluPublicationInfo>();
 965  50
         if (!NodeOperation.CREATE.equals(operation)) {
 966  
 
 967  
             // Get the current publications and put them in a map
 968  
             try {
 969  34
                 List<CluPublicationInfo> cluPubs = luService.getCluPublicationsByCluId(program.getId());
 970  34
                 for(CluPublicationInfo cluPub : cluPubs){
 971  30
                     if (!cluPub.getType().equals(ProgramAssemblerConstants.CATALOG)) {
 972  11
                         currentPubs.put(cluPub.getType(), cluPub);                        
 973  
                     }
 974  
                 }
 975  0
             } catch (DoesNotExistException e) {
 976  0
             } catch (Exception e) {
 977  0
                 throw new AssemblyException("Error finding publications");
 978  34
             }
 979  
         }
 980  
 
 981  50
         if (program.getCatalogPublicationTargets() != null && !program.getCatalogPublicationTargets().isEmpty()) {
 982  21
             for (String publicationType : program.getCatalogPublicationTargets()) {
 983  
                 //  If this is a create then create new publication
 984  42
                 if (NodeOperation.CREATE == operation
 985  
                         || (NodeOperation.UPDATE == operation && !currentPubs.containsKey(publicationType) )) {
 986  
                     // the publication does not exist, so create
 987  31
                     CluPublicationInfo pubInfo = buildCluPublicationInfo(program.getId(), publicationType);
 988  
 
 989  31
                     BaseDTOAssemblyNode<Object, CluPublicationInfo> pubNode = new BaseDTOAssemblyNode<Object, CluPublicationInfo>(
 990  
                             null);
 991  31
                     pubNode.setNodeData(pubInfo);
 992  31
                     pubNode.setOperation(NodeOperation.CREATE);
 993  
 
 994  31
                     results.add(pubNode);
 995  31
                 } else if (NodeOperation.UPDATE == operation
 996  
                         && currentPubs.containsKey(publicationType)) {
 997  
                     //Only types are exposed to the user so don't need to do anything here
 998  
 
 999  11
                     currentPubs.remove(publicationType);
 1000  0
                 } else if (NodeOperation.DELETE == operation
 1001  
                         && currentPubs.containsKey(publicationType))  {
 1002  
 
 1003  0
                     CluPublicationInfo pubToDelete = new CluPublicationInfo();
 1004  0
                     pubToDelete.setId(publicationType);
 1005  0
                     BaseDTOAssemblyNode<Object, CluPublicationInfo> pubToDeleteNode = new BaseDTOAssemblyNode<Object, CluPublicationInfo>(
 1006  
                             null);
 1007  0
                     pubToDeleteNode.setNodeData(pubToDelete);
 1008  0
                     pubToDeleteNode.setOperation(NodeOperation.DELETE);
 1009  0
                     results.add(pubToDeleteNode);
 1010  
 
 1011  0
                     currentPubs.remove(publicationType);
 1012  42
                 }
 1013  
             }
 1014  
         }
 1015  
 
 1016  
 
 1017  50
         for (Map.Entry<String, CluPublicationInfo> entry : currentPubs.entrySet()) {
 1018  
             // Create a new relation with the id of the relation we want to
 1019  
             // delete
 1020  0
             deletePublicationInfo(results, entry.getValue());
 1021  
         }
 1022  
 
 1023  50
         return results;
 1024  
     }
 1025  
 
 1026  
     private CluPublicationInfo buildCluPublicationInfo(String programId, String publicationType) throws AssemblyException {
 1027  
 
 1028  55
         CluPublicationInfo pubInfo = new CluPublicationInfo();
 1029  55
         pubInfo.setType(publicationType);
 1030  55
         pubInfo.setCluId(programId);
 1031  55
         pubInfo.setState(ProgramAssemblerConstants.ACTIVE);
 1032  
 
 1033  55
         return pubInfo;
 1034  
     }
 1035  
 
 1036  
     // Spring setters
 1037  
     public void setLuService(LuService luService) {
 1038  1
         this.luService = luService;
 1039  1
     }
 1040  
 
 1041  
     public void setCluAssemblerUtils(CluAssemblerUtils cluAssemblerUtils) {
 1042  1
         this.cluAssemblerUtils = cluAssemblerUtils;
 1043  1
     }
 1044  
 
 1045  
     public String getCredentialProgramID(String cluId) throws AssemblyException {
 1046  
 
 1047  14
         List<String> credentialProgramIDs = null;
 1048  
         try {
 1049  14
             credentialProgramIDs = luService.getCluIdsByRelation(cluId, ProgramAssemblerConstants.HAS_MAJOR_PROGRAM);
 1050  0
         } catch (Exception e) {
 1051  0
             throw new AssemblyException(e);
 1052  14
         }
 1053  
         // Can a Program have more than one Credential Program?
 1054  
         // TODO - do we need to validate that?
 1055  14
         if (null == credentialProgramIDs || credentialProgramIDs.isEmpty()) {
 1056  0
             throw new AssemblyException("Program with ID == " + cluId + " has no Credential Program associated with it.");
 1057  14
         } else if (credentialProgramIDs.size() > 1) {
 1058  0
             throw new AssemblyException("Program with ID == " + cluId + " has more than one Credential Program associated with it.");
 1059  
         }
 1060  14
         return credentialProgramIDs.get(0);
 1061  
     }
 1062  
 }