FOSSology  4.4.0
Open Source License Compliance by Open Source Software
ClearingDecisionProcessor.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2015 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
9 
19 
25 {
31 
34  private $clearingDao;
43  private $dbManager;
44 
53  {
54  $this->clearingDao = $clearingDao;
55  $this->agentLicenseEventProcessor = $agentLicenseEventProcessor;
56  $this->clearingEventProcessor = $clearingEventProcessor;
57  $this->dbManager = $dbManager;
58  }
59 
70  public function hasUnhandledScannerDetectedLicenses(ItemTreeBounds $itemTreeBounds, $groupId, $additionalEventIds = array(), $licenseMap=null)
71  {
72  if (!empty($licenseMap) && !($licenseMap instanceof LicenseMap)) {
73  throw new Exception('invalid license map');
74  }
75  $userEvents = $this->clearingDao->getRelevantClearingEvents($itemTreeBounds, $groupId);
76  $usageId = empty($licenseMap) ? LicenseMap::TRIVIAL : $licenseMap->getUsage();
77  $scannerDetectedEvents = $this->agentLicenseEventProcessor->getScannerEvents($itemTreeBounds,$usageId);
78  $eventLicenceIds = array();
79  foreach (array_keys($userEvents) as $licenseId) {
80  $eventLicenceIds[empty($licenseMap)? $licenseId: $licenseMap->getProjectedId($licenseId)] = $licenseId;
81  }
82  foreach (array_keys($additionalEventIds) as $licenseId) {
83  $eventLicenceIds[empty($licenseMap)? $licenseId: $licenseMap->getProjectedId($licenseId)] = $licenseId;
84  }
85  foreach (array_keys($scannerDetectedEvents) as $licenseId) {
86  if (!array_key_exists(empty($licenseMap)? $licenseId: $licenseMap->getProjectedId($licenseId), $eventLicenceIds)) {
87  return true;
88  }
89  }
90  return false;
91  }
92 
103  private function insertClearingEventsForAgentFindings(ItemTreeBounds $itemBounds, $userId, $groupId, $remove = false, $type = ClearingEventTypes::AGENT, $removedIds = array())
104  {
105  $eventIds = array();
106  foreach ($this->agentLicenseEventProcessor->getScannerEvents($itemBounds) as $licenseId => $scannerEvents) {
107  if (array_key_exists($licenseId, $removedIds)) {
108  continue;
109  }
110  $scannerLicenseRef = $scannerEvents[0]->getLicenseRef();
111  $eventIds[$scannerLicenseRef->getId()] = $this->clearingDao->insertClearingEvent($itemBounds->getItemId(), $userId, $groupId, $scannerLicenseRef->getId(), $remove, $type);
112  }
113  return $eventIds;
114  }
115 
124  private function clearingDecisionIsDifferentFrom(ClearingDecision $decision, $type, $scope, $clearingEventIds)
125  {
126  $clearingEvents = $decision->getClearingEvents();
127  if (count($clearingEvents) != count($clearingEventIds)) {
128  return true;
129  }
130 
131  foreach ($clearingEvents as $clearingEvent) {
132  if (false === array_search($clearingEvent->getEventId(), $clearingEventIds)) {
133  return true;
134  }
135  }
136  return ($type !== $decision->getType()) || ($scope !== $decision->getScope());
137  }
138 
148  public function makeDecisionFromLastEvents(ItemTreeBounds $itemBounds, $userId, $groupId, $type, $global, $additionalEventIds = array())
149  {
150  if ($type < self::NO_LICENSE_KNOWN_DECISION_TYPE) {
151  return;
152  }
153  $this->dbManager->begin();
154 
155  $itemId = $itemBounds->getItemId();
156  $includeSubFolders = false;
157  if (($global == DecisionScopes::REPO) && ($type != self::NO_LICENSE_KNOWN_DECISION_TYPE)) {
158  $includeSubFolders = true;
159  }
160  $previousEvents = $this->clearingDao->getRelevantClearingEvents($itemBounds, $groupId, $includeSubFolders);
161  if ($type === self::NO_LICENSE_KNOWN_DECISION_TYPE) {
162  $type = DecisionTypes::IDENTIFIED;
163  $clearingEventIds = $this->insertClearingEventsForAgentFindings($itemBounds, $userId, $groupId, true, ClearingEventTypes::USER);
164  foreach ($previousEvents as $eventId => $clearingEvent) {
165  if (!in_array($eventId, $clearingEventIds) && !$clearingEvent->isRemoved()) {
166  $licenseId = $clearingEvent->getLicenseId();
167  $newEventId = $this->clearingDao->insertClearingEvent($itemBounds->getItemId(), $userId, $groupId, $licenseId, true);
168  $clearingEventIds[$licenseId] = $newEventId;
169  }
170  }
171  } else {
172  $clearingEventIds = $this->insertClearingEventsForAgentFindings($itemBounds, $userId, $groupId, false, ClearingEventTypes::AGENT, $previousEvents);
173  foreach ($previousEvents as $clearingEvent) {
174  $clearingEventIds[$clearingEvent->getLicenseId()] = $clearingEvent->getEventId();
175  }
176  }
177 
178  $currentDecision = $this->clearingDao->getRelevantClearingDecision($itemBounds, $groupId);
179  $clearingEventIds = array_unique(array_merge($clearingEventIds, $additionalEventIds));
180 
181  $scope = $global ? DecisionScopes::REPO : DecisionScopes::ITEM;
182  if (null === $currentDecision || $this->clearingDecisionIsDifferentFrom($currentDecision, $type, $scope, $clearingEventIds)) {
183  $this->clearingDao->createDecisionFromEvents($itemBounds->getItemId(), $userId, $groupId, $type, $scope,
184  $clearingEventIds);
185  } else {
186  $this->clearingDao->removeWipClearingDecision($itemId, $groupId);
187  }
188 
189  $this->dbManager->commit();
190  }
191 
200  public function getCurrentClearings(ItemTreeBounds $itemTreeBounds, $groupId, $usageId=LicenseMap::TRIVIAL)
201  {
202  $agentEvents = $this->agentLicenseEventProcessor->getScannerEvents($itemTreeBounds, $usageId);
203  $events = $this->clearingDao->getRelevantClearingEvents($itemTreeBounds, $groupId);
204 
205  $addedResults = array();
206  $removedResults = array();
207 
208  foreach (array_unique(array_merge(array_keys($events), array_keys($agentEvents))) as $licenseId) {
209  $licenseDecisionEvent = array_key_exists($licenseId, $events) ? $events[$licenseId] : null;
210  $agentClearingEvents = array_key_exists($licenseId, $agentEvents) ? $agentEvents[$licenseId] : array();
211 
212  if (($licenseDecisionEvent === null) && (count($agentClearingEvents) == 0)) {
213  throw new Exception('not in merge');
214  }
215  $licenseDecisionResult = new ClearingResult($licenseDecisionEvent, $agentClearingEvents);
216  if ($licenseDecisionResult->isRemoved()) {
217  $removedResults[$licenseId] = $licenseDecisionResult;
218  } else {
219  $addedResults[$licenseId] = $licenseDecisionResult;
220  }
221  }
222 
223  return array($addedResults, $removedResults);
224  }
225 }
Utility functions to process ClearingDecision.
getCurrentClearings(ItemTreeBounds $itemTreeBounds, $groupId, $usageId=LicenseMap::TRIVIAL)
For a given item, get the clearing decisions.
makeDecisionFromLastEvents(ItemTreeBounds $itemBounds, $userId, $groupId, $type, $global, $additionalEventIds=array())
Create clearing decisions from clearing events.
insertClearingEventsForAgentFindings(ItemTreeBounds $itemBounds, $userId, $groupId, $remove=false, $type=ClearingEventTypes::AGENT, $removedIds=array())
Insert clearing events in DB for agent findings.
__construct(ClearingDao $clearingDao, AgentLicenseEventProcessor $agentLicenseEventProcessor, ClearingEventProcessor $clearingEventProcessor, DbManager $dbManager)
hasUnhandledScannerDetectedLicenses(ItemTreeBounds $itemTreeBounds, $groupId, $additionalEventIds=array(), $licenseMap=null)
clearingDecisionIsDifferentFrom(ClearingDecision $decision, $type, $scope, $clearingEventIds)
Check if clearing decisions are different from clearing event ids.
Wrapper class for license map.
Definition: LicenseMap.php:19
Fossology exception.
Definition: Exception.php:15
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16
Contains business rules for FOSSology.