FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
ObligationMap.php
1 <?php
2 /*
3 Copyright (C) 2017, 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 
22 
28 {
29 
32  private $dbManager;
33 
38  public function __construct(DbManager $dbManager)
39  {
40  $this->dbManager = $dbManager;
41  }
42 
48  public function getAvailableShortnames($candidate=false)
49  {
50  if ($candidate) {
51  $sql = "SELECT rf_shortname FROM license_candidate;";
52  $stmt = __METHOD__.".rf_candidate_shortnames";
53  } else {
54  $sql = "SELECT rf_shortname FROM ONLY license_ref;";
55  $stmt = __METHOD__.".rf_shortnames";
56  }
57  $this->dbManager->prepare($stmt,$sql);
58  $res = $this->dbManager->execute($stmt);
59  $vars = $this->dbManager->fetchAll($res);
60  $this->dbManager->freeResult($res);
61 
62  $licshortnames = array();
63  foreach ($vars as $rf_entry) {
64  $shortname = $rf_entry['rf_shortname'];
65  $licshortnames[$shortname] = $shortname;
66  }
67 
68  return $licshortnames;
69  }
70 
77  public function getIdFromShortname($shortname,$candidate=false)
78  {
79  $tableName = "";
80  if ($candidate) {
81  $tableName = "license_candidate";
82  } else {
83  $tableName = "license_ref";
84  }
85  $sql = "SELECT * FROM ONLY $tableName WHERE rf_shortname = $1;";
86  $statement = __METHOD__ . ".getLicId.$tableName";
87  $results = $this->dbManager->getRows($sql, array($shortname), $statement);
88  $licenseIds = array();
89  foreach ($results as $row) {
90  $licenseIds[] = $row['rf_pk'];
91  }
92  return $licenseIds;
93  }
94 
101  public function getShortnameFromId($rfId,$candidate=false)
102  {
103  if ($candidate) {
104  $sql = "SELECT * FROM ONLY license_candidate WHERE rf_pk = $1;";
105  } else {
106  $sql = "SELECT * FROM ONLY license_ref WHERE rf_pk = $1;";
107  }
108  $statement = __METHOD__ . "." . ($candidate ? "candidate" : "license");
109  $result = $this->dbManager->getSingleRow($sql,array($rfId), $statement);
110  return $result['rf_shortname'];
111  }
112 
119  public function getLicenseList($obId,$candidate=false)
120  {
121  $liclist = array();
122  if ($candidate) {
123  $sql = "SELECT rf_fk FROM obligation_candidate_map WHERE ob_fk=$1;";
124  $stmt = __METHOD__.".om_candidate";
125  } else {
126  $sql = "SELECT rf_fk FROM obligation_map WHERE ob_fk=$1;";
127  $stmt = __METHOD__.".om_license";
128  }
129  $this->dbManager->prepare($stmt,$sql);
130  $res = $this->dbManager->execute($stmt, array($obId));
131  $vars = $this->dbManager->fetchAll($res);
132  $this->dbManager->freeResult($res);
133  foreach ($vars as $map_entry) {
134  $liclist[] = $this->getShortnameFromId($map_entry['rf_fk'], $candidate);
135  }
136 
137  return join(";", array_unique($liclist));
138  }
139 
147  public function isLicenseAssociated($obId,$licId,$candidate=false)
148  {
149  $tableName = "";
150  if ($candidate) {
151  $stmt = __METHOD__.".om_testcandidate";
152  $tableName .= "obligation_candidate_map";
153  } else {
154  $stmt = __METHOD__.".om_testlicense";
155  $tableName .= "obligation_map";
156  }
157  $sql = "SELECT * FROM $tableName WHERE ob_fk = $1 AND rf_fk = $2;";
158  $this->dbManager->prepare($stmt,$sql);
159  $res = $this->dbManager->execute($stmt,array($obId,$licId));
160  $vars = $this->dbManager->fetchAll($res);
161  $this->dbManager->freeResult($res);
162 
163  if (!empty($vars)) {
164  return true;
165  }
166 
167  return false;
168  }
169 
176  public function associateLicenseWithObligation($obId,$licId,$candidate=false)
177  {
178  if (! $this->isLicenseAssociated($obId, $licId, $candidate)) {
179  if ($candidate) {
180  $sql = "INSERT INTO obligation_candidate_map (ob_fk, rf_fk) VALUES ($1, $2)";
181  $stmt = __METHOD__ . ".om_addcandidate";
182  } else {
183  $sql = "INSERT INTO obligation_map (ob_fk, rf_fk) VALUES ($1, $2)";
184  $stmt = __METHOD__ . ".om_addlicense";
185  }
186  $this->dbManager->prepare($stmt, $sql);
187  $res = $this->dbManager->execute($stmt, array($obId,$licId));
188  $this->dbManager->fetchArray($res);
189  $this->dbManager->freeResult($res);
190  }
191  }
192 
199  public function unassociateLicenseFromObligation($obId,$licId=0,$candidate=false)
200  {
201  if ($licId == 0) {
202  $stmt = __METHOD__.".omdel_all";
203  if ($candidate) {
204  $sql = "DELETE FROM obligation_candidate_map WHERE ob_fk=$1";
205  $stmt .= ".candidate";
206  } else {
207  $sql = "DELETE FROM obligation_map WHERE ob_fk=$1";
208  }
209  $this->dbManager->prepare($stmt,$sql);
210  $res = $this->dbManager->execute($stmt,array($obId));
211  } else {
212  $stmt = __METHOD__.".omdel_lic";
213  if ($candidate) {
214  $sql = "DELETE FROM obligation_candidate_map WHERE ob_fk=$1 AND rf_fk=$2";
215  $stmt .= ".candidate";
216  } else {
217  $sql = "DELETE FROM obligation_map WHERE ob_fk=$1 AND rf_fk=$2";
218  }
219  $this->dbManager->prepare($stmt,$sql);
220  $res = $this->dbManager->execute($stmt,array($obId,$licId));
221  }
222  $this->dbManager->fetchArray($res);
223  $this->dbManager->freeResult($res);
224  }
225 
230  public function getObligations()
231  {
232  $sql = "SELECT * FROM obligation_ref;";
233  return $this->dbManager->getRows($sql);
234  }
235 
241  public function getTopicNameFromId($ob_pk)
242  {
243  $sql = "SELECT ob_topic FROM obligation_ref WHERE ob_pk = $1;";
244  $result = $this->dbManager->getSingleRow($sql,array($ob_pk));
245  return $result['ob_topic'];
246  }
247 
255  public function associateLicenseFromLicenseList($obligationId, $licenses, $candidate = false)
256  {
257  $updated = false;
258  foreach ($licenses as $license) {
259  if (! $this->isLicenseAssociated($obligationId, $license, $candidate)) {
260  $this->associateLicenseWithObligation($obligationId, $license, $candidate);
261  $updated = true;
262  }
263  }
264  return $updated;
265  }
266 
273  public function unassociateLicenseFromLicenseList($obligationId, $licenses, $candidate = false)
274  {
275  foreach ($licenses as $license) {
276  $this->unassociateLicenseFromObligation($obligationId, $license, $candidate);
277  }
278  }
279 }
getObligations()
Get all obligations from DB.
associateLicenseFromLicenseList($obligationId, $licenses, $candidate=false)
unassociateLicenseFromLicenseList($obligationId, $licenses, $candidate=false)
unassociateLicenseFromObligation($obId, $licId=0, $candidate=false)
Unassociate a license from an obligation.
associateLicenseWithObligation($obId, $licId, $candidate=false)
Associate a license with an obligation.
getAvailableShortnames($candidate=false)
Get the license id from the shortname.
Contains business rules for FOSSology.
getShortnameFromId($rfId, $candidate=false)
Get the shortname of the license by Id.
getIdFromShortname($shortname, $candidate=false)
Get the license ids from the shortname.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
getTopicNameFromId($ob_pk)
Get the obligation topic from the obligation id.
getLicenseList($obId, $candidate=false)
Get the list of licenses associated with the obligation.
isLicenseAssociated($obId, $licId, $candidate=false)
Check if the obligation is already associated with the license.
Wrapper class for obligation map.