Import.php 13.2 KB
Newer Older
1 2 3 4 5 6 7
<?php

namespace Biopen\GeoDirectoryBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
8
use Biopen\CoreBundle\Document\AbstractFile;
9

10 11 12
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

13 14
use Gedmo\Mapping\Annotation as Gedmo;

Sebastian Castro's avatar
Sebastian Castro committed
15
abstract class ImportState
Sebastian Castro's avatar
Sebastian Castro committed
16 17
{
    const Started = "started";
Sebastian Castro's avatar
Sebastian Castro committed
18
    const Downloading = "downloading";
Sebastian Castro's avatar
Sebastian Castro committed
19 20 21
    const InProgress = "in_progress";
    const Completed = "completed";
    const Errors = "errors";
Sebastian Castro's avatar
Sebastian Castro committed
22
    const Failed = "failed";
Sebastian Castro's avatar
Sebastian Castro committed
23 24
}

Sebastian Castro's avatar
Sebastian Castro committed
25 26
/**
* @MongoDB\Document
27
* @Vich\Uploadable
28 29
* Import data into GoGoCarto. the data can imported through a static file, or via API url
* The Import can be made once for all (static import) or dynamically every X days (ImportDynamic)
30 31 32 33
*
* @MongoDB\InheritanceType("SINGLE_COLLECTION")
* @MongoDB\DiscriminatorField("type")
* @MongoDB\DiscriminatorMap({"normal"="Import", "dynamic"="ImportDynamic"})
34
*/
35
class Import extends AbstractFile
36
{
Sebastian Castro's avatar
Sebastian Castro committed
37 38
    protected $vichUploadFileKey = "import_file";

39 40
    /**
     * @var int
Sebastian Castro's avatar
Sebastian Castro committed
41
     * @MongoDB\Id(strategy="INCREMENT")
42 43 44 45
     */
    private $id;

    /**
Sebastian Castro's avatar
Sebastian Castro committed
46
     * @var string
47
     * @MongoDB\Field(type="string")
48
     */
49
    public $sourceName;
50 51

    /**
Sebastian Castro's avatar
Sebastian Castro committed
52
     * @var string
53 54 55 56 57 58
     * Url of API to get the data
     * @MongoDB\Field(type="string")
     */
    private $url;

    /**
59
     * @MongoDB\ReferenceOne(targetDocument="Biopen\GeoDirectoryBundle\Document\Category", cascade={"persist"})
60
     */
61
    private $parentCategoryToCreateOptions = null;
62 63

    /**
64
     * @MongoDB\ReferenceMany(targetDocument="Biopen\GeoDirectoryBundle\Document\Option", cascade={"persist"})
65
     */
66
    private $optionsToAddToEachElement = [];
67 68 69 70

    /**
     * @MongoDB\Field(type="bool")
     */
71
    private $createMissingOptions = false;
72

Sebastian Castro's avatar
Sebastian Castro committed
73 74 75 76 77 78
    /**
     * @MongoDB\Field(type="bool")
     */
    private $needToHaveOptionsOtherThanTheOnesAddedToEachElements = false;

    /**
Sebastian Castro's avatar
Sebastian Castro committed
79
     * @var string
Sebastian Castro's avatar
Sebastian Castro committed
80 81 82 83
     * @MongoDB\Field(type="string")
     */
    public $fieldToCheckElementHaveBeenUpdated;

84 85 86
    /**
     * @MongoDB\Field(type="bool")
     */
Sebastian Castro's avatar
Sebastian Castro committed
87
    private $geocodeIfNecessary = false;
88 89 90 91

    /**
    * @MongoDB\ReferenceMany(targetDocument="Biopen\CoreBundle\Document\GoGoLog", cascade={"all"})
    */
Sebastian Castro's avatar
Sebastian Castro committed
92 93 94
    private $logs;

    /**
Sebastian Castro's avatar
Sebastian Castro committed
95
     * State of the import when processing. Type of ImportState
Sebastian Castro's avatar
Sebastian Castro committed
96
     * When processing import, this variable is being updated in realtime, so the client can follow
Sebastian Castro's avatar
Sebastian Castro committed
97
     * the state of the import also in realtime
Sebastian Castro's avatar
Sebastian Castro committed
98 99 100 101 102
     * @MongoDB\Field(type="string")
     */
    private $currState;

    /**
Sebastian Castro's avatar
Sebastian Castro committed
103
     * A message can be added to the state information
Sebastian Castro's avatar
Sebastian Castro committed
104 105 106
     * @MongoDB\Field(type="string")
     */
    private $currMessage;
Sebastian Castro's avatar
Sebastian Castro committed
107

Sebastian Castro's avatar
Sebastian Castro committed
108 109 110 111 112 113 114 115
    /**
     * After importing some Data, if the user hard delete some elements, their ids will be remembered
     * so next time we do not import them again
     *
     * @MongoDB\Field(type="collection")
     */
    private $idsToIgnore = [];

116 117 118 119

    /**
     * @MongoDB\Field(type="hash")
     */
Sebastian Castro's avatar
Sebastian Castro committed
120
    private $ontologyMapping = [];
121 122 123 124

    /**
     * @MongoDB\Field(type="hash")
     */
Sebastian Castro's avatar
Sebastian Castro committed
125 126
    private $taxonomyMapping = [];

127 128 129 130 131 132
    /**
     * Custom code made by the user to be run on the $data object when importing
     * @MongoDB\Field(type="string")
     */
    private $customCode = "<?php";

133 134 135 136 137 138 139
    /**
     * @var date $lastRefresh
     *
     * @MongoDB\Field(type="date")
     */
    private $lastRefresh = null;

140 141 142 143 144 145 146 147 148 149 150 151
    /**
     * @MongoDB\Field(type="date")
     * @Gedmo\Timestampable(on="create")
     */
    private $createdAt;

    /**
     * @MongoDB\Field(type="date")
     * @Gedmo\Timestampable(on="update")
     */
    private $updatedAt;

Sebastian Castro's avatar
Sebastian Castro committed
152

153 154 155
    public function __construct() {
        $this->logs = new \Doctrine\Common\Collections\ArrayCollection();;
    }
156

Sebastian Castro's avatar
Sebastian Castro committed
157 158
    public function __toString() { return "Import " . $this->sourceName; }

159 160
    public function isDynamicImport() { return false; }

Sebastian Castro's avatar
Sebastian Castro committed
161 162 163 164 165 166 167
    public function addIdToIgnore($id)
    {
        $this->idsToIgnore[] = $id;
    }

    public function isCategoriesFieldMapped()
    {
168 169 170 171 172 173 174 175
        return $this->getOntologyMapping() ? in_array('categories', array_values($this->getOntologyMapping())) : false;
    }

    /**
     * @Assert\Callback
     */
    public function validate(ExecutionContextInterface $context)
    {
176 177
        if (preg_match("/new |process|mongo|this|symfony/i", $this->customCode)) {
            $context->buildViolation("Il est interdit d'utiliser les mots suivants: new, mongo, process, this, symfony... Merci de ne pas faire de betises !")
178 179 180
                ->atPath('customCode')
                ->addViolation();
        }
Sebastian Castro's avatar
Sebastian Castro committed
181
    }
Sebastian Castro's avatar
Sebastian Castro committed
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
    /**
     * Get id
     *
     * @return int_id $id
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set parentCategoryToCreateOptions
     *
     * @param Biopen\GeoDirectoryBundle\Document\Category $parentCategoryToCreateOptions
     * @return $this
     */
    public function setParentCategoryToCreateOptions(\Biopen\GeoDirectoryBundle\Document\Category $parentCategoryToCreateOptions)
    {
        $this->parentCategoryToCreateOptions = $parentCategoryToCreateOptions;
        return $this;
    }

    /**
     * Get parentCategoryToCreateOptions
     *
     * @return Biopen\GeoDirectoryBundle\Document\Category $parentCategoryToCreateOptions
     */
    public function getParentCategoryToCreateOptions()
    {
        return $this->parentCategoryToCreateOptions;
    }

    /**
216
     * Set url
217
     *
218
     * @param string $url
219 220
     * @return $this
     */
221
    public function setUrl($url)
222
    {
223
        $this->url = $url;
224 225 226 227
        return $this;
    }

    /**
228
     * Get url
229
     *
230
     * @return string $url
231
     */
232
    public function getUrl()
233
    {
234
        return $this->url;
Sebastian Castro's avatar
Sebastian Castro committed
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

    /**
     * Set createMissingOptions
     *
     * @param bool $createMissingOptions
     * @return $this
     */
    public function setCreateMissingOptions($createMissingOptions)
    {
        $this->createMissingOptions = $createMissingOptions;
        return $this;
    }

    /**
     * Get createMissingOptions
     *
     * @return bool $createMissingOptions
     */
    public function getCreateMissingOptions()
    {
        return $this->createMissingOptions;
    }

    /**
     * Set geocodeIfNecessary
     *
     * @param bool $geocodeIfNecessary
     * @return $this
     */
    public function setGeocodeIfNecessary($geocodeIfNecessary)
    {
        $this->geocodeIfNecessary = $geocodeIfNecessary;
        return $this;
    }

    /**
     * Get geocodeIfNecessary
     *
     * @return bool $geocodeIfNecessary
     */
    public function getGeocodeIfNecessary()
    {
        return $this->geocodeIfNecessary;
    }
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
     * Add optionsToAddToEachElement
     *
     * @param Biopen\GeoDirectoryBundle\Document\Option $optionsToAddToEachElement
     */
    public function addOptionsToAddToEachElement(\Biopen\GeoDirectoryBundle\Document\Option $optionsToAddToEachElement)
    {
        $this->optionsToAddToEachElement[] = $optionsToAddToEachElement;
    }

    /**
     * Remove optionsToAddToEachElement
     *
     * @param Biopen\GeoDirectoryBundle\Document\Option $optionsToAddToEachElement
     */
    public function removeOptionsToAddToEachElement(\Biopen\GeoDirectoryBundle\Document\Option $optionsToAddToEachElement)
    {
        $this->optionsToAddToEachElement->removeElement($optionsToAddToEachElement);
    }

    /**
     * Get optionsToAddToEachElement
     *
     * @return \Doctrine\Common\Collections\Collection $optionsToAddToEachElement
     */
    public function getOptionsToAddToEachElement()
    {
        return $this->optionsToAddToEachElement;
    }

    /**
     * Set sourceName
313
     *
314
     * @param string $sourceName
315 316
     * @return $this
     */
317
    public function setSourceName($sourceName)
318
    {
319
        $this->sourceName = $sourceName;
320 321 322 323
        return $this;
    }

    /**
324
     * Get sourceName
325
     *
326
     * @return string $sourceName
327
     */
328
    public function getSourceName()
329
    {
330
        return $this->sourceName;
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

    /**
     * Add log
     *
     * @param Biopen\CoreBundle\Document\GoGoLog $log
     */
    public function addLog(\Biopen\CoreBundle\Document\GoGoLog $log)
    {
        $this->logs[] = $log;
    }

    /**
     * Remove log
     *
     * @param Biopen\CoreBundle\Document\GoGoLog $log
     */
    public function removeLog(\Biopen\CoreBundle\Document\GoGoLog $log)
    {
        $this->logs->removeElement($log);
    }

    /**
     * Get logs
     *
     * @return \Doctrine\Common\Collections\Collection $logs
     */
    public function getLogs()
Sebastian Castro's avatar
Sebastian Castro committed
359
    {
360 361 362 363
        $logs = is_array($this->logs) ? $this->logs : $this->logs->toArray();
        usort( $logs, function ($a, $b) { return $b->getCreatedAt()->getTimestamp() - $a->getCreatedAt()->getTimestamp(); });
        return $logs;
    }
Sebastian Castro's avatar
Sebastian Castro committed
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

    /**
     * Set currState
     *
     * @param string $currState
     * @return $this
     */
    public function setCurrState($currState)
    {
        $this->currState = $currState;
        return $this;
    }

    /**
     * Get currState
     *
     * @return string $currState
     */
    public function getCurrState()
    {
        return $this->currState;
    }

    /**
     * Set currMessage
     *
     * @param string $currMessage
     * @return $this
     */
    public function setCurrMessage($currMessage)
    {
        $this->currMessage = $currMessage;
        return $this;
    }

    /**
     * Get currMessage
     *
     * @return string $currMessage
     */
    public function getCurrMessage()
    {
        return $this->currMessage;
    }
Sebastian Castro's avatar
Sebastian Castro committed
408

Sebastian Castro's avatar
Sebastian Castro committed
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
    /**
     * Set idsToIgnore
     *
     * @param collection $idsToIgnore
     * @return $this
     */
    public function setIdsToIgnore($idsToIgnore)
    {
        $this->idsToIgnore = $idsToIgnore;
        return $this;
    }

    /**
     * Get idsToIgnore
     *
     * @return collection $idsToIgnore
     */
    public function getIdsToIgnore()
    {
        return $this->idsToIgnore;
    }
Sebastian Castro's avatar
Sebastian Castro committed
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

    /**
     * Set needToHaveOptionsOtherThanTheOnesAddedToEachElements
     *
     * @param bool $needToHaveOptionsOtherThanTheOnesAddedToEachElements
     * @return $this
     */
    public function setNeedToHaveOptionsOtherThanTheOnesAddedToEachElements($needToHaveOptionsOtherThanTheOnesAddedToEachElements)
    {
        $this->needToHaveOptionsOtherThanTheOnesAddedToEachElements = $needToHaveOptionsOtherThanTheOnesAddedToEachElements;
        return $this;
    }

    /**
     * Get needToHaveOptionsOtherThanTheOnesAddedToEachElements
     *
     * @return bool $needToHaveOptionsOtherThanTheOnesAddedToEachElements
     */
    public function getNeedToHaveOptionsOtherThanTheOnesAddedToEachElements()
    {
        return $this->needToHaveOptionsOtherThanTheOnesAddedToEachElements;
    }

    /**
     * Set fieldToCheckElementHaveBeenUpdated
     *
     * @param string $fieldToCheckElementHaveBeenUpdated
     * @return $this
     */
    public function setFieldToCheckElementHaveBeenUpdated($fieldToCheckElementHaveBeenUpdated)
    {
        $this->fieldToCheckElementHaveBeenUpdated = $fieldToCheckElementHaveBeenUpdated;
        return $this;
    }

    /**
     * Get fieldToCheckElementHaveBeenUpdated
     *
     * @return string $fieldToCheckElementHaveBeenUpdated
     */
    public function getFieldToCheckElementHaveBeenUpdated()
    {
        return $this->fieldToCheckElementHaveBeenUpdated;
    }
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

    /**
     * Set ontologyMapping
     *
     * @param hash $ontologyMapping
     * @return $this
     */
    public function setOntologyMapping($ontologyMapping)
    {
        $this->ontologyMapping = $ontologyMapping;
        return $this;
    }

    /**
     * Get ontologyMapping
     *
     * @return hash $ontologyMapping
     */
    public function getOntologyMapping()
    {
        return $this->ontologyMapping;
    }

    /**
     * Set taxonomyMapping
     *
     * @param hash $taxonomyMapping
     * @return $this
     */
    public function setTaxonomyMapping($taxonomyMapping)
    {
        $this->taxonomyMapping = $taxonomyMapping;
        return $this;
    }

    /**
     * Get taxonomyMapping
     *
     * @return hash $taxonomyMapping
     */
    public function getTaxonomyMapping()
    {
        return $this->taxonomyMapping;
    }
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539

    /**
     * Set customCode
     *
     * @param string $customCode
     * @return $this
     */
    public function setCustomCode($customCode)
    {
        $this->customCode = $customCode;
        return $this;
    }

    /**
     * Get customCode
     *
     * @return string $customCode
     */
    public function getCustomCode()
    {
        return $this->customCode;
    }
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561

    /**
     * Set lastRefresh
     *
     * @param date $lastRefresh
     * @return $this
     */
    public function setLastRefresh($lastRefresh)
    {
        $this->lastRefresh = $lastRefresh;
        return $this;
    }

    /**
     * Get lastRefresh
     *
     * @return date $lastRefresh
     */
    public function getLastRefresh()
    {
        return $this->lastRefresh;
    }
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583

    /**
     * Set createdAt
     *
     * @param date $createdAt
     * @return $this
     */
    public function setCreatedAt($createdAt)
    {
        $this->createdAt = $createdAt;
        return $this;
    }

    /**
     * Get createdAt
     *
     * @return date $createdAt
     */
    public function getCreatedAt()
    {
        return $this->createdAt;
    }
584
}