aboutsummaryrefslogtreecommitdiff
path: root/lib/sfIsisImporter.class.php
blob: d0d9a9fe7fb7dcd4951bc31b7976481aefedb052 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
<?php

/**
 * IsisImporter: provides ISIS import methods for importing data into
 * a Symfony project.
 */
class sfIsisImporter extends IsisConnector
{
  /**
   * Log dispatcher.
   *
   * @param string $message Log message
   * @param string $level   Log level
   */
  public function log($message, $level = 'info')
  {
    $this->logger->log($message, $level);
  }

  /**
   * Constructor.
   */ 
  public function __construct($config = null) {
    parent::__construct($config);
    $this->stats = sfIsisImporterStats::getInstance();

    // Get a logger instance.
    $this->logger = sfIsisImporterLog::getInstance();
  }

  /**
   * Get the model foreign table id.
   *
   * @param  object $model Model
   * @return string        Model table id
   */
  static function getModelId($model) {
    return sfInflector::underscore(get_class($model)) .'_id';
  }

  /**
   * Get the relation foreign table id.
   *
   * @param  string $model Relation name
   * @return string        Relation table id
   */
  static function getRelationId($relation)
  {
    return sfInflector::underscore($relation) .'_id';
  }

  /**
   * Get the model and relation tablename.
   *
   * @param  object $model Model
   * @param  string $model Relation name
   * @return string        Relation table name
   */
  static function getModelRelation($model, $relation)
  {
    return sfInflector::camelize(self::getModelName($model)) . sfInflector::camelize($relation);
  }

  /**
   * Get the entity name from a subfield.
   *
   * @param  string $subfield Subfield name
   * @return string           Genre name
   */
  static function entityName($subfield)
  {
    return ucfirst($subfield);
  }  

  /**
   * Get the model name.
   *
   * @param  object $model Model
   * @return string        Model name
   */
  static function getModelName($model)
  {
    return get_class($model);
  }

  /**
   * Default implementation for parseName.
   *
   * @param  mixed $value Name
   * @return mixed        Name
   */
  public function parseName($value)
  {
    return $value;
  }  

  /**
   * Add a single entry from the database.
   *
   * @param string $base_model Model to use
   * @param int    $entry      Entry number
   */
  public function addEntry($base_model, $entry)
  {
    // Get data and setup the model.
    $this->read($entry);
    $model = $this->newModel($base_model, $entry);

    if ($model)
    {
      $this->log("Importing $base_model $entry...");

      // Dispatch to custom import procedures.
      foreach (new IsisMethodIterator($this) as $method => $field)
      {
        if (!$this->hasDeniedCombinations($base_model, $field))
        {
          $this->{$method}($model, $field);
        }
      }      

      $model->save();
      $model->free(true);
    }
    else {
      $this->log("Skipping existing entry $entry for $base_model.");
    }
  }

  /**
   * Create a new model just if doesn't exist for a given entry. For that
   * to work the entry must provide and id.
   *
   * @param  string $base_model Model to use
   * @param  int    $entry      Entry number
   * @return mixed              New model or false
   */
  public function newModel($base_model, $entry)
  {
    $model = new $base_model();
    $id    = $this->getBaseModelId($model);

    if ($id)
    {
      $existing = call_user_func(array($base_model, 'getById'), $id);

      if (!$existing)
      {
        $this->setBaseModelId($model);
        return $model;
      }
      elseif ($this->skipExisting())
      {
        return false;
      }
      else
      {
        return $existing;
      }
    }

    return $model;
  }

  /**
   * Check if ISIS database configuration is set to not skip existing
   * entries during an import.
   *
   * @return boolean
   */
  public function skipExisting()
  {
    if (isset($this->format['import']['skip_existing']))
    {
      return $this->format['import']['skip_existing'];
    }

    return true;
  }

  /**
   * Set the primary key for the model by getting it or just saving it.
   *
   * @param object $model Base model
   */
  public function setBaseModelId(&$model)
  {
    $model->id = $this->getBaseModelId($model);
    $model->save();
  }

  /**
   * Get the primary key for the base model.
   *
   * @param  object $model Base model
   * @return int           Base model id
   */
  public function getBaseModelId(&$model)
  {
    $method = get . $this->getModelName($model) .'PrimaryKey';
    if (method_exists($this, $method))
    {
      return $this->{$method}($model);
    }
  }

  /**
   * Import a single field into a model.
   *
   * @param object $model    Model
   * @param array  $field    Field data from ISIS database schema
   * @param int    $row      Row number
   */
  public function addField(&$model, $field, $row = 0)
  {
    $value = $this->filterBrackets($this->getMainItem($field, $row));

    if ($value != null)
    {
      $map   = $this->getMap($field);
      $model->{$map}($value);
    }
  }

  /**
   * Import a single subfield into a model.
   *
   * @param object $model    Model
   * @param array  $field    Field data from ISIS database schema
   * @param string $subfield Subfield name
   * @param int    $row      Row number
   */
  public function addSubfield(&$model, $field, $subfield, $row = 0)
  {
    $value = $this->filterBrackets($this->getSubfield($field, $subfield, $row));

    if ($value != null)
    {
      $map   = $this->getMap($field, $subfield);
      $model->{$map}($value);
    }
  }

  /**
   * Import single values into the model.
   *
   * Currently undefined mappings for a field/subfield
   * are not saved as we would need to make sure a corresponding
   * field exists in the model. This prevents the map
   * configurations like $map = array('type' => 'value'); to work.
   *
   * As we are importing single values, here we don't care with
   * row numbers as we assume that just the first row should be
   * imported.
   *
   * @param object $model Model object
   * @param array  $field Field data
   */
  public function importValues(&$model, array $field)
  {
    if ($this->fieldHasMap($field))
    {
      $this->addField($model, $field);
    }

    foreach ($this->getSubfieldList($field) as $subfield)
    {
      if ($this->subfieldHasMap($field, $subfield))
      {
        $this->addSubfield($model, $field, $subfield);
      }
    }
  }

  /**
   * Get an existing entity.
   *
   * @param  string $entity Entity name
   * @param  string $value  Value to search for
   * @param  string $by     Field to search $value
   * @return mixed          Entity data or false
   */
  public function getEntity($entity, $value, $by = 'name')
  {
    $findby = 'findOneBy'. ucfirst($by);
    $data   = Doctrine_Core::getTable($entity)->{$findby}($value);

    if ($data)
    {
      return $data;
    }

    return false;
  }

  /**
   * Add a new entity into the database if needed, returning
   * the corresponding object.
   *
   * @param  string $entity Entity name
   * @param  string $name   Name value
   * @return object         Entity data
   */
  public function addEntity($entity, $name)
  {
    $name = $this->entityName($name);
    $data = $this->getEntity($entity, $name);

    if (!$data)
    {
      $this->log("Adding new $entity $name.");
      $data       = new $entity();
      $data->name = $name;
      $data->save();
    }

    return $data;
  }

  /**
   * Import one to one data.
   *
   * @param object $model    Model
   * @param array  $field    Field data from ISIS database schema
   * @param string $relation Relation name
   */
  public function addOneToOne(&$model, array $field, $relation)
  {
    foreach (new IsisRowIterator($this, $field) as $row)
    {
      $data = new $relation();

      foreach ($this->getSubfieldList($field) as $subfield)
      {
        $this->addSubfield($data, $field, $subfield, $row);
      }

      $data->save();
      $key         = sfInflector::underscore($relation) .'_id';
      $model->$key = $relation->id;
    }
  }

  /**
   * Import many to many data.
   *
   * @param object $model    Model
   * @param array  $values   Values to be added
   * @param string $relation Relation name
   */
  public function addManyToMany(&$model, array $values, $relation)
  {
    $method = 'add'. $relation;

    foreach ($values as $value)
    {
      // Populate related data.
      if (is_callable(array($this, $method)))
      {
        $data = $this->{$method}($value);
      }
      else
      {
        $data = $this->addEntity($relation, $value);
      }

      // Get model and relation names and id fields.
      $model_id                   = $this->getModelId($model);
      $relation_id                = $this->getRelationId($relation);
      $model_relation             = $this->getModelRelation($model, $relation);

      // Make the relation.
      $model_data                 = new $model_relation();
      $model_data->{$model_id}    = $model->id;
      $model_data->{$relation_id} = $data->id;
      $model_data->save();
    }
  }

  /**
   * Import one to one data.
   *
   * @param  object $model    Model
   * @param  string $relation Relation name
   * @return object           Relation model object
   */  
  public function addOneToMany(&$model, $relation)
  {
    $model_id          = $this->getModelId($model);
    $data              = new $relation();
    $data->{$model_id} = $model->id;
    $data->save();
    return $data;
  }

  /**
   * Add simple entities data into the model.
   *
   * @param object $model Model
   * @param array  $field Field data from ISIS database schema
   */  
  public function addOneToManyEntities(&$model, array $field, $entity, $key = 'name')
  {
    foreach (new IsisMainItemIterator($this, $field) as $row => $value)
    {
      $this->log("Entity: $entity; Value: $value", 'debug');
      $data         = $this->addOneToMany($model, $entity);
      $data->{$key} = $value;
      $data->save();
    }    
  }

  /**
   * Add field values in a many-to-many relation.
   *
   * @param object $model    Model
   * @param array  $field    Field data from ISIS database schema
   * @param string $relation Relation name
   */
  public function addManyToManyField(&$model, array $field, $relation)
  {
    foreach (new IsisMainItemIterator($this, $field) as $value)
    {
      $this->addManyToMany($model, $this->explodeBrackets($value), $relation);
    }    
  }

  /**
   * Add an element into the database if needed, returning
   * the resulting object.
   *
   * @param  string $entity Entity name
   * @param  string $value  Entity value
   * @return object         Entity data
   */
  public function newOrExisting($entity, $value)
  {
    // Check for a null value.
    if ($value == null)
    {
      $this->log("Null element value for $entity.", 'debug');
      return;
    }

    // Get name.
    $name = $this->parseName($value);

    // Get existing element.
    $element = call_user_func(array($entity, 'getByName'), $name);

    // Create new element if needed.
    if (!$element)
    {
      $this->log("Adding new $entity $value.");
      $element = call_user_func(array($entity, 'addByName'), $name);
    }

    return $element;
  }

  /**
   * Check denied combinations inside a field.
   *
   * @param  string  $model Model name
   * @param  array   $field Field data from ISIS database schema
   * @return boolean        True if has a denied combination, false otherwise 
   * @todo                  Wildcard
   * @todo                  Test
   */
  public function hasDeniedCombinations($model, $field)
  {
    if (method_exists($this, 'getDeniedCombinations'))
    {
      foreach ($this->getDeniedCombinations($model, $field) as $combination)
      {
        $has = true;

        foreach ($combination as $item)
        {
          foreach (new IsisRowIterator($this, $field) as $row)
          {
            if (!$this->hasDeniedItem($field, $item, $row))
            {
              $has = false;
              break 2;
            }
          }
        }

        if ($has)
        {
          $denied[$row] = $combination;
        }
      }
    }

    if (isset($denied))
    {
      foreach ($denied as $row => $combination)
      {
        $combination = implode(',', $combination);
        $this->log("Found denied combination for row $row: $combination");
      }

      return true;
    }

    return false;
  }

  /**
   * Check if a field has a denied item.
   *
   * @param  array   $field Field data from ISIS database schema
   * @param  string  $item  Item code
   * @param  int     $row   Row number
   * @return boolean        True if has a denied combination, false otherwise 
   * @todo                  Wildcard
   * @todo                  Test
   */
  public function hasDeniedItem($field, $item, $row)
  {
    // Default condition: presence requirement for an item.
    $condition = true;

    // Check if item has the negation (absence) requirement for an item.
    if (substr($item, 0, 1) == '!')
    {
      $item      = substr($item, 1);
      $condition = false;
    }

    // Check if the item exists.
    $has = $this->hasItem($field, $item, $row);

    if ($condition == true)
    {
      // If the condition is true, a denied combination won't be fulfilled if
      // the item is not present.
      if (!$has)
      {
        return false;
      }
    }
    else
    {
      // If the condition is false, a denied combination won't be fulfilled if
      // the item is present.
      if ($has)
      {
        return false;
      }
    }

    return true;
  }
}