FOSSology  4.4.0
Open Source License Compliance by Open Source Software
DecisionImporterIdFetcher.php
Go to the documentation of this file.
1 <?php
2 /*
3  Author: Gaurav Mishra <mishra.gaurav@siemens.com>
4  SPDX-FileCopyrightText: © 2022 Siemens AG
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
14 namespace Fossology\DecisionImporter;
15 
19 use UnexpectedValueException;
20 
21 require_once "FoDecisionData.php";
22 
27 {
29  private $dbManager;
30 
32  private $pfileDao;
33 
35  private $licenseDao;
36 
41  private $groupId;
42 
47  private $userId;
48 
53  private $uploadId;
54 
60  function __construct(DbManager $dbManager, PfileDao $pfileDao, LicenseDao $licenseDao)
61  {
62  $this->dbManager = $dbManager;
63  $this->pfileDao = $pfileDao;
64  $this->licenseDao = $licenseDao;
65  }
66 
70  public function setGroupId(int $groupId): void
71  {
72  $this->groupId = $groupId;
73  }
74 
78  public function setUserId(int $userId): void
79  {
80  $this->userId = $userId;
81  }
82 
86  public function setUploadId(int $uploadId): void
87  {
88  $this->uploadId = $uploadId;
89  }
90 
97  public function getOrCreateIds(FoDecisionData &$reportData, DecisionImporterAgent &$agentObj): void
98  {
99  $pfileList = $reportData->getPfileList();
100  $uploadTreeList = $reportData->getUploadtreeList();
101  $clearingDecisionList = $reportData->getClearingDecisionList();
102  $copyrightList = $reportData->getCopyrightList();
103  $copyrightDecisionList = $reportData->getCopyrightDecisionList();
104  $copyrightEventList = $reportData->getCopyrightEventList();
105  $eccList = $reportData->getEccList();
106  $eccDecisionList = $reportData->getEccDecisionList();
107  $eccEventList = $reportData->getEccEventList();
108  $ipraList = $reportData->getIpraList();
109  $ipraDecisionList = $reportData->getIpraDecisionList();
110  $ipraEventList = $reportData->getIpraEventList();
111  $licenseList = $reportData->getLicensesList();
112  $clearingEventList = $reportData->getClearingEventList();
113  $licenseSetBulkList = $reportData->getLicenseSetBulkList();
114  $mainLicenseList = $reportData->getMainLicenseList();
115 
116  $this->updatePfileIds($pfileList);
117  $agentObj->heartbeat(0);
118  $this->updateUploadTreeIds($uploadTreeList, $pfileList, $agentObj);
119  $agentObj->heartbeat(0);
120  $this->updateClearingDecision($clearingDecisionList, $uploadTreeList, $pfileList);
121  $agentObj->heartbeat(0);
122  $this->updateCxList($copyrightList, $pfileList);
123  $agentObj->heartbeat(0);
124  $this->updateDecisionList($copyrightDecisionList, $pfileList);
125  $agentObj->heartbeat(0);
126  $this->updateEventList($copyrightEventList, $uploadTreeList);
127  $agentObj->heartbeat(0);
128  $this->updateCxList($eccList, $pfileList);
129  $agentObj->heartbeat(0);
130  $this->updateDecisionList($eccDecisionList, $pfileList);
131  $agentObj->heartbeat(0);
132  $this->updateEventList($eccEventList, $uploadTreeList);
133  $agentObj->heartbeat(0);
134  $this->updateCxList($ipraList, $pfileList);
135  $agentObj->heartbeat(0);
136  $this->updateDecisionList($ipraDecisionList, $pfileList);
137  $agentObj->heartbeat(0);
138  $this->updateEventList($ipraEventList, $uploadTreeList);
139  $agentObj->heartbeat(0);
140  $this->updateLicenses($licenseList);
141  $agentObj->heartbeat(0);
142  $this->updateClearingEvent($clearingEventList, $uploadTreeList, $licenseList);
143  $agentObj->heartbeat(0);
144  $this->updateLicenseSetBulk($licenseSetBulkList, $licenseList);
145  $agentObj->heartbeat(0);
146  $this->updateMainLicenseList($mainLicenseList, $licenseList);
147  $agentObj->heartbeat(0);
148 
149  $reportData->setPfileList($pfileList)
150  ->setUploadtreeList($uploadTreeList)
151  ->setClearingDecisionList($clearingDecisionList)
152  ->setCopyrightList($copyrightList)
153  ->setCopyrightDecisionList($copyrightDecisionList)
154  ->setCopyrightEventList($copyrightEventList)
155  ->setEccList($eccList)
156  ->setEccDecisionList($eccDecisionList)
157  ->setEccEventList($eccEventList)
158  ->setIpraList($ipraList)
159  ->setIpraDecisionList($ipraDecisionList)
160  ->setIpraEventList($ipraEventList)
161  ->setLicensesList($licenseList)
162  ->setClearingEventList($clearingEventList)
163  ->setLicenseSetBulkList($licenseSetBulkList)
164  ->setMainLicenseList($mainLicenseList);
165  }
166 
171  private function updatePfileIds(array &$pfileList): void
172  {
173  foreach ($pfileList as $oldId => $item) {
174  $hash = $item["hash"];
175  $hashList = explode(".", $item["hash"]);
176  $pfile = $this->pfileDao->getPfile($hashList[0], $hashList[1], null, $hashList[2]);
177  if ($pfile == null) {
178  throw new UnexpectedValueException("Can't find pfile with hash '$hash' in DB");
179  }
180  $pfileList[$oldId]["new_pfile"] = $pfile['pfile_pk'];
181  }
182  }
183 
190  private function updateUploadTreeIds(array &$uploadTreeList, array $pfileList,
191  DecisionImporterAgent &$agentObj): void
192  {
193  $sqlAllTree = "SELECT * FROM uploadtree WHERE upload_fk = $1 AND ufile_mode & (1<<28) = 0;";
194  $statementAllTree = __METHOD__ . ".allTree";
195  $allUploadTree = $this->dbManager->getRows($sqlAllTree, [$this->uploadId], $statementAllTree);
196  $i = 0;
197  foreach ($uploadTreeList as $oldItemId => $item) {
198  $new_pfile = $pfileList[$item["old_pfile"]]["new_pfile"];
199  $matchIndex = -INF;
200  $j = 0;
201  foreach ($allUploadTree as $index => $uploadTreeItem) {
202  if ($uploadTreeItem["pfile_fk"] == $new_pfile) {
203  if (array_key_exists("path", $item)) {
204  $newpath = Dir2Path($uploadTreeItem["uploadtree_pk"]);
205  $newpath = implode("/", array_column($newpath, "ufile_name"));
206  if ($newpath == $item["path"]) {
207  $matchIndex = $index;
208  break;
209  }
210  } elseif ($uploadTreeItem["lft"] == $item["lft"] && $uploadTreeItem["rgt"] == $item["rgt"]) {
211  $matchIndex = $index;
212  break;
213  }
214  }
215  $j++;
217  $agentObj->heartbeat(0);
218  $j = 0;
219  }
220  }
221  if ($matchIndex == -INF) {
222  $path = $oldItemId;
223  if (array_key_exists("path", $item)) {
224  $path = $item["path"];
225  }
226  echo "Can't find item with pfile '$new_pfile' in upload " .
227  "'$this->uploadId'.\nIgnoring: $path";
228  $uploadTreeList[$oldItemId]["new_itemid"] = null;
229  } else {
230  $uploadTreeList[$oldItemId]["new_itemid"] = $allUploadTree[$matchIndex]["uploadtree_pk"];
231  }
232  $i++;
234  $agentObj->heartbeat(0);
235  $i = 0;
236  }
237  }
238  }
239 
246  private function updateClearingDecision(array &$clearingDecisionList, array $uploadTreeList, array $pfileList): void
247  {
248  foreach ($clearingDecisionList as $index => $item) {
249  $newItemId = $uploadTreeList[$item["old_itemid"]]["new_itemid"];
250  $newPfileId = $pfileList[$item["old_pfile"]]["new_pfile"];
251  $clearingDecisionList[$index]["new_itemid"] = $newItemId;
252  $clearingDecisionList[$index]["new_pfile"] = $newPfileId;
253  }
254  }
255 
261  private function updateCxList(array &$cxList, array $pfileList): void
262  {
263  foreach ($cxList as $index => $item) {
264  $newPfileId = $pfileList[$item["old_pfile"]]["new_pfile"];
265  $cxList[$index]["new_pfile"] = $newPfileId;
266  }
267  }
268 
274  private function updateDecisionList(array &$decisionList, array $pfileList): void
275  {
276  foreach ($decisionList as $index => $decisionItem) {
277  $newPfileId = $pfileList[$decisionItem["old_pfile"]]["new_pfile"];
278  $decisionList[$index]["new_pfile"] = $newPfileId;
279  }
280  }
281 
287  private function updateEventList(array &$eventList, array $uploadtreeList): void
288  {
289  foreach ($eventList as $index => $eventItem) {
290  if (!array_key_exists($eventItem["old_itemid"], $uploadtreeList)) {
291  echo "Unable to find item id for old_id: " . $eventItem["old_itemid"] . "\n";
292  $newItemId = null;
293  } else {
294  $newItemId = $uploadtreeList[$eventItem["old_itemid"]]["new_itemid"];
295  }
296  $eventList[$index]["new_itemid"] = $newItemId;
297  }
298  }
299 
304  private function updateLicenses(array &$licenseList): void
305  {
306  foreach ($licenseList as $index => $item) {
307  $newLicenseId = null;
308  $license = $this->licenseDao->getLicenseByShortName($item["rf_shortname"], $this->groupId);
309  if ($license == null) {
310  $newLicenseData = [
311  "rf_fullname" => $item["rf_fullname"],
312  "rf_url" => $item["rf_url"],
313  "rf_notes" => $item["rf_notes"],
314  "rf_risk" => $item["rf_risk"],
315  "rf_md5" => $item["rf_md5"],
316  ];
317  if ($item["is_candidate"] == "t") {
318  $newLicenseId = $this->licenseDao->insertUploadLicense($item["rf_shortname"], $item["rf_text"],
319  $this->groupId, $this->userId);
320  } else {
321  $newLicenseId = $this->licenseDao->insertLicense($item["rf_shortname"], $item["rf_text"]);
322  }
323  $this->dbManager->updateTableRow("license_ref", $newLicenseData, "rf_pk", $newLicenseId);
324  } else {
325  $newLicenseId = $license->getId();
326  }
327  $licenseList[$index]["new_rfid"] = $newLicenseId;
328  }
329  }
330 
337  private function updateClearingEvent(array &$clearingEventList, array $uploadTreeList, array $licenseList): void
338  {
339  foreach ($clearingEventList as $index => $item) {
340  $newItemId = $uploadTreeList[$item["old_itemid"]]["new_itemid"];
341  $newLicenseId = $licenseList[$item["old_rfid"]]["new_rfid"];
342  $clearingEventList[$index]["new_itemid"] = $newItemId;
343  $clearingEventList[$index]["new_rfid"] = $newLicenseId;
344  }
345  }
346 
352  private function updateLicenseSetBulk(array &$licenseSetBulkList, array $licenseList): void
353  {
354  foreach ($licenseSetBulkList as $lrbId => $licenseSetBulks) {
355  foreach ($licenseSetBulks as $index => $licenseSetBulkItem) {
356  $newRfId = $licenseList[$licenseSetBulkItem["old_rfid"]]["new_rfid"];
357  $licenseSetBulkList[$lrbId][$index]["new_rfid"] = $newRfId;
358  }
359  }
360  }
361 
367  private function updateMainLicenseList(array &$mainLicenseList, array $licenseList): void
368  {
369  foreach ($mainLicenseList as $oldId => $mainLicenseItem) {
370  $newLicenseId = $licenseList[$mainLicenseItem["old_rfid"]]["new_rfid"];
371  $mainLicenseList[$oldId]["new_rfid"] = $newLicenseId;
372  }
373  }
374 }
Import decisions generated by Decision Exporter from JSON files.
updateMainLicenseList(array &$mainLicenseList, array $licenseList)
__construct(DbManager $dbManager, PfileDao $pfileDao, LicenseDao $licenseDao)
updateClearingEvent(array &$clearingEventList, array $uploadTreeList, array $licenseList)
updateEventList(array &$eventList, array $uploadtreeList)
getOrCreateIds(FoDecisionData &$reportData, DecisionImporterAgent &$agentObj)
updateLicenseSetBulk(array &$licenseSetBulkList, array $licenseList)
updateClearingDecision(array &$clearingDecisionList, array $uploadTreeList, array $pfileList)
updateDecisionList(array &$decisionList, array $pfileList)
updateUploadTreeIds(array &$uploadTreeList, array $pfileList, DecisionImporterAgent &$agentObj)
heartbeat($newProcessed)
Send hear beat to the scheduler.
Definition: Agent.php:203
Dir2Path($uploadtree_pk, $uploadtree_tablename='uploadtree')
Return the path (without artifacts) of an uploadtree_pk.
Definition: common-dir.php:222
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16