FOSSology  3.2.0rc1
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  Copyright (C) 2014, Siemens AG
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License
8  version 2 as published by the Free Software Foundation.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
44 namespace Fossology\DeciderJob;
45 
55 
56 define("CLEARING_DECISION_IS_GLOBAL", false);
57 
58 include_once(__DIR__ . "/version.php");
59 
64 class DeciderJobAgent extends Agent
65 {
66  const FORCE_DECISION = 1;
67 
75  private $uploadDao;
87  private $clearingDao;
91  private $highlightDao;
95  private $decisionIsGlobal = CLEARING_DECISION_IS_GLOBAL;
99  private $decisionTypes;
103  private $licenseMap = null;
107  private $licenseMapUsage = null;
108 
109  function __construct($licenseMapUsage=null)
110  {
111  parent::__construct(AGENT_DECIDER_JOB_NAME, AGENT_DECIDER_JOB_VERSION, AGENT_DECIDER_JOB_REV);
112 
113  $this->uploadDao = $this->container->get('dao.upload');
114  $this->clearingDao = $this->container->get('dao.clearing');
115  $this->highlightDao = $this->container->get('dao.highlight');
116  $this->decisionTypes = $this->container->get('decision.types');
117  $this->clearingDecisionProcessor = $this->container->get('businessrules.clearing_decision_processor');
118  $this->agentLicenseEventProcessor = $this->container->get('businessrules.agent_license_event_processor');
119 
120  $this->agentSpecifOptions = "k:";
121  $this->licenseMapUsage = $licenseMapUsage;
122  }
123 
128  {
132 
133  $eventsOfThisJob = $this->clearingDao->getEventIdsOfJob($jobId);
134  foreach ($eventsOfThisJob as $uploadTreeId => $additionalEventsFromThisJob) {
135  $containerBounds = $this->uploadDao->getItemTreeBounds($uploadTreeId);
136  foreach ($this->loopContainedItems($containerBounds) as $itemTreeBounds) {
137  $this->processClearingEventsForItem($itemTreeBounds, $userId, $groupId, $additionalEventsFromThisJob);
138  }
139  }
140  }
141 
147  private function loopContainedItems($itemTreeBounds)
148  {
149  if (!$itemTreeBounds->containsFiles()) {
150  return array($itemTreeBounds);
151  }
152  $result = array();
153  $condition = "(ut.lft BETWEEN $1 AND $2) AND ((ut.ufile_mode & (3<<28)) = 0)";
154  $params = array($itemTreeBounds->getLeft(), $itemTreeBounds->getRight());
155  foreach ($this->uploadDao->getContainedItems($itemTreeBounds, $condition, $params) as $item) {
156  $result[] = $item->getItemTreeBounds();
157  }
158  return $result;
159  }
160 
165  function processUploadId($uploadId)
166  {
167  $args = $this->args;
168  $this->conflictStrategyId = array_key_exists('k', $args) ? $args['k'] : null;
169 
170  $this->licenseMap = new LicenseMap($this->dbManager, $this->groupId, $this->licenseMapUsage);
171 
172  if ($this->conflictStrategyId == 'global') {
173  $uploadTreeId = 0; // zero because we are checking candidate license for whole upload.
174  if (!empty($this->clearingDao->getCandidateLicenseCountForCurrentDecisions($uploadTreeId, $uploadId))) {
175  throw new \Exception( _("Cannot add candidate license as global decision\n") );
176  }
177  $this->heartbeat(1);
178  $this->heartbeat($this->clearingDao->marklocalDecisionsAsGlobal($uploadId));
179  } else {
181  }
182  return true;
183  }
184 
192  protected function processClearingEventsForItem(ItemTreeBounds $itemTreeBounds, $userId, $groupId, $additionalEventsFromThisJob)
193  {
194  $this->dbManager->begin();
195 
196  $itemId = $itemTreeBounds->getItemId();
197 
198  switch ($this->conflictStrategyId) {
199  case self::FORCE_DECISION:
200  $createDecision = true;
201  break;
202 
203  default:
204  $createDecision = !$this->clearingDecisionProcessor->hasUnhandledScannerDetectedLicenses($itemTreeBounds, $groupId, $additionalEventsFromThisJob, $this->licenseMap);
205  }
206 
207  if ($createDecision) {
208  $this->clearingDecisionProcessor->makeDecisionFromLastEvents($itemTreeBounds, $userId, $groupId, DecisionTypes::IDENTIFIED, $this->decisionIsGlobal, $additionalEventsFromThisJob);
209  } else {
210  foreach ($additionalEventsFromThisJob as $eventId) {
211  $this->clearingDao->copyEventIdTo($eventId, $itemId, $userId, $groupId);
212  }
213  $this->clearingDao->markDecisionAsWip($itemId, $userId, $groupId);
214  }
215  $this->heartbeat(1);
216 
217  $this->dbManager->commit();
218  }
219 }
heartbeat($newProcessed)
Send hear beat to the scheduler.
Definition: Agent.php:214
Namespace of DeciderJob agent.
Definition: deciderjob.php:20
processUploadId($uploadId)
Given an upload ID, process the items in it.
Structure of an Agent with all required parameters.
Definition: Agent.php:51
Wrapper class for license map.
Definition: LicenseMap.php:29
processClearingEventOfCurrentJob()
Process clearing events of current job handled by agent.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
loopContainedItems($itemTreeBounds)
Get items contained inside an item tree.
Get the decision from Monk bulk and apply decisions.
processClearingEventsForItem(ItemTreeBounds $itemTreeBounds, $userId, $groupId, $additionalEventsFromThisJob)
Get an item, process events and create new decisions.