8 namespace Fossology\Lib\Dao;
28 lrf.rf_shortname AS first_rf_shortname,
29 lrs.rf_shortname AS second_rf_shortname, first_type, second_type,
30 comment, compatibility
32 LEFT JOIN license_ref lrf ON lrf.rf_pk = first_rf_fk
33 LEFT JOIN license_ref lrs ON lrs.rf_pk = second_rf_fk";
44 private $defaultCompatibility;
51 $this->logger = $logger;
52 $this->licenseDao = $licenseDao;
53 $this->agentDao = $agentDao;
54 $this->agentName =
"compatibility";
55 $sql =
"SELECT compatibility FROM license_rules
56 WHERE first_rf_fk IS NULL AND second_rf_fk IS NULL
57 AND first_type IS NULL AND second_type IS NULL;";
58 $result = $this->
dbManager->getSingleRow($sql, [], __METHOD__ .
60 if (!empty($result) && array_key_exists(
"compatibility", $result)) {
61 $this->defaultCompatibility = $result[
"compatibility"];
63 $this->defaultCompatibility =
false;
75 public function getCompatibilityForFile($itemTreeBounds, $shortname)
77 $uploadTreeTableName = $itemTreeBounds->getUploadTreeTableName();
78 $uploadTreePk = $itemTreeBounds->getItemId();
79 $stmt = __METHOD__.$uploadTreeTableName;
80 $sql =
"SELECT pfile_fk FROM $uploadTreeTableName UT
81 WHERE UT.uploadtree_pk = $1";
82 $result = $this->
dbManager->getSingleRow($sql, [$uploadTreePk], $stmt);
83 $pfileId = $result[
"pfile_fk"];
84 $uploadId = $itemTreeBounds->getUploadId();
86 $scanJobProxy =
new ScanJobProxy($this->agentDao, $uploadId);
87 $scanJobProxy->createAgentStatus([$this->agentName]);
88 $selectedScanners = $scanJobProxy->getLatestSuccessfulAgentIds();
89 if (!array_key_exists($this->agentName, $selectedScanners)) {
91 " has not been scheduled/completed on the upload.");
93 $latestAgentId = $selectedScanners[$this->agentName];
94 $license = $this->licenseDao->getLicenseByShortName($shortname, $groupId=
null);
95 if ($license ===
null) {
98 $licenseId = $license->getId();
99 $stmt = __METHOD__ .
"getCompResult";
100 $sql =
"SELECT result FROM comp_result
101 WHERE pfile_fk = $1 AND agent_fk= $2 AND result = FALSE AND
102 (first_rf_fk = $3 OR second_rf_fk = $3);";
103 $res = $this->
dbManager->getSingleRow($sql,
104 [$pfileId, $latestAgentId, $licenseId], $stmt);
115 public function getAllRules($limit = 10, $offset = 0, $searchTerm =
'')
120 if (!empty($searchTerm)) {
121 $params[] = $searchTerm;
122 $sql .=
' WHERE comment ILIKE $' . count($params);
126 $sql .=
' ORDER BY lr_pk LIMIT $' . count($params);
128 $sql .=
' OFFSET $' . count($params) .
';';
129 return $this->
dbManager->getRows($sql, $params, $stmt);
139 $row = $this->
dbManager->getSingleRow(self::RULE_SELECT .
140 ' WHERE lr_pk = $1;', [$rulePk], __METHOD__);
141 return empty($row) ? null : $row;
153 $query =
"SELECT COUNT(*) as count FROM license_rules";
154 if (!empty($searchTerm)) {
155 $params[] = $searchTerm;
156 $query .=
' WHERE comment ILIKE $' . count($params);
159 $count = $this->
dbManager->getSingleRow($query, $params, $stmt);
160 return $count ? intval($count[
'count']) : 0;
173 'first_rf_fk' =>
null,
174 'second_rf_fk' =>
null,
175 'first_type' =>
null,
176 'second_type' =>
null,
178 'compatibility' => false
181 $statement = __METHOD__ .
".insertEmptyLicCompatibilityRule";
182 $returning =
"lr_pk";
186 $returnVal = $this->
dbManager->insertTableRow(
"license_rules", $params, $statement, $returning);
203 function insertRule($firstName, $secondName, $firstType, $secondType, $comment,
210 $comment =
trim($comment);
211 if (empty($comment)) {
216 'first_rf_fk' => $firstName,
217 'second_rf_fk' => $secondName,
218 'first_type' => $firstType,
219 'second_type' => $secondType,
220 'comment' => $comment,
221 'compatibility' => $result
223 $statement = __METHOD__ .
".insertNewLicCompatibilityRule";
224 $returning =
"lr_pk";
227 $returnVal = $this->
dbManager->insertTableRow(
"license_rules",
228 $params, $statement, $returning);
248 foreach ($ruleArray as $rulePk => $rule) {
249 if (count($rule) < 1) {
250 throw new \UnexpectedValueException(
"Rule has no values");
253 $statement = __METHOD__;
255 $updateStatement = [];
256 if (array_key_exists(
"firstLic", $rule)) {
257 $params[] = $rule[
"firstLic"];
258 $updateStatement[] =
"first_rf_fk = $" . count($params);
259 $statement .=
".first_rf_fk";
261 if (array_key_exists(
"secondLic", $rule)) {
262 $params[] = $rule[
"secondLic"];
263 $updateStatement[] =
"second_rf_fk = $" . count($params);
264 $statement .=
".second_rf_fk";
266 if (array_key_exists(
"firstType", $rule)) {
267 $params[] = $rule[
"firstType"];
268 $updateStatement[] =
"first_type = $" . count($params);
269 $statement .=
".first_type";
271 if (array_key_exists(
"secondType", $rule)) {
272 $params[] = $rule[
"secondType"];
273 $updateStatement[] =
"second_type = $" . count($params);
274 $statement .=
".second_type";
276 if (array_key_exists(
"comment", $rule)) {
277 $params[] = $rule[
"comment"];
278 $updateStatement[] =
"comment = $" . count($params);
279 $statement .=
".comment";
281 if (array_key_exists(
"result", $rule)) {
283 $params[] = is_bool($rule[
"result"]) ?
284 $this->
dbManager->booleanToDb($rule[
"result"]) : $rule[
"result"];
285 $updateStatement[] =
"compatibility = $" . count($params);
286 $statement .=
".compatibility";
288 $sql =
"UPDATE license_rules " .
289 "SET " . join(
",", $updateStatement) .
290 " WHERE lr_pk = $1 " .
291 "RETURNING 1 AS updated;";
292 $retVal = $this->
dbManager->getSingleRow($sql, $params, $statement);
293 $updated += intval($retVal[
"updated"]);
305 if (! is_int($rulePk)) {
306 throw new \UnexpectedValueException(
"Invalid rule id $rulePk");
308 $sql =
"SELECT count(*) AS cnt FROM license_rules " .
311 $ruleCount = $this->
dbManager->getSingleRow($sql, [$rulePk]);
312 if ($ruleCount[
'cnt'] < 1) {
314 throw new \UnexpectedValueException(
"Invalid rule id $rulePk");
330 $stmt = __METHOD__ .
"DeletionOfRule";
331 $sql =
"DELETE FROM license_rules " .
332 "WHERE lr_pk = $1 " .
335 $retVal = $this->
dbManager->getSingleRow($sql, [$rulePk], $stmt);
336 if ($retVal[
"lr_pk"] == $rulePk) {
349 return $this->defaultCompatibility;
Contains the constants and helpers for authentication of user.
static isAdmin()
Check if user is admin.
deleteRule($rulePk)
Delete a license compatibility rule.
getDefaultCompatibility()
updateRuleFromArray($ruleArray)
Update the existing rules.
getTotalRulesCount($searchTerm='')
Get the total count of license compatibility rules.
insertRule($firstName, $secondName, $firstType, $secondType, $comment, $result)
Insert new rule in the database.
insertEmptyRule()
Insert a new empty rule in the database.
getAllRules($limit=10, $offset=0, $searchTerm='')
Get all the existing license compatibility rules from the database.
getRuleById($rulePk)
Get a single license compatibility rule from the database.
isRuleIdValid($rulePk)
Check if rule ID exists in DB.
Exception when an agent's stage is invalid.
char * trim(char *ptext)
Trimming whitespace.
fo_dbManager * dbManager
fo_dbManager object