-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy patharchiving.service.ts
131 lines (117 loc) · 3.62 KB
/
archiving.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { Injectable } from "@angular/core";
import { Store } from "@ngrx/store";
import { combineLatest, Observable } from "rxjs";
import { first, map } from "rxjs/operators";
import { Dataset, Job, User } from "state-management/models";
import { submitJobAction } from "state-management/actions/jobs.actions";
import {
selectCurrentUser,
selectTapeCopies,
selectProfile,
} from "state-management/selectors/user.selectors";
import { RetrieveDestinations } from "app-config.service";
@Injectable()
export class ArchivingService {
private currentUser$ = this.store.select(selectCurrentUser);
private tapeCopies$ = this.store.select(selectTapeCopies);
constructor(private store: Store) {}
private createJob(
user: User,
datasets: Dataset[],
archive: boolean,
destinationPath?: Record<string, string>,
// Do not specify tape copies here
): Job {
const extra = archive ? {} : destinationPath;
const jobParams = {
username: user.username,
...extra,
};
this.store.select(selectProfile).subscribe((profile) => {
user.email = profile.email;
});
const data = {
jobParams,
emailJobInitiator: user.email,
// Revise this, files == []...? See earlier version of this method in dataset-table component for context
datasetList: datasets.map((dataset) => ({
pid: dataset.pid,
files: [],
})),
type: archive ? "archive" : "retrieve",
};
return new Job(data);
}
private archiveOrRetrieve(
datasets: Dataset[],
archive: boolean,
destPath?: Record<string, string>,
): Observable<void> {
return combineLatest([this.currentUser$, this.tapeCopies$]).pipe(
first(),
map(([user, tapeCopies]) => {
if (user) {
const email = user.email;
if (email == null || email.length === 0) {
throw new Error(
"No email for this user could be found, the job will not be submitted",
);
}
if (datasets.length === 0) {
throw new Error("No datasets selected");
}
const job = this.createJob(user, datasets, archive, destPath);
this.store.dispatch(submitJobAction({ job }));
}
}),
);
}
public archive(datasets: Dataset[]): Observable<void> {
return this.archiveOrRetrieve(datasets, true);
}
public retrieve(
datasets: Dataset[],
destinationPath: Record<string, string>,
): Observable<void> {
return this.archiveOrRetrieve(datasets, false, destinationPath);
}
public generateOptionLocation(
result: RetrieveDestinations,
retrieveDestinations: RetrieveDestinations[] = [],
): object | { option: string } | { location: string; option: string } {
if (retrieveDestinations.length > 0) {
const prefix = retrieveDestinations.filter(
(element) => element.option == result.option,
);
let location =
prefix.length > 0
? (prefix[0].location || "") + (result.location || "")
: "";
let option = result.option;
if (!result.option) {
location = retrieveDestinations[0].location || "";
option = retrieveDestinations[0].option;
}
return {
option: option,
...(location != "" ? { location: location } : {}),
};
}
return {};
}
public retriveDialogOptions(
retrieveDestinations: RetrieveDestinations[] = [],
): object {
return {
width: "auto",
data: {
title: "Retrieve to",
question: "",
choice: {
options: retrieveDestinations,
},
option: retrieveDestinations?.[0]?.option,
},
};
}
}