-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDocumentServiceImpl.java
More file actions
144 lines (109 loc) · 5.64 KB
/
DocumentServiceImpl.java
File metadata and controls
144 lines (109 loc) · 5.64 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
package com.example.caselabproject.services.implementations;
import com.example.caselabproject.exceptions.*;
import com.example.caselabproject.models.DTOs.request.DocumentRequestDto;
import com.example.caselabproject.models.DTOs.response.DocumentCreateResponseDto;
import com.example.caselabproject.models.DTOs.response.DocumentResponseDto;
import com.example.caselabproject.models.entities.Document;
import com.example.caselabproject.models.entities.User;
import com.example.caselabproject.models.enums.RecordState;
import com.example.caselabproject.repositories.DocumentConstructorTypeRepository;
import com.example.caselabproject.repositories.DocumentPageRepository;
import com.example.caselabproject.repositories.DocumentRepository;
import com.example.caselabproject.repositories.UserRepository;
import com.example.caselabproject.services.DocumentService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import java.time.LocalDateTime;
import java.util.List;
@Service
@RequiredArgsConstructor
@Validated
public class DocumentServiceImpl implements DocumentService {
private final DocumentRepository documentRepository;
private final DocumentPageRepository documentPageRepository;
private final DocumentConstructorTypeRepository documentConstructorTypeRepository;
private final UserRepository userRepository;
@Override
public DocumentCreateResponseDto createDocument(String username, DocumentRequestDto request) {
Document document = request.mapToEntity();
document.setRecordState(RecordState.ACTIVE);
User user = userRepository.findByUsername(username)
.orElseThrow(() -> new UserByPrincipalUsernameDoesNotExistException(username));
document.setCreator(user);
document.setDocumentConstructorType(documentConstructorTypeRepository
.findByName(request.getConstructorTypeName()).orElseThrow(
() -> new DocumentConstructorTypeNameNotFoundException(request.getConstructorTypeName())
));
try {
documentRepository.save(document);
} catch (Exception e) {
throw new DocumentCreateException(document.getName());
}
return DocumentCreateResponseDto.mapFromEntity(document);
}
@Override
public DocumentResponseDto findDocument(Long documentId) {
Document document = documentRepository.findById(documentId)
.orElseThrow(() -> new DocumentDoesNotExistException(documentId));
if (document.getRecordState().equals(RecordState.DELETED)) {
throw new DocumentDoesNotExistException(documentId);
}
return DocumentResponseDto.mapFromEntity(document);
}
@Override
public List<DocumentResponseDto> filteredDocument(Pageable pageable,
String name,
RecordState state,
LocalDateTime start,
LocalDateTime end) {
Page<Document> documents;
if (!(name.length() == 0)) {
documents = documentPageRepository
.findAllByNameContainingIgnoreCaseAndCreationDateAfterAndCreationDateBeforeAndRecordState(
name, pageable, start, end, state);
} else {
documents = documentPageRepository.findAllByCreationDateAfterAndCreationDateBeforeAndRecordState(
pageable, start, end, state);
}
if (documents.isEmpty()) {
throw new NoDocumentPageFoundException(pageable.getPageNumber());
}
return documents.map(DocumentResponseDto::mapFromEntity).toList();
}
@Override
public DocumentResponseDto updateDocument(String username, DocumentRequestDto request, Long documentId) {
if (!userRepository.existsByUsernameAndDocuments_id(username, documentId)) {
throw new DocumentAccessException(username);
}
Document updateDocument = documentRepository.findById(documentId)
.orElseThrow(() -> new DocumentDoesNotExistException(documentId));
if (updateDocument.getRecordState().equals(RecordState.DELETED)) {
throw new DocumentDoesNotExistException(documentId);
}
updateDocument.setUpdateDate(request.mapToEntity().getUpdateDate());
updateDocument.setName(request.getName());
updateDocument.setDocumentConstructorType(documentConstructorTypeRepository
.findByName(request.getConstructorTypeName()).orElseThrow(
() -> new DocumentConstructorTypeNameNotFoundException(request.getConstructorTypeName())
));
documentRepository.save(updateDocument);
return DocumentResponseDto.mapFromEntity(updateDocument);
}
@Override
public boolean deleteDocument(String username, Long documentId) {
if (!userRepository.existsByUsernameAndDocuments_id(username, documentId)) {
throw new DocumentAccessException(username);
}
Document document = documentRepository.findById(documentId)
.orElseThrow(() -> new DocumentDoesNotExistException(documentId));
if (document.getRecordState().equals(RecordState.DELETED)) {
throw new DocumentDoesNotExistException(documentId);
}
document.setRecordState(RecordState.DELETED);
documentRepository.save(document);
return true;
}
}