8 namespace Fossology\Lib\Dao;
32 private $defaultCompatibility;
39 $this->logger = $logger;
40 $this->licenseDao = $licenseDao;
41 $this->agentDao = $agentDao;
42 $this->agentName =
"compatibility";
43 $sql =
"SELECT compatibility FROM license_rules
44 WHERE first_rf_fk IS NULL AND second_rf_fk IS NULL
45 AND first_type IS NULL AND second_type IS NULL;";
46 $result = $this->
dbManager->getSingleRow($sql, [], __METHOD__ .
48 if (!empty($result) && array_key_exists(
"compatibility", $result)) {
49 $this->defaultCompatibility = $result[
"compatibility"];
51 $this->defaultCompatibility =
false;
63 public function getCompatibilityForFile($itemTreeBounds, $shortname)
65 $uploadTreeTableName = $itemTreeBounds->getUploadTreeTableName();
66 $uploadTreePk = $itemTreeBounds->getItemId();
67 $stmt = __METHOD__.$uploadTreeTableName;
68 $sql =
"SELECT pfile_fk FROM $uploadTreeTableName UT
69 WHERE UT.uploadtree_pk = $1";
70 $result = $this->
dbManager->getSingleRow($sql, [$uploadTreePk], $stmt);
71 $pfileId = $result[
"pfile_fk"];
72 $uploadId = $itemTreeBounds->getUploadId();
74 $scanJobProxy =
new ScanJobProxy($this->agentDao, $uploadId);
75 $scanJobProxy->createAgentStatus([$this->agentName]);
76 $selectedScanners = $scanJobProxy->getLatestSuccessfulAgentIds();
77 if (!array_key_exists($this->agentName, $selectedScanners)) {
79 " has not been scheduled/completed on the upload.");
81 $latestAgentId = $selectedScanners[$this->agentName];
82 $license = $this->licenseDao->getLicenseByShortName($shortname, $groupId=
null);
83 if ($license ===
null) {
86 $licenseId = $license->getId();
87 $stmt = __METHOD__ .
"getCompResult";
88 $sql =
"SELECT result FROM comp_result
89 WHERE pfile_fk = $1 AND agent_fk= $2 AND result = FALSE AND
90 (first_rf_fk = $3 OR second_rf_fk = $3);";
91 $res = $this->
dbManager->getSingleRow($sql,
92 [$pfileId, $latestAgentId, $licenseId], $stmt);
103 public function getAllRules($limit = 10, $offset = 0, $searchTerm =
'')
105 $sql =
"SELECT lr_pk, first_rf_fk, second_rf_fk, first_type, second_type,
106 comment, compatibility
108 if (!empty($searchTerm)) {
109 $sql .=
" WHERE comment ILIKE '$searchTerm'";
111 $sql .=
" ORDER BY lr_pk LIMIT $limit OFFSET $offset;";
122 $query =
"SELECT COUNT(*) as count FROM license_rules";
123 if (!empty($searchTerm)) {
124 $query .=
" WHERE comment ILIKE '$searchTerm'";
126 $count = $this->
dbManager->getSingleRow($query);
127 return $count ? reset($count) : 0;
140 'first_rf_fk' =>
null,
141 'second_rf_fk' =>
null,
142 'first_type' =>
null,
143 'second_type' =>
null,
145 'compatibility' => false
148 $statement = __METHOD__ .
".insertEmptyLicCompatibilityRule";
149 $returning =
"lr_pk";
153 $returnVal = $this->
dbManager->insertTableRow(
"license_rules", $params, $statement, $returning);
170 function insertRule($firstName, $secondName, $firstType, $secondType, $comment,
177 $comment =
trim($comment);
178 if (empty($comment)) {
183 'first_rf_fk' => $firstName,
184 'second_rf_fk' => $secondName,
185 'first_type' => $firstType,
186 'second_type' => $secondType,
187 'comment' => $comment,
188 'compatibility' => $result
190 $statement = __METHOD__ .
".insertNewLicCompatibilityRule";
191 $returning =
"lr_pk";
194 $returnVal = $this->
dbManager->insertTableRow(
"license_rules",
195 $params, $statement, $returning);
215 foreach ($ruleArray as $rulePk => $rule) {
216 if (count($rule) < 1) {
217 throw new \UnexpectedValueException(
"Rule has no values");
220 $statement = __METHOD__;
222 $updateStatement = [];
223 if (array_key_exists(
"firstLic", $rule)) {
224 $params[] = $rule[
"firstLic"];
225 $updateStatement[] =
"first_rf_fk = $" . count($params);
226 $statement .=
".first_rf_fk";
228 if (array_key_exists(
"secondLic", $rule)) {
229 $params[] = $rule[
"secondLic"];
230 $updateStatement[] =
"second_rf_fk = $" . count($params);
231 $statement .=
".second_rf_fk";
233 if (array_key_exists(
"firstType", $rule)) {
234 $params[] = $rule[
"firstType"];
235 $updateStatement[] =
"first_type = $" . count($params);
236 $statement .=
".first_type";
238 if (array_key_exists(
"secondType", $rule)) {
239 $params[] = $rule[
"secondType"];
240 $updateStatement[] =
"second_type = $" . count($params);
241 $statement .=
".second_type";
243 if (array_key_exists(
"comment", $rule)) {
244 $params[] = $rule[
"comment"];
245 $updateStatement[] =
"comment = $" . count($params);
246 $statement .=
".comment";
248 if (array_key_exists(
"result", $rule)) {
249 $params[] = $rule[
"result"];
250 $updateStatement[] =
"compatibility = $" . count($params);
251 $statement .=
".compatibility";
253 $sql =
"UPDATE license_rules " .
254 "SET " . join(
",", $updateStatement) .
255 " WHERE lr_pk = $1 " .
256 "RETURNING 1 AS updated;";
257 $retVal = $this->
dbManager->getSingleRow($sql, $params, $statement);
258 $updated += intval($retVal[
"updated"]);
270 if (! is_int($rulePk)) {
271 throw new \UnexpectedValueException(
"Invalid rule id $rulePk");
273 $sql =
"SELECT count(*) AS cnt FROM license_rules " .
276 $ruleCount = $this->
dbManager->getSingleRow($sql, [$rulePk]);
277 if ($ruleCount[
'cnt'] < 1) {
279 throw new \UnexpectedValueException(
"Invalid rule id $rulePk");
295 $stmt = __METHOD__ .
"DeletionOfRule";
296 $sql =
"DELETE FROM license_rules " .
297 "WHERE lr_pk = $1 " .
300 $retVal = $this->
dbManager->getSingleRow($sql, [$rulePk], $stmt);
301 if ($retVal[
"lr_pk"] == $rulePk) {
314 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.
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