36 include_once(__DIR__ .
"/version.php");
75 function __construct()
77 parent::__construct(REUSER_AGENT_NAME, AGENT_VERSION, AGENT_REV);
78 $this->uploadDao = $this->container->get(
'dao.upload');
79 $this->clearingDao = $this->container->get(
'dao.clearing');
80 $this->decisionTypes = $this->container->get(
'decision.types');
81 $this->clearingEventProcessor = $this->container->get(
'businessrules.clearing_event_processor');
82 $this->clearingDecisionFilter = $this->container->get(
'businessrules.clearing_decision_filter');
83 $this->clearingDecisionProcessor = $this->container->get(
'businessrules.clearing_decision_processor');
84 $this->agentLicenseEventProcessor = $this->container->get(
'businessrules.agent_license_event_processor');
94 $itemTreeBounds = $this->uploadDao->getParentItemBounds($uploadId);
95 foreach ($this->uploadDao->getReusedUpload($uploadId, $this->groupId) as $reuseTriple) {
97 $reusedUploadId = $reuseTriple[
'reused_upload_fk'];
99 $reusedGroupId = $reuseTriple[
'reused_group_fk'];
101 $reuseMode = $reuseTriple[
'reuse_mode'];
103 $itemTreeBoundsReused = $this->uploadDao->getParentItemBounds($reusedUploadId);
104 if (
false === $itemTreeBoundsReused) {
108 if ($reuseMode & UploadDao::REUSE_ENHANCED) {
111 $this->processUploadReuse($itemTreeBounds, $itemTreeBoundsReused, $reusedGroupId);
114 if ($reuseMode & UploadDao::REUSE_MAIN) {
115 $this->
reuseMainLicense($uploadId, $this->groupId, $reusedUploadId, $reusedGroupId);
118 if ($reuseMode & UploadDao::REUSE_CONF) {
119 $this->reuseConfSettings($uploadId, $reusedUploadId);
124 protected function reuseConfSettings($uploadId, $reusedUploadId)
126 if (!$this->uploadDao->insertReportConfReuse($uploadId, $reusedUploadId)) {
127 echo
"INFO :: Report configuration for select upload doesn't exists. Unable to copy!!!";
144 $mainLicenseIds = $this->clearingDao->getMainLicenseIds($reusedUploadId, $reusedGroupId);
145 if (!empty($mainLicenseIds)) {
146 foreach ($mainLicenseIds as $mainLicenseId) {
147 if (in_array($mainLicenseId, $this->clearingDao->getMainLicenseIds($uploadId,
$groupId))) {
150 $this->clearingDao->makeMainLicense($uploadId,
$groupId, $mainLicenseId);
165 protected function processUploadReuse($itemTreeBounds, $itemTreeBoundsReused, $reusedGroupId)
170 $clearingDecisions = $this->clearingDao->getFileClearingsFolder($itemTreeBoundsReused, $reusedGroupId);
171 $currenlyVisibleClearingDecisions = $this->clearingDao->getFileClearingsFolder($itemTreeBounds,
$groupId);
173 $currenlyVisibleClearingDecisionsById = $this->
mapByClearingId($currenlyVisibleClearingDecisions);
176 $clearingDecisionsToImport = array_diff_key($clearingDecisionsById,$currenlyVisibleClearingDecisionsById);
178 $clearingDecisionToImportByFileId = $this->
mapByFileId($clearingDecisionsToImport);
182 $containedItems = ArrayOperation::callChunked(
183 function ($fileIds) use ($itemTreeBounds,
$uploadDao)
187 "pfile_fk = ANY($1)",
188 array(
'{' . implode(
', ', $fileIds) .
'}')
190 }, array_keys($clearingDecisionToImportByFileId), 100);
192 foreach ($containedItems as $item) {
193 $fileId = $item->getFileId();
194 if (array_key_exists($fileId, $clearingDecisionToImportByFileId)) {
196 $clearingDecisionToImportByFileId[$fileId]);
198 throw new \Exception(
"bad internal state");
216 $clearingDecisions = $this->clearingDao->getFileClearingsFolder($itemTreeBoundsReused, $reusedGroupId);
217 $currenlyVisibleClearingDecisions = $this->clearingDao->getFileClearingsFolder($itemTreeBounds, $this->groupId);
219 $currenlyVisibleClearingDecisionsById = $this->
mapByClearingId($currenlyVisibleClearingDecisions);
222 $clearingDecisionsToImport = array_diff_key($clearingDecisionsById,$currenlyVisibleClearingDecisionsById);
224 $sql =
"SELECT ut.* FROM uploadtree ur, uploadtree ut WHERE ur.upload_fk=$2" 225 .
" AND ur.pfile_fk=$3 AND ut.upload_fk=$1 AND ut.ufile_name=ur.ufile_name";
226 $stmt = __METHOD__.
'.reuseByName';
228 $treeDao = $this->container->get(
'dao.tree');
230 foreach ($clearingDecisionsToImport as $clearingDecision) {
231 $reusedPath = $treeDao->getRepoPathOfPfile($clearingDecision->getPfileId());
233 $res = $this->
dbManager->execute($stmt,array($itemTreeBounds->getUploadId(),
234 $itemTreeBoundsReused->getUploadId(),$clearingDecision->getPfileId()));
235 while ($row = $this->
dbManager->fetchArray($res)) {
236 $newPath = $treeDao->getRepoPathOfPfile($row[
'pfile_fk']);
256 $diffLevel = system(
"diff $reusedPath $newPath | wc -l");
257 if ($diffLevel ===
false) {
258 throw new \Exception(
'cannot use diff tool');
260 if ($diffLevel < 5) {
261 $this->createCopyOfClearingDecision($itemId, $this->userId, $this->groupId, $clearingDecision);
275 $clearingDecisionByFileId = array();
276 foreach ($clearingDecisions as $clearingDecision) {
277 $fileId = $clearingDecision->getPfileId();
278 if (!array_key_exists($fileId, $clearingDecisionByFileId)) {
279 $clearingDecisionByFileId[$fileId] = $clearingDecision;
282 return $clearingDecisionByFileId;
292 protected function createCopyOfClearingDecision($itemId,
$userId,
$groupId, $clearingDecisionToCopy)
294 $clearingEventIdsToCopy = array();
296 foreach ($clearingDecisionToCopy->getClearingEvents() as $clearingEvent) {
297 $licenseId = $clearingEvent->getLicenseId();
298 $uploadTreeId = $itemId;
299 $isRemoved = $clearingEvent->isRemoved();
300 $type = ClearingEventTypes::USER;
301 $reportInfo = $clearingEvent->getReportinfo();
302 $comment = $clearingEvent->getComment();
303 $acknowledgement = $clearingEvent->getAcknowledgement();
305 $clearingEventIdsToCopy[] = $this->clearingDao->insertClearingEvent(
307 $type, $reportInfo, $comment, $acknowledgement,
$jobId);
310 $this->clearingDao->createDecisionFromEvents(
314 $clearingDecisionToCopy->getType(),
315 $clearingDecisionToCopy->getScope(),
316 $clearingEventIdsToCopy
332 foreach ($clearingDecisions as $clearingDecision) {
333 $mapped[$clearingDecision->getClearingId()] = $clearingDecision;
heartbeat($newProcessed)
Send hear beat to the scheduler.
processEnhancedUploadReuse($itemTreeBounds, $itemTreeBoundsReused, $reusedGroupId)
Get clearing decisions and use copyClearingDecisionIfDifferenceIsSmall()
reuseMainLicense($uploadId, $groupId, $reusedUploadId, $reusedGroupId)
Reuse main license from previous upload.
Structure of an Agent with all required parameters.
$agentLicenseEventProcessor
$clearingDecisionProcessor
processUploadId($uploadId)
Get the upload items and reuse based on reuse mode.
mapByFileId($clearingDecisions)
Maps clearing decisions by file id.
fo_dbManager * dbManager
fo_dbManager object
mapByClearingId($clearingDecisions)
Map clearing decisions by clearing id.
copyClearingDecisionIfDifferenceIsSmall($reusedPath, $newPath, $clearingDecision, $itemId)
Use diff tool to compare files.