Skip to content

Commit 9bf0320

Browse files
authored
Merge pull request DSpace#1707 from atmire/fix-1705
Fix curation tasks started from the edit community or collection page
2 parents e0109b9 + b55e432 commit 9bf0320

5 files changed

Lines changed: 122 additions & 9 deletions

File tree

src/app/curation-form/curation-form.component.spec.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { By } from '@angular/platform-browser';
1515
import { ConfigurationDataService } from '../core/data/configuration-data.service';
1616
import { ConfigurationProperty } from '../core/shared/configuration-property.model';
1717
import { getProcessDetailRoute } from '../process-page/process-page-routing.paths';
18+
import { HandleService } from '../shared/handle.service';
1819

1920
describe('CurationFormComponent', () => {
2021
let comp: CurationFormComponent;
@@ -23,6 +24,7 @@ describe('CurationFormComponent', () => {
2324
let scriptDataService: ScriptDataService;
2425
let processDataService: ProcessDataService;
2526
let configurationDataService: ConfigurationDataService;
27+
let handleService: HandleService;
2628
let notificationsService;
2729
let router;
2830

@@ -51,18 +53,23 @@ describe('CurationFormComponent', () => {
5153
}))
5254
});
5355

56+
handleService = {
57+
normalizeHandle: (a) => a
58+
} as any;
59+
5460
notificationsService = new NotificationsServiceStub();
5561
router = new RouterStub();
5662

5763
TestBed.configureTestingModule({
5864
imports: [TranslateModule.forRoot(), FormsModule, ReactiveFormsModule],
5965
declarations: [CurationFormComponent],
6066
providers: [
61-
{provide: ScriptDataService, useValue: scriptDataService},
62-
{provide: ProcessDataService, useValue: processDataService},
63-
{provide: NotificationsService, useValue: notificationsService},
64-
{provide: Router, useValue: router},
65-
{provide: ConfigurationDataService, useValue: configurationDataService},
67+
{ provide: ScriptDataService, useValue: scriptDataService },
68+
{ provide: ProcessDataService, useValue: processDataService },
69+
{ provide: NotificationsService, useValue: notificationsService },
70+
{ provide: HandleService, useValue: handleService },
71+
{ provide: Router, useValue: router},
72+
{ provide: ConfigurationDataService, useValue: configurationDataService },
6673
],
6774
schemas: [CUSTOM_ELEMENTS_SCHEMA]
6875
}).compileComponents();
@@ -143,4 +150,13 @@ describe('CurationFormComponent', () => {
143150
{name: '-i', value: 'all'},
144151
], []);
145152
});
153+
154+
it(`should show an error notification and return when an invalid dsoHandle is provided`, () => {
155+
comp.dsoHandle = 'test-handle';
156+
spyOn(handleService, 'normalizeHandle').and.returnValue(null);
157+
comp.submit();
158+
159+
expect(notificationsService.error).toHaveBeenCalled();
160+
expect(scriptDataService.invoke).not.toHaveBeenCalled();
161+
});
146162
});

src/app/curation-form/curation-form.component.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { getFirstCompletedRemoteData } from '../core/shared/operators';
55
import { find, map } from 'rxjs/operators';
66
import { NotificationsService } from '../shared/notifications/notifications.service';
77
import { TranslateService } from '@ngx-translate/core';
8-
import { hasValue, isEmpty, isNotEmpty } from '../shared/empty.util';
8+
import { hasValue, isEmpty, isNotEmpty, hasNoValue } from '../shared/empty.util';
99
import { RemoteData } from '../core/data/remote-data';
1010
import { Router } from '@angular/router';
1111
import { ProcessDataService } from '../core/data/processes/process-data.service';
@@ -14,9 +14,9 @@ import { ConfigurationDataService } from '../core/data/configuration-data.servic
1414
import { ConfigurationProperty } from '../core/shared/configuration-property.model';
1515
import { Observable } from 'rxjs';
1616
import { getProcessDetailRoute } from '../process-page/process-page-routing.paths';
17+
import { HandleService } from '../shared/handle.service';
1718

1819
export const CURATION_CFG = 'plugin.named.org.dspace.curate.CurationTask';
19-
2020
/**
2121
* Component responsible for rendering the Curation Task form
2222
*/
@@ -39,6 +39,7 @@ export class CurationFormComponent implements OnInit {
3939
private processDataService: ProcessDataService,
4040
private notificationsService: NotificationsService,
4141
private translateService: TranslateService,
42+
private handleService: HandleService,
4243
private router: Router
4344
) {
4445
}
@@ -76,13 +77,19 @@ export class CurationFormComponent implements OnInit {
7677
const taskName = this.form.get('task').value;
7778
let handle;
7879
if (this.hasHandleValue()) {
79-
handle = this.dsoHandle;
80+
handle = this.handleService.normalizeHandle(this.dsoHandle);
81+
if (isEmpty(handle)) {
82+
this.notificationsService.error(this.translateService.get('curation.form.submit.error.head'),
83+
this.translateService.get('curation.form.submit.error.invalid-handle'));
84+
return;
85+
}
8086
} else {
81-
handle = this.form.get('handle').value;
87+
handle = this.handleService.normalizeHandle(this.form.get('handle').value);
8288
if (isEmpty(handle)) {
8389
handle = 'all';
8490
}
8591
}
92+
8693
this.scriptDataService.invoke('curate', [
8794
{ name: '-t', value: taskName },
8895
{ name: '-i', value: handle },
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { HandleService } from './handle.service';
2+
3+
describe('HandleService', () => {
4+
let service: HandleService;
5+
6+
beforeEach(() => {
7+
service = new HandleService();
8+
});
9+
10+
describe(`normalizeHandle`, () => {
11+
it(`should simply return an already normalized handle`, () => {
12+
let input, output;
13+
14+
input = '123456789/123456';
15+
output = service.normalizeHandle(input);
16+
expect(output).toEqual(input);
17+
18+
input = '12.3456.789/123456';
19+
output = service.normalizeHandle(input);
20+
expect(output).toEqual(input);
21+
});
22+
23+
it(`should normalize a handle url`, () => {
24+
let input, output;
25+
26+
input = 'https://hdl.handle.net/handle/123456789/123456';
27+
output = service.normalizeHandle(input);
28+
expect(output).toEqual('123456789/123456');
29+
30+
input = 'https://rest.api/server/handle/123456789/123456';
31+
output = service.normalizeHandle(input);
32+
expect(output).toEqual('123456789/123456');
33+
});
34+
35+
it(`should return null if the input doesn't contain a handle`, () => {
36+
let input, output;
37+
38+
input = 'https://hdl.handle.net/handle/123456789';
39+
output = service.normalizeHandle(input);
40+
expect(output).toBeNull();
41+
42+
input = 'something completely different';
43+
output = service.normalizeHandle(input);
44+
expect(output).toBeNull();
45+
});
46+
});
47+
});

src/app/shared/handle.service.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Injectable } from '@angular/core';
2+
import { isNotEmpty, isEmpty } from './empty.util';
3+
4+
const PREFIX_REGEX = /handle\/([^\/]+\/[^\/]+)$/;
5+
const NO_PREFIX_REGEX = /^([^\/]+\/[^\/]+)$/;
6+
7+
@Injectable({
8+
providedIn: 'root'
9+
})
10+
export class HandleService {
11+
12+
13+
/**
14+
* Turns a handle string into the default 123456789/12345 format
15+
*
16+
* @param handle the input handle
17+
*
18+
* normalizeHandle('123456789/123456') // '123456789/123456'
19+
* normalizeHandle('12.3456.789/123456') // '12.3456.789/123456'
20+
* normalizeHandle('https://hdl.handle.net/handle/123456789/123456') // '123456789/123456'
21+
* normalizeHandle('https://rest.api/server/handle/123456789/123456') // '123456789/123456'
22+
* normalizeHandle('https://rest.api/server/handle/123456789') // null
23+
*/
24+
normalizeHandle(handle: string): string {
25+
let matches: string[];
26+
if (isNotEmpty(handle)) {
27+
matches = handle.match(PREFIX_REGEX);
28+
}
29+
30+
if (isEmpty(matches) || matches.length < 2) {
31+
matches = handle.match(NO_PREFIX_REGEX);
32+
}
33+
34+
if (isEmpty(matches) || matches.length < 2) {
35+
return null;
36+
} else {
37+
return matches[1];
38+
}
39+
}
40+
41+
}

src/assets/i18n/en.json5

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,6 +1291,8 @@
12911291

12921292
"curation.form.submit.error.content": "An error occured when trying to start the curation task.",
12931293

1294+
"curation.form.submit.error.invalid-handle": "Couldn't determine the handle for this object",
1295+
12941296
"curation.form.handle.label": "Handle:",
12951297

12961298
"curation.form.handle.hint": "Hint: Enter [your-handle-prefix]/0 to run a task across entire site (not all tasks may support this capability)",

0 commit comments

Comments
 (0)