40 'type'=>array(
'type',
'Type'),
41 'topic'=>array(
'topic',
'Obligation or Risk topic'),
42 'text'=>array(
'text',
'Full Text'),
43 'classification'=>array(
'classification',
'Classification'),
44 'modifications'=>array(
'modifications',
'Apply on modified source code'),
45 'comment'=>array(
'comment',
'Comment'),
46 'licnames'=>array(
'licnames',
'Associated Licenses'),
47 'candidatenames'=>array(
'candidatenames',
'Associated candidate Licenses'),
48 'external_id'=>array(
'id',
'LicenseDB Id'),
91 if (!is_file($filename) || ($handle = fopen($filename,
'r')) ===
false) {
92 return _(
'Internal error');
97 if ($fileExtension ==
'csv') {
98 while (($row = fgetcsv($handle,0,$this->delimiter,$this->enclosure)) !==
false) {
105 $msg .= _(
'Read csv').(
": $cnt ")._(
'obligations');
107 $jsonContent = fread($handle, filesize($filename));
108 $data = json_decode($jsonContent,
true);
109 if ($data ===
null && json_last_error() !== JSON_ERROR_NONE) {
110 $msg .=
"Error decoding JSON: " . json_last_error_msg() .
"\n";
113 $msg .= _(
'Read json').(
":". count($data) .
" ")._(
'obligations');
117 return $msg .= _(
'Error while parsing file').
': '.$e->getMessage();
132 foreach ($row as $key => $value) {
134 foreach ($this->alias as $aliasKey => $aliasValues) {
135 if (in_array($key, $aliasValues)) {
140 $newArray[$newKey] = $value;
153 if ($this->headrow===
null) {
159 foreach (array(
'type',
'topic',
'text',
'classification',
'modifications',
'comment',
'licnames',
'candidatenames') as $needle) {
160 $mRow[$needle] = $row[$this->headrow[$needle]];
175 foreach (array(
'type',
'topic',
'text',
'classification',
'modifications',
'comment',
'licnames',
'candidatenames') as $needle) {
176 $col = ArrayOperation::multiSearch($this->alias[$needle], $row);
177 if (
false === $col) {
178 throw new \Exception(
"Undetermined position of $needle");
192 $req = array($row[
'topic'], $row[
'text']);
193 $row = $this->
dbManager->getSingleRow(
'SELECT ob_pk FROM obligation_ref WHERE ob_topic=$1 AND ob_md5=md5($2)',$req);
194 return ($row ===
false) ? false : $row[
'ob_pk'];
207 $getList = $this->obligationMap->getLicenseList($exists, $candidate);
210 return strcmp($listFromDb, $listFromCsv);
221 $string = explode(
";", $string);
223 return implode(
",", $string);
235 $this->obligationMap->unassociateLicenseFromObligation($exists, $licId, $candidate);
252 $associatedLicenses =
"";
253 $candidateLicenses =
"";
255 if ($exists !==
false) {
257 if ( $this->
compareLicList($exists, $row[
'licnames'],
false, $row) === 0 ) {
258 $msg .=
" No Changes in AssociateLicense";
261 if (!empty($row[
'licnames'])) {
264 $msg .=
" Updated AssociatedLicense license";
266 if ($this->
compareLicList($exists, $row[
'candidatenames'],
true, $row) === 0) {
267 $msg .=
" No Changes in CandidateLicense";
270 if (!empty($row[
'candidatenames'])) {
273 $msg .=
" Updated CandidateLicense";
275 return $msg .
"\n" . $associatedLicenses .
"\n";
278 $stmtInsert = __METHOD__.
'.insert';
279 $dbManager->prepare($stmtInsert,
'INSERT INTO obligation_ref (ob_type,ob_topic,ob_text,ob_classification,ob_modifications,ob_comment,ob_md5)'
280 .
' VALUES ($1,$2,$3,$4,$5,$6,md5($3)) RETURNING ob_pk');
281 $resi =
$dbManager->execute($stmtInsert,array($row[
'type'],$row[
'topic'],$row[
'text'],$row[
'classification'],$row[
'modifications'],$row[
'comment']));
285 if (!empty($row[
'licnames'])) {
288 if (!empty($row[
'candidatenames'])) {
292 $message =
"License association results for obligation '$row[topic]':\n";
293 $message .=
"$associatedLicenses";
294 $message .=
"$candidateLicenses";
295 $message .=
"Obligation with id=$new[ob_pk] was added successfully.\n";
311 if (empty($row[
"external_id"])) {
312 return "Error: external_id cannot be empty";
314 $stmt = __METHOD__ .
".getExistingObligation";
315 $obPk = $this->
dbManager->getSingleRow(
"SELECT ob_pk FROM " .
316 "obligation_ref WHERE ob_external_id=$1", array($row[
"external_id"]), $stmt);
324 $stmt = __METHOD__ .
"getNewLicenseAssociations";
325 $newLicIds = array();
326 foreach ($row[
'license_ids'] as $licExternalId) {
327 $rfPk = $this->
dbManager->getSingleRow(
"SELECT rf_pk FROM license_ref WHERE rf_external_id = $1;",
328 array($licExternalId), $stmt);
330 $newLicIds[] = $rfPk[
'rf_pk'];
332 $log .= \sprintf(
'license with rf_external_id %s not found', $licExternalId);
337 $stmt = __METHOD__ .
"getOldLiceneAssociations";
338 $oldLicIdsDb = $this->
dbManager->getRows(
"SELECT rf_fk FROM obligation_map WHERE ob_fk = $1", array($obPk[
'ob_pk']), $stmt);
339 $oldLicIds = array();
340 foreach ($oldLicIdsDb as $licId) {
341 $oldLicIds[] = $licId[
'rf_fk'];
348 if (!empty($diff[
'remove'])) {
349 $log .=
' removed license ids [' . implode(
',', $diff[
'remove']) .
']';
351 if (!empty($diff[
'add'])) {
352 $log .=
' added license ids [' . implode(
',', $diff[
'add']) .
']';
356 $this->obligationMap->unassociateLicenseFromLicenseList($obPk[
'ob_pk'], $diff[
'remove']);
359 $this->obligationMap->associateLicenseFromLicenseList($obPk[
'ob_pk'], $diff[
'add']);
363 $stmtInsert = __METHOD__.
'.insert';
364 $this->
dbManager->prepare($stmtInsert,
'INSERT INTO obligation_ref (ob_type,ob_topic,ob_text,ob_classification,ob_modifications,ob_comment,ob_md5,ob_external_id)'
365 .
' VALUES ($1,$2,$3,$4,$5,$6,md5($3),$7) RETURNING ob_pk');
366 $resi = $this->
dbManager->execute($stmtInsert,array($row[
'type'],$row[
'topic'],$row[
'text'],$row[
'classification'],
'False',$row[
'comment'], $row[
'external_id']));
367 $new = $this->
dbManager->fetchArray($resi);
371 $newLicIds = array();
372 $stmt = __METHOD__ .
"getNewLicensesForInsert";
373 foreach ($row[
'license_ids'] as $licExternalId) {
374 $rfPk = $this->
dbManager->getSingleRow(
"SELECT rf_pk FROM license_ref WHERE rf_external_id = $1;",
375 array($licExternalId), $stmt);
377 $newLicIds[] = $rfPk[
'rf_pk'];
379 $message .= \sprintf(
'license with rf_external_id %s not found', $licExternalId);
384 $this->obligationMap->associateLicenseFromLicenseList($new[
'ob_pk'], $newLicIds);
385 if (!empty($newLicIds)) {
386 $log .=
' added license ids [' . implode(
',', $newLicIds) .
']';
404 $associatedLicenses =
"";
407 $licenses = explode(
";",$licList);
408 foreach ($licenses as $license) {
409 $licIds = $this->obligationMap->getIdFromShortname($license, $candidate);
411 if (empty($licIds)) {
412 $message .=
"License $license could not be found in the DB.\n";
414 $updated = $this->obligationMap->associateLicenseFromLicenseList($obPk,
415 $licIds, $candidate);
418 if ($associatedLicenses ==
"") {
419 $associatedLicenses =
"$license";
421 $associatedLicenses .=
";$license";
426 if (!empty($associatedLicenses)) {
427 $message .=
"$associatedLicenses were associated.\n";
430 $message .= $candidate ?
"candidate":
"";
431 $message .=
"licenses were associated.\n";
454 $stmt = __METHOD__ .
'.getOldObligation';
455 $oldObligation = $this->
dbManager->getSingleRow(
456 'SELECT ob_topic, ob_text, ob_classification, ob_modifications, ob_comment, ob_type FROM obligation_ref WHERE ob_pk=$1',
461 if (empty($oldObligation)) {
462 return "Obligation with id=$exists not found";
465 $log =
"Obligation topic '$oldObligation[ob_topic]' already exists in DB (id = $exists)";
466 $stmt = __METHOD__ .
'.updateOtherOb';
467 $sql =
'UPDATE obligation_ref SET ';
468 $extraParams = array();
469 $params = array($exists);
471 if (!empty($row[
'external_id'])) {
472 if (isset($row[
'topic']) && $row[
'topic'] != $oldObligation[
'ob_topic']) {
473 $params[] = $row[
'topic'];
475 $extraParams[] =
'ob_topic=$' . count($params);
476 $log .=
', updated topic';
478 if (isset($row[
'text']) && $row[
'text'] != $oldObligation[
'ob_text']) {
479 $params[] = $row[
'text'];
481 $extraParams[] =
'ob_text=$' . count($params) .
',ob_md5=md5($' .
482 count($params) .
')';
483 $log .=
', updated text';
485 if (isset($row[
'classification']) && $row[
'classification'] != $oldObligation[
'ob_classification']) {
486 $params[] = $row[
'classification'];
487 $stmt .=
'.classification';
488 $extraParams[] =
'ob_classification=$' . count($params);
489 $log .=
', updated classification';
491 if (isset($row[
'comment']) && $row[
'comment'] != $oldObligation[
'ob_comment']) {
492 $params[] = $row[
'comment'];
494 $extraParams[] =
'ob_comment=$' . count($params);
495 $log .=
', updated comment';
497 if (isset($row[
'type']) && $row[
'type'] != $oldObligation[
'ob_type']) {
498 $params[] = $row[
'type'];
500 $extraParams[] =
'ob_type=$' . count($params);
501 $log .=
', updated type';
504 if (isset($row[
'classification']) && $row[
'classification'] != $oldObligation[
'ob_classification']) {
505 $params[] = $row[
'classification'];
506 $stmt .=
'.classification';
507 $extraParams[] =
'ob_classification=$' . count($params);
508 $log .=
', updated classification';
510 if (isset($row[
'modifications']) && $row[
'modifications'] != $oldObligation[
'ob_modifications']) {
511 $params[] = $row[
'modifications'];
512 $stmt .=
'.modifications';
513 $extraParams[] =
'ob_modifications=$' . count($params);
514 $log .=
', updated modifications';
516 if (isset($row[
'comment']) && $row[
'comment'] != $oldObligation[
'ob_comment']) {
517 $params[] = $row[
'comment'];
519 $extraParams[] =
'ob_comment=$' . count($params);
520 $log .=
', updated comment';
524 if (count($params) > 1) {
525 $sql .= join(
',', $extraParams);
526 $sql .=
' WHERE ob_pk=$1';
527 $this->
dbManager->getSingleRow($sql, $params, $stmt);
540 foreach ($data as $row) {
Helper class for Obligation CSV Import.
setDelimiter($delimiter=',')
Update the delimiter.
setEnclosure($enclosure='"')
Update the enclosure.
clearListFromDb($exists, $candidate)
Clear all license maps for given obligation.
__construct(DbManager $dbManager, $obligationMap=null)
handleHeadCsv($row)
Handle a row as head row.
getKeyFromTopicAndText($row)
Get the Obligation key from obligation topic and obligation text.
handleCsvObligation($row)
Handle a single row from CSV.
updateOtherFields($exists, $row)
Update other fields of the obligation.
AssociateWithLicenses($licList, $obPk, $candidate=False)
Associate selected licenses to the obligation.
handleFile($filename, $fileExtension)
Read the CSV line by line and import it.
handleLicenseDBObligationImport($row)
Handle a single row from csv import form LicenseDB.
importJsonData($data, string $msg)
compareLicList($exists, $listFromCsv, $candidate, $row)
Compare licenses from Database and CSV.
Wrapper class for obligation map.
static getArrayDiffs(array $oldArray, array $newArray)
Get list of additions/removals to make $oldArray equal to $newArray.
fo_dbManager * dbManager
fo_dbManager object
Utility functions for specific applications.