FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
ClearingDecisionProcessor.php
1 <?php
2 /*
3 Copyright (C) 2014-2015, Siemens AG
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 
20 
30 
36 {
42 
45  private $clearingDao;
54  private $dbManager;
55 
64  {
65  $this->clearingDao = $clearingDao;
66  $this->agentLicenseEventProcessor = $agentLicenseEventProcessor;
67  $this->clearingEventProcessor = $clearingEventProcessor;
68  $this->dbManager = $dbManager;
69  }
70 
81  public function hasUnhandledScannerDetectedLicenses(ItemTreeBounds $itemTreeBounds, $groupId, $additionalEventIds = array(), $licenseMap=null)
82  {
83  if (!empty($licenseMap) && !($licenseMap instanceof LicenseMap)) {
84  throw new Exception('invalid license map');
85  }
86  $userEvents = $this->clearingDao->getRelevantClearingEvents($itemTreeBounds, $groupId);
87  $usageId = empty($licenseMap) ? LicenseMap::TRIVIAL : $licenseMap->getUsage();
88  $scannerDetectedEvents = $this->agentLicenseEventProcessor->getScannerEvents($itemTreeBounds,$usageId);
89  $eventLicenceIds = array();
90  foreach (array_keys($userEvents) as $licenseId) {
91  $eventLicenceIds[empty($licenseMap)? $licenseId: $licenseMap->getProjectedId($licenseId)] = $licenseId;
92  }
93  foreach (array_keys($additionalEventIds) as $licenseId) {
94  $eventLicenceIds[empty($licenseMap)? $licenseId: $licenseMap->getProjectedId($licenseId)] = $licenseId;
95  }
96  foreach (array_keys($scannerDetectedEvents) as $licenseId) {
97  if (!array_key_exists(empty($licenseMap)? $licenseId: $licenseMap->getProjectedId($licenseId), $eventLicenceIds)) {
98  return true;
99  }
100  }
101  return false;
102  }
103 
114  private function insertClearingEventsForAgentFindings(ItemTreeBounds $itemBounds, $userId, $groupId, $remove = false, $type = ClearingEventTypes::AGENT, $removedIds = array())
115  {
116  $eventIds = array();
117  foreach ($this->agentLicenseEventProcessor->getScannerEvents($itemBounds) as $licenseId => $scannerEvents) {
118  if (array_key_exists($licenseId, $removedIds)) {
119  continue;
120  }
121  $scannerLicenseRef = $scannerEvents[0]->getLicenseRef();
122  $eventIds[$scannerLicenseRef->getId()] = $this->clearingDao->insertClearingEvent($itemBounds->getItemId(), $userId, $groupId, $scannerLicenseRef->getId(), $remove, $type);
123  }
124  return $eventIds;
125  }
126 
135  private function clearingDecisionIsDifferentFrom(ClearingDecision $decision, $type, $scope, $clearingEventIds)
136  {
137  $clearingEvents = $decision->getClearingEvents();
138  if (count($clearingEvents) != count($clearingEventIds)) {
139  return true;
140  }
141 
142  foreach ($clearingEvents as $clearingEvent) {
143  if (false === array_search($clearingEvent->getEventId(), $clearingEventIds)) {
144  return true;
145  }
146  }
147  return ($type !== $decision->getType()) || ($scope !== $decision->getScope());
148  }
149 
159  public function makeDecisionFromLastEvents(ItemTreeBounds $itemBounds, $userId, $groupId, $type, $global, $additionalEventIds = array())
160  {
161  if ($type < self::NO_LICENSE_KNOWN_DECISION_TYPE) {
162  return;
163  }
164  $this->dbManager->begin();
165 
166  $itemId = $itemBounds->getItemId();
167 
168  $previousEvents = $this->clearingDao->getRelevantClearingEvents($itemBounds, $groupId, $includeSubFolders=false);
169  if ($type === self::NO_LICENSE_KNOWN_DECISION_TYPE) {
170  $type = DecisionTypes::IDENTIFIED;
171  $clearingEventIds = $this->insertClearingEventsForAgentFindings($itemBounds, $userId, $groupId, true, ClearingEventTypes::USER);
172  foreach ($previousEvents as $eventId => $clearingEvent) {
173  if (!in_array($eventId, $clearingEventIds) && !$clearingEvent->isRemoved()) {
174  $licenseId = $clearingEvent->getLicenseId();
175  $newEventId = $this->clearingDao->insertClearingEvent($itemBounds->getItemId(), $userId, $groupId, $licenseId, true);
176  $clearingEventIds[$licenseId] = $newEventId;
177  }
178  }
179  } else {
180  $clearingEventIds = $this->insertClearingEventsForAgentFindings($itemBounds, $userId, $groupId, false, ClearingEventTypes::AGENT, $previousEvents);
181  foreach ($previousEvents as $clearingEvent) {
182  $clearingEventIds[$clearingEvent->getLicenseId()] = $clearingEvent->getEventId();
183  }
184  }
185 
186  $currentDecision = $this->clearingDao->getRelevantClearingDecision($itemBounds, $groupId);
187  $clearingEventIds = array_unique(array_merge($clearingEventIds, $additionalEventIds));
188 
189  $scope = $global ? DecisionScopes::REPO : DecisionScopes::ITEM;
190  if (null === $currentDecision || $this->clearingDecisionIsDifferentFrom($currentDecision, $type, $scope, $clearingEventIds)) {
191  $this->clearingDao->createDecisionFromEvents($itemBounds->getItemId(), $userId, $groupId, $type, $scope,
192  $clearingEventIds);
193  } else {
194  $this->clearingDao->removeWipClearingDecision($itemId, $groupId);
195  }
196 
197  $this->dbManager->commit();
198  }
199 
208  public function getCurrentClearings(ItemTreeBounds $itemTreeBounds, $groupId, $usageId=LicenseMap::TRIVIAL)
209  {
210  $agentEvents = $this->agentLicenseEventProcessor->getScannerEvents($itemTreeBounds, $usageId);
211  $events = $this->clearingDao->getRelevantClearingEvents($itemTreeBounds, $groupId);
212 
213  $addedResults = array();
214  $removedResults = array();
215 
216  foreach (array_unique(array_merge(array_keys($events), array_keys($agentEvents))) as $licenseId) {
217  $licenseDecisionEvent = array_key_exists($licenseId, $events) ? $events[$licenseId] : null;
218  $agentClearingEvents = array_key_exists($licenseId, $agentEvents) ? $agentEvents[$licenseId] : array();
219 
220  if (($licenseDecisionEvent === null) && (count($agentClearingEvents) == 0)) {
221  throw new Exception('not in merge');
222  }
223  $licenseDecisionResult = new ClearingResult($licenseDecisionEvent, $agentClearingEvents);
224  if ($licenseDecisionResult->isRemoved()) {
225  $removedResults[$licenseId] = $licenseDecisionResult;
226  } else {
227  $addedResults[$licenseId] = $licenseDecisionResult;
228  }
229  }
230 
231  return array($addedResults, $removedResults);
232  }
233 }
Utility functions to process ClearingDecision.
makeDecisionFromLastEvents(ItemTreeBounds $itemBounds, $userId, $groupId, $type, $global, $additionalEventIds=array())
Create clearing decisions from clearing events.
__construct(ClearingDao $clearingDao, AgentLicenseEventProcessor $agentLicenseEventProcessor, ClearingEventProcessor $clearingEventProcessor, DbManager $dbManager)
clearingDecisionIsDifferentFrom(ClearingDecision $decision, $type, $scope, $clearingEventIds)
Check if clearing decisions are different from clearing event ids.
getCurrentClearings(ItemTreeBounds $itemTreeBounds, $groupId, $usageId=LicenseMap::TRIVIAL)
For a given item, get the clearing decisions.
Wrapper class for license map.
Definition: LicenseMap.php:29
Contains business rules for FOSSology.
hasUnhandledScannerDetectedLicenses(ItemTreeBounds $itemTreeBounds, $groupId, $additionalEventIds=array(), $licenseMap=null)
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
Fossology exception.
Definition: Exception.php:25
insertClearingEventsForAgentFindings(ItemTreeBounds $itemBounds, $userId, $groupId, $remove=false, $type=ClearingEventTypes::AGENT, $removedIds=array())
Insert clearing events in DB for agent findings.