|
| 1 | +package com.objectcomputing.checkins.services.file; |
| 2 | + |
| 3 | +import com.objectcomputing.checkins.services.checkindocument.CheckinDocument; |
| 4 | +import com.objectcomputing.checkins.services.checkindocument.CheckinDocumentServices; |
| 5 | +import com.objectcomputing.checkins.services.checkins.CheckIn; |
| 6 | +import com.objectcomputing.checkins.services.checkins.CheckInServices; |
| 7 | +import com.objectcomputing.checkins.services.memberprofile.MemberProfile; |
| 8 | +import com.objectcomputing.checkins.services.memberprofile.MemberProfileServices; |
| 9 | +import com.objectcomputing.checkins.services.memberprofile.MemberProfileUtils; |
| 10 | +import com.objectcomputing.checkins.services.memberprofile.currentuser.CurrentUserServices; |
| 11 | + |
| 12 | +import io.micronaut.core.annotation.Nullable; |
| 13 | +import io.micronaut.http.multipart.CompletedFileUpload; |
| 14 | + |
| 15 | +import jakarta.inject.Singleton; |
| 16 | +import jakarta.validation.constraints.NotNull; |
| 17 | +import org.slf4j.Logger; |
| 18 | +import org.slf4j.LoggerFactory; |
| 19 | + |
| 20 | +import java.io.FileOutputStream; |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.HashSet; |
| 24 | +import java.util.Set; |
| 25 | +import java.util.UUID; |
| 26 | +import java.util.function.Function; |
| 27 | + |
| 28 | +import static com.objectcomputing.checkins.services.validate.PermissionsValidation.NOT_AUTHORIZED_MSG; |
| 29 | + |
| 30 | +@Singleton |
| 31 | +abstract public class FileServicesBaseImpl implements FileServices { |
| 32 | + private static final Logger LOG = LoggerFactory.getLogger(FileServicesBaseImpl.class); |
| 33 | + |
| 34 | + protected final CheckInServices checkInServices; |
| 35 | + protected final CheckinDocumentServices checkinDocumentServices; |
| 36 | + protected final MemberProfileServices memberProfileServices; |
| 37 | + protected final CurrentUserServices currentUserServices; |
| 38 | + |
| 39 | + public FileServicesBaseImpl(CheckInServices checkInServices, |
| 40 | + CheckinDocumentServices checkinDocumentServices, |
| 41 | + MemberProfileServices memberProfileServices, |
| 42 | + CurrentUserServices currentUserServices) { |
| 43 | + this.checkInServices = checkInServices; |
| 44 | + this.checkinDocumentServices = checkinDocumentServices; |
| 45 | + this.memberProfileServices = memberProfileServices; |
| 46 | + this.currentUserServices = currentUserServices; |
| 47 | + } |
| 48 | + |
| 49 | + abstract protected void getCheckinDocuments( |
| 50 | + Set<FileInfoDTO> result, Set<CheckinDocument> checkinDocuments) throws IOException; |
| 51 | + abstract protected void downloadSingleFile( |
| 52 | + String docId, FileOutputStream myWriter) throws IOException; |
| 53 | + abstract protected FileInfoDTO uploadSingleFile( |
| 54 | + CompletedFileUpload file, String directoryName, |
| 55 | + Function<String, CheckinDocument> consumer) throws IOException; |
| 56 | + abstract protected void deleteSingleFile(String docId) throws IOException; |
| 57 | + |
| 58 | + @Override |
| 59 | + public Set<FileInfoDTO> findFiles(@Nullable UUID checkInID) { |
| 60 | + boolean isAdmin = currentUserServices.isAdmin(); |
| 61 | + validate(checkInID == null && !isAdmin, NOT_AUTHORIZED_MSG); |
| 62 | + |
| 63 | + try { |
| 64 | + Set<FileInfoDTO> result = new HashSet<>(); |
| 65 | + if (checkInID == null && isAdmin) { |
| 66 | + getCheckinDocuments(result, Collections.emptySet()); |
| 67 | + } else if (checkInID != null) { |
| 68 | + validate(!checkInServices.accessGranted(checkInID, currentUserServices.getCurrentUser().getId()), |
| 69 | + "You are not authorized to perform this operation"); |
| 70 | + |
| 71 | + // If there aren't any documents, do not call |
| 72 | + // getCheckinDocument. It assumes that an empty set means |
| 73 | + // that it should attempt to get all documents. And, in this |
| 74 | + // case, we just want an empty result set. |
| 75 | + Set<CheckinDocument> checkinDocuments = checkinDocumentServices.read(checkInID); |
| 76 | + if (!checkinDocuments.isEmpty()) { |
| 77 | + getCheckinDocuments(result, checkinDocuments); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return result; |
| 82 | + } catch (IOException e) { |
| 83 | + LOG.error("Error occurred while retrieving files.", e); |
| 84 | + throw new FileRetrievalException(e.getMessage()); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public java.io.File downloadFiles(@NotNull String uploadDocId) { |
| 90 | + MemberProfile currentUser = currentUserServices.getCurrentUser(); |
| 91 | + boolean isAdmin = currentUserServices.isAdmin(); |
| 92 | + |
| 93 | + CheckinDocument cd = checkinDocumentServices.getFindByUploadDocId(uploadDocId); |
| 94 | + validate(cd == null, String.format("Unable to find record with id %s", uploadDocId)); |
| 95 | + |
| 96 | + CheckIn associatedCheckin = checkInServices.read(cd.getCheckinsId()); |
| 97 | + |
| 98 | + if(!isAdmin) { |
| 99 | + validate((!currentUser.getId().equals(associatedCheckin.getTeamMemberId()) && !currentUser.getId().equals(associatedCheckin.getPdlId())), NOT_AUTHORIZED_MSG); |
| 100 | + } |
| 101 | + try { |
| 102 | + java.io.File file = java.io.File.createTempFile("tmp", ".txt"); |
| 103 | + file.deleteOnExit(); |
| 104 | + try( |
| 105 | + FileOutputStream myWriter = new FileOutputStream(file) |
| 106 | + ) { |
| 107 | + downloadSingleFile(uploadDocId, myWriter); |
| 108 | + return file; |
| 109 | + } catch (IOException e) { |
| 110 | + LOG.error("Error occurred while retrieving files.", e); |
| 111 | + throw new FileRetrievalException(e.getMessage()); |
| 112 | + } |
| 113 | + } catch(IOException e) { |
| 114 | + LOG.error("Error occurred while attempting to create a temporary file.", e); |
| 115 | + throw new FileRetrievalException(e.getMessage()); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public FileInfoDTO uploadFile(@NotNull UUID checkInID, @NotNull CompletedFileUpload file) { |
| 121 | + MemberProfile currentUser = currentUserServices.getCurrentUser(); |
| 122 | + boolean isAdmin = currentUserServices.isAdmin(); |
| 123 | + validate((file.getFilename() == null || file.getFilename().equals("")), "Please select a valid file before uploading."); |
| 124 | + |
| 125 | + CheckIn checkIn = checkInServices.read(checkInID); |
| 126 | + validate(checkIn == null, "Unable to find checkin record with id %s", checkInID); |
| 127 | + if(!isAdmin) { |
| 128 | + validate((!currentUser.getId().equals(checkIn.getTeamMemberId()) && !currentUser.getId().equals(checkIn.getPdlId())), "You are not authorized to perform this operation"); |
| 129 | + validate(checkIn.isCompleted(), NOT_AUTHORIZED_MSG); |
| 130 | + } |
| 131 | + |
| 132 | + // create folder for each team member |
| 133 | + final String directoryName = MemberProfileUtils.getFullName(memberProfileServices.getById(checkIn.getTeamMemberId())); |
| 134 | + |
| 135 | + try { |
| 136 | + return uploadSingleFile(file, directoryName, |
| 137 | + (fileId) -> { |
| 138 | + //create record in checkin-document service |
| 139 | + CheckinDocument cd = new CheckinDocument(checkInID, fileId); |
| 140 | + checkinDocumentServices.save(cd); |
| 141 | + return cd; |
| 142 | + }); |
| 143 | + } catch (IOException e) { |
| 144 | + LOG.error("Unexpected error processing file upload.", e); |
| 145 | + throw new FileRetrievalException(e.getMessage()); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public boolean deleteFile(@NotNull String uploadDocId) { |
| 151 | + MemberProfile currentUser = currentUserServices.getCurrentUser(); |
| 152 | + boolean isAdmin = currentUserServices.isAdmin(); |
| 153 | + |
| 154 | + CheckinDocument cd = checkinDocumentServices.getFindByUploadDocId(uploadDocId); |
| 155 | + validate(cd == null, String.format("Unable to find record with id %s", uploadDocId)); |
| 156 | + |
| 157 | + CheckIn associatedCheckin = checkInServices.read(cd.getCheckinsId()); |
| 158 | + if(!isAdmin) { |
| 159 | + validate((!currentUser.getId().equals(associatedCheckin.getTeamMemberId()) && !currentUser.getId().equals(associatedCheckin.getPdlId())), NOT_AUTHORIZED_MSG); |
| 160 | + } |
| 161 | + |
| 162 | + try { |
| 163 | + deleteSingleFile(uploadDocId); |
| 164 | + checkinDocumentServices.deleteByUploadDocId(uploadDocId); |
| 165 | + return true; |
| 166 | + } catch (IOException e) { |
| 167 | + LOG.error("Error occurred while deleting files.", e); |
| 168 | + throw new FileRetrievalException(e.getMessage()); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + protected void validate(boolean isError, String message, Object... args) { |
| 173 | + if(isError) { |
| 174 | + throw new FileRetrievalException(String.format(message, args)); |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments