Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions src/lib/php/Application/ObligationCsvExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/*
Copyright (C) 2015, Siemens AG

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

namespace Fossology\Lib\Application;

use Fossology\Lib\BusinessRules\LicenseMap;
use Fossology\Lib\Db\DbManager;

class ObligationCsvExport {
/** @var DbManager */
protected $dbManager;
/** @var string */
protected $delimiter = ',';
/** @var string */
protected $enclosure = '"';

public function __construct(DbManager $dbManager)
{
$this->dbManager = $dbManager;
$this->obligationMap = $GLOBALS['container']->get('businessrules.obligationmap');
}

public function setDelimiter($delimiter=',')
{
$this->delimiter = substr($delimiter,0,1);
}

public function setEnclosure($enclosure='"')
{
$this->enclosure = substr($enclosure,0,1);
}

/**
* @param int $rf
* @return string csv
*/
public function createCsv($ob=0)
{
$csvarray = array();
$sql = "SELECT ob_pk, ob_topic,ob_text
FROM obligation_ref;";
if ($ob>0)
{
$stmt = __METHOD__.'.ob';
$sql .= ' WHERE ob_pk=$'.$ob;
$row = $this->dbManager->getSingleRow($sql,$stmt);
$vars = $row ? array( $row ) : array();
$liclist = $this->obligationMap->getLicenseList($ob);
array_shift($vars);
array_push($vars,$liclist);
$csvarray = $vars;
}
else
{
$stmt = __METHOD__;
$this->dbManager->prepare($stmt,$sql);
$res = $this->dbManager->execute($stmt);
$vars = $this->dbManager->fetchAll($res);
$this->dbManager->freeResult($res);

foreach ($vars as $row)
{
$liclist = $this->obligationMap->getLicenseList($row['ob_pk']);
array_shift($row);
array_push($row,$liclist);
array_push($csvarray,$row);
}
}

$out = fopen('php://output', 'w');
ob_start();
$head = array('Obligation or Risk topic','Full Text','Associated Licenses');
fputcsv($out, $head, $this->delimiter, $this->enclosure);
foreach($csvarray as $row)
{
fputcsv($out, $row, $this->delimiter, $this->enclosure);
}
$content = ob_get_contents();
ob_end_clean();
return $content;
}

}
184 changes: 184 additions & 0 deletions src/lib/php/Application/ObligationCsvImport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
<?php
/*
Copyright (C) 2014-2015, Siemens AG

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

namespace Fossology\Lib\Application;

use Fossology\Lib\Db\DbManager;
use Fossology\Lib\Util\ArrayOperation;

class ObligationCsvImport {
/** @var DbManager */
protected $dbManager;
/** @var string */
protected $delimiter = ',';
/** @var string */
protected $enclosure = '"';
/** @var null|array */
protected $headrow = null;
/** @var array */
protected $alias = array(
'topic'=>array('topic','Obligation or Risk topic'),
'text'=>array('text','Full Text'),
'licnames'=>array('licnames','Associated Licenses')
);

public function __construct(DbManager $dbManager)
{
$this->dbManager = $dbManager;
$this->obligationMap = $GLOBALS['container']->get('businessrules.obligationmap');
}

public function setDelimiter($delimiter=',')
{
$this->delimiter = substr($delimiter,0,1);
}

public function setEnclosure($enclosure='"')
{
$this->enclosure = substr($enclosure,0,1);
}

/**
* @param string $filename
* @return string message
*/
public function handleFile($filename)
{
if (!is_file($filename) || ($handle = fopen($filename, 'r')) === FALSE) {
return _('Internal error');
}
$cnt = -1;
$msg = '';
try
{
while(($row = fgetcsv($handle,0,$this->delimiter,$this->enclosure)) !== FALSE) {
$log = $this->handleCsv($row);
if (!empty($log))
{
$msg .= "$log\n";
}
$cnt++;
}
$msg .= _('Read csv').(": $cnt ")._('obligations');
}
catch(\Exception $e)
{
fclose($handle);
return $msg .= _('Error while parsing file').': '.$e->getMessage();
}
fclose($handle);
return $msg;
}

/**
* @param array $row
* @return string $log
*/
private function handleCsv($row)
{
if($this->headrow===null)
{
$this->headrow = $this->handleHeadCsv($row);
return 'head okay';
}

$mRow = array();
foreach( array('topic','text','licnames') as $needle){
$mRow[$needle] = $row[$this->headrow[$needle]];
}

return $this->handleCsvObligation($mRow);
}

private function handleHeadCsv($row)
{
$headrow = array();
foreach( array('topic','text','licnames') as $needle){
$col = ArrayOperation::multiSearch($this->alias[$needle], $row);
if (false === $col)
{
throw new \Exception("Undetermined position of $needle");
}
$headrow[$needle] = $col;
}
return $headrow;
}

private function getKeyFromTopicAndText($row)
{
$req = array($row['topic'], $row['text']);
$row = $this->dbManager->getSingleRow('SELECT ob_pk FROM obligation_ref WHERE ob_topic=$1 AND ob_md5=md5($2)',$req);
return ($row === false) ? false : $row['ob_pk'];
}


/**
* @param array $row
* @return string
*/
private function handleCsvObligation($row)
{
/* @var $dbManager DbManager */
$dbManager = $this->dbManager;
$exists = $this->getKeyFromTopicAndText($row);
if ($exists !== false)
{
return "Obligation topic '$row[topic]' with text '$row[text]' already exists in DB (id=".$exists.")";
}

$stmtInsert = __METHOD__.'.insert';
$dbManager->prepare($stmtInsert,'INSERT INTO obligation_ref (ob_topic,ob_text,ob_md5)'
. ' VALUES ($1,$2,md5($2)) RETURNING ob_pk');
$resi = $dbManager->execute($stmtInsert,
array($row['topic'],$row['text']));
$new = $dbManager->fetchArray($resi);
$dbManager->freeResult($resi);

$associatedLicenses = "";
$licenses = explode(";",$row['licnames']);
foreach ($licenses as $license)
{
$licId = $this->obligationMap->getIdFromShortname($license);
if ($licId == '0')
{
$message = _("ERROR: License with shortname '$license' not found in the DB. Obligation not updated.");
return "<b>$message</b><p>";
}

if ($this->obligationMap->isLicenseAssociated($new['ob_pk'],$licId))
{
continue;
}

$this->obligationMap->associateLicenseWithObligation($new['ob_pk'],$licId);
if ($associatedLicenses == "")
{
$associatedLicenses = "$license";
}
else
{
$associatedLicenses .= ";$license";
}
}

$return = "Obligation topic '$row[topic]' was added and associated with licenses '$associatedLicenses' in DB";

return $return;
}

}
111 changes: 111 additions & 0 deletions src/lib/php/BusinessRules/ObligationMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php
/*
Copyright (C) 2017, Siemens AG

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

namespace Fossology\Lib\BusinessRules;

use Fossology\Lib\Db\DbManager;
use Fossology\Lib\Util\Object;

class ObligationMap extends Object
{

/** @var DbManager */
private $dbManager;

public function __construct(DbManager $dbManager)
{
$this->dbManager = $dbManager;
}

/** @brief get the license id from the shortname */
public function getIdFromShortname($shortname)
{
$sql = "SELECT * from license_ref where rf_shortname = $1;";
$result = $this->dbManager->getSingleRow($sql,array($shortname));
return $result['rf_pk'];
}

/** @brief get the shortname of the license by Id */
public function getShortnameFromId($rfId)
{
$sql = "SELECT * FROM license_ref WHERE rf_pk = $1;";
$result = $this->dbManager->getSingleRow($sql,array($rfId));
return $result['rf_shortname'];
}

/** @brief get the list of licenses associated with the obligation */
public function getLicenseList($obId)
{
$liclist = "";
$sql = "SELECT rf_fk FROM obligation_map WHERE ob_fk=$obId;";
$stmt = __METHOD__.".om_$obId";
$this->dbManager->prepare($stmt,$sql);
$res = $this->dbManager->execute($stmt);
$vars = $this->dbManager->fetchAll($res);
$this->dbManager->freeResult($res);
foreach ($vars as $map_entry)
{
$licname = $this->getShortnameFromId($map_entry['rf_fk']);
if ($liclist == "")
{
$liclist = "$licname";
}
else
{
$liclist .= ";$licname";
}
}

return $liclist;
}

/** @brief check if the obligation is already associated with the license */
public function isLicenseAssociated($obId,$licId)
{
$sql = "SELECT * from obligation_map where ob_fk = $1 and rf_fk = $2;";
$result = $this->dbManager->getSingleRow($sql,array($obId,$licId));
if ($result)
{
return True;
}

return False;
}

/** @brief check if the text of this obligation is existing */
public function associateLicenseWithObligation($obId,$licId)
{
$sql = "INSERT INTO obligation_map (ob_fk, rf_fk) VALUES ($1, $2)";
$this->dbManager->prepare($stmt,$sql);
$res = $this->dbManager->execute($stmt,array($obId,$licId));
$this->dbManager->fetchArray($res);
$this->dbManager->freeResult($res);
}

/** @brief check if the text of this obligation is existing */
public function unassociateLicenseFromObligation($obId,$licId)
{
$sql = "DELETE FROM obligation_map WHERE ob_fk=$1 AND rf_fk=$2";
$stmt = __METHOD__.".omdel_$licId";
$this->dbManager->prepare($stmt,$sql);
$res = $this->dbManager->execute($stmt,array($obId,$licId));
$this->dbManager->fetchArray($res);
$this->dbManager->freeResult($res);
}

}
Loading