FOSSology  4.4.0
Open Source License Compliance by Open Source Software
DecisionImporterDataCreator.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 
23 use UnexpectedValueException;
24 
25 require_once "FoDecisionData.php";
26 
31 {
36  private $dbManager;
37 
42  private $agentDao;
43 
48  private $copyrightDao;
49 
54  private $licenseDao;
55 
60  private $uploadDao;
61 
66  private $clearingDao;
67 
72  private $userId;
73 
78  private $groupId;
79 
84  private $uploadId;
85 
96  {
97  $this->dbManager = $dbManager;
98  $this->agentDao = $agentDao;
99  $this->copyrightDao = $copyrightDao;
100  $this->licenseDao = $licenseDao;
101  $this->uploadDao = $uploadDao;
102  $this->clearingDao = $clearingDao;
103  }
104 
108  public function setUserId(int $userId): void
109  {
110  $this->userId = $userId;
111  }
112 
116  public function setGroupId(int $groupId): void
117  {
118  $this->groupId = $groupId;
119  }
120 
124  public function setUploadId(int $uploadId): void
125  {
126  $this->uploadId = $uploadId;
127  }
128 
135  public function createClearingDecisions(FoDecisionData &$reportData,
136  DecisionImporterAgent &$agentObj): void
137  {
138  $clearingDecisionList = $reportData->getClearingDecisionList();
139  $clearingEventList = $reportData->getClearingEventList();
140  $clearingDecisionEventList = $reportData->getClearingDecisionEventList();
141 
142  $i = 0;
143  foreach ($clearingDecisionList as $oldDecisionId => $decisionItem) {
144  if ($decisionItem["new_itemid"] !== null) {
145  $newCdId = $this->dbManager->insertTableRow("clearing_decision", [
146  "uploadtree_fk" => $decisionItem["new_itemid"],
147  "pfile_fk" => $decisionItem["new_pfile"],
148  "decision_type" => $decisionItem["decision_type"],
149  "group_fk" => $this->groupId,
150  "user_fk" => $this->userId,
151  "scope" => $decisionItem["scope"],
152  "date_added" => $decisionItem["date_added"]
153  ], __METHOD__ . ".insertCd", "clearing_decision_pk");
154  } else {
155  $newCdId = null;
156  }
157  $clearingDecisionList[$oldDecisionId]["new_decision"] = $newCdId;
158  $i++;
161  $i = 0;
162  }
163  }
164  if ($i != 0) {
165  $agentObj->heartbeat($i);
166  }
167 
168  $i = 0;
169  foreach ($clearingEventList as $oldEventId => $eventItem) {
170  if ($eventItem["new_itemid"] !== null) {
171  $newCeId = $this->dbManager->insertTableRow("clearing_event", [
172  "uploadtree_fk" => $eventItem["new_itemid"],
173  "rf_fk" => $eventItem["new_rfid"],
174  "removed" => $eventItem["removed"],
175  "user_fk" => $this->userId,
176  "group_fk" => $this->groupId,
177  "job_fk" => null,
178  "type_fk" => $eventItem["type_fk"],
179  "comment" => $eventItem["comment"],
180  "reportinfo" => $eventItem["reportinfo"],
181  "acknowledgement" => $eventItem["acknowledgement"],
182  "date_added" => $eventItem["date_added"]
183  ], __METHOD__ . ".insertCe", "clearing_event_pk");
184  } else {
185  $newCeId = null;
186  }
187  $clearingEventList[$oldEventId]["new_event"] = $newCeId;
188  $i++;
191  $i = 0;
192  }
193  }
194  if ($i != 0) {
195  $agentObj->heartbeat($i);
196  }
197 
198  foreach ($clearingDecisionEventList as $oldCdId => $ceList) {
199  $newCdId = $clearingDecisionList[$oldCdId]["new_decision"];
200  if ($newCdId === null) {
201  continue;
202  }
203  foreach ($ceList as $oldCeId) {
204  $newCeId = $clearingEventList[$oldCeId]["new_event"];
205  if ($newCeId === null) {
206  continue;
207  }
208  $this->dbManager->insertTableRow("clearing_decision_event", [
209  "clearing_decision_fk" => $newCdId,
210  "clearing_event_fk" => $newCeId
211  ], __METHOD__ . ".insertCdCe");
212  }
213  }
214  $agentObj->heartbeat(0);
215 
216  $reportData->setClearingDecisionList($clearingDecisionList)
217  ->setClearingEventList($clearingEventList);
218  }
219 
225  public function createReportData(FoDecisionData &$reportData): void
226  {
227  $reportInfo = $reportData->getReportInfo();
228  $assocParams = [
229  "upload_fk" => $this->uploadId,
230  "ri_ga_checkbox_selection" => $reportInfo["ri_ga_checkbox_selection"],
231  "ri_spdx_selection" => $reportInfo["ri_spdx_selection"],
232  "ri_excluded_obligations" => $reportInfo["ri_excluded_obligations"],
233  "ri_reviewed" => $reportInfo["ri_reviewed"],
234  "ri_footer" => $reportInfo["ri_footer"],
235  "ri_report_rel" => $reportInfo["ri_report_rel"],
236  "ri_community" => $reportInfo["ri_community"],
237  "ri_component" => $reportInfo["ri_component"],
238  "ri_version" => $reportInfo["ri_version"],
239  "ri_release_date" => $reportInfo["ri_release_date"],
240  "ri_sw360_link" => $reportInfo["ri_sw360_link"],
241  "ri_general_assesment" => $reportInfo["ri_general_assesment"],
242  "ri_ga_additional" => $reportInfo["ri_ga_additional"],
243  "ri_ga_risk" => $reportInfo["ri_ga_risk"],
244  "ri_department" => $reportInfo["ri_department"],
245  "ri_depnotes" => $reportInfo["ri_depnotes"],
246  "ri_exportnotes" => $reportInfo["ri_exportnotes"],
247  "ri_copyrightnotes" => $reportInfo["ri_copyrightnotes"],
248  "ri_unifiedcolumns" => $reportInfo["ri_unifiedcolumns"],
249  "ri_globaldecision" => $reportInfo["ri_globaldecision"],
250  "ri_component_id" => $reportInfo["ri_component_id"],
251  "ri_component_type" => $reportInfo["ri_component_type"]
252  ];
253  $existsSql = "SELECT ri_pk FROM report_info WHERE upload_fk = $1;";
254  $existsStatement = __METHOD__ . ".reportInfoExists";
255 
256  if ($this->dbManager->getSingleRow($existsSql, [$this->uploadId], $existsStatement)) {
257  $this->dbManager->updateTableRow("report_info", $assocParams, "upload_fk", $this->uploadId,
258  __METHOD__ . ".updateReportInfo");
259  } else {
260  $this->dbManager->insertTableRow("report_info", $assocParams, __METHOD__ . ".insertReportInfo");
261  }
262  }
263 
275  public function createCopyrightData(FoDecisionData &$reportData,
276  DecisionImporterAgent &$agentObj,
277  string $agentName, int $jobId): void
278  {
279  $type = $agentName;
280  if ($agentName == "copyright") {
281  $type = "statement";
282  }
283  $capitalizedAgentName = strtoupper(substr($agentName, 0, 1)) . substr($agentName, 1);
284  $cxListMethod = "get" . $capitalizedAgentName . "List";
285  $decisionListMethod = "get" . $capitalizedAgentName . "DecisionList";
286  $eventListMethod = "get" . $capitalizedAgentName . "EventList";
287  $cxList = $reportData->$cxListMethod();
288  $decisionList = $reportData->$decisionListMethod();
289  $eventList = $reportData->$eventListMethod();
290 
291  if (!$cxList and !$decisionList and !$eventList) {
292  // No relevant data in the report - nothing to do
293  return;
294  }
295 
296  if (!$this->agentDao->arsTableExists($agentName)) {
297  // FIXME This requires the user to manually run the respective agent to get past this point
298  throw new UnexpectedValueException("No agent '$agentName' exists on server.");
299  }
300  $latestAgentId = $this->agentDao->getCurrentAgentId($agentName);
301  $this->createCxJobs($agentName, $jobId, $latestAgentId);
302  $cxExistSql = "SELECT " . $agentName . "_pk FROM $agentName WHERE pfile_fk = $1 AND agent_fk = $2 AND hash = $3;";
303  $cxExistStatement = __METHOD__ . ".$agentName" . "Exist";
304 
305  $ceExistSql = "SELECT " . $agentName . "_event_pk FROM " . $agentName .
306  "_event WHERE uploadtree_fk = $1 AND hash = $2 AND is_enabled = $3;";
307  $ceExistStatement = __METHOD__ . ".$agentName" . "EventExist";
308 
309  $i = 0;
310  foreach ($cxList as $oldId => $cItem) {
311  $newCp = $this->dbManager->getSingleRow($cxExistSql, [
312  $cItem["new_pfile"],
313  $latestAgentId,
314  $cItem["hash"]
315  ], $cxExistStatement);
316  if (empty($newCp) && !empty($cItem["new_pfile"]) && !empty($cItem["hash"])) {
317  $newCp = $this->dbManager->insertTableRow($agentName, [
318  "agent_fk" => $latestAgentId,
319  "pfile_fk" => $cItem["new_pfile"],
320  "content" => $cItem["content"],
321  "type" => $type,
322  "hash" => $cItem["hash"],
323  "copy_startbyte" => $cItem["copy_startbyte"],
324  "copy_endbyte" => $cItem["copy_endbyte"]
325  ], __METHOD__ . ".insertCp." . $agentName, $agentName . "_pk");
326  } else {
327  $newCp = $newCp[$agentName . "_pk"];
328  }
329  $cxList[$oldId]["new_id"] = $newCp;
330  $i++;
333  $i = 0;
334  }
335  }
336  if ($i != 0) {
337  $agentObj->heartbeat($i);
338  }
339 
340  $i = 0;
341  foreach ($decisionList as $oldId => $decisionItem) {
342  $newDecision = $this->copyrightDao->saveDecision($agentName . "_decision", $decisionItem['new_pfile'],
343  $this->userId, $decisionItem['clearing_decision_type_fk'], $decisionItem['description'],
344  $decisionItem['textfinding'], $decisionItem['comment']);
345  $decisionList[$oldId]["new_id"] = $newDecision;
346  $i++;
349  $i = 0;
350  }
351  }
352  if ($i != 0) {
353  $agentObj->heartbeat($i);
354  }
355 
356  $i = 0;
357  foreach ($eventList as $eventItem) {
358  if ($eventItem["new_itemid"] == null) {
359  echo "ItemId of event hash " . $eventItem["hash"] . " is NULL\n";
360  continue;
361  }
362  $ceId = $this->dbManager->getSingleRow($ceExistSql, [
363  $eventItem["new_itemid"],
364  $eventItem["hash"],
365  $eventItem["is_enabled"]
366  ], $ceExistStatement);
367  if (empty($ceId)) {
368  $this->dbManager->insertTableRow($agentName . "_event", [
369  "upload_fk" => $this->uploadId,
370  "uploadtree_fk" => $eventItem["new_itemid"],
371  $agentName . "_fk" => $cxList[$eventItem["old_cpid"]]["new_id"],
372  "content" => $eventItem["content"],
373  "hash" => $eventItem["hash"],
374  "is_enabled" => $eventItem["is_enabled"],
375  "scope" => $eventItem["scope"]
376  ], __METHOD__ . ".insertCe." . $agentName);
377  }
378  $i++;
381  $i = 0;
382  }
383  }
384  if ($i != 0) {
385  $agentObj->heartbeat($i);
386  }
387 
388  $cxListMethod = "set" . $capitalizedAgentName . "List";
389  $reportData->$cxListMethod($cxList);
390  }
391 
400  private function createCxJobs(string $agentName, int $currentJobId, int $agentId): void
401  {
402  $scanJobProxy = new ScanJobProxy($this->agentDao, $this->uploadId);
403  $scanJobProxy->createAgentStatus(array($agentName));
404  $selectedScanners = $scanJobProxy->getLatestSuccessfulAgentIds();
405  if (!empty($selectedScanners)) {
406  return;
407  }
408  $markJobCompletedSql = "UPDATE jobqueue SET jq_end_bits = 1, jq_endtext = 'Completed', jq_endtime = NOW(), " .
409  "jq_starttime = NOW() WHERE jq_pk = $1;";
410  $markJobCompletedStatement = __METHOD__ . ".markComplete";
411 
412  $jqId = JobQueueAdd($currentJobId, $agentName, $this->uploadId, "no", NULL);
413  $this->dbManager->getSingleRow($markJobCompletedSql, [$jqId], $markJobCompletedStatement);
414 
415  $arsId = $this->agentDao->writeArsRecord($agentName, $agentId, $this->uploadId);
416  $this->agentDao->writeArsRecord($agentName, $agentId, $this->uploadId, $arsId, true);
417  }
418 
429  public function createMonkBulkData(FoDecisionData &$reportData,
430  DecisionImporterAgent &$agentObj): void
431  {
432  $licenseRefBulkList = $reportData->getLicenseRefBulkList();
433  $licenseSetBulkList = $reportData->getLicenseSetBulkList();
434  $highlightBulkList = $reportData->getHighlightBulk();
435  $clearingEventList = $reportData->getClearingEventList();
436 
437  // NOTE: Always run monkbulk on entire upload even if it was set for a folder, as it is done in reuser with bulk.
438  // BulkReuser::rerunBulkAndDeciderOnUpload() ¯\_(ツ)_/¯
439  $parentItem = $this->uploadDao->getUploadParent($this->uploadId);
440 
441  $i = 0;
442  foreach ($licenseRefBulkList as $oldId => $licenseRefBulkItem) {
443  $oldEventId = $this->getClearingEventForLrb($clearingEventList, $oldId);
444  if (empty($oldEventId)) {
445  echo "No clearing event for old LRB: $oldId\n";
446  continue;
447  }
448  $licenseRemovals = [];
449  foreach ($licenseSetBulkList[$oldId] as $licenseSetItem) {
450  $licenseRemovals[$licenseSetItem["new_rfid"]] = [
451  $licenseSetItem["removing"],
452  $licenseSetItem["comment"],
453  $licenseSetItem["reportinfo"],
454  $licenseSetItem["acknowledgement"],
455  ];
456  }
457 
458  $newLrbId = $this->licenseDao->insertBulkLicense($this->userId, $this->groupId, $parentItem, $licenseRemovals,
459  $licenseRefBulkItem["rf_text"], $licenseRefBulkItem["ignore_irrelevant"],
460  $licenseRefBulkItem["bulk_delimiters"], $licenseRefBulkItem["scan_findings"]);
461  $licenseRefBulkList[$oldId]["new_lrbid"] = $newLrbId;
462  $jobId = $this->createMonkBulkJobs($newLrbId);
463  $clearingEventList[$oldEventId]["new_lrbid"] = $newLrbId;
464  $clearingEventList[$oldEventId]["job_fk"] = $jobId;
465  $i++;
468  $i = 0;
469  }
470  }
471  if ($i != 0) {
472  $agentObj->heartbeat($i);
473  }
474 
475  $i = 0;
476  foreach ($clearingEventList as $clearingEventItem) {
477  if (array_key_exists("job_fk", $clearingEventItem)) {
478  $assocParams = [
479  "job_fk" => $clearingEventItem["job_fk"]
480  ];
481  $this->dbManager->updateTableRow("clearing_event", $assocParams, "clearing_event_pk",
482  $clearingEventItem["new_event"], __METHOD__ . ".updateCeJob");
483  $i++;
486  $i = 0;
487  }
488  }
489  }
490  if ($i != 0) {
491  $agentObj->heartbeat($i);
492  }
493 
494  $i = 0;
495  foreach ($highlightBulkList as $highlightBulkItem) {
496  $assocParams = [
497  "clearing_event_fk" => $clearingEventList[$highlightBulkItem["old_ceid"]]["new_event"],
498  "lrb_fk" => $licenseRefBulkList[$highlightBulkItem["old_lrbid"]]["new_lrbid"],
499  "start" => $highlightBulkItem["start"],
500  "len" => $highlightBulkItem["len"]
501  ];
502  $this->dbManager->insertTableRow("highlight_bulk", $assocParams, __METHOD__ . ".insertHighlightBulk");
503  $i++;
505  $agentObj->heartbeat(0);
506  $i = 0;
507  }
508  }
509 
510  $reportData->setLicenseRefBulkList($licenseRefBulkList)
511  ->setClearingEventList($clearingEventList);
512  }
513 
520  private function getClearingEventForLrb(array $clearingEventList, int $oldLrbId)
521  {
522  foreach ($clearingEventList as $index => $clearingEventId) {
523  if ($clearingEventId["old_lrbid"] == $oldLrbId) {
524  return $index;
525  }
526  }
527  return null;
528  }
529 
536  private function createMonkBulkJobs(int $bulkId): int
537  {
538  $markJobCompletedSql = "UPDATE jobqueue SET jq_end_bits = 1, jq_endtext = 'Completed', jq_endtime = NOW(), " .
539  "jq_starttime = NOW() WHERE jq_pk = $1;";
540  $markJobCompletedStatement = __METHOD__ . ".markComplete";
541 
542  $upload = $this->uploadDao->getUpload($this->uploadId);
543  $uploadName = $upload->getFilename();
544  $latestDeciderAgentId = $this->agentDao->getCurrentAgentId("deciderjob");
545  $latestMonkBulkAgentId = $this->agentDao->getCurrentAgentId("monkbulk");
546  $jobId = JobAddJob($this->userId, $this->groupId, $uploadName, $this->uploadId);
547 
548  $monkbulkJobId = JobQueueAdd($jobId, "monkbulk", $bulkId, "no", null);
549  $deciderJobId = JobQueueAdd($jobId, "deciderjob", $this->uploadId, "no", [$monkbulkJobId]);
550 
551  $monkbulkArsId = $this->agentDao->writeArsRecord("monkbulk", $latestMonkBulkAgentId, $this->uploadId);
552  $this->agentDao->writeArsRecord("monkbulk", $latestDeciderAgentId, $this->uploadId, $monkbulkArsId, true);
553  $this->dbManager->getSingleRow($markJobCompletedSql, [$monkbulkJobId], $markJobCompletedStatement);
554 
555  $deciderArsId = $this->agentDao->writeArsRecord("deciderjob", $latestDeciderAgentId, $this->uploadId);
556  $this->agentDao->writeArsRecord("deciderjob", $latestDeciderAgentId, $this->uploadId, $deciderArsId, true);
557  $this->dbManager->getSingleRow($markJobCompletedSql, [$deciderJobId], $markJobCompletedStatement);
558 
559  return $jobId;
560  }
561 
568  public function createMainLicenses(FoDecisionData &$reportData, int $uploadId, int $groupId)
569  {
570  $mainLicenseList = $reportData->getMainLicenseList();
571  foreach ($mainLicenseList as $mainLicenseItem) {
572  $this->clearingDao->makeMainLicense($uploadId, $groupId, $mainLicenseItem["new_rfid"]);
573  }
574  }
575 }
Import decisions generated by Decision Exporter from JSON files.
createClearingDecisions(FoDecisionData &$reportData, DecisionImporterAgent &$agentObj)
createMainLicenses(FoDecisionData &$reportData, int $uploadId, int $groupId)
getClearingEventForLrb(array $clearingEventList, int $oldLrbId)
createCxJobs(string $agentName, int $currentJobId, int $agentId)
createCopyrightData(FoDecisionData &$reportData, DecisionImporterAgent &$agentObj, string $agentName, int $jobId)
Create copyright and sibling agent's related data in DB.
createMonkBulkData(FoDecisionData &$reportData, DecisionImporterAgent &$agentObj)
Create monkbulk findings from the report.
__construct(DbManager $dbManager, AgentDao $agentDao, CopyrightDao $copyrightDao, LicenseDao $licenseDao, UploadDao $uploadDao, ClearingDao $clearingDao)
setLicenseRefBulkList(array $license_ref_bulkList)
setClearingDecisionList(array $clearing_decisionList)
heartbeat($newProcessed)
Send hear beat to the scheduler.
Definition: Agent.php:203
JobQueueAdd($job_pk, $jq_type, $jq_args, $jq_runonpfile, $Depends, $host=NULL, $jq_cmd_args=NULL)
Insert a jobqueue + jobdepends records.
Definition: common-job.php:157
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16
FUNCTION char * strtoupper(char *s)
Helper function to upper case a string.
Definition: utils.c:39