|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Controller\UI; |
| 4 | + |
| 5 | +use App\Entity\Dataset; |
| 6 | +use App\Entity\DOI; |
| 7 | +use App\Enum\DatasetLifecycleStatus; |
| 8 | +use App\Repository\DatasetRepository; |
| 9 | +use App\Util\FundingOrgFilter; |
| 10 | +use App\Util\Geometry; |
| 11 | +use Doctrine\ORM\EntityManagerInterface; |
| 12 | +use Doctrine\ORM\Query\Expr\Join; |
| 13 | +use Symfony\Component\HttpFoundation\StreamedResponse; |
| 14 | +use Symfony\Component\Routing\Annotation\Route; |
| 15 | + |
| 16 | +/** |
| 17 | + * Dataset export report generator. |
| 18 | + */ |
| 19 | +class DatasetExportController extends ReportController |
| 20 | +{ |
| 21 | + //DateTime format used for date range |
| 22 | + public const INREPORT_DATETIMEFORMAT = 'm-d-Y'; |
| 23 | + |
| 24 | + /** |
| 25 | + * This is a parameterless report, so all is in the default action. |
| 26 | + */ |
| 27 | + #[Route('/dataset/export')] |
| 28 | + public function generateCsv(DatasetRepository $datasetRepository, FundingOrgFilter $fundingOrgFilter, EntityManagerInterface $entityManager): StreamedResponse |
| 29 | + { |
| 30 | + $data = $this->getData($datasetRepository, $fundingOrgFilter, $entityManager); |
| 31 | + |
| 32 | + return $this->writeCsvResponse( |
| 33 | + $data, |
| 34 | + 'DatasetExport-' . |
| 35 | + (new \DateTime('now'))->format(parent::FILENAME_DATETIMEFORMAT) . |
| 36 | + '.csv' |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * This method gets data for the report. |
| 42 | + */ |
| 43 | + protected function getData(DatasetRepository $datasetRepository, FundingOrgFilter $fundingOrgFilter, EntityManagerInterface $entityManager): array |
| 44 | + { |
| 45 | + $researchGroupIDs = []; |
| 46 | + if ($fundingOrgFilter->isActive()) { |
| 47 | + $researchGroupIDs = $fundingOrgFilter->getResearchGroupsIdArray(); |
| 48 | + } |
| 49 | + |
| 50 | + //Query for datasets, filtered as appropriate. |
| 51 | + $qb = $entityManager->createQueryBuilder(); |
| 52 | + $qb |
| 53 | + ->select('d.udi, d.id') |
| 54 | + ->from('\App\Entity\Dataset', 'd') |
| 55 | + ->join('\App\Entity\ResearchGroup', 'rg', Join::WITH, 'rg.id = d.researchGroup') |
| 56 | + ->join('\App\Entity\FundingCycle', 'fc', Join::WITH, 'fc.id = rg.fundingCycle') |
| 57 | + ->join('\App\Entity\FundingOrganization', 'fo', Join::WITH, 'fo.id = fc.fundingOrganization') |
| 58 | + ->orderBy('d.id', 'DESC'); |
| 59 | + |
| 60 | + $researchGroupIDs = []; |
| 61 | + if ($fundingOrgFilter->isActive()) { |
| 62 | + $researchGroupIDs = $fundingOrgFilter->getResearchGroupsIdArray(); |
| 63 | + $qb->andWhere('fo.id IN (:fundingOrg)'); |
| 64 | + $qb->setParameter('fundingOrg', $fundingOrgFilter->getFilterIdArray()); |
| 65 | + } |
| 66 | + $query = $qb->getQuery(); |
| 67 | + $results = $query->getResult(); |
| 68 | + |
| 69 | + $dataArray = []; |
| 70 | + //process result query into an array with organized data |
| 71 | + $currentIndex = 0; |
| 72 | + // used to calculate bounding-box envelope from too-complex for CSV GML. |
| 73 | + $geometryUtil = new Geometry($entityManager); |
| 74 | + |
| 75 | + foreach ($results as $result) { |
| 76 | + $dataset = $datasetRepository->findOneBy(array('udi' => $result['udi'])); |
| 77 | + |
| 78 | + /** @psalm-suppress PossiblyNullReference, guaranteed by DQL query above. */ |
| 79 | + $datasetLifeCycleStatus = $dataset->getDatasetLifecycleStatus(); |
| 80 | + if ($datasetLifeCycleStatus === DatasetLifecycleStatus::NONE) { |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + //initialize array with key = udi |
| 85 | + if (isset($dataArray[$currentIndex]['udi']) && $result['udi'] != $dataArray[$currentIndex]['udi']) { |
| 86 | + $currentIndex++; |
| 87 | + } |
| 88 | + if (!isset($dataArray[$currentIndex])) { |
| 89 | + $dataArray[$currentIndex] = array( |
| 90 | + 'fundingOrg.name' => null, |
| 91 | + 'fundingCycle.name' => null, |
| 92 | + 'researchGroup.name' => null, |
| 93 | + 'udi' => $result['udi'], |
| 94 | + 'doi' => null, |
| 95 | + 'LifeCycleStatus' => $datasetLifeCycleStatus->value, |
| 96 | + 'title' => null, |
| 97 | + 'totalFileSizeMB' => null, |
| 98 | + 'parameters.units' => null, |
| 99 | + 'locationDescription' => null, |
| 100 | + 'spatialExtent' => null, |
| 101 | + 'fileFormat' => null, |
| 102 | + 'timePeriodDescription' => null, |
| 103 | + 'temporalExtent.start' => null, |
| 104 | + 'temporalExtent.end' => null, |
| 105 | + 'themeKeywords' => null, |
| 106 | + 'placeKeywords' => null, |
| 107 | + 'topicKeywords' => null, |
| 108 | + ); |
| 109 | + |
| 110 | + $dataArray[$currentIndex]['fundingOrg.name'] = $dataset->getResearchGroup()->getFundingCycle()->getFundingOrganization()->getName(); |
| 111 | + $dataArray[$currentIndex]['fundingCycle.name'] = $dataset->getResearchGroup()->getFundingCycle()->getName(); |
| 112 | + $dataArray[$currentIndex]['researchGroup.name'] = $dataset->getResearchGroup()->getName(); |
| 113 | + if ($dataset->getDoi() instanceof DOI) { |
| 114 | + $dataArray[$currentIndex]['doi'] = $dataset->getDoi()->getDoi(); |
| 115 | + } |
| 116 | + $dataArray[$currentIndex]['title'] = $dataset->getTitle(); |
| 117 | + $dataArray[$currentIndex]['totalFileSizeMB'] = ($dataset->getTotalFileSize()) / 1000 ** 2; |
| 118 | + $dataArray[$currentIndex]['parameters.units'] = $dataset->getDatasetSubmission()?->getSuppParams(); |
| 119 | + $dataArray[$currentIndex]['locationDescription'] = $dataset->getDatasetSubmission()?->getSpatialExtentDescription(); |
| 120 | + if ($dataset?->getSpatialExtentGeometry() !== null) { |
| 121 | + $dataArray[$currentIndex]['spatialExtent'] = $geometryUtil->calculateEnvelopeFromGml($dataset->getSpatialExtentGeometry()); |
| 122 | + }; |
| 123 | + $dataArray[$currentIndex]['fileFormat'] = $dataset->getDatasetSubmission()?->getDistributionFormatName(); |
| 124 | + $dataArray[$currentIndex]['timePeriodDescription'] = $dataset->getDatasetSubmission()?->getTemporalExtentDesc(); |
| 125 | + $dataArray[$currentIndex]['temporalExtent.start'] = $dataset->getDatasetSubmission()?->getTemporalExtentBeginPosition()?->format(self::INREPORT_DATETIMEFORMAT); |
| 126 | + $dataArray[$currentIndex]['temporalExtent.end'] = $dataset->getDatasetSubmission()?->getTemporalExtentEndPosition()?->format(self::INREPORT_DATETIMEFORMAT); |
| 127 | + $dataArray[$currentIndex]['themeKeywords'] = $dataset->getDatasetSubmission()?->getThemeKeywordsString(); |
| 128 | + $dataArray[$currentIndex]['placeKeywords'] = $dataset->getDatasetSubmission()?->getPlaceKeywordsString(); |
| 129 | + $dataArray[$currentIndex]['topicKeywords'] = $dataset->getDatasetSubmission()?->getTopicKeywordsString(); |
| 130 | + } |
| 131 | + } |
| 132 | + return array_merge($this->getDefaultHeaders(), $this->getLabels(), $dataArray); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Get labels for the report. |
| 137 | + * |
| 138 | + * @param string $reportName Name of the report. |
| 139 | + * |
| 140 | + * @return array |
| 141 | + */ |
| 142 | + private function getLabels(): array |
| 143 | + { |
| 144 | + //prepare labels |
| 145 | + $labels = ['labels' => [ |
| 146 | + 'fundingOrg.name', |
| 147 | + 'fundingCycle.name', |
| 148 | + 'researchGroup.name', |
| 149 | + 'udi', |
| 150 | + 'doi', |
| 151 | + 'status', |
| 152 | + 'title', |
| 153 | + 'totalFileSizeMB', |
| 154 | + 'parameters.units', |
| 155 | + 'locationDescription', |
| 156 | + 'spatialExtentEnvelope', |
| 157 | + 'fileFormat', |
| 158 | + 'timePeriodDescription', |
| 159 | + 'temporalExtent.start', |
| 160 | + 'temporalExtent.end', |
| 161 | + 'themeKeywords', |
| 162 | + 'placeKeywords', |
| 163 | + 'topicKeywords', |
| 164 | + ]]; |
| 165 | + return $labels; |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * This returns the Report name extracted from the controller name and a creation timestamp. |
| 170 | + * |
| 171 | + * @return array Report Name to be displayed and a creation time stamp for the csv |
| 172 | + */ |
| 173 | + protected function getDefaultHeaders() |
| 174 | + { |
| 175 | + //generic report name extracted from the controller's name |
| 176 | + $reportNameCamelCase = preg_replace('/Controller$/', '', (new \ReflectionClass($this))->getShortName()); |
| 177 | + return array( |
| 178 | + array(trim(strtoupper(preg_replace('/(?<!\ )[A-Z]/', ' $0', $reportNameCamelCase)))), |
| 179 | + array('CREATED AT', (new \DateTime('now'))->format(self::INREPORT_DATETIMEFORMAT)), |
| 180 | + array(self::BLANK_LINE) |
| 181 | + ); |
| 182 | + } |
| 183 | +} |
0 commit comments