11import { Component , OnInit , Input } from '@angular/core' ;
22import { UsageReportDataService } from 'src/app/core/statistics/usage-report-data.service' ;
3+ import { ConfigurationDataService } from 'src/app/core/data/configuration-data.service' ;
34import { catchError } from 'rxjs/operators' ;
4- import { of } from 'rxjs' ;
5+ import { BehaviorSubject , of } from 'rxjs' ;
56
67/**
78 * Component that displays the total number of downloads for all bitstreams within a DSpace item.
89 *
9- * This component fetches download statistics for a given item using its UUID and aggregates
10- * the download counts from all bitstreams associated with that item. The result is displayed
11- * as a single total download count.
10+ * This component checks the 'item.view.total.downloads.enabled' configuration property
11+ * to determine if download statistics should be displayed. If enabled, it fetches download
12+ * statistics for a given item using its UUID and aggregates the download counts from all
13+ * bitstreams associated with that item. The result is displayed as a single total download count.
14+ *
15+ * If the configuration is disabled or set to 'false', the component will not be displayed.
1216 */
1317@Component ( {
1418 selector : 'ds-total-downloads' ,
@@ -26,23 +30,36 @@ export class TotalDownloadsComponent implements OnInit {
2630 * The total number of downloads across all bitstreams for the item.
2731 * Defaults to 0 and will show 0 if no data is available or an error occurs.
2832 */
29- totalDownloads : number = 0 ;
33+ totalDownloads : number | null = 0 ;
34+
35+ /**
36+ * Flag indicating whether the total downloads feature is enabled in the configuration.
37+ * Uses BehaviorSubject to allow reactive updates. Defaults to false and will only be
38+ * set to true if the configuration explicitly contains 'true' value.
39+ */
40+ totalDownloadsEnabled = new BehaviorSubject < boolean > ( false ) ;
3041
3142 /**
3243 * The translation key for the downloadsLabel displayed alongside the download count.
3344 */
3445 readonly downloadsLabel = 'item.page.files.downloads' ;
3546
3647
37- constructor ( private usageReportDataService : UsageReportDataService ) { }
48+ constructor (
49+ private usageReportDataService : UsageReportDataService ,
50+ private configService : ConfigurationDataService
51+ ) { }
3852
3953 /**
40- * Fetches the total download statistics for the item specified by itemUuid.
54+ * Fetches the configuration to check if total downloads should be shown,
55+ * and if enabled, fetches the total download statistics for the item specified by itemUuid.
4156 * The component will:
42- * 1. Call the UsageReportDataService with the item UUID and 'TotalDownloads' report type
43- * 2. Aggregate all download counts (views) from all bitstreams in the response
44- * 3. Set the totalDownloads property with the sum
45- * 4. Handle errors gracefully by setting totalDownloads to 0 and logging the error
57+ * 1. Check the 'item.view.total.downloads.enabled' configuration property
58+ * 2. If enabled (configuration value is explicitly 'true'), call the UsageReportDataService
59+ * If configuration is not found or fails to load, defaults to false (disabled)
60+ * 3. Aggregate all download counts (views) from all bitstreams in the response
61+ * 4. Set the totalDownloads property with the sum
62+ * 5. Handle errors gracefully by returning null and logging the error
4663 *
4764 * @throws Will log an error to console if the API call fails, but won't throw an exception
4865 */
@@ -51,6 +68,34 @@ export class TotalDownloadsComponent implements OnInit {
5168 return ;
5269 }
5370
71+ // First, check if total downloads feature is enabled in configuration
72+ this . configService . findByPropertyName ( 'item.view.total.downloads.enabled' )
73+ . pipe (
74+ catchError ( error => {
75+ console . error ( 'Failed to fetch total downloads configuration:' , error ) ;
76+ // Default to false if configuration cannot be retrieved
77+ return of ( null ) ;
78+ } )
79+ )
80+ . subscribe ( configData => {
81+ // Extract configuration value, default to 'false' if not found
82+ const itemViewTotalDownloadsEnabled = configData ?. payload ?. values ?. [ 0 ] ;
83+ this . totalDownloadsEnabled . next ( itemViewTotalDownloadsEnabled === 'true' ) ;
84+
85+ // Only fetch download statistics if the feature is enabled
86+ if ( this . totalDownloadsEnabled . value ) {
87+ this . fetchDownloadStatistics ( ) ;
88+ } else {
89+ this . totalDownloads = null ; // Ensure it's null when disabled
90+ }
91+ } ) ;
92+ }
93+
94+ /**
95+ * Private method to fetch download statistics from the usage report service.
96+ * This method is called only when the total downloads feature is enabled.
97+ */
98+ private fetchDownloadStatistics ( ) : void {
5499 const reportType = 'TotalDownloads' ;
55100 this . usageReportDataService . getStatistic ( this . itemUuid , reportType )
56101 . pipe (
0 commit comments