39 'type'=>array(
'type',
'Type'),
40 'topic'=>array(
'topic',
'Obligation or Risk topic'),
41 'text'=>array(
'text',
'Full Text'),
42 'classification'=>array(
'classification',
'Classification'),
43 'modifications'=>array(
'modifications',
'Apply on modified source code'),
44 'comment'=>array(
'comment',
'Comment'),
45 'licnames'=>array(
'licnames',
'Associated Licenses'),
46 'candidatenames'=>array(
'candidatenames',
'Associated candidate Licenses')
56 $this->obligationMap = $GLOBALS[
'container']->get(
'businessrules.obligationmap');
85 if (!is_file($filename) || ($handle = fopen($filename,
'r')) ===
false) {
86 return _(
'Internal error');
91 if ($fileExtension ==
'csv') {
92 while (($row = fgetcsv($handle,0,$this->delimiter,$this->enclosure)) !==
false) {
99 $msg .= _(
'Read csv').(
": $cnt ")._(
'obligations');
101 $jsonContent = fread($handle, filesize($filename));
102 $data = json_decode($jsonContent,
true);
103 if ($data ===
null && json_last_error() !== JSON_ERROR_NONE) {
104 $msg .=
"Error decoding JSON: " . json_last_error_msg().
"\n";
106 foreach ($data as $row) {
112 $msg .= _(
'Read json').(
":". count($data) .
" ")._(
'obligations');
116 return $msg .= _(
'Error while parsing file').
': '.$e->getMessage();
131 foreach ($row as $key => $value) {
133 foreach ($this->alias as $aliasKey => $aliasValues) {
134 if (in_array($key, $aliasValues)) {
139 $newArray[$newKey] = $value;
152 if ($this->headrow===
null) {
158 foreach (array(
'type',
'topic',
'text',
'classification',
'modifications',
'comment',
'licnames',
'candidatenames') as $needle) {
159 $mRow[$needle] = $row[$this->headrow[$needle]];
174 foreach (array(
'type',
'topic',
'text',
'classification',
'modifications',
'comment',
'licnames',
'candidatenames') as $needle) {
175 $col = ArrayOperation::multiSearch($this->alias[$needle], $row);
176 if (
false === $col) {
177 throw new \Exception(
"Undetermined position of $needle");
191 $req = array($row[
'topic'], $row[
'text']);
192 $row = $this->
dbManager->getSingleRow(
'SELECT ob_pk FROM obligation_ref WHERE ob_topic=$1 AND ob_md5=md5($2)',$req);
193 return ($row ===
false) ? false : $row[
'ob_pk'];
206 $getList = $this->obligationMap->getLicenseList($exists, $candidate);
209 return strcmp($listFromDb, $listFromCsv);
220 $string = explode(
";", $string);
222 return implode(
",", $string);
234 $this->obligationMap->unassociateLicenseFromObligation($exists, $licId, $candidate);
251 $associatedLicenses =
"";
252 $candidateLicenses =
"";
254 if ($exists !==
false) {
255 $msg =
"Obligation topic '$row[topic]' already exists in DB (id=".$exists.
"),";
256 if ( $this->
compareLicList($exists, $row[
'licnames'],
false, $row) === 0 ) {
257 $msg .=
" No Changes in AssociateLicense";
260 if (!empty($row[
'licnames'])) {
263 $msg .=
" Updated AssociatedLicense license";
265 if ($this->
compareLicList($exists, $row[
'candidatenames'],
true, $row) === 0) {
266 $msg .=
" No Changes in CandidateLicense";
269 if (!empty($row[
'candidatenames'])) {
272 $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";
309 $associatedLicenses =
"";
312 $licenses = explode(
";",$licList);
313 foreach ($licenses as $license) {
314 $licIds = $this->obligationMap->getIdFromShortname($license, $candidate);
316 if (empty($licIds)) {
317 $message .=
"License $license could not be found in the DB.\n";
319 $updated = $this->obligationMap->associateLicenseFromLicenseList($obPk,
320 $licIds, $candidate);
323 if ($associatedLicenses ==
"") {
324 $associatedLicenses =
"$license";
326 $associatedLicenses .=
";$license";
331 if (!empty($associatedLicenses)) {
332 $message .=
"$associatedLicenses were associated.\n";
335 $message .= $candidate ?
"candidate":
"";
336 $message .=
"licenses were associated.\n";
353 $this->
dbManager->getSingleRow(
'UPDATE obligation_ref SET ob_classification=$2, ob_modifications=$3, ob_comment=$4 where ob_pk=$1',
354 array($exists, $row[
'classification'], $row[
'modifications'], $row[
'comment']),
355 __METHOD__ .
'.updateOtherOb');
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.
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.
__construct(DbManager $dbManager)
AssociateWithLicenses($licList, $obPk, $candidate=False)
Associate selected licenses to the obligation.
handleFile($filename, $fileExtension)
Read the CSV line by line and import it.
compareLicList($exists, $listFromCsv, $candidate, $row)
Compare licenses from Database and CSV.
fo_dbManager * dbManager
fo_dbManager object
Utility functions for specific applications.