FOSSology  4.5.0-rc1
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(), $autoConclude = false)
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,
173  $autoConclude ? ClearingEventTypes::AUTO : ClearingEventTypes::AGENT, $previousEvents);
174  foreach ($previousEvents as $clearingEvent) {
175  $clearingEventIds[$clearingEvent->getLicenseId()] = $clearingEvent->getEventId();
176  }
177  }
178 
179  $currentDecision = $this->clearingDao->getRelevantClearingDecision($itemBounds, $groupId);
180  $clearingEventIds = array_unique(array_merge($clearingEventIds, $additionalEventIds));
181 
182  $scope = $global ? DecisionScopes::REPO : DecisionScopes::ITEM;
183  if (null === $currentDecision || $this->clearingDecisionIsDifferentFrom($currentDecision, $type, $scope, $clearingEventIds)) {
184  $this->clearingDao->createDecisionFromEvents($itemBounds->getItemId(), $userId, $groupId, $type, $scope,
185  $clearingEventIds);
186  } else {
187  $this->clearingDao->removeWipClearingDecision($itemId, $groupId);
188  }
189 
190  $this->dbManager->commit();
191  }
192 
201  public function getCurrentClearings(ItemTreeBounds $itemTreeBounds, $groupId, $usageId=LicenseMap::TRIVIAL)
202  {
203  $agentEvents = $this->agentLicenseEventProcessor->getScannerEvents($itemTreeBounds, $usageId);
204  $events = $this->clearingDao->getRelevantClearingEvents($itemTreeBounds, $groupId);
205 
206  $addedResults = array();
207  $removedResults = array();
208 
209  foreach (array_unique(array_merge(array_keys($events), array_keys($agentEvents))) as $licenseId) {
210  $licenseDecisionEvent = array_key_exists($licenseId, $events) ? $events[$licenseId] : null;
211  $agentClearingEvents = array_key_exists($licenseId, $agentEvents) ? $agentEvents[$licenseId] : array();
212 
213  if (($licenseDecisionEvent === null) && (count($agentClearingEvents) == 0)) {
214  throw new Exception('not in merge');
215  }
216  $licenseDecisionResult = new ClearingResult($licenseDecisionEvent, $agentClearingEvents);
217  if ($licenseDecisionResult->isRemoved()) {
218  $removedResults[$licenseId] = $licenseDecisionResult;
219  } else {
220  $addedResults[$licenseId] = $licenseDecisionResult;
221  }
222  }
223 
224  return array($addedResults, $removedResults);
225  }
226 }
Utility functions to process ClearingDecision.
getCurrentClearings(ItemTreeBounds $itemTreeBounds, $groupId, $usageId=LicenseMap::TRIVIAL)
For a given item, get the clearing decisions.
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.
makeDecisionFromLastEvents(ItemTreeBounds $itemBounds, $userId, $groupId, $type, $global, $additionalEventIds=array(), $autoConclude=false)
Create clearing decisions from clearing events.
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.