15
15
import org .folio .entlinks .domain .entity .AuthoritySourceFile ;
16
16
import org .folio .entlinks .domain .entity .AuthoritySourceFileCode ;
17
17
import org .folio .entlinks .domain .repository .AuthorityRepository ;
18
+ import org .folio .entlinks .domain .repository .AuthoritySourceFileJdbcRepository ;
18
19
import org .folio .entlinks .domain .repository .AuthoritySourceFileRepository ;
19
20
import org .folio .entlinks .exception .AuthoritySourceFileHridException ;
20
21
import org .folio .entlinks .exception .AuthoritySourceFileNotFoundException ;
21
22
import org .folio .entlinks .exception .OptimisticLockingException ;
22
23
import org .folio .entlinks .exception .RequestBodyValidationException ;
23
24
import org .folio .spring .FolioExecutionContext ;
24
- import org .folio .spring .FolioModuleMetadata ;
25
25
import org .folio .spring .data .OffsetRequest ;
26
26
import org .folio .tenant .domain .dto .Parameter ;
27
+ import org .springframework .context .annotation .Primary ;
27
28
import org .springframework .dao .DataAccessException ;
28
29
import org .springframework .data .domain .Page ;
29
30
import org .springframework .jdbc .core .JdbcTemplate ;
33
34
import org .springframework .transaction .annotation .Propagation ;
34
35
import org .springframework .transaction .annotation .Transactional ;
35
36
36
- @ Service
37
+ @ Primary
38
+ @ Service ("authoritySourceFileService" )
37
39
@ AllArgsConstructor
38
40
@ Log4j2
39
- public class AuthoritySourceFileService {
41
+ public class AuthoritySourceFileService implements AuthoritySourceFileServiceI {
40
42
41
43
private static final String AUTHORITY_SEQUENCE_NAME_TEMPLATE = "hrid_authority_local_file_%s_seq" ;
42
44
private final AuthoritySourceFileRepository repository ;
45
+ private final AuthoritySourceFileJdbcRepository jdbcRepository ;
43
46
private final AuthorityRepository authorityRepository ;
44
47
private final AuthoritySourceFileMapper mapper ;
45
48
private final JdbcTemplate jdbcTemplate ;
46
- private final FolioModuleMetadata moduleMetadata ;
47
49
private final FolioExecutionContext folioExecutionContext ;
48
50
51
+ @ Override
49
52
public Page <AuthoritySourceFile > getAll (Integer offset , Integer limit , String cql ) {
50
53
log .debug ("getAll:: Attempts to find all AuthoritySourceFile by [offset: {}, limit: {}, cql: {}]" , offset , limit ,
51
54
cql );
@@ -57,27 +60,14 @@ public Page<AuthoritySourceFile> getAll(Integer offset, Integer limit, String cq
57
60
return repository .findByCql (cql , new OffsetRequest (offset , limit ));
58
61
}
59
62
60
- /**
61
- * Retrieves {@link AuthoritySourceFile} by the given id.
62
- *
63
- * @param id {@link UUID} ID of the authority source file being retrieved
64
- * @return retrieved {@link AuthoritySourceFile}
65
- *
66
- * Note: This method assumes the authority source file exists for the given ID and thus throws exception in case
67
- * no authority source file is found
68
- */
63
+ @ Override
69
64
public AuthoritySourceFile getById (UUID id ) {
70
65
log .debug ("getById:: Loading AuthoritySourceFile by ID [id: {}]" , id );
71
66
72
67
return repository .findById (id ).orElseThrow (() -> new AuthoritySourceFileNotFoundException (id ));
73
68
}
74
69
75
- /**
76
- * Searches for the Authority Source File for the given ID.
77
- *
78
- * @param id {@link UUID} ID of the authority source file being searched
79
- * @return found {@link AuthoritySourceFile} instance or null if it is not found
80
- */
70
+ @ Override
81
71
public AuthoritySourceFile findById (UUID id ) {
82
72
log .debug ("findById:: Querying for AuthoritySourceFile by ID [id: {}]" , id );
83
73
@@ -88,12 +78,7 @@ public AuthoritySourceFile findById(UUID id) {
88
78
return repository .findById (id ).orElse (null );
89
79
}
90
80
91
- /**
92
- * Searches for the Authority Source File for the given name.
93
- *
94
- * @param name Name of the authority source file being searched
95
- * @return found {@link AuthoritySourceFile} instance or null if it is not found
96
- */
81
+ @ Override
97
82
public AuthoritySourceFile findByName (String name ) {
98
83
log .debug ("findById:: Querying for AuthoritySourceFile by name [name: {}]" , name );
99
84
@@ -104,6 +89,7 @@ public AuthoritySourceFile findByName(String name) {
104
89
return repository .findByName (name ).orElse (null );
105
90
}
106
91
92
+ @ Override
107
93
@ Transactional
108
94
public AuthoritySourceFile create (AuthoritySourceFile entity ) {
109
95
log .debug ("create:: Attempting to create AuthoritySourceFile [entity: {}]" , entity );
@@ -115,6 +101,7 @@ public AuthoritySourceFile create(AuthoritySourceFile entity) {
115
101
return repository .save (entity );
116
102
}
117
103
104
+ @ Override
118
105
@ Transactional (propagation = Propagation .REQUIRES_NEW )
119
106
@ Retryable (
120
107
retryFor = OptimisticLockingException .class ,
@@ -124,6 +111,7 @@ public AuthoritySourceFile update(UUID id, AuthoritySourceFile modified) {
124
111
return updateInner (id , modified , null );
125
112
}
126
113
114
+ @ Override
127
115
@ Transactional (propagation = Propagation .REQUIRES_NEW )
128
116
@ Retryable (
129
117
retryFor = OptimisticLockingException .class ,
@@ -134,18 +122,14 @@ public AuthoritySourceFile update(UUID id, AuthoritySourceFile modified,
134
122
return updateInner (id , modified , publishConsumer );
135
123
}
136
124
125
+ @ Override
137
126
public void deleteById (UUID id ) {
138
127
log .debug ("deleteById:: Attempt to delete AuthoritySourceFile by [id: {}]" , id );
139
- var authoritySourceFile = repository .findById (id )
140
- .orElseThrow (() -> new AuthoritySourceFileNotFoundException (id ));
141
- if (!FOLIO .equals (authoritySourceFile .getSource ())) {
142
- repository .deleteById (id );
143
- } else {
144
- throw new RequestBodyValidationException ("Cannot delete Authority source file with source 'folio'" ,
145
- List .of (new Parameter ("source" ).value (authoritySourceFile .getSource ().name ())));
146
- }
128
+ validateOnDelete (id );
129
+ repository .deleteById (id );
147
130
}
148
131
132
+ @ Override
149
133
public String nextHrid (UUID id ) {
150
134
log .debug ("nextHrid:: Attempting to get next AuthoritySourceFile HRID [id: {}]" , id );
151
135
var sourceFile = getById (id );
@@ -162,17 +146,19 @@ public String nextHrid(UUID id) {
162
146
}
163
147
}
164
148
149
+ @ Override
165
150
public boolean authoritiesExistForSourceFile (UUID sourceFileId ) {
166
151
return authorityRepository .existsAuthorityByAuthoritySourceFileId (sourceFileId );
167
152
}
168
153
154
+ @ Override
169
155
public boolean authoritiesExistForSourceFile (UUID sourceFileId , String tenantId , String tableName ) {
170
156
if (sourceFileId == null || tenantId == null ) {
171
157
return false ;
172
158
}
173
159
174
160
var command = String .format ("select exists (select true from %s.%s a where a.source_file_id='%s' limit 1)" ,
175
- moduleMetadata .getDBSchemaName (tenantId ), tableName , sourceFileId );
161
+ folioExecutionContext . getFolioModuleMetadata () .getDBSchemaName (tenantId ), tableName , sourceFileId );
176
162
return Boolean .TRUE .equals (jdbcTemplate .queryForObject (command , Boolean .class ));
177
163
}
178
164
@@ -268,21 +254,17 @@ private void copyModifiableFields(AuthoritySourceFile existingEntity, AuthorityS
268
254
}
269
255
}
270
256
257
+ @ Override
271
258
public void createSequence (String sequenceName , int startNumber ) {
272
- var command = String .format ("""
273
- CREATE SEQUENCE %s MINVALUE %d INCREMENT BY 1 OWNED BY %s.authority_source_file.sequence_name;
274
- """ ,
275
- sequenceName , startNumber , moduleMetadata .getDBSchemaName (folioExecutionContext .getTenantId ()));
276
- jdbcTemplate .execute (command );
259
+ jdbcRepository .createSequence (sequenceName , startNumber );
277
260
}
278
261
262
+ @ Override
279
263
public void deleteSequence (String sequenceName ) {
280
- var command = String .format ("DROP SEQUENCE IF EXISTS %s.%s;" ,
281
- moduleMetadata .getDBSchemaName (folioExecutionContext .getTenantId ()), sequenceName );
282
- jdbcTemplate .execute (command );
264
+ jdbcRepository .dropSequence (sequenceName );
283
265
}
284
266
285
- private void updateSequenceStartNumber (AuthoritySourceFile existing , AuthoritySourceFile modified ) {
267
+ protected void updateSequenceStartNumber (AuthoritySourceFile existing , AuthoritySourceFile modified ) {
286
268
if (!Objects .equals (existing .getHridStartNumber (), modified .getHridStartNumber ())
287
269
&& existing .getHridStartNumber () != null && modified .getHridStartNumber () != null ) {
288
270
var sequenceName = existing .getSequenceName ();
@@ -291,4 +273,12 @@ private void updateSequenceStartNumber(AuthoritySourceFile existing, AuthoritySo
291
273
createSequence (sequenceName , modifiedStartNumber );
292
274
}
293
275
}
276
+
277
+ protected void validateOnDelete (UUID id ) {
278
+ var sourceFile = repository .findById (id ).orElseThrow (() -> new AuthoritySourceFileNotFoundException (id ));
279
+ if (FOLIO .equals (sourceFile .getSource ())) {
280
+ throw new RequestBodyValidationException ("Cannot delete Authority source file with source 'folio'" ,
281
+ List .of (new Parameter ("source" ).value (sourceFile .getSource ().name ())));
282
+ }
283
+ }
294
284
}
0 commit comments