private void assignTeamMemberjButton2ActionPerformed(
      java.awt.event.ActionEvent evt) { // GEN-FIRST:event_assignTeamMemberjButton2ActionPerformed

    int selectedRequest2 = workRequestJTable.getSelectedRow();
    int selectedCareTeamMember = careTeamMemberjTable2.getSelectedRow();
    if (selectedRequest2 < 0 || selectedCareTeamMember < 0) {
      JOptionPane.showMessageDialog(
          null, "Please select row from both Request and team Member table ");
      return;
    }

    WorkRequest request = (WorkRequest) workRequestJTable.getValueAt(selectedRequest2, 0);
    CareTeamMember ctm =
        (CareTeamMember) careTeamMemberjTable2.getValueAt(selectedCareTeamMember, 0);

    SurgeryOperationWorkRequest surRequest = (SurgeryOperationWorkRequest) request;

    if (surRequest.getRequiredCareTeamSize() == 0) {
      JOptionPane.showMessageDialog(null, "Team members already allotted");
      return;
    }

    if (ctm.getAvailabilityStatus() == null) {
      JOptionPane.showMessageDialog(null, "Member not have not updated profile for scheduling");
      return;
    }

    if (ctm.getAvailabilityStatus()
        .equalsIgnoreCase(CareTeamMember.availabilityStatusType.AVAILABLE.getValue())) {
      int assgignedCount = surRequest.getAssignedTeamSize();
      int requestedCount = surRequest.getRequiredCareTeamSize();

      assgignedCount = assgignedCount + 1;
      requestedCount = requestedCount - 1;

      surRequest.setAssignedTeamSize(assgignedCount);
      surRequest.setRequiredCareTeamSize(requestedCount);
      surRequest.getCareTeamDirectory().addCareTeamMemberAsSpecified(ctm);

      ctm.setAvailabilityStatus(CareTeamMember.availabilityStatusType.SCHEDULED.getValue());
      ctm.setNextScheduledActivityDate(surRequest.getOperationDate());

      JOptionPane.showMessageDialog(null, "Team Member Added for operation");
    } else {
      JOptionPane.showMessageDialog(null, "Member not available for scheduling");
      return;
    }

    populateRequestTable();
    populateCareTeamTable();
  } // GEN-LAST:event_assignTeamMemberjButton2ActionPerformed
  public void populateCareTeamTable() {

    DefaultTableModel dtm = (DefaultTableModel) careTeamMemberjTable2.getModel();
    dtm.setRowCount(0);

    for (Organization org : enterprise.getOrganizationDirectory().getOrganizationList()) {
      if (org instanceof CareTeamOrganization) {
        CareTeamOrganization ctOrg = (CareTeamOrganization) org;
        for (CareTeamMember ctm : ctOrg.getCareTeamDirectory().getCareTeamMemberList()) {
          if (ctm.getRole() instanceof CareTeamMemberRole) {
            Object[] row = new Object[5];
            row[0] = ctm;
            row[1] = ctm.getSpeciality();
            row[2] = ctm.getAvailabilityStatus();
            row[3] = ctm.getCurrentVacantStatus();
            row[4] = ctm.getNextScheduledActivityDate();
            dtm.addRow(row);
          }
        }
      }
    }
  }