FOSSology  4.4.0
Open Source License Compliance by Open Source Software
decisionexporter.php
1 <?php
2 /*
3  Author: Shaheem Azmal M MD <shaheem.azmal@siemens.com>
4  SPDX-FileCopyrightText: © 2022 Siemens AG
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
9 define("DECISIONEXPORTER_AGENT_NAME", "decisionexporter");
10 
15 
16 include_once(__DIR__ . "/version.php");
17 
22 class DecisionExporter extends Agent
23 {
27  private $uploadDao;
28 
33 
38  private $clearingDao;
39 
40  function __construct()
41  {
42  parent::__construct(DECISIONEXPORTER_AGENT_NAME, AGENT_VERSION, AGENT_REV);
43 
44  $this->uploadDao = $this->container->get('dao.upload');
45  $this->allDecisionsDao = $this->container->get('dao.alldecisions');
46  $this->clearingDao = $this->container->get('dao.clearing');
47  }
48 
49 
54  function processUploadId($uploadId)
55  {
58  $tableName = "decision_exporter_pfile_" . $uploadId;
59 
60  $pfileData = $this->allDecisionsDao->getAllAgentPfileIdsForUpload($uploadId, $groupId, $userId);
61  $this->createPfileTable($uploadId, $tableName);
62  $this->heartbeat(count($pfileData));
63 
64  $this->insertPfileData($uploadId, $pfileData, $tableName);
65  $this->heartbeat(1);
66  $uploadTreeData = $this->allDecisionsDao->getAllAgentUploadTreeDataForUpload($uploadId, $tableName);
67  $this->heartbeat(1);
68  $clearingDecisonData = $this->allDecisionsDao->getAllClearingDecisionDataForUpload($uploadId, $tableName);
69  $this->heartbeat(1);
70  $clearingEventData = $this->allDecisionsDao->getAllClearingEventDataForUpload($uploadId, $tableName);
71  $this->heartbeat(1);
72  $clearingDecisonEventData = $this->allDecisionsDao->getAllClearingDecisionEventDataForUpload($uploadId, $tableName);
73  $this->heartbeat(1);
74  $licenseRefBulkData = $this->allDecisionsDao->getAllLicenseRefBulkDataForUpload($uploadId);
75  $this->heartbeat(1);
76  $licenseSetBulkData = $this->allDecisionsDao->getAllLicenseSetBulkDataForUpload($uploadId);
77  $this->heartbeat(1);
78  $bulkHighlightData = $this->allDecisionsDao->getAllBulkHighlightDataForUpload($uploadId);
79  $this->heartbeat(1);
80  $copyrightData = $this->allDecisionsDao->getAllDataForGivenTableUpload($tableName, 'copyright');
81  $this->heartbeat(1);
82  $copyrightDecisionData = $this->allDecisionsDao->getAllDataForGivenDecisionTableUpload($tableName, 'copyright_decision');
83  $this->heartbeat(1);
84  $copyrightEventData = $this->allDecisionsDao->getAllDataForGivenEventTableUpload($uploadId, 'copyright_event', 'copyright');
85  $this->heartbeat(1);
86  $eccData = $this->allDecisionsDao->getAllDataForGivenTableUpload($tableName, 'ecc');
87  $this->heartbeat(1);
88  $eccDecisionData = $this->allDecisionsDao->getAllDataForGivenDecisionTableUpload($tableName, 'ecc_decision');
89  $this->heartbeat(1);
90  $eccEventData = $this->allDecisionsDao->getAllDataForGivenEventTableUpload($uploadId, 'ecc_event', 'ecc');
91  $this->heartbeat(1);
92  $ipraData = $this->allDecisionsDao->getAllDataForGivenTableUpload($tableName, 'ipra');
93  $this->heartbeat(1);
94  $ipraDecisionData = $this->allDecisionsDao->getAllDataForGivenDecisionTableUpload($tableName, 'ipra_decision');
95  $this->heartbeat(1);
96  $ipraEventData = $this->allDecisionsDao->getAllDataForGivenEventTableUpload($uploadId, 'ipra_event', 'ipra');
97  $this->heartbeat(1);
98  $reportInfoData = $this->uploadDao->getReportInfo($uploadId);
99  $this->heartbeat(1);
100  $licenseData = $this->allDecisionsDao->getAllLicenseDataForUpload($uploadId);
101  $this->heartbeat(1);
102  $mainLicenseData = $this->clearingDao->getMainLicenseIds($uploadId, $groupId);
103 
104  $contents = array(
105  'pfile'=>$pfileData,
106  'uploadtree'=>$uploadTreeData,
107  'clearing_decision'=>$clearingDecisonData,
108  'clearing_event'=>$clearingEventData,
109  'clearing_decision_event'=>$clearingDecisonEventData,
110  'license_ref_bulk'=>$licenseRefBulkData,
111  'license_set_bulk'=>$licenseSetBulkData,
112  'highlight_bulk'=>$bulkHighlightData,
113  'copyright'=>$copyrightData,
114  'copyright_decision'=>$copyrightDecisionData,
115  'copyright_event'=>$copyrightEventData,
116  'ecc'=>$eccData,
117  'ecc_decision'=>$eccDecisionData,
118  'ecc_event'=>$eccEventData,
119  'ipra'=>$ipraData,
120  'ipra_decision'=>$ipraDecisionData,
121  'ipra_event'=>$ipraEventData,
122  'report_info'=>$reportInfoData,
123  'licenses'=>$licenseData,
124  'upload_clearing_license'=>array_values($mainLicenseData)
125  );
126 
127  $this->dropPfileTable($uploadId, $tableName);
128  $this->writeReport($contents, $uploadId);
129 
130  return true;
131  }
132 
141  private function writeReport($contents, $uploadId)
142  {
143  global $SysConf;
144 
145  $packageName = $this->uploadDao->getUpload($uploadId)->getFilename();
146 
147  $fileBase = $SysConf['FOSSOLOGY']['path'] . "/report/";
148  $fileName = $fileBase . "FOSSology_Decisions_" . $packageName . '_' . date("d_m_Y_H_i_s") . ".json";
149 
150  if (!is_dir($fileBase)) {
151  mkdir($fileBase, 0777, true);
152  }
153  umask(0133);
154 
155  file_put_contents($fileName, json_encode($contents, JSON_UNESCAPED_SLASHES));
156 
157  $this->updateReportTable($uploadId, $this->jobId, $fileName);
158  }
159 
165  private function insertPfileData($uploadId, $pfileData, $tableName)
166  {
167  $allPfileFk = array_keys($pfileData);
168  foreach ($allPfileFk as $pfileFk) {
169  $this->dbManager->insertInto($tableName, 'pfile_fk', array($pfileFk));
170  }
171  }
172 
177  private function createPfileTable($uploadId, $tableName)
178  {
179  $this->dbManager->getSingleRow("CREATE TABLE IF NOT EXISTS ".$tableName." (pfile_fk BIGINT NOT NULL);",
180  array(), __METHOD__);
181  }
182 
187  private function dropPfileTable($uploadId, $tableName)
188  {
189  $this->dbManager->getSingleRow("DROP TABLE IF EXISTS ".$tableName.";",
190  array(), __METHOD__);
191  }
192 
199  private function updateReportTable($uploadId, $jobId, $filename)
200  {
201  $this->dbManager->getSingleRow("INSERT INTO reportgen(upload_fk, job_fk, filepath) VALUES($1,$2,$3)",
202  array($uploadId, $jobId, $filename), __METHOD__);
203  }
204 }
205 
206 $agent = new DecisionExporter();
207 $agent->scheduler_connect();
208 $agent->run_scheduler_event_loop();
209 $agent->scheduler_disconnect(0);
Generates Decision Exporter JSON.
processUploadId($uploadId)
Given an upload ID, process the items in it.
dropPfileTable($uploadId, $tableName)
Create database table.
createPfileTable($uploadId, $tableName)
Create database table.
insertPfileData($uploadId, $pfileData, $tableName)
Create database table.
writeReport($contents, $uploadId)
Writes the data to a json file.
updateReportTable($uploadId, $jobId, $filename)
Update database with generated report path.
Structure of an Agent with all required parameters.
Definition: Agent.php:41
heartbeat($newProcessed)
Send hear beat to the scheduler.
Definition: Agent.php:203
int jobId
The id of the job.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16