From Divya M Jose, 5 Years ago, written in Plain Text.
Embed
  1.     private Mono<ProvisioningPojo> provisionClass(List<Member> namesRoleMembers, ProvisioningPojo provisioningPojo) {
  2.         return this.checkIfClassExists(provisioningPojo).flatMap(isClassPresent -> {
  3.            
  4.  /****  Move the code into a new private method     ****/
  5.             if (isClassPresent) {
  6.                 if (isTeacher(provisioningPojo, provisioningPojo.getToolUserId())) {
  7.                     List<Staff> existingTeacher = provisioningPojo.getRoster().getExistingTeacherIds()
  8.                         .stream()
  9.                         .filter(staff -> staff.getStaffPiId().equalsIgnoreCase(provisioningPojo.getToolUserId()))
  10.                         .collect(Collectors.toList());
  11.                     if(existingTeacher.isEmpty()) {
  12.                         // call the roster service to update Roster with teacher
  13.                     }
  14.                 } else if (isStudent(provisioningPojo, provisioningPojo.getToolUserId())) {
  15.                     List<Student> existingStudent = provisioningPojo.getRoster().getExistingStudentIds()
  16.                         .stream()
  17.                         .filter(staff -> staff.getStudentPiId().equalsIgnoreCase(provisioningPojo.getToolUserId()))
  18.                         .collect(Collectors.toList());
  19.                     if (existingStudent.isEmpty()) {
  20.                         // call the roster service to update Roster with Student
  21.                     }
  22.                 }
  23.                
  24.                 // update product
  25.                 List<Product> existingProduct = provisioningPojo.getRoster().getExistingProductIds()
  26.                     .stream()
  27.                     .filter(products -> products.getProductId().equalsIgnoreCase(provisioningPojo.getProductId()))
  28.                     .collect(Collectors.toList());
  29.                 if (existingProduct.isEmpty()) {
  30.                     // call the roster service to update Roster with Product
  31.                 }
  32.                    
  33.                 return Mono.just(provisioningPojo);
  34.             } else {
  35.                 return processMembersFromNamesRolesResponse(namesRoleMembers, provisioningPojo)
  36.                         .flatMap(this::createClassAndMapping)
  37.                         .filter(p -> p.getRole().equalsIgnoreCase("T"))
  38.                         .flatMap(this::updateProductRoster);
  39.             }
  40.         });
  41.     }
  42.    
  43.     /***  New method to update the Roster if role is teacher *****/
  44.     private Mono<ProvisioningPojo> updateProductRoster(ProvisioningPojo provisioningPojo) {
  45.         String sectionId = provisioningPojo.getRoster().getToolSectionId();
  46.         RosterProductRequest rosterProductRequest = new RosterProductRequest();
  47.         SectionWrapper sectionWrapper = new SectionWrapper();
  48.         SectionProduct section = new SectionProduct();
  49.         SectionDataProduct sectionData = new SectionDataProduct();
  50.  
  51.         ProductInfo productInfo = new ProductInfo();
  52.         productInfo.setProductId(provisioningPojo.getProductId());
  53.         productInfo.setAddedBy(provisioningPojo.getToolUserId());
  54.        
  55.         List<ProductInfo> productsInfo = new ArrayList<>();
  56.         productsInfo.add(productInfo);
  57.        
  58.         sectionData.setProductsInfo(productsInfo);
  59.         section.setSectionData(sectionData);
  60.  
  61.         sectionWrapper.setSection(section);
  62.         List<SectionWrapper> sectionWrapperList = new ArrayList<>();
  63.         sectionWrapperList.add(sectionWrapper);
  64.        
  65.         rosterProductRequest.setSectionWrapperList(sectionWrapperList);
  66.        
  67.         /***  This is just to start a discussion, I know we wont be doing lines 216 to 224   ***/
  68.         return rosterServiceClient.addProductsToSection(sectionId, provisioningPojo.getToolUserId() ,rosterProductRequest)
  69.             .flatMap(classProductResponse -> { // 216
  70.              Optional<Integer> x = Optional.ofNullable(classProductResponse)
  71.                     .map(ClassProductResponse::getErrorCode);
  72.              
  73.              if(x.isEmpty()) {
  74.                  return Mono.just(provisioningPojo);
  75.              }
  76.              return Mono.just(provisioningPojo); // 224
  77.             });
  78.     }
  79.  
  80.     private Mono<Boolean> checkIfClassExists(ProvisioningPojo provisioningPojo) {
  81.         return emsClassMappingClient.classExistsForExternalClassId(provisioningPojo.getRoster().getExternalClassId())
  82.             .flatMap(externalClassMappingData -> {
  83.                 Optional<String> optionalClassId = Optional.ofNullable(externalClassMappingData)
  84.                     .map(ExternalClassMappingData::getData)
  85.                     .map(map -> map.entrySet().stream())
  86.                     .map(Stream::findFirst)
  87.                     .map(Optional::get)
  88.                     .map(Entry::getValue)
  89.                     .map(ExternalClassMapping::getClassId);
  90.  
  91.                 if (optionalClassId.isPresent()) {
  92.                     return rosterServiceClient.getClassBySectionId(optionalClassId.get())
  93.                         .flatMap(cmsClass -> {
  94.                             Optional<String> cmsClassId = Optional.ofNullable(cmsClass)
  95.                                 .map(CMSClass::getData)
  96.                                 .map(ClassData::getSection)
  97.                                 .map(Section::getId);
  98.                             /****  update ProvisioningPojo Pojo     ****/
  99.                             if (cmsClassId.isPresent()) {
  100.                                 provisioningPojo.getRoster().setToolSectionId(cmsClassId.get());
  101.                                 provisioningPojo.getRoster()
  102.                                         .setExistingTeacherIds(cmsClass.getData().getSection().getData().getSectionInfo().getStaff());
  103.                                 provisioningPojo.getRoster()
  104.                                         .setExistingStudentIds(cmsClass.getData().getSection().getData().getSectionInfo().getStudents());
  105.                                 provisioningPojo.getRoster()
  106.                                         .setExistingProductIds(cmsClass.getData().getSection().getData().getSectionProductsAssociationList());
  107.                                
  108.                                 if (isStudent(provisioningPojo, provisioningPojo.getToolUserId()) && provisioningPojo.getRoster().getExistingTeacherIds().isEmpty()) {
  109.                                     provisioningPojo.setUpdateStaffIfStudent(true);
  110.                                 }
  111.                                
  112.                                 return Mono.just(Boolean.TRUE);
  113.                             }
  114.                             return Mono.just(Boolean.FALSE);
  115.                         });
  116.                 }
  117.                 return Mono.just(Boolean.FALSE);
  118.  
  119.             });
  120.     }
  121.  
  122.     private Mono<ProvisioningPojo> processMembersFromNamesRolesResponse(List<Member> namesRoleMembers, ProvisioningPojo provisioningPojo) {
  123.         LOGGER.debug("provisioning additional Teacher users from names and roles API ");
  124.  
  125.         Optional<Member> instructor = namesRoleMembers.stream()
  126.                 .filter(member -> RumbaRole.T.name()
  127.                         .equalsIgnoreCase(classProvisoningHelper.obtainRumbaRoleFromLTIRoles(member.getRoles())))
  128.                 .findFirst();
  129.  
  130.         /****  If student user then check if staff present in cms class if not then add the staff to Staff Roster     ****/
  131.         if(provisioningPojo.getRole().equals(RumbaRole.S.name()) && instructor.isPresent()) {
  132.             if (provisioningPojo.updateStaffIfStudent) {
  133.                 // call Roster service to update the Staff
  134.             }
  135.            
  136.             MemberData memberData = constructMemberData(instructor.get(), provisioningPojo);
  137.             return provisioningService.enableUserProvisioning(memberData)
  138.                     .map(rumbaTeacherId -> {
  139.                         RosterMember rosterMember = new RosterMember();
  140.                         rosterMember.setUserId(instructor.get().getUserId());
  141.                         rosterMember.setRole(memberData.getRole());
  142.                         rosterMember.setExternalUserId(memberData.getExternalUserId());
  143.                         rosterMember.setToolUserId(rumbaTeacherId);
  144.                         provisioningPojo.getMembers().add(rosterMember);
  145.                         LOGGER.debug("provisioningPojo : {}", provisioningPojo);
  146.                         return provisioningPojo;
  147.                     });
  148.         }
  149.         return Mono.just(provisioningPojo);
  150.     }