~fabiocbalbuquerque/sahana-agasti/web-services

« back to all changes in this revision

Viewing changes to apps/frontend/lib/util/agImportNormalization.class.php

  • Committer: Shirley Chan
  • Date: 2011-03-31 05:29:38 UTC
  • mto: (1.26.1 push-trunk) (7.1.1 mayon)
  • mto: This revision was merged to the branch mainline in revision 7.
  • Revision ID: shirley.chan@mail.cuny.edu-20110331052938-j5xgisus5tq6mbj4
Applying transaction/savepoint best practices to facility import

Show diffs side-by-side

added added

removed removed

Lines of Context:
469
469
          $scenarioFacilityResource = $this->createScenarioFacilityResource($facilityResource,
470
470
                                                                            $scenarioFacilityGroup,
471
471
                                                                            $facility_resource_allocation_status_id,
472
 
                                                                            $facility_activation_sequence);
 
472
                                                                            $facility_activation_sequence,
 
473
                                                                            $conn);
473
474
        } else {
474
475
          $scenarioFacilityResource = $this->updateScenarioFacilityResource($scenarioFacilityResource,
475
476
                                                                            $scenarioFacilityGroup->id,
476
477
                                                                            $facility_resource_allocation_status_id,
477
 
                                                                            $facility_activation_sequence);
 
478
                                                                            $facility_activation_sequence,
 
479
                                                                            $conn);
478
480
        }
479
481
 
480
482
        // email
481
483
        if ($validEmail) {
482
 
          $this->updateFacilityEmail($facility, $email, $workEmailTypeId);
 
484
          $this->updateFacilityEmail($facility, $email, $workEmailTypeId, $conn);
483
485
        }
484
486
 
485
487
        // phone
486
488
        if ($validPhone) {
487
 
          $this->updateFacilityPhone($facility, $phone, $workPhoneTypeId, $workPhoneFormatId);
 
489
          $this->updateFacilityPhone($facility, $phone, $workPhoneTypeId, $workPhoneFormatId, $conn);
488
490
        }
489
491
 
490
492
        // address
498
500
                                 $geoInfo, $this->geoTypeId, $this->geoSourceId, $this->geoMatchScoreId);
499
501
 
500
502
        //facility staff resource
501
 
        $this->updateFacilityStaffResources($scenarioFacilityResource->getId(), $staffing);
 
503
        $this->updateFacilityStaffResources($scenarioFacilityResource->getId(), $staffing, $conn);
502
504
 
503
505
        // Set summary counts
504
506
        if ($isNewFacilityRecord) {
515
517
        }
516
518
 
517
519
//        $this->conn->commit();
518
 
        $conn->commit();
 
520
        if ($useSavepoint) { $conn->commit(__FUNCTION__); } else { $conn->commit(); }
519
521
      } catch (Exception $e) {
520
522
        $this->errMsg .= '  Unable to normalize data.  Exception error message: ' . $e->getMessage();
521
523
        $this->nonprocessedRecords[] = array('message' => $this->errMsg,
522
524
                                             'record' => $record);
523
525
//        $this->conn->rollBack();
524
526
        sfContext::getInstance()->getLogger()->err($errMsg);
 
527
 
525
528
        // if we started with a savepoint, let's end with one, otherwise, rollback globally
526
529
        if ($useSavepoint) { $conn->rollback(__FUNCTION__); } else { $conn->rollback(); }
527
530
 
551
554
//
552
555
  /* Facility */
553
556
 
554
 
  protected function createFacility($facilityName, $facilityCode, $conn)
 
557
  protected function createFacility($facilityName, $facilityCode, $conn = NULL)
555
558
  {
 
559
    // here you can pick up the default connection if not passed one explicitly
 
560
    if (is_null($conn)) { $conn = Doctrine_Manager::connection(); }
 
561
 
 
562
    // here we check our current transaction scope and create a transaction or savepoint based on need
 
563
    $useSavepoint = ($conn->getTransactionLevel() > 0) ? TRUE : FALSE;
 
564
    if ($useSavepoint)
 
565
    {
 
566
      $conn->beginTransaction(__FUNCTION__);
 
567
    }
 
568
    else
 
569
    {
 
570
      $conn->beginTransaction();
 
571
    }
 
572
 
556
573
    try {
557
574
      $entity = new agEntity();
558
575
      $entity->save($conn);
564
581
          ->set('facility_name', $facilityName)
565
582
          ->set('facility_code', $facilityCode);
566
583
      $facility->save($conn);
 
584
 
 
585
      if ($useSavepoint) { $conn->commit(__FUNCTION__); } else { $conn->commit(); }
567
586
    } catch(Exception $e) {
568
587
      // ALWAYS log rollbacks with as much useful information as possible
569
 
      $this->errMsg = sprintf('Couldn\'t insert facility %s! Rolled back changes!', $facilityCode);
 
588
      $this->errMsg = sprintf('Couldn\'t insert facility  with facility code %s! Rolled back changes!', $facilityCode);
 
589
 
 
590
      // if we started with a savepoint, let's end with one, otherwise, rollback globally
 
591
      if ($useSavepoint) { $conn->rollback(__FUNCTION__); } else { $conn->rollback(); }
 
592
 
570
593
      throw $e; // always remember to throw an exception after rollback
571
594
    }
572
595
 
573
596
    return $facility;
574
597
  }
575
598
 
576
 
  protected function updateFacility($facility, $facilityName, $conn)
 
599
  protected function updateFacility($facility, $facilityName, $conn = NULL)
577
600
  {
 
601
    // here you can pick up the default connection if not passed one explicitly
 
602
    if (is_null($conn)) { $conn = Doctrine_Manager::connection(); }
 
603
 
 
604
    // here we check our current transaction scope and create a transaction or savepoint based on need
 
605
    $useSavepoint = ($conn->getTransactionLevel() > 0) ? TRUE : FALSE;
 
606
    if ($useSavepoint)
 
607
    {
 
608
      $conn->beginTransaction(__FUNCTION__);
 
609
    }
 
610
    else
 
611
    {
 
612
      $conn->beginTransaction();
 
613
    }
 
614
 
578
615
    try {
579
616
      if ($facility->facility_name != $facilityName)
580
617
      {
584
621
               ->where('f.id = ?', $facility->id)
585
622
               ->execute();
586
623
      }
 
624
 
 
625
      if ($useSavepoint) { $conn->commit(__FUNCTION__); } else { $conn->commit(); }
587
626
    } catch(Exception $e) {
588
627
      // ALWAYS log rollbacks with as much useful information as possible
589
 
      $this->errMsg = sprintf('Couldn\'t update facility %s! Rolled back changes!', $facility->facility_code);
 
628
      $this->errMsg = sprintf('Couldn\'t update facility with facility code %s! Rolled back changes!', $facility->facility_code);
 
629
 
 
630
      // if we started with a savepoint, let's end with one, otherwise, rollback globally
 
631
      if ($useSavepoint) { $conn->rollback(__FUNCTION__); } else { $conn->rollback(); }
 
632
 
 
633
 
590
634
      throw $e; // always remember to throw an exception after rollback
591
635
    }
592
 
 
593
636
    return $facility;
594
637
  }
595
638
 
596
639
  /* Facility Resource */
597
640
 
598
641
  protected function createFacilityResource($facility, $facilityResourceTypeAbbrId,
599
 
                                            $facilityResourceStatusId, $capacity, $conn)
 
642
                                            $facilityResourceStatusId, $capacity, $conn = NULL)
600
643
  {
 
644
    // here you can pick up the default connection if not passed one explicitly
 
645
    if (is_null($conn)) { $conn = Doctrine_Manager::connection(); }
 
646
 
 
647
    // here we check our current transaction scope and create a transaction or savepoint based on need
 
648
    $useSavepoint = ($conn->getTransactionLevel() > 0) ? TRUE : FALSE;
 
649
    if ($useSavepoint)
 
650
    {
 
651
      $conn->beginTransaction(__FUNCTION__);
 
652
    }
 
653
    else
 
654
    {
 
655
      $conn->beginTransaction();
 
656
    }
 
657
 
601
658
    try {
602
659
      $facilityResource = new agFacilityResource();
603
660
      $facilityResource->set('facility_id', $facility->id)
605
662
          ->set('facility_resource_status_id', $facilityResourceStatusId)
606
663
          ->set('capacity', $capacity);
607
664
      $facilityResource->save($conn);
 
665
 
 
666
      if ($useSavepoint) { $conn->commit(__FUNCTION__); } else { $conn->commit(); }
608
667
    } catch(Exception $e) {
609
668
      // ALWAYS log rollbacks with as much useful information as possible
610
669
      $this->errMsg = sprintf('Couldn\'t insert facility resource (%s, %s)! Rolled back changes!',
611
670
                              $facility->facility_code,
612
671
                              array_search($facilityResourceTypeAbbrId, $this->facilityResourceTypes));
 
672
 
 
673
      // if we started with a savepoint, let's end with one, otherwise, rollback globally
 
674
      if ($useSavepoint) { $conn->rollback(__FUNCTION__); } else { $conn->rollback(); }
 
675
 
613
676
     throw $e; // always remember to throw an exception after rollback
614
677
    }
615
678
 
617
680
  }
618
681
 
619
682
  protected function updateFacilityResource($facilityResource, $facilityResourceStatusId,
620
 
                                            $capacity, $conn)
 
683
                                            $capacity, $conn = NULL)
621
684
  {
 
685
    // here you can pick up the default connection if not passed one explicitly
 
686
    if (is_null($conn)) { $conn = Doctrine_Manager::connection(); }
 
687
 
 
688
    // here we check our current transaction scope and create a transaction or savepoint based on need
 
689
    $useSavepoint = ($conn->getTransactionLevel() > 0) ? TRUE : FALSE;
 
690
    if ($useSavepoint)
 
691
    {
 
692
      $conn->beginTransaction(__FUNCTION__);
 
693
    }
 
694
    else
 
695
    {
 
696
      $conn->beginTransaction();
 
697
    }
 
698
 
622
699
    try {
623
700
      $doUpdate = FALSE;
624
 
      $updateQuery = agDoctrineQuery::create()
 
701
      $updateQuery = agDoctrineQuery::create($conn)
625
702
                      ->update('agFacilityResource fr')
626
703
                      ->where('id = ?', $facilityResource->id);
627
704
 
637
714
 
638
715
      if ($doUpdate) {
639
716
        $updateQuery = $updateQuery->execute();
 
717
        if ($useSavepoint) { $conn->commit(__FUNCTION__); } else { $conn->commit(); }
640
718
      } else {
641
719
        $updateQuery = NULL;
642
720
      }
643
721
    } catch(Exception $e) {
644
722
      // ALWAYS log rollbacks with as much useful information as possible
645
723
      $this->errMsg = sprintf('Couldn\'t update facility resource (%s, %s)! Rolled back changes!',
646
 
                              $facility->facility_code,
 
724
                              $facilityResource->getAgFacility()->facility_code,
647
725
                              array_search($facilityResource->facility_resource_type_id, $this->facilityResourceTypes));
 
726
 
 
727
      // if we started with a savepoint, let's end with one, otherwise, rollback globally
 
728
      if ($useSavepoint) { $conn->rollback(__FUNCTION__); } else { $conn->rollback(); }
 
729
 
648
730
      throw $e; // always remember to throw an exception after rollback
649
731
    }
650
732
 
651
733
    return $facilityResource;
652
734
  }
653
735
 
654
 
  protected function updateFacilityStaffResources($scenarioFacilityResourceId, $staff_data)
 
736
  protected function updateFacilityStaffResources($scenarioFacilityResourceId, $staff_data, $conn = NULL)
655
737
  {
656
738
    $facilityStaffResource = agDoctrineQuery::create()
657
739
                    ->select('srt.id, fsr.minimum_staff, fsr.maximum_staff, fsr.id')
663
745
    $deleteFacStfResId = array();
664
746
    foreach ($staff_data as $staffTypeId => $count) {
665
747
      if (array_key_exists($staffTypeId, $facilityStaffResource)) {
666
 
        $doUpdate = FALSE;
667
 
        $updateQuery = agDoctrineQuery::create()
668
 
                        ->update('agFacilityStaffResource')
669
 
                        ->where('id = ?', $facilityStaffResource[$staffTypeId][2]);
670
 
 
671
 
        if ($count['min'] == 0 && $count['max'] == 0) {
672
 
          $deleteFacStfResId[] = $facilityStaffResource[$staffTypeId][2];
673
 
          continue;
674
 
        }
675
 
 
676
 
        if ($facilityStaffResource[$staffTypeId][0] != $count['min']) {
677
 
          $updateQuery->set('minimum_staff', $count['min']);
678
 
          $doUpdate = TRUE;
679
 
        }
680
 
 
681
 
        if ($facilityStaffResource[$staffTypeId][1] != $count['max']) {
682
 
          $updateQuery->set('maximum_staff', $count['max']);
683
 
          $doUpdate = TRUE;
684
 
        }
685
 
 
686
 
        if ($doUpdate) {
687
 
          $updateQuery->execute();
688
 
        } else {
689
 
          $updateQuery = NULL;
 
748
        // here you can pick up the default connection if not passed one explicitly
 
749
        if (is_null($conn)) { $conn = Doctrine_Manager::connection(); }
 
750
 
 
751
        // here we check our current transaction scope and create a transaction or savepoint based on need
 
752
        $useSavepoint = ($conn->getTransactionLevel() > 0) ? TRUE : FALSE;
 
753
        if ($useSavepoint)
 
754
        {
 
755
          $conn->beginTransaction(__FUNCTION__);
 
756
        }
 
757
        else
 
758
        {
 
759
          $conn->beginTransaction();
 
760
        }
 
761
 
 
762
        try {
 
763
          $doUpdate = FALSE;
 
764
          $updateQuery = agDoctrineQuery::create($conn)
 
765
                          ->update('agFacilityStaffResource')
 
766
                          ->where('id = ?', $facilityStaffResource[$staffTypeId][2]);
 
767
 
 
768
          if ($count['min'] == 0 && $count['max'] == 0) {
 
769
            $deleteFacStfResId[] = $facilityStaffResource[$staffTypeId][2];
 
770
            continue;
 
771
          }
 
772
 
 
773
          if ($facilityStaffResource[$staffTypeId][0] != $count['min']) {
 
774
            $updateQuery->set('minimum_staff', $count['min']);
 
775
            $doUpdate = TRUE;
 
776
          }
 
777
 
 
778
          if ($facilityStaffResource[$staffTypeId][1] != $count['max']) {
 
779
            $updateQuery->set('maximum_staff', $count['max']);
 
780
            $doUpdate = TRUE;
 
781
          }
 
782
 
 
783
          if ($doUpdate) {
 
784
            $updateQuery->execute();
 
785
            if ($useSavepoint) { $conn->commit(__FUNCTION__); } else { $conn->commit(); }
 
786
          } else {
 
787
            $updateQuery = NULL;
 
788
          }
 
789
        } catch(Exception $e) {
 
790
          // ALWAYS log rollbacks with as much useful information as possible
 
791
          $this->errMsg = sprintf('Couldn\'t update facility staff resource! Rolled back changes!');
 
792
 
 
793
          // if we started with a savepoint, let's end with one, otherwise, rollback globally
 
794
          if ($useSavepoint) { $conn->rollback(__FUNCTION__); } else { $conn->rollback(); }
 
795
 
 
796
          throw $e; // always remember to throw an exception after rollback
690
797
        }
691
798
      } else {
692
799
        if ($count['min'] != 0 && $count['max'] != 0) {
693
 
          $this->createFacilityStaffResource($scenarioFacilityResourceId, $staffTypeId, $count['min'], $count['max']);
 
800
          $this->createFacilityStaffResource($scenarioFacilityResourceId, $staffTypeId, $count['min'], $count['max'], $conn);
694
801
        }
695
802
      }
696
803
    }
697
804
 
698
805
    if (!empty($deleteFacStfResId)) {
699
 
      $deleteQuery = agDoctrineQuery::create()
700
 
                      ->delete('agFacilityStaffResource')
701
 
                      ->whereIn('id', $deleteFacStfResId)
702
 
                      ->execute();
 
806
      // here you can pick up the default connection if not passed one explicitly
 
807
      if (is_null($conn)) { $conn = Doctrine_Manager::connection(); }
 
808
 
 
809
      // here we check our current transaction scope and create a transaction or savepoint based on need
 
810
      $useSavepoint = ($conn->getTransactionLevel() > 0) ? TRUE : FALSE;
 
811
      if ($useSavepoint)
 
812
      {
 
813
        $conn->beginTransaction(__FUNCTION__);
 
814
      }
 
815
      else
 
816
      {
 
817
        $conn->beginTransaction();
 
818
      }
 
819
 
 
820
      try {
 
821
        $deleteQuery = agDoctrineQuery::create($conn)
 
822
                        ->delete('agFacilityStaffResource')
 
823
                        ->whereIn('id', $deleteFacStfResId)
 
824
                        ->execute();
 
825
        if ($useSavepoint) { $conn->commit(__FUNCTION__); } else { $conn->commit(); }
 
826
      } catch(Exception $e) {
 
827
        // ALWAYS log rollbacks with as much useful information as possible
 
828
        $this->errMsg = sprintf('Couldn\'t update facility staff resource! Rolled back changes!');
 
829
 
 
830
        // if we started with a savepoint, let's end with one, otherwise, rollback globally
 
831
        if ($useSavepoint) { $conn->rollback(__FUNCTION__); } else { $conn->rollback(); }
 
832
 
 
833
        throw $e; // always remember to throw an exception after rollback
 
834
      }
703
835
    }
704
836
 
705
837
    return $facilityStaffResource;
706
838
  }
707
839
 
708
 
  protected function createFacilityStaffResource($scenarioFacilityResourceId, $staffTypeId, $min_staff,
709
 
                                                 $max_staff)
 
840
  protected function createFacilityStaffResource($scenarioFacilityResourceId,
 
841
                                                 $staffTypeId, $min_staff,
 
842
                                                 $max_staff, $conn = NULL)
710
843
  {
711
 
    $facilityStaffResource = new agFacilityStaffResource();
712
 
    $facilityStaffResource->set('staff_resource_type_id', $staffTypeId)
713
 
        ->set('scenario_facility_resource_id', $scenarioFacilityResourceId)
714
 
        ->set('minimum_staff', $min_staff)
715
 
        ->set('maximum_staff', $max_staff);
716
 
    $facilityStaffResource->save();
 
844
    // here you can pick up the default connection if not passed one explicitly
 
845
    if (is_null($conn)) { $conn = Doctrine_Manager::connection(); }
 
846
 
 
847
    // here we check our current transaction scope and create a transaction or savepoint based on need
 
848
    $useSavepoint = ($conn->getTransactionLevel() > 0) ? TRUE : FALSE;
 
849
    if ($useSavepoint)
 
850
    {
 
851
      $conn->beginTransaction(__FUNCTION__);
 
852
    }
 
853
    else
 
854
    {
 
855
      $conn->beginTransaction();
 
856
    }
 
857
 
 
858
    try {
 
859
      $facilityStaffResource = new agFacilityStaffResource();
 
860
      $facilityStaffResource->set('staff_resource_type_id', $staffTypeId)
 
861
          ->set('scenario_facility_resource_id', $scenarioFacilityResourceId)
 
862
          ->set('minimum_staff', $min_staff)
 
863
          ->set('maximum_staff', $max_staff);
 
864
      $facilityStaffResource->save($conn);
 
865
      if ($useSavepoint) { $conn->commit(__FUNCTION__); } else { $conn->commit(); }
 
866
    } catch(Exception $e) {
 
867
      $scenarioFacilityResource = Doctrine_Core::getTable('agScenarioFacilityResource')->find($scenarioFacilityResourceId);
 
868
      // ALWAYS log rollbacks with as much useful information as possible
 
869
      $this->errMsg = sprintf('Couldn\'t create facility staff resource (%s, %s)! Rolled back changes!',
 
870
                              $scenarioFacilityResource->getAgFacilityResource()->getAgFacility()->facility_name,
 
871
                              $scenarioFacilityResource->getAgStaffResourceType()->staff_resource_type);
 
872
 
 
873
      // if we started with a savepoint, let's end with one, otherwise, rollback globally
 
874
      if ($useSavepoint) { $conn->rollback(__FUNCTION__); } else { $conn->rollback(); }
 
875
 
 
876
      throw $e; // always remember to throw an exception after rollback
 
877
    }
717
878
    return $facilityStaffResource;
718
879
  }
719
880
 
722
883
  protected function createScenarioFacilityGroup($facilityGroup, $facilityGroupTypeId,
723
884
                                                 $facilityGroupAllocationStatusId,
724
885
                                                 $facilityGroupActivationSequence, 
725
 
                                                 $conn)
 
886
                                                 $conn = NULL)
726
887
  {
 
888
    // here you can pick up the default connection if not passed one explicitly
 
889
    if (is_null($conn)) { $conn = Doctrine_Manager::connection(); }
 
890
 
 
891
    // here we check our current transaction scope and create a transaction or savepoint based on need
 
892
    $useSavepoint = ($conn->getTransactionLevel() > 0) ? TRUE : FALSE;
 
893
    if ($useSavepoint)
 
894
    {
 
895
      $conn->beginTransaction(__FUNCTION__);
 
896
    }
 
897
    else
 
898
    {
 
899
      $conn->beginTransaction();
 
900
    }
 
901
 
727
902
    try {
728
903
      $scenarioFacilityGroup = new agScenarioFacilityGroup();
729
904
      $scenarioFacilityGroup->set('scenario_id', $this->scenarioId)
731
906
          ->set('facility_group_type_id', $facilityGroupTypeId)
732
907
          ->set('facility_group_allocation_status_id', $facilityGroupAllocationStatusId)
733
908
          ->set('activation_sequence', $facilityGroupActivationSequence);
734
 
      $scenarioFacilityGroup->save();
 
909
      $scenarioFacilityGroup->save($conn);
 
910
      if ($useSavepoint) { $conn->commit(__FUNCTION__); } else { $conn->commit(); }
735
911
    } catch(Exception $e) {
736
912
      // ALWAYS log rollbacks with as much useful information as possible
737
913
      $this->errMsg = sprintf('Couldn\'t insert facility group %s! Rolled back changes!',
738
914
                              $facilityGroup);
 
915
 
 
916
      // if we started with a savepoint, let's end with one, otherwise, rollback globally
 
917
      if ($useSavepoint) { $conn->rollback(__FUNCTION__); } else { $conn->rollback(); }
 
918
 
739
919
      throw $e; // always remember to throw an exception after rollback
740
920
    }
741
921
 
744
924
 
745
925
  protected function updateScenarioFacilityGroup($scenarioFacilityGroup, $facilityGroupTypeId,
746
926
                                                 $facilityGroupAllocationStatusId,
747
 
                                                 $facilityGroupActivationSequence, $conn)
 
927
                                                 $facilityGroupActivationSequence, $conn = NULL)
748
928
  {
 
929
    // here you can pick up the default connection if not passed one explicitly
 
930
    if (is_null($conn)) { $conn = Doctrine_Manager::connection(); }
 
931
 
 
932
    // here we check our current transaction scope and create a transaction or savepoint based on need
 
933
    $useSavepoint = ($conn->getTransactionLevel() > 0) ? TRUE : FALSE;
 
934
    if ($useSavepoint)
 
935
    {
 
936
      $conn->beginTransaction(__FUNCTION__);
 
937
    }
 
938
    else
 
939
    {
 
940
      $conn->beginTransaction();
 
941
    }
 
942
 
749
943
    try {
750
944
      $doUpdate = FALSE;
751
 
      $updateQuery = agDoctrineQuery::create()
 
945
      $updateQuery = agDoctrineQuery::create($conn)
752
946
                      ->update('agScenarioFacilityGroup sfg')
753
947
                      ->where('id = ?', $scenarioFacilityGroup->id);
754
948
 
755
 
      if (strtolower($scenarioFacilityGroup->facility_group_type_id) != strtolower($facilityGroupTypeId)) {
 
949
//      if (strtolower($scenarioFacilityGroup->facility_group_type_id) != strtolower($facilityGroupTypeId)) {
756
950
        $updateQuery->set('facility_group_type_id', '?', $facilityGroupTypeId);
757
951
        $doUpdate = TRUE;
758
 
      }
 
952
//      }
759
953
 
760
954
      if ($scenarioFacilityGroup->facility_group_allocation_status_id != $facilityGroupAllocationStatusId) {
761
955
        $updateQuery->set('facility_group_allocation_status_id', '?', $facilityGroupAllocationStatusId);
769
963
 
770
964
      if ($doUpdate) {
771
965
        $updateQuery = $updateQuery->execute();
 
966
        if ($useSavepoint) { $conn->commit(__FUNCTION__); } else { $conn->commit(); }
772
967
      } else {
773
968
        $updateQuery = NULL;
774
969
      }
776
971
      // ALWAYS log rollbacks with as much useful information as possible
777
972
      $this->errMsg = sprintf('Couldn\'t update facility group %s! Rolled back changes!',
778
973
                              $scenarioFacilityGroup->scenario_facility_group);
 
974
 
 
975
      // if we started with a savepoint, let's end with one, otherwise, rollback globally
 
976
      if ($useSavepoint) { $conn->rollback(__FUNCTION__); } else { $conn->rollback(); }
 
977
 
779
978
      throw $e; // always remember to throw an exception after rollback
780
979
    }
781
980
 
786
985
 
787
986
  protected function createScenarioFacilityResource($facilityResource, $scenarioFacilityGroup,
788
987
                                                    $facilityResourceAllocationStatusId,
789
 
                                                    $facilityActivationSequence)
 
988
                                                    $facilityActivationSequence, $conn = NULL)
790
989
  {
791
 
    $scenarioFacilityResource = new agScenarioFacilityResource();
792
 
    $scenarioFacilityResource->set('facility_resource_id', $facilityResource->id)
793
 
            ->set('scenario_facility_group_id', $scenarioFacilityGroup->id)
794
 
            ->set('facility_resource_allocation_status_id', $facilityResourceAllocationStatusId)
795
 
            ->set('activation_sequence', $facilityActivationSequence);
796
 
    $scenarioFacilityResource->save();
 
990
    // here you can pick up the default connection if not passed one explicitly
 
991
    if (is_null($conn)) { $conn = Doctrine_Manager::connection(); }
 
992
 
 
993
    // here we check our current transaction scope and create a transaction or savepoint based on need
 
994
    $useSavepoint = ($conn->getTransactionLevel() > 0) ? TRUE : FALSE;
 
995
    if ($useSavepoint)
 
996
    {
 
997
      $conn->beginTransaction(__FUNCTION__);
 
998
    }
 
999
    else
 
1000
    {
 
1001
      $conn->beginTransaction();
 
1002
    }
 
1003
 
 
1004
    try {
 
1005
      $scenarioFacilityResource = new agScenarioFacilityResource();
 
1006
      $scenarioFacilityResource->set('facility_resource_id', $facilityResource->id)
 
1007
              ->set('scenario_facility_group_id', $scenarioFacilityGroup->id)
 
1008
              ->set('facility_resource_allocation_status_id', $facilityResourceAllocationStatusId)
 
1009
              ->set('activation_sequence', $facilityActivationSequence);
 
1010
      $scenarioFacilityResource->save($conn);
 
1011
      if ($useSavepoint) { $conn->commit(__FUNCTION__); } else { $conn->commit(); }
 
1012
    } catch(Exception $e) {
 
1013
      // ALWAYS log rollbacks with as much useful information as possible
 
1014
      $this->errMsg = sprintf('Couldn\'t insert scenario facility resource (%s, %s, %s)! Rolled back changes!',
 
1015
                              $facilityResource->getAgFacility()->facility_name,
 
1016
                              $facilityResource->getAgFacilityResourceType()->facility_resource_type_abbr,
 
1017
                              $scenarioFacilityGroup->scenario_facility_group);
 
1018
 
 
1019
      // if we started with a savepoint, let's end with one, otherwise, rollback globally
 
1020
      if ($useSavepoint) { $conn->rollback(__FUNCTION__); } else { $conn->rollback(); }
 
1021
 
 
1022
      throw $e; // always remember to throw an exception after rollback
 
1023
    }
 
1024
 
797
1025
    return $scenarioFacilityResource;
798
1026
  }
799
1027
 
800
 
  protected function updateScenarioFacilityResource($scenarioFacilityResource, $scenarioFacilityGroupId, $facilityResourceAllocationStatusId, $facilityActivationSequence)
 
1028
  protected function updateScenarioFacilityResource($scenarioFacilityResource, 
 
1029
                                                    $scenarioFacilityGroupId,
 
1030
                                                    $facilityResourceAllocationStatusId,
 
1031
                                                    $facilityActivationSequence, $conn = NULL)
801
1032
  {
802
 
    $doUpdate = FALSE;
803
 
    $updateQuery = agDoctrineQuery::create()
804
 
                    ->update('agScenarioFacilityResource')
805
 
                    ->where('id = ?', $scenarioFacilityResource->id);
806
 
 
807
 
    if ($scenarioFacilityResource->scenario_facility_group_id != $scenarioFacilityGroupId) {
808
 
      $updateQuery->set('scenario_facility_group_id', $scenarioFacilityGroupId);
809
 
      $doUpdate = TRUE;
810
 
    }
811
 
 
812
 
    if ($scenarioFacilityResource->facility_resource_allocation_status_id != $scenarioFacilityResourceAllocationStatusId) {
813
 
      $updateQuery->set('facility_resource_allocation_status_id', $facilityResourceAllocationStatusId);
814
 
      $doUpdate = TRUE;
815
 
    }
816
 
 
817
 
    if ($scenarioFacilityResource->activation_sequence != $facilityActivationSequence) {
818
 
      $updateQuery->set('activation_sequence', $facilityActivationSequence);
819
 
      $doUpdate = TRUE;
820
 
    }
821
 
 
822
 
    if ($doUpdate) {
823
 
      $updateQuery = $updateQuery->execute();
824
 
    } else {
825
 
      $updateQuery = NULL;
 
1033
    // here you can pick up the default connection if not passed one explicitly
 
1034
    if (is_null($conn)) { $conn = Doctrine_Manager::connection(); }
 
1035
 
 
1036
    // here we check our current transaction scope and create a transaction or savepoint based on need
 
1037
    $useSavepoint = ($conn->getTransactionLevel() > 0) ? TRUE : FALSE;
 
1038
    if ($useSavepoint)
 
1039
    {
 
1040
      $conn->beginTransaction(__FUNCTION__);
 
1041
    }
 
1042
    else
 
1043
    {
 
1044
      $conn->beginTransaction();
 
1045
    }
 
1046
 
 
1047
    try {
 
1048
      $doUpdate = FALSE;
 
1049
      $updateQuery = agDoctrineQuery::create($conn)
 
1050
                      ->update('agScenarioFacilityResource')
 
1051
                      ->where('id = ?', $scenarioFacilityResource->id);
 
1052
 
 
1053
      if ($scenarioFacilityResource->scenario_facility_group_id != $scenarioFacilityGroupId) {
 
1054
        $updateQuery->set('scenario_facility_group_id', $scenarioFacilityGroupId);
 
1055
        $doUpdate = TRUE;
 
1056
      }
 
1057
 
 
1058
      if ($scenarioFacilityResource->facility_resource_allocation_status_id != $scenarioFacilityResourceAllocationStatusId) {
 
1059
        $updateQuery->set('facility_resource_allocation_status_id', $facilityResourceAllocationStatusId);
 
1060
        $doUpdate = TRUE;
 
1061
      }
 
1062
 
 
1063
      if ($scenarioFacilityResource->activation_sequence != $facilityActivationSequence) {
 
1064
        $updateQuery->set('activation_sequence', $facilityActivationSequence);
 
1065
        $doUpdate = TRUE;
 
1066
      }
 
1067
 
 
1068
      if ($doUpdate) {
 
1069
        $updateQuery = $updateQuery->execute();
 
1070
        if ($useSavepoint) { $conn->commit(__FUNCTION__); } else { $conn->commit(); }
 
1071
      } else {
 
1072
        $updateQuery = NULL;
 
1073
      }
 
1074
    } catch(Exception $e) {
 
1075
      // ALWAYS log rollbacks with as much useful information as possible
 
1076
      $this->errMsg = sprintf('Couldn\'t update scenario facility resource (%s, %s, %s)! Rolled back changes!',
 
1077
                              $scenarioFacilityResource->getAgFacilityResource()->getAgFacility()->facility_name,
 
1078
                              $scenarioFacilityResource->getAgFacilityResource()->getAgFacilityResourceType()->facility_resource_type_abbr,
 
1079
                              Doctrine_Core::getTable('agScenarioFacilityGroup')->find($scenarioFacilityGroupId)->scenario_facility_group);
 
1080
 
 
1081
      // if we started with a savepoint, let's end with one, otherwise, rollback globally
 
1082
      if ($useSavepoint) { $conn->rollback(__FUNCTION__); } else { $conn->rollback(); }
 
1083
 
 
1084
      throw $e; // always remember to throw an exception after rollback
826
1085
    }
827
1086
 
828
1087
    return $scenarioFacilityResource;
839
1098
    return $emailContact;
840
1099
  }
841
1100
 
842
 
  protected function createEmail($email)
 
1101
  protected function createEmail($email, $conn = NULL)
843
1102
  {
844
 
    $emailContact = new agEmailContact();
845
 
    $emailContact->set('email_contact', $email);
846
 
    $emailContact->save();
 
1103
    // here you can pick up the default connection if not passed one explicitly
 
1104
    if (is_null($conn)) { $conn = Doctrine_Manager::connection(); }
 
1105
 
 
1106
    // here we check our current transaction scope and create a transaction or savepoint based on need
 
1107
    $useSavepoint = ($conn->getTransactionLevel() > 0) ? TRUE : FALSE;
 
1108
    if ($useSavepoint)
 
1109
    {
 
1110
      $conn->beginTransaction(__FUNCTION__);
 
1111
    }
 
1112
    else
 
1113
    {
 
1114
      $conn->beginTransaction();
 
1115
    }
 
1116
 
 
1117
    try {
 
1118
      $emailContact = new agEmailContact();
 
1119
      $emailContact->set('email_contact', $email);
 
1120
      $emailContact->save($conn);
 
1121
      if ($useSavepoint) { $conn->commit(__FUNCTION__); } else { $conn->commit(); }
 
1122
    } catch (Exception $e) {
 
1123
      // ALWAYS log rollbacks with as much useful information as possible
 
1124
      $this->errMsg = sprintf('Couldn\'t create email %s! Rolled back changes!', $email);
 
1125
 
 
1126
      // if we started with a savepoint, let's end with one, otherwise, rollback globally
 
1127
      if ($useSavepoint) { $conn->rollback(__FUNCTION__); } else { $conn->rollback(); }
 
1128
 
 
1129
      throw $e; // always remember to throw an exception after rollback
 
1130
    }
 
1131
 
847
1132
    return $emailContact;
848
1133
  }
849
1134
 
850
 
  protected function createEntityEmail($entityId, $emailId, $typeId)
 
1135
  protected function createEntityEmail($entityId, $emailId, $typeId, $conn = NULl)
851
1136
  {
852
1137
    $priority = $this->getPriorityCounter('email', $entityId);
853
1138
 
854
 
    $entityEmail = new agEntityEmailContact();
855
 
    $entityEmail->set('entity_id', $entityId)
856
 
        ->set('email_contact_id', $emailId)
857
 
        ->set('email_contact_type_id', $typeId)
858
 
        ->set('priority', $priority);
859
 
    $entityEmail->save();
 
1139
    // here you can pick up the default connection if not passed one explicitly
 
1140
    if (is_null($conn)) { $conn = Doctrine_Manager::connection(); }
 
1141
 
 
1142
    // here we check our current transaction scope and create a transaction or savepoint based on need
 
1143
    $useSavepoint = ($conn->getTransactionLevel() > 0) ? TRUE : FALSE;
 
1144
    if ($useSavepoint)
 
1145
    {
 
1146
      $conn->beginTransaction(__FUNCTION__);
 
1147
    }
 
1148
    else
 
1149
    {
 
1150
      $conn->beginTransaction();
 
1151
    }
 
1152
 
 
1153
    try {
 
1154
      $entityEmail = new agEntityEmailContact();
 
1155
      $entityEmail->set('entity_id', $entityId)
 
1156
          ->set('email_contact_id', $emailId)
 
1157
          ->set('email_contact_type_id', $typeId)
 
1158
          ->set('priority', $priority);
 
1159
      $entityEmail->save($conn);
 
1160
      if ($useSavepoint) { $conn->commit(__FUNCTION__); } else { $conn->commit(); }
 
1161
    } catch (Exception $e) {
 
1162
      // ALWAYS log rollbacks with as much useful information as possible
 
1163
      $this->errMsg = sprintf('Couldn\'t create entity email (%s, %s)! Rolled back changes!',
 
1164
                              array_search($typeId, $this->emailContactTypes),
 
1165
                              Doctrine_Core::getTable('agEmailContact')->find($emailId)->email_contact);
 
1166
 
 
1167
      // if we started with a savepoint, let's end with one, otherwise, rollback globally
 
1168
      if ($useSavepoint) { $conn->rollback(__FUNCTION__); } else { $conn->rollback(); }
 
1169
 
 
1170
      throw $e; // always remember to throw an exception after rollback
 
1171
    }
860
1172
 
861
1173
    return $entityEmail;
862
1174
  }
863
1175
 
864
 
  protected function updateEntityEmail($entityEmailObject, $emailObject)
 
1176
  protected function updateEntityEmail($entityEmailObject, $emailObject, $conn = NULL)
865
1177
  {
866
 
    $entityEmailObject->set('email_contact_id', $emailObject->id);
867
 
    $entityEmailObject->save();
 
1178
    // here you can pick up the default connection if not passed one explicitly
 
1179
    if (is_null($conn)) { $conn = Doctrine_Manager::connection(); }
 
1180
 
 
1181
    // here we check our current transaction scope and create a transaction or savepoint based on need
 
1182
    $useSavepoint = ($conn->getTransactionLevel() > 0) ? TRUE : FALSE;
 
1183
    if ($useSavepoint)
 
1184
    {
 
1185
      $conn->beginTransaction(__FUNCTION__);
 
1186
    }
 
1187
    else
 
1188
    {
 
1189
      $conn->beginTransaction();
 
1190
    }
 
1191
 
 
1192
    try {
 
1193
      $entityEmailObject->set('email_contact_id', $emailObject->id);
 
1194
      $entityEmailObject->save($conn);
 
1195
      if ($useSavepoint) { $conn->commit(__FUNCTION__); } else { $conn->commit(); }
 
1196
    } catch (Exception $e) {
 
1197
      // ALWAYS log rollbacks with as much useful information as possible
 
1198
      $this->errMsg = sprintf('Couldn\'t update entity email (%s, %s)! Rolled back changes!',
 
1199
                              $entityEmailObject->getAgEmailContactType()->email_contact_type,
 
1200
                              $emailObject->email_contact);
 
1201
 
 
1202
      // if we started with a savepoint, let's end with one, otherwise, rollback globally
 
1203
      if ($useSavepoint) { $conn->rollback(__FUNCTION__); } else { $conn->rollback(); }
 
1204
 
 
1205
      throw $e; // always remember to throw an exception after rollback
 
1206
    }
 
1207
 
868
1208
    return $entityEmailObject;
869
1209
  }
870
1210
 
875
1215
   * @param <type> $workEmailTypeId
876
1216
   * @return bool true if an update or create happened, false otherwise.
877
1217
   */
878
 
  protected function updateFacilityEmail($facility, $email, $workEmailTypeId)
 
1218
  protected function updateFacilityEmail($facility, $email, $workEmailTypeId, $conn)
879
1219
  {
880
1220
    $entityId = $facility->getAgSite()->entity_id;
881
1221
    $entityEmail = $this->getEntityContactObject('email', $entityId, $workEmailTypeId);
894
1234
    // only useful for nonrequired emails
895
1235
    //  if importedEmail null
896
1236
    if (empty($email) && !empty($facilityEmail)) {
897
 
      $entityEmail->delete();
 
1237
      // here we check our current transaction scope and create a transaction or savepoint based on need
 
1238
      $useSavepoint = ($conn->getTransactionLevel() > 0) ? TRUE : FALSE;
 
1239
      if ($useSavepoint)
 
1240
      {
 
1241
        $conn->beginTransaction(__FUNCTION__);
 
1242
      }
 
1243
      else
 
1244
      {
 
1245
        $conn->beginTransaction();
 
1246
      }
 
1247
      try {
 
1248
        $entityEmail->delete($conn);
 
1249
        if ($useSavepoint) { $conn->commit(__FUNCTION__); } else { $conn->commit(); }
 
1250
        return TRUE;
 
1251
      } catch (Exception $e) {
 
1252
        // ALWAYS log rollbacks with as much useful information as possible
 
1253
        $this->errMsg = sprintf('Couldn\'t remove old entity email (%s, %s)! Rolled back changes!',
 
1254
                                array_search($workEmailTypeId, $this->emailContactTypes),
 
1255
                                $facilityEmail);
 
1256
 
 
1257
        // if we started with a savepoint, let's end with one, otherwise, rollback globally
 
1258
        if ($useSavepoint) { $conn->rollback(__FUNCTION__); } else { $conn->rollback(); }
 
1259
 
 
1260
        throw $e; // always remember to throw an exception after rollback
 
1261
      }
898
1262
      return TRUE;
899
1263
    }
900
1264
 
901
1265
    $emailEntity = $this->getEmailObject($email);
902
1266
 
903
1267
    if (empty($emailEntity)) {
904
 
      $emailEntity = $this->createEmail($email);
 
1268
      $emailEntity = $this->createEmail($email, $conn);
905
1269
    }
906
1270
 
907
1271
    if (empty($facilityEmail)) {
908
 
      $this->createEntityEmail($entityId, $emailEntity->id, $workEmailTypeId);
 
1272
      $this->createEntityEmail($entityId, $emailEntity->id, $workEmailTypeId, $conn);
909
1273
      return TRUE;
910
1274
    }
911
1275
 
912
1276
    // Facility email exists and does not match import email.
913
 
    $this->updateEntityEmail($entityEmail, $emailEntity);
 
1277
    $this->updateEntityEmail($entityEmail, $emailEntity, $conn);
914
1278
    return TRUE;
915
1279
  }
916
1280
 
926
1290
    return $phoneContact;
927
1291
  }
928
1292
 
929
 
  protected function createPhone($phone, $phoneFormatId)
 
1293
  protected function createPhone($phone, $phoneFormatId, $conn = NULL)
930
1294
  {
931
 
    $phoneContact = new agPhoneContact();
932
 
    $phoneContact
933
 
        ->set('phone_contact', $phone)
934
 
        ->set('phone_format_id', $phoneFormatId);
935
 
    $phoneContact->save();
 
1295
    // here you can pick up the default connection if not passed one explicitly
 
1296
    if (is_null($conn)) { $conn = Doctrine_Manager::connection(); }
 
1297
 
 
1298
    // here we check our current transaction scope and create a transaction or savepoint based on need
 
1299
    $useSavepoint = ($conn->getTransactionLevel() > 0) ? TRUE : FALSE;
 
1300
    if ($useSavepoint)
 
1301
    {
 
1302
      $conn->beginTransaction(__FUNCTION__);
 
1303
    }
 
1304
    else
 
1305
    {
 
1306
      $conn->beginTransaction();
 
1307
    }
 
1308
 
 
1309
    try {
 
1310
      $phoneContact = new agPhoneContact();
 
1311
      $phoneContact
 
1312
          ->set('phone_contact', $phone)
 
1313
          ->set('phone_format_id', $phoneFormatId);
 
1314
      $phoneContact->save($conn);
 
1315
      if ($useSavepoint) { $conn->commit(__FUNCTION__); } else { $conn->commit(); }
 
1316
    } catch (Exception $e) {
 
1317
      // ALWAYS log rollbacks with as much useful information as possible
 
1318
      $this->errMsg = sprintf('Couldn\'t insert phone %s! Rolled back changes!', $phone);
 
1319
 
 
1320
      // if we started with a savepoint, let's end with one, otherwise, rollback globally
 
1321
      if ($useSavepoint) { $conn->rollback(__FUNCTION__); } else { $conn->rollback(); }
 
1322
 
 
1323
      throw $e; // always remember to throw an exception after rollback
 
1324
    }
 
1325
 
936
1326
    return $phoneContact;
937
1327
  }
938
1328
 
939
 
  protected function createEntityPhone($entityId, $phoneContactId, $typeId)
 
1329
  protected function createEntityPhone($entityId, $phoneContactId, $typeId, $conn = NULL)
940
1330
  {
941
1331
    $priority = $this->getPriorityCounter('phone', $entityId);
942
1332
 
943
 
    $entityPhone = new agEntityPhoneContact();
944
 
    $entityPhone->set('entity_id', $entityId)
945
 
        ->set('phone_contact_id', $phoneContactId)
946
 
        ->set('phone_contact_type_id', $typeId)
947
 
        ->set('priority', $priority);
948
 
    $entityPhone->save();
 
1333
    // here you can pick up the default connection if not passed one explicitly
 
1334
    if (is_null($conn)) { $conn = Doctrine_Manager::connection(); }
 
1335
 
 
1336
    // here we check our current transaction scope and create a transaction or savepoint based on need
 
1337
    $useSavepoint = ($conn->getTransactionLevel() > 0) ? TRUE : FALSE;
 
1338
    if ($useSavepoint)
 
1339
    {
 
1340
      $conn->beginTransaction(__FUNCTION__);
 
1341
    }
 
1342
    else
 
1343
    {
 
1344
      $conn->beginTransaction();
 
1345
    }
 
1346
 
 
1347
    try {
 
1348
      $entityPhone = new agEntityPhoneContact();
 
1349
      $entityPhone->set('entity_id', $entityId)
 
1350
          ->set('phone_contact_id', $phoneContactId)
 
1351
          ->set('phone_contact_type_id', $typeId)
 
1352
          ->set('priority', $priority);
 
1353
      $entityPhone->save($conn);
 
1354
      if ($useSavepoint) { $conn->commit(__FUNCTION__); } else { $conn->commit(); }
 
1355
    } catch (Exception $e) {
 
1356
      // ALWAYS log rollbacks with as much useful information as possible
 
1357
      $this->errMsg = sprintf('Couldn\'t create entity phone (%s, %s)! Rolled back changes!',
 
1358
                              array_search($typeId, $this->phoneContactTypes),
 
1359
                              Doctrine_Core::getTable('agPhoneContact')->find($phoneContactId)->phone_contact);
 
1360
 
 
1361
      // if we started with a savepoint, let's end with one, otherwise, rollback globally
 
1362
      if ($useSavepoint) { $conn->rollback(__FUNCTION__); } else { $conn->rollback(); }
 
1363
 
 
1364
      throw $e; // always remember to throw an exception after rollback
 
1365
    }
949
1366
 
950
1367
    return $entityPhone;
951
1368
  }
952
1369
 
953
 
  protected function updateEntityPhone($entityPhoneObject, $phoneObject)
 
1370
  protected function updateEntityPhone($entityPhoneObject, $phoneObject, $conn = NULL)
954
1371
  {
955
 
    $entityPhoneObject->set('phone_contact_id', $phoneObject->id);
956
 
    $entityPhoneObject->save();
 
1372
    // here you can pick up the default connection if not passed one explicitly
 
1373
    if (is_null($conn)) { $conn = Doctrine_Manager::connection(); }
 
1374
 
 
1375
    // here we check our current transaction scope and create a transaction or savepoint based on need
 
1376
    $useSavepoint = ($conn->getTransactionLevel() > 0) ? TRUE : FALSE;
 
1377
    if ($useSavepoint)
 
1378
    {
 
1379
      $conn->beginTransaction(__FUNCTION__);
 
1380
    }
 
1381
    else
 
1382
    {
 
1383
      $conn->beginTransaction();
 
1384
    }
 
1385
 
 
1386
    try {
 
1387
      $entityPhoneObject->set('phone_contact_id', $phoneObject->id);
 
1388
      $entityPhoneObject->save($conn);
 
1389
      if ($useSavepoint) { $conn->commit(__FUNCTION__); } else { $conn->commit(); }
 
1390
    } catch (Exception $e) {
 
1391
      // ALWAYS log rollbacks with as much useful information as possible
 
1392
      $this->errMsg = sprintf('Couldn\'t update entity email (%s, %s)! Rolled back changes!',
 
1393
                              $entityEmailObject->getAgEmailContactType()->email_contact_type,
 
1394
                              $emailObject->email_contact);
 
1395
 
 
1396
      // if we started with a savepoint, let's end with one, otherwise, rollback globally
 
1397
      if ($useSavepoint) { $conn->rollback(__FUNCTION__); } else { $conn->rollback(); }
 
1398
 
 
1399
      throw $e; // always remember to throw an exception after rollback
 
1400
    }
 
1401
 
957
1402
    return $entityPhone;
958
1403
  }
959
1404
 
964
1409
   * @param <type> $workEmailTypeId
965
1410
   * @return bool true if an update or create happened, false otherwise.
966
1411
   */
967
 
  protected function updateFacilityPhone($facility, $phone, $workPhoneTypeId, $phoneFormatId)
 
1412
  protected function updateFacilityPhone($facility, $phone, $workPhoneTypeId, $phoneFormatId, $conn = NULL)
968
1413
  {
969
1414
    //TODO
970
1415
    $entityId = $facility->getAgSite()->entity_id;
984
1429
    // only useful for nonrequired phones
985
1430
    //  if importedEmail null
986
1431
    if (empty($phone)) {
987
 
      $entityPhone->delete();
 
1432
      // here we check our current transaction scope and create a transaction or savepoint based on need
 
1433
      $useSavepoint = ($conn->getTransactionLevel() > 0) ? TRUE : FALSE;
 
1434
      if ($useSavepoint)
 
1435
      {
 
1436
        $conn->beginTransaction(__FUNCTION__);
 
1437
      }
 
1438
      else
 
1439
      {
 
1440
        $conn->beginTransaction();
 
1441
      }
 
1442
      try {
 
1443
        $entityPhone->delete($conn);
 
1444
        if ($useSavepoint) { $conn->commit(__FUNCTION__); } else { $conn->commit(); }
 
1445
      } catch (Exception $e) {
 
1446
        // ALWAYS log rollbacks with as much useful information as possible
 
1447
        $this->errMsg = sprintf('Couldn\'t remove old entity phone (%s, %s)! Rolled back changes!',
 
1448
                                array_search($workPhoneTypeId, $this->phoneContactTypes),
 
1449
                                $facilityPhone);
 
1450
 
 
1451
        // if we started with a savepoint, let's end with one, otherwise, rollback globally
 
1452
        if ($useSavepoint) { $conn->rollback(__FUNCTION__); } else { $conn->rollback(); }
 
1453
 
 
1454
        throw $e; // always remember to throw an exception after rollback
 
1455
      }
988
1456
      return TRUE;
989
1457
    }
990
1458
 
991
1459
    $phoneEntity = $this->getPhoneObject($phone);
992
1460
 
993
1461
    if (empty($phoneEntity)) {
994
 
      $phoneEntity = $this->createPhone($phone, $phoneFormatId);
 
1462
      $phoneEntity = $this->createPhone($phone, $phoneFormatId, $conn);
995
1463
    }
996
1464
 
997
1465
    if (empty($facilityPhone)) {
998
 
      $this->createEntityPhone($entityId, $phoneEntity->id, $workPhoneTypeId);
 
1466
      $this->createEntityPhone($entityId, $phoneEntity->id, $workPhoneTypeId, $conn);
999
1467
      return TRUE;
1000
1468
    }
1001
1469
 
1002
1470
    // Facility phone exists and does not match import phone
1003
 
    $this->updateEntityPhone($entityPhone, $phoneEntity);
 
1471
    $this->updateEntityPhone($entityPhone, $phoneEntity, $conn);
1004
1472
    return TRUE;
1005
1473
  }
1006
1474