FOSSology  4.4.0
Open Source License Compliance by Open Source Software
ObligationMap.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2017 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
9 
11 
17 {
18 
21  private $dbManager;
22 
27  public function __construct(DbManager $dbManager)
28  {
29  $this->dbManager = $dbManager;
30  }
31 
41  public function getAvailableShortnames($candidate=false)
42  {
43  $params = [];
44  if ($candidate) {
45  $sql = "SELECT rf_shortname FROM license_candidate;";
46  $stmt = __METHOD__.".rf_candidate_shortnames";
47  } else {
49  $stmt = __METHOD__.".rf_shortnames";
50  $params[] = LicenseMap::CONCLUSION;
51  }
52  $this->dbManager->prepare($stmt,$sql);
53  $res = $this->dbManager->execute($stmt, $params);
54  $vars = $this->dbManager->fetchAll($res);
55  $this->dbManager->freeResult($res);
56 
57  $licshortnames = array();
58  foreach ($vars as $rf_entry) {
59  $shortname = $rf_entry['rf_shortname'];
60  $licshortnames[$shortname] = $shortname;
61  }
62 
63  return $licshortnames;
64  }
65 
72  public function getIdFromShortname($shortname, $candidate=false)
73  {
74  $tableName = "";
75  if ($candidate) {
76  $tableName = "license_candidate";
77  } else {
78  $tableName = "license_ref";
79  }
80  $sql = "SELECT * FROM ONLY $tableName WHERE rf_shortname = $1;";
81  $statement = __METHOD__ . ".getLicId.$tableName";
82  $results = $this->dbManager->getRows($sql, array($shortname), $statement);
83  $licenseIds = array();
84  foreach ($results as $row) {
85  $licenseIds[] = $row['rf_pk'];
86  }
87  return $licenseIds;
88  }
89 
96  public function getShortnameFromId($rfId,$candidate=false)
97  {
98  if ($candidate) {
99  $sql = "SELECT * FROM ONLY license_candidate WHERE rf_pk = $1;";
100  } else {
101  $sql = "SELECT * FROM ONLY license_ref WHERE rf_pk = $1;";
102  }
103  $statement = __METHOD__ . "." . ($candidate ? "candidate" : "license");
104  $result = $this->dbManager->getSingleRow($sql,array($rfId), $statement);
105  return $result['rf_shortname'];
106  }
107 
114  public function getLicenseList($obId,$candidate=false)
115  {
116  $liclist = array();
117  if ($candidate) {
118  $sql = "SELECT rf_fk FROM obligation_candidate_map WHERE ob_fk=$1;";
119  $stmt = __METHOD__.".om_candidate";
120  } else {
121  $sql = "SELECT rf_fk FROM obligation_map WHERE ob_fk=$1;";
122  $stmt = __METHOD__.".om_license";
123  }
124  $this->dbManager->prepare($stmt,$sql);
125  $res = $this->dbManager->execute($stmt, array($obId));
126  $vars = $this->dbManager->fetchAll($res);
127  $this->dbManager->freeResult($res);
128  foreach ($vars as $map_entry) {
129  $liclist[] = $this->getShortnameFromId($map_entry['rf_fk'], $candidate);
130  }
131 
132  return join(";", array_unique($liclist));
133  }
134 
142  public function isLicenseAssociated($obId,$licId,$candidate=false)
143  {
144  $tableName = "";
145  if ($candidate) {
146  $stmt = __METHOD__.".om_testcandidate";
147  $tableName .= "obligation_candidate_map";
148  } else {
149  $stmt = __METHOD__.".om_testlicense";
150  $tableName .= "obligation_map";
151  }
152  $sql = "SELECT * FROM $tableName WHERE ob_fk = $1 AND rf_fk = $2;";
153  $this->dbManager->prepare($stmt,$sql);
154  $res = $this->dbManager->execute($stmt,array($obId,$licId));
155  $vars = $this->dbManager->fetchAll($res);
156  $this->dbManager->freeResult($res);
157 
158  if (!empty($vars)) {
159  return true;
160  }
161 
162  return false;
163  }
164 
171  public function associateLicenseWithObligation($obId,$licId,$candidate=false)
172  {
173  if (! $this->isLicenseAssociated($obId, $licId, $candidate)) {
174  if ($candidate) {
175  $sql = "INSERT INTO obligation_candidate_map (ob_fk, rf_fk) VALUES ($1, $2)";
176  $stmt = __METHOD__ . ".om_addcandidate";
177  } else {
178  $sql = "INSERT INTO obligation_map (ob_fk, rf_fk) VALUES ($1, $2)";
179  $stmt = __METHOD__ . ".om_addlicense";
180  }
181  $this->dbManager->prepare($stmt, $sql);
182  $res = $this->dbManager->execute($stmt, array($obId,$licId));
183  $this->dbManager->fetchArray($res);
184  $this->dbManager->freeResult($res);
185  }
186  }
187 
194  public function unassociateLicenseFromObligation($obId,$licId=0,$candidate=false)
195  {
196  if ($licId == 0) {
197  $stmt = __METHOD__.".omdel_all";
198  if ($candidate) {
199  $sql = "DELETE FROM obligation_candidate_map WHERE ob_fk=$1";
200  $stmt .= ".candidate";
201  } else {
202  $sql = "DELETE FROM obligation_map WHERE ob_fk=$1";
203  }
204  $this->dbManager->prepare($stmt,$sql);
205  $res = $this->dbManager->execute($stmt,array($obId));
206  } else {
207  $stmt = __METHOD__.".omdel_lic";
208  if ($candidate) {
209  $sql = "DELETE FROM obligation_candidate_map WHERE ob_fk=$1 AND rf_fk=$2";
210  $stmt .= ".candidate";
211  } else {
212  $sql = "DELETE FROM obligation_map WHERE ob_fk=$1 AND rf_fk=$2";
213  }
214  $this->dbManager->prepare($stmt,$sql);
215  $res = $this->dbManager->execute($stmt,array($obId,$licId));
216  }
217  $this->dbManager->fetchArray($res);
218  $this->dbManager->freeResult($res);
219  }
220 
225  public function getObligations()
226  {
227  $sql = "SELECT * FROM obligation_ref;";
228  return $this->dbManager->getRows($sql);
229  }
230 
236  public function getTopicNameFromId($ob_pk)
237  {
238  $sql = "SELECT ob_topic FROM obligation_ref WHERE ob_pk = $1;";
239  $result = $this->dbManager->getSingleRow($sql,array($ob_pk));
240  return $result['ob_topic'];
241  }
242 
250  public function associateLicenseFromLicenseList($obligationId, $licenses, $candidate = false)
251  {
252  $updated = false;
253  foreach ($licenses as $license) {
254  if (! $this->isLicenseAssociated($obligationId, $license, $candidate)) {
255  $this->associateLicenseWithObligation($obligationId, $license, $candidate);
256  $updated = true;
257  }
258  }
259  return $updated;
260  }
261 
268  public function unassociateLicenseFromLicenseList($obligationId, $licenses, $candidate = false)
269  {
270  foreach ($licenses as $license) {
271  $this->unassociateLicenseFromObligation($obligationId, $license, $candidate);
272  }
273  }
274 
280  public function getObligationById($ob_pk)
281  {
282  $sql = "SELECT * FROM obligation_ref WHERE ob_pk = $1;";
283  return $this->dbManager->getSingleRow($sql, [$ob_pk]);
284  }
285 
292  public function deleteObligation($ob_pk)
293  {
294  $stmt = __METHOD__ . '.deleteObligation';
295  $sql = "DELETE FROM obligation_ref WHERE ob_pk = $1";
296  $this->dbManager->getSingleRow($sql, [$ob_pk], $stmt);
297 
298  $this->unassociateLicenseFromObligation($ob_pk);
299  $this->unassociateLicenseFromObligation($ob_pk, 0, true);
300  }
301 }
static getMappedLicenseRefView($usageExpr=' $1')
Query to get license map view along with license ref.
Definition: LicenseMap.php:191
Wrapper class for obligation map.
isLicenseAssociated($obId, $licId, $candidate=false)
Check if the obligation is already associated with the license.
getObligations()
Get all obligations from DB.
getAvailableShortnames($candidate=false)
Get the list of license shortnames.
getShortnameFromId($rfId, $candidate=false)
Get the shortname of the license by Id.
getIdFromShortname($shortname, $candidate=false)
Get the license ids from the shortname.
associateLicenseFromLicenseList($obligationId, $licenses, $candidate=false)
unassociateLicenseFromLicenseList($obligationId, $licenses, $candidate=false)
getLicenseList($obId, $candidate=false)
Get the list of licenses associated with the obligation.
unassociateLicenseFromObligation($obId, $licId=0, $candidate=false)
Unassociate a license from an obligation.
getTopicNameFromId($ob_pk)
Get the obligation topic from the obligation id.
associateLicenseWithObligation($obId, $licId, $candidate=false)
Associate a license with an obligation.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16
Contains business rules for FOSSology.