~fabiocbalbuquerque/sahana-agasti/web-services

« back to all changes in this revision

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

  • Committer: Chad Heuschober
  • Date: 2011-08-03 23:10:59 UTC
  • mto: (1.26.1 push-trunk)
  • mto: This revision was merged to the branch mainline in revision 25.
  • Revision ID: chad.heuschober@mail.cuny.edu-20110803231059-mel2ic5rhlaldqc5
Fixed facility import to uniquely create facilities.

Show diffs side-by-side

added added

removed removed

Lines of Context:
370
370
  {
371
371
    // explicit declarations are good
372
372
    $rawFacilityCodes = array();
 
373
    $uniqFacilities = array();
 
374
    $facilityEntities = array();
373
375
 
374
376
    // loop our import data and pick up any existing facility code
375
377
    foreach ($this->importData as $rowId => $rowData)
376
378
    {
377
379
      $rawData = $rowData['_rawData'];
378
380
 
379
 
      if (array_key_exists('facility_code', $rawData))
 
381
      if (isset($rawData['facility_code']))
380
382
      {
381
 
        $rawFacilityCodes[$rowId] = strtolower($rawData['facility_code']);
 
383
        $uniqFacilities[strtolower($rawData['facility_code'])][] = $rowId;
 
384
        $rawFacilityCodes[$rowId] = strtolower($rawData['facility_code']); // >>>> REMOVE?
382
385
      }
383
386
    }
384
387
    unset($rowData);
387
390
    $q = agDoctrineQuery::create($conn)
388
391
                    ->select('f.*')
389
392
                    ->addSelect('s.*')
390
 
                    ->from('agFacility f')
 
393
                    ->from('agFacility f INDEXBY f.facility_code')
391
394
                    ->innerJoin('f.agSite s')
392
 
                    ->whereIn('f.facility_code', $rawFacilityCodes);
 
395
                    ->whereIn('f.facility_code', array_keys($uniqFacilities));
393
396
    $facilityColl = $q->execute();
394
397
 
395
398
    $this->eh->logDebug('{' . count($facilityColl) . '} facility entities found in the database.');
399
402
 
400
403
    // Add entity id and facility id to $this->importData['primaryKeys'].
401
404
    // While we're looping the existing facility, make facility update where necessary.
402
 
    foreach ($facilityColl AS $facility)
 
405
    foreach ($facilityColl AS $facilityCode => $facility)
403
406
    {
404
 
      $rowId = array_search(strtolower($facility['facility_code']), $rawFacilityCodes);
405
 
      $this->importData[$rowId]['primaryKeys']['entity_id'] = $facility->agSite['entity_id'];
406
 
      $this->importData[$rowId]['primaryKeys']['facility_id'] = $facility['id'];
 
407
      foreach ($uniqFacilities[$facilityCode] as $index => $rowId) {
 
408
        $this->importData[$rowId]['primaryKeys']['entity_id'] = $facility->agSite['entity_id'];
 
409
        $this->importData[$rowId]['primaryKeys']['facility_id'] = $facility['id'];
 
410
      }
 
411
 
407
412
      $facility['facility_name'] = $this->importData[$rowId]['_rawData']['facility_name'];
408
 
      unset($rawFacilityCodes[$rowId]);
 
413
      unset($uniqFacilities[$facilityCode]);
409
414
    }
410
415
    $facilityColl->save($conn);
411
416
    $facilityColl->free(TRUE);
418
423
    $siteTable = $conn->getTable('agSite');
419
424
    $siteColl = new Doctrine_Collection('agSite');
420
425
    $facilityTable = $conn->getTable('agFacility');
 
426
    $facilityColl = new Doctrine_Collection('agFacility');
421
427
 
422
428
    // Create new entity using rowId as the key to $entityColl.
423
 
    foreach ($rawFacilityCodes AS $rowId => $facilityCode)
 
429
    foreach ($uniqFacilities AS $facilityCode => $rowIds)
424
430
    {
425
431
      $entity = new agEntity($entityTable, TRUE);
426
 
      $entityColl->add($entity, $rowId);
427
 
      unset($rawFacilityCodes[$rowId]);
 
432
      $entityColl->add($entity, $facilityCode);
428
433
    }
429
434
    $entityColl->save($conn);
430
435
 
431
436
    // Create new site using rowId as the key to $siteColl.
432
 
    foreach ($entityColl AS $rowId => $entity)
 
437
    foreach ($entityColl AS $facilityCode => $entity)
433
438
    {
434
439
      $entityId = $entity['id'];
435
440
      $site = new agSite($siteTable, TRUE);
436
441
      $site['entity_id'] = $entityId;
437
 
      $siteColl->add($site, $rowId);
 
442
      $siteColl->add($site, $facilityCode);
438
443
 
439
 
      $this->importData[$rowId]['primaryKeys']['entity_id'] = $entityId;
 
444
      $facilityEntities[$facilityCode] = $entityId;
440
445
    }
441
446
    $siteColl->save($conn);
442
447
    $entityColl->free(TRUE);
443
448
    unset($entityColl);
444
449
 
445
450
    // Create new facility using rowId as the key to $facilityColl.
446
 
    $facilityColl = new Doctrine_Collection('agFacility');
447
 
    foreach ($siteColl AS $rowId => $site)
 
451
    foreach ($siteColl AS $facilityCode => $site)
448
452
    {
449
 
      $rawData = $this->importData[$rowId]['_rawData'];
 
453
      // whoah! what??? how do you not isset()? well, because of how uniq is built we are guaranteed
 
454
      // that each entry has at least one rowId, eg, position [0]
 
455
      $rawData = $this->importData[$uniqFacilities[$facilityCode][0]]['_rawData'];
450
456
      $facility = new agFacility($facilityTable, TRUE);
451
457
      $facility['site_id'] = $site['id'];
452
458
      $facility['facility_name'] = $rawData['facility_name'];
453
 
      $facility['facility_code'] = $rawData['facility_code'];
454
 
      $facilityColl->add($facility, $rowId);
 
459
      $facility['facility_code'] = $facilityCode;
 
460
      $facilityColl->add($facility, $facilityCode);
455
461
    }
456
462
    $facilityColl->save($conn);
457
463
 
458
 
    // Store new facility ids to $this->importData['primaryKeys'].
459
 
    foreach ($this->importData AS $rowId => &$rowData)
460
 
    {
461
 
      if (!isset($rowData['primaryKeys']['facility_id']))
462
 
      {
463
 
        $rowData['primaryKeys']['facility_id'] = $facilityColl[$rowId]['id'];
 
464
    foreach ($uniqFacilities as $facilityCode => $rowIds) {
 
465
      foreach ($rowIds as $rowId) {
 
466
        $this->importData[$rowId]['primaryKeys']['entity_id'] = $facilityEntities[$facilityCode];
 
467
        $this->importData[$rowId]['primaryKeys']['facility_id'] = $facilityColl[$facilityCode]['id'];
464
468
      }
465
469
    }
466
470
    $facilityColl->free(TRUE);
467
471
    unset($facilityColl);
 
472
 
 
473
    unset($facilityEntities);
 
474
    unset($uniqFacilities);
468
475
  }
469
476
 
470
477
  /**