Skip to content

Commit 810a2e8

Browse files
authored
Merge pull request #493 from cap-java/hideUploadButton
Disable upload button when max count is reached
2 parents e2678ac + 27546f9 commit 810a2e8

3 files changed

Lines changed: 1295 additions & 4 deletions

File tree

README.md

Lines changed: 86 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ This plugin can be consumed by the CAP application deployed on BTP to store thei
1414
- Draft functionality : Provides the capability of working with draft attachments.
1515
- Display attachments specific to repository: Lists attachments contained in the repository that is configured with the CAP application.
1616
- Custom properties : Provides the capability to define custom properties for attachments.
17-
- Maximum allowed uploads: Provides the capability to define the maximum number of uploads allowed for the user.
17+
- Maximum allowed uploads: Provides the capability to define the maximum number of uploads allowed for the user. Automatically disables the upload button in the UI when the limit is reached.
1818
- Maximum file size: Provides the capability to specify the maximum file size for attachments.
1919
- Multiple attachment facets: Provides the capability to define multiple attachment facets/sections in the CAP Entity.
2020
- Technical user support: Provides the capability to consume the plugin using technical user.
@@ -37,6 +37,7 @@ This plugin can be consumed by the CAP application deployed on BTP to store thei
3737
- [Support for Multitenancy](#support-for-multitenancy)
3838
- [Support for Custom Properties](#support-for-custom-properties)
3939
- [Support for Maximum allowed uploads](#support-for-maximum-allowed-uploads)
40+
- [Upload Button Auto-Disable in the UI](#upload-button-auto-disable-in-the-ui)
4041
- [Support for Maximum File Size](#support-for-maximum-file-size)
4142
- [Support for Multiple attachment facets](#support-for-multiple-attachment-facets)
4243
- [Support for Technical user](#support-for-technical-user)
@@ -494,9 +495,90 @@ Example for German language in `messages_de.properties`:
494495
SDM.maxCountErrorMessage = Maximale Anzahl von Anhängen erreicht
495496
```
496497

497-
> **Note**
498-
>
499-
> Once the maxCount is configured, it is recommended not to alter it. If the maxCount is altered, the previously uploaded documents will still be visible.
498+
#### Upload Button Auto-Disable in the UI
499+
500+
When `maxCount` is configured, the plugin automatically computes a virtual boolean field (e.g. `isAttachmentsUploadable`) on the parent entity at read time and sets it to `false` when the limit is reached. To wire this up in your Fiori UI so the **Upload button is automatically disabled**, follow the steps below.
501+
502+
**1. Declare the virtual field on the parent entity**
503+
504+
Add a `virtual` boolean field for each `maxCount` annotated composition directly on the parent entity in your CDS schema. The field name must follow the pattern `is` + capitalised composition name + `Uploadable`:
505+
506+
```cds
507+
entity Books : managed, cuid {
508+
// ... other fields ...
509+
virtual isAttachmentsUploadable : Boolean;
510+
virtual isReferencesUploadable : Boolean;
511+
512+
attachments : Composition of many Attachments @SDM.Attachments:{maxCount: 4};
513+
references : Composition of many Attachments @SDM.Attachments:{maxCount: 2};
514+
}
515+
```
516+
517+
- For a composition named `attachments` declare `virtual isAttachmentsUploadable : Boolean`.
518+
- For `references` declare `virtual isReferencesUploadable : Boolean`, and so on.
519+
- Virtual fields are never stored in the database; the plugin populates them at read time.
520+
521+
**2. Disable the Upload button via `InsertRestrictions`**
522+
523+
Annotate each attachment entity in your service to bind its insertability to the virtual field on the parent:
524+
525+
```cds
526+
annotate MyService.Books.attachments with @(
527+
Capabilities: {InsertRestrictions: {Insertable: up_.isAttachmentsUploadable}}
528+
);
529+
```
530+
531+
- Replace `MyService.Books.attachments` with your service and entity path.
532+
- Repeat for every composition facet that has a `maxCount`.
533+
534+
**3. Refresh the parent after attachment changes via `SideEffects`**
535+
536+
After an upload or deletion, Fiori must re-read the parent entity to pick up the updated virtual field and reflect the new button state. Add a named `Common.SideEffects` annotation on the parent entity:
537+
538+
```cds
539+
annotate MyService.Books with @(
540+
Common.SideEffects #attachmentsUploadable: {
541+
SourceEntities: ['attachments'],
542+
TargetProperties: ['']
543+
}
544+
);
545+
```
546+
547+
- `SourceEntities: ['attachments']` — triggers the refresh when the `attachments` list changes.
548+
- `TargetEntities: ['']` — re-fetches the parent entity (`Books`) so the updated `isAttachmentsUploadable` value is returned to the UI.
549+
- The qualifier (e.g. `#attachmentsUploadable`) can be any unique name; it is only needed to distinguish multiple `SideEffects` annotations on the same entity.
550+
551+
**Example with multiple facets**
552+
553+
If an entity has several `maxCount`-annotated compositions, add one `virtual field` declaration, one `InsertRestrictions`, and one `SideEffects` per facet:
554+
555+
556+
```cds
557+
annotate MyService.Books.attachments with @(
558+
Capabilities: {InsertRestrictions: {Insertable: up_.isAttachmentsUploadable}}
559+
);
560+
561+
annotate MyService.Books.references with @(
562+
Capabilities: {InsertRestrictions: {Insertable: up_.isReferencesUploadable}}
563+
);
564+
565+
annotate MyService.Books with @(
566+
Common.SideEffects #attachmentsUploadable: {
567+
SourceEntities: ['attachments'],
568+
TargetProperties: ['isAttachmentsUploadable']
569+
},
570+
Common.SideEffects #referencesUploadable: {
571+
SourceEntities: ['references'],
572+
TargetProperties: ['isAttachmentsUploadable']
573+
}
574+
);
575+
```
576+
See this [example](https://github.com/cap-java/sdm/blob/cc537c5c855ad59fa14100a397536ab22f4b1aa7/cap-notebook/demoapp/db/schema.cds#L19) of `virtual field` declaration from a sample Bookshop app.
577+
578+
See this [example](https://github.com/cap-java/sdm/blob/cc537c5c855ad59fa14100a397536ab22f4b1aa7/cap-notebook/demoapp/srv/admin-service.cds#L46) of `InsertRestriction` annotation from a sample Bookshop app.
579+
580+
See this [example](https://github.com/cap-java/sdm/blob/cc537c5c855ad59fa14100a397536ab22f4b1aa7/cap-notebook/demoapp/srv/admin-service.cds#L279) of `SideEffects` annotation from a sample Bookshop app.
581+
500582

501583
## Support for Maximum File Size
502584

0 commit comments

Comments
 (0)