FOSSology  4.4.0
Open Source License Compliance by Open Source Software
DeciderJobAgent.php
Go to the documentation of this file.
1 <?php
2 /*
3  Author: Daniele Fognini
4  SPDX-FileCopyrightText: © 2014 Siemens AG
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
33 namespace Fossology\DeciderJob;
34 
44 
45 define("CLEARING_DECISION_IS_GLOBAL", false);
46 
47 include_once(__DIR__ . "/version.php");
48 
53 class DeciderJobAgent extends Agent
54 {
55  const FORCE_DECISION = 1;
56 
64  private $uploadDao;
76  private $clearingDao;
80  private $highlightDao;
84  private $decisionIsGlobal = CLEARING_DECISION_IS_GLOBAL;
88  private $decisionTypes;
92  private $licenseMap = null;
96  private $licenseMapUsage = null;
97 
98  function __construct($licenseMapUsage=null)
99  {
100  parent::__construct(AGENT_DECIDER_JOB_NAME, AGENT_DECIDER_JOB_VERSION, AGENT_DECIDER_JOB_REV);
101 
102  $this->uploadDao = $this->container->get('dao.upload');
103  $this->clearingDao = $this->container->get('dao.clearing');
104  $this->highlightDao = $this->container->get('dao.highlight');
105  $this->decisionTypes = $this->container->get('decision.types');
106  $this->clearingDecisionProcessor = $this->container->get('businessrules.clearing_decision_processor');
107  $this->agentLicenseEventProcessor = $this->container->get('businessrules.agent_license_event_processor');
108 
109  $this->agentSpecifOptions = "k:";
110  $this->licenseMapUsage = $licenseMapUsage;
111  }
112 
117  {
121 
122  $eventsOfThisJob = $this->clearingDao->getEventIdsOfJob($jobId);
123  foreach ($eventsOfThisJob as $uploadTreeId => $additionalEventsFromThisJob) {
124  $containerBounds = $this->uploadDao->getItemTreeBounds($uploadTreeId);
125  foreach ($this->loopContainedItems($containerBounds) as $itemTreeBounds) {
126  $this->processClearingEventsForItem($itemTreeBounds, $userId, $groupId, $additionalEventsFromThisJob);
127  }
128  }
129  }
130 
136  private function loopContainedItems($itemTreeBounds)
137  {
138  if (!$itemTreeBounds->containsFiles()) {
139  return array($itemTreeBounds);
140  }
141  $result = array();
142  $condition = "(ut.lft BETWEEN $1 AND $2) AND ((ut.ufile_mode & (3<<28)) = 0)";
143  $params = array($itemTreeBounds->getLeft(), $itemTreeBounds->getRight());
144  foreach ($this->uploadDao->getContainedItems($itemTreeBounds, $condition, $params) as $item) {
145  $result[] = $item->getItemTreeBounds();
146  }
147  return $result;
148  }
149 
154  function processUploadId($uploadId)
155  {
156  $args = $this->args;
157  $this->conflictStrategyId = array_key_exists('k', $args) ? $args['k'] : null;
158 
159  $this->licenseMap = new LicenseMap($this->dbManager, $this->groupId, $this->licenseMapUsage);
160 
161  if ($this->conflictStrategyId == 'global') {
162  $uploadTreeId = 0; // zero because we are checking candidate license for whole upload.
163  if (!empty($this->clearingDao->getCandidateLicenseCountForCurrentDecisions($uploadTreeId, $uploadId))) {
164  throw new \Exception( _("Cannot add candidate license as global decision\n") );
165  }
166  $this->heartbeat(1);
167  $this->heartbeat($this->clearingDao->marklocalDecisionsAsGlobal($uploadId));
168  } else {
170  }
171  return true;
172  }
173 
181  protected function processClearingEventsForItem(ItemTreeBounds $itemTreeBounds, $userId, $groupId, $additionalEventsFromThisJob)
182  {
183  $this->dbManager->begin();
184 
185  $itemId = $itemTreeBounds->getItemId();
186 
187  switch ($this->conflictStrategyId) {
188  case self::FORCE_DECISION:
189  $createDecision = true;
190  break;
191 
192  default:
193  $createDecision = !$this->clearingDecisionProcessor->hasUnhandledScannerDetectedLicenses($itemTreeBounds, $groupId, $additionalEventsFromThisJob, $this->licenseMap);
194  }
195 
196  if ($createDecision) {
197  $this->clearingDecisionProcessor->makeDecisionFromLastEvents($itemTreeBounds, $userId, $groupId, DecisionTypes::IDENTIFIED, $this->decisionIsGlobal, $additionalEventsFromThisJob);
198  } else {
199  foreach ($additionalEventsFromThisJob as $eventId) {
200  $this->clearingDao->copyEventIdTo($eventId, $itemId, $userId, $groupId);
201  }
202  $this->clearingDao->markDecisionAsWip($itemId, $userId, $groupId);
203  }
204  $this->heartbeat(1);
205 
206  $this->dbManager->commit();
207  }
208 }
Get the decision from Monk bulk and apply decisions.
processUploadId($uploadId)
Given an upload ID, process the items in it.
processClearingEventOfCurrentJob()
Process clearing events of current job handled by agent.
loopContainedItems($itemTreeBounds)
Get items contained inside an item tree.
processClearingEventsForItem(ItemTreeBounds $itemTreeBounds, $userId, $groupId, $additionalEventsFromThisJob)
Get an item, process events and create new decisions.
Structure of an Agent with all required parameters.
Definition: Agent.php:41
heartbeat($newProcessed)
Send hear beat to the scheduler.
Definition: Agent.php:203
Utility functions to process ClearingDecision.
Wrapper class for license map.
Definition: LicenseMap.php:19
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16
Namespace of DeciderJob agent.
Definition: deciderjob.php:9