|
2 | 2 |
|
3 | 3 | import com.google.gson.Gson;
|
4 | 4 | import com.google.inject.Inject;
|
5 |
| -import jakarta.ws.rs.BadRequestException; |
6 |
| -import jakarta.ws.rs.NotAuthorizedException; |
7 |
| -import jakarta.ws.rs.NotFoundException; |
8 | 5 | import jakarta.ws.rs.core.StreamingOutput;
|
9 | 6 | import java.io.InputStream;
|
10 | 7 | import java.sql.SQLException;
|
11 |
| -import java.util.Date; |
12 | 8 | import java.util.List;
|
13 | 9 | import java.util.Map;
|
14 |
| -import java.util.Objects; |
15 |
| -import java.util.Optional; |
16 | 10 | import java.util.Set;
|
17 | 11 | import java.util.UUID;
|
18 |
| -import org.broadinstitute.consent.http.db.DraftDAO; |
19 |
| -import org.broadinstitute.consent.http.enumeration.UserRoles; |
| 12 | +import org.broadinstitute.consent.http.cloudstore.GCSService; |
20 | 13 | import org.broadinstitute.consent.http.models.DraftInterface;
|
21 | 14 | import org.broadinstitute.consent.http.models.DraftSummary;
|
22 | 15 | import org.broadinstitute.consent.http.models.FileStorageObject;
|
23 | 16 | import org.broadinstitute.consent.http.models.User;
|
| 17 | +import org.broadinstitute.consent.http.service.dao.DraftServiceDAO; |
| 18 | +import org.broadinstitute.consent.http.util.ConsentLogger; |
24 | 19 | import org.broadinstitute.consent.http.util.gson.GsonUtil;
|
25 | 20 | import org.glassfish.jersey.media.multipart.FormDataBodyPart;
|
26 |
| -import org.jdbi.v3.core.Jdbi; |
27 | 21 |
|
28 |
| -public class DraftService { |
29 |
| - |
30 |
| - private final Jdbi jdbi; |
31 |
| - private final DraftDAO draftDAO; |
32 |
| - private final DraftFileStorageService draftFileStorageService; |
| 22 | +public class DraftService implements ConsentLogger { |
| 23 | + DraftServiceDAO draftServiceDAO; |
| 24 | + GCSService gcsService; |
33 | 25 |
|
34 | 26 | @Inject
|
35 |
| - public DraftService(Jdbi jdbi, DraftDAO draftDAO, |
36 |
| - DraftFileStorageService draftFileStorageService) { |
37 |
| - this.jdbi = jdbi; |
38 |
| - this.draftDAO = draftDAO; |
39 |
| - this.draftFileStorageService = draftFileStorageService; |
40 |
| - } |
41 |
| - |
42 |
| - public void insertDraft(DraftInterface draft) |
43 |
| - throws SQLException, BadRequestException { |
44 |
| - jdbi.useHandle(handle -> { |
45 |
| - handle.getConnection().setAutoCommit(false); |
46 |
| - try { |
47 |
| - draftDAO.insert(draft.getName(), draft.getCreateDate().toInstant(), |
48 |
| - draft.getCreateUser().getUserId(), draft.getJson(), draft.getUUID(), |
49 |
| - draft.getType().getValue()); |
50 |
| - } catch (Exception e) { |
51 |
| - handle.rollback(); |
52 |
| - throw new BadRequestException( |
53 |
| - "Error submitting draft. Drafts require valid json to be submitted."); |
54 |
| - } |
55 |
| - handle.commit(); |
56 |
| - }); |
57 |
| - } |
58 |
| - |
59 |
| - public DraftInterface updateDraft(DraftInterface draft, User user) throws SQLException { |
60 |
| - draft.setUpdateUser(user); |
61 |
| - draft.setUpdateDate(new Date()); |
62 |
| - jdbi.useHandle(handle -> { |
63 |
| - handle.getConnection().setAutoCommit(false); |
64 |
| - try { |
65 |
| - draftDAO.updateDraftByDraftUUID(draft.getName(), |
66 |
| - draft.getUpdateDate().toInstant(), draft.getUpdateUser().getUserId(), draft.getJson(), |
67 |
| - draft.getUUID(), draft.getType().getValue()); |
68 |
| - } catch (Exception e) { |
69 |
| - handle.rollback(); |
70 |
| - } |
71 |
| - handle.commit(); |
72 |
| - }); |
73 |
| - return getAuthorizedDraft(draft.getUUID(), user); |
| 27 | + public DraftService(DraftServiceDAO draftServiceDAO, GCSService gcsService) { |
| 28 | + this.draftServiceDAO = draftServiceDAO; |
| 29 | + this.gcsService = gcsService; |
74 | 30 | }
|
75 | 31 |
|
76 |
| - public DraftInterface getAuthorizedDraft(UUID draftUUID, User user) { |
77 |
| - DraftInterface draft; |
78 |
| - try { |
79 |
| - draft = findDraftByDraftUUID(draftUUID); |
80 |
| - } catch (SQLException e) { |
81 |
| - throw new NotFoundException( |
82 |
| - String.format("Draft with UUID %s not found.", draftUUID.toString())); |
83 |
| - } |
84 |
| - if (Objects.isNull(draft)) { |
85 |
| - throw new NotFoundException( |
86 |
| - String.format("Draft with UUID %s not found.", draftUUID.toString())); |
87 |
| - } |
88 |
| - if (!user.getUserId().equals(draft.getCreateUser().getUserId()) && !user.hasUserRole( |
89 |
| - UserRoles.ADMIN)) { |
90 |
| - throw new NotAuthorizedException("User not authorized to modify resource."); |
91 |
| - } |
92 |
| - return draft; |
| 32 | + public DraftInterface getAuthorizedDraft(UUID uuid, User user) { |
| 33 | + return draftServiceDAO.getAuthorizedDraft(uuid, user); |
93 | 34 | }
|
94 | 35 |
|
95 |
| - public void deleteDraftsByUser(User user) { |
96 |
| - Set<DraftInterface> userDrafts = findDraftsForUser(user); |
97 |
| - for (DraftInterface draft : userDrafts) { |
98 |
| - deleteDraft(draft, user); |
99 |
| - } |
100 |
| - } |
101 |
| - |
102 |
| - public Set<DraftSummary> findDraftSummariesForUser(User user) { |
103 |
| - return draftDAO.findDraftSummariesByUserId(user.getUserId()); |
| 36 | + public List<FileStorageObject> addAttachments(DraftInterface draft, User user, Map<String, FormDataBodyPart> files) |
| 37 | + throws SQLException { |
| 38 | + return draftServiceDAO.addAttachments(draft, user, files); |
104 | 39 | }
|
105 | 40 |
|
106 |
| - public Set<DraftInterface> findDraftsForUser(User user) { |
107 |
| - return draftDAO.findDraftsByUserId(user.getUserId()); |
| 41 | + public InputStream getDraftAttachmentStream(FileStorageObject targetAttachment) { |
| 42 | + return gcsService.getDocument(targetAttachment.getBlobId()); |
108 | 43 | }
|
109 | 44 |
|
110 |
| - private DraftInterface findDraftByDraftUUID( |
111 |
| - UUID draftUUID) throws SQLException { |
112 |
| - return draftDAO.findDraftById(draftUUID); |
| 45 | + public void deleteDraftAttachment(DraftInterface draft, User user, Integer fileId) |
| 46 | + throws SQLException { |
| 47 | + draftServiceDAO.deleteDraftAttachment(draft, user, fileId); |
113 | 48 | }
|
114 | 49 |
|
115 |
| - public List<FileStorageObject> addAttachments(DraftInterface draft, User user, |
116 |
| - Map<String, FormDataBodyPart> files) throws SQLException { |
117 |
| - List<FileStorageObject> storedFiles = draftFileStorageService.storeDraftFiles(draft.getUUID(), user, files); |
118 |
| - draftDAO.updateDraftByDraftUUID(draft.getUUID(), |
119 |
| - new Date().toInstant(), user.getUserId()); |
120 |
| - return storedFiles; |
| 50 | + public StreamingOutput draftAsJson(DraftInterface draft) { |
| 51 | + Gson gson = GsonUtil.buildGson(); |
| 52 | + return output -> { |
| 53 | + output.write("{ \"document\":".getBytes()); |
| 54 | + output.write(draft.getJson().getBytes()); |
| 55 | + output.write(", \"meta\":".getBytes()); |
| 56 | + output.write(gson.toJson(draft).getBytes()); |
| 57 | + output.write("}".getBytes()); |
| 58 | + }; |
121 | 59 | }
|
122 | 60 |
|
123 |
| - public void deleteDraftAttachment(DraftInterface draft, User user, Integer fileId) |
124 |
| - throws SQLException { |
125 |
| - Optional<FileStorageObject> fileStorageObjectToDelete = draft.getStoredFiles().stream() |
126 |
| - .filter(fileStorageObject -> fileStorageObject.getFileStorageObjectId().equals(fileId)) |
127 |
| - .findFirst(); |
128 |
| - if (fileStorageObjectToDelete.isPresent()) { |
129 |
| - draftFileStorageService.deleteStoredFile(fileStorageObjectToDelete.get(), user); |
130 |
| - draftDAO.updateDraftByDraftUUID(draft.getUUID(), |
131 |
| - new Date().toInstant(), user.getUserId()); |
132 |
| - } else { |
133 |
| - throw new NotFoundException( |
134 |
| - String.format("Draft attachment is not found. Draft: %s, Attachment: %d", |
135 |
| - draft.getUUID(), fileId)); |
136 |
| - } |
| 61 | + public void insertDraft(DraftInterface draft) throws SQLException { |
| 62 | + draftServiceDAO.insertDraft(draft); |
137 | 63 | }
|
138 | 64 |
|
139 |
| - public void deleteDraft(DraftInterface draft, User user) |
140 |
| - throws RuntimeException { |
141 |
| - jdbi.useHandle(handle -> { |
142 |
| - try { |
143 |
| - handle.useTransaction(handler -> { |
144 |
| - draftDAO.deleteDraftByUUIDList(List.of(draft.getUUID())); |
145 |
| - draft.getStoredFiles().forEach(fileStorageObject -> { |
146 |
| - try { |
147 |
| - draftFileStorageService.deleteStoredFile(fileStorageObject, user); |
148 |
| - } catch (SQLException e) { |
149 |
| - throw new RuntimeException(e); |
150 |
| - } |
151 |
| - }); |
152 |
| - }); |
153 |
| - } catch (Exception e) { |
154 |
| - handle.rollback(); |
155 |
| - } |
156 |
| - handle.commit(); |
157 |
| - }); |
| 65 | + public Set<DraftSummary> findDraftSummariesForUser(User user) { |
| 66 | + return draftServiceDAO.findDraftSummariesForUser(user); |
158 | 67 | }
|
159 | 68 |
|
160 |
| - public StreamingOutput draftAsJson(DraftInterface draft) { |
161 |
| - Gson gson = GsonUtil.buildGson(); |
162 |
| - return output -> { |
163 |
| - output.write("{ \"document\":".getBytes()); |
164 |
| - output.write(draft.getJson().getBytes()); |
165 |
| - output.write(", \"meta\":".getBytes()); |
166 |
| - output.write(gson.toJson(draft).getBytes()); |
167 |
| - output.write("}".getBytes()); |
168 |
| - }; |
| 69 | + public DraftInterface updateDraft(DraftInterface draft, User user) throws SQLException { |
| 70 | + return draftServiceDAO.updateDraft(draft, user); |
169 | 71 | }
|
170 | 72 |
|
171 |
| - public InputStream getDraftAttachmentStream(FileStorageObject targetAttachment) { |
172 |
| - return draftFileStorageService.get(targetAttachment); |
| 73 | + public void deleteDraft(DraftInterface draft, User user) { |
| 74 | + draftServiceDAO.deleteDraft(draft, user); |
173 | 75 | }
|
174 | 76 | }
|
0 commit comments