Skip to content

Commit 217d8f3

Browse files
author
Flurb
committed
Added check for duplicate name
Signed-off-by: Flurb <[email protected]>
1 parent 430d547 commit 217d8f3

File tree

6 files changed

+49
-1
lines changed

6 files changed

+49
-1
lines changed

app/src/test/java/org/lfenergy/compas/scl/data/rest/mock/CompasSclDataRepositoryMock.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public String findByUUID(SclFileType type, UUID id, Version version) {
4242
throw new IllegalStateException("Mock method using Mockito. Only needed to startup.");
4343
}
4444

45+
@Override
46+
public boolean hasDuplicateSclName(SclFileType type, UUID id) {
47+
throw new IllegalStateException("Mock method using Mockito. Only needed to startup.");
48+
}
49+
4550
@Override
4651
public void create(SclFileType type, UUID id, String name, String scl, Version version, String who) {
4752
throw new IllegalStateException("Mock method using Mockito. Only needed to startup.");

repository-basex/src/main/java/org/lfenergy/compas/scl/data/basex/repository/CompasSclDataBaseXRepository.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ public String findByUUID(SclFileType type, UUID id, Version version) {
153153
return result.get(0);
154154
}
155155

156+
@Override
157+
public boolean hasDuplicateSclName(SclFileType type, String name) {
158+
return false;
159+
}
160+
156161
@Override
157162
public SclMetaInfo findMetaInfoByUUID(SclFileType type, UUID id) {
158163
// This find method always searches for the latest version.

repository-postgresql/src/main/java/org/lfenergy/compas/scl/data/repository/postgresql/CompasSclDataPostgreSQLRepository.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ public String findByUUID(SclFileType type, UUID id, Version version) {
155155
}
156156
}
157157

158+
@Override
159+
public boolean hasDuplicateSclName(SclFileType type, String name) {
160+
return false;
161+
}
162+
158163
@Override
159164
public SclMetaInfo findMetaInfoByUUID(SclFileType type, UUID id) {
160165
var sql = SELECT_METADATA_CLAUSE

repository/src/main/java/org/lfenergy/compas/scl/data/exception/CompasSclDataServiceErrorCode.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class CompasSclDataServiceErrorCode {
1414
public static final String HEADER_NOT_FOUND_ERROR_CODE = "SDS-0004";
1515
public static final String NO_SCL_ELEMENT_FOUND_ERROR_CODE = "SDS-0005";
1616
public static final String NO_DATA_FOUND_ERROR_CODE = "SDS-0006";
17+
public static final String DULPICATE_SCL_NAME_ERROR_CODE = "SDS-0007";
1718

1819
public static final String BASEX_CLIENT_CREATION_ERROR_CODE = "SDS-1000";
1920
public static final String BASEX_QUERY_ERROR_CODE = "SDS-1001";

repository/src/main/java/org/lfenergy/compas/scl/data/repository/CompasSclDataRepository.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ public interface CompasSclDataRepository {
7070
@Transactional(SUPPORTS)
7171
String findByUUID(SclFileType type, UUID id, Version version);
7272

73+
/**
74+
* Return the specific version of a specific SCL Entry.
75+
*
76+
* @param type The type of SCL to search for the specific SCL.
77+
* @param id The ID of the SCL to search for.
78+
* @return True if name is already used by another SCL File of the same File type, otherwise false.
79+
*/
80+
@Transactional(SUPPORTS)
81+
boolean hasDuplicateSclName(SclFileType type, String name);
82+
7383
/**
7484
* Create a new entry for the passed UUID with the version number passed.
7585
* <p>

service/src/main/java/org/lfenergy/compas/scl/data/service/impl/CompasSclDataServiceImpl.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import org.lfenergy.compas.scl.data.service.CompasSclDataService;
1515
import org.lfenergy.compas.scl.data.util.SclElementProcessor;
1616
import org.lfenergy.compas.scl.extensions.model.SclFileType;
17+
import org.slf4j.Logger;
18+
import org.slf4j.LoggerFactory;
1719
import org.w3c.dom.Element;
1820

1921
import javax.transaction.Transactional;
@@ -27,13 +29,17 @@
2729
import static javax.transaction.Transactional.TxType.REQUIRED;
2830
import static javax.transaction.Transactional.TxType.SUPPORTS;
2931
import static org.lfenergy.compas.scl.data.SclDataServiceConstants.*;
32+
import static org.lfenergy.compas.scl.data.exception.CompasSclDataServiceErrorCode.DULPICATE_SCL_NAME_ERROR_CODE;
3033
import static org.lfenergy.compas.scl.data.exception.CompasSclDataServiceErrorCode.NO_SCL_ELEMENT_FOUND_ERROR_CODE;
3134

3235
/**
3336
* Service class that will be using a Repository instance to retrieve, create, update, delete SCL XML Files.
3437
* These methods contain standard behaviour that is executed for every type of repository.
3538
*/
3639
public class CompasSclDataServiceImpl implements CompasSclDataService {
40+
41+
private static final Logger LOGGER = LoggerFactory.getLogger(CompasSclDataServiceImpl.class);
42+
3743
private final CompasSclDataRepository repository;
3844
private final ElementConverter converter;
3945
private final SclElementProcessor sclElementProcessor;
@@ -120,6 +126,10 @@ public String create(SclFileType type, String name, String who, String comment,
120126
throw new CompasException(NO_SCL_ELEMENT_FOUND_ERROR_CODE, "No valid SCL found in the passed SCL Data.");
121127
}
122128

129+
if (hasDuplicateSclName(type, name)) {
130+
throw new CompasException(DULPICATE_SCL_NAME_ERROR_CODE, "Given name of SCL File already used.");
131+
}
132+
123133
// A unique ID is generated to store it under.
124134
var id = UUID.randomUUID();
125135
// When the SCL is created the version will be set to 1.0.0
@@ -158,6 +168,14 @@ public String update(SclFileType type, UUID id, ChangeSetType changeSetType, Str
158168
}
159169

160170
var currentSclMetaInfo = repository.findMetaInfoByUUID(type, id);
171+
var newFileName = getFilenameFromXML(scl);
172+
173+
if (newFileName.isPresent()
174+
&& !newFileName.get().equals(currentSclMetaInfo.getName())
175+
&& hasDuplicateSclName(type, newFileName.get())) {
176+
throw new CompasException(DULPICATE_SCL_NAME_ERROR_CODE, "Given name of SCL File already used.");
177+
}
178+
161179
// We always add a new version to the database, so add version record to the SCL and create a new record.
162180
var version = new Version(currentSclMetaInfo.getVersion());
163181
version = version.getNextVersion(changeSetType);
@@ -167,7 +185,7 @@ public String update(SclFileType type, UUID id, ChangeSetType changeSetType, Str
167185
createHistoryItem(header, "SCL updated", who, comment, version);
168186

169187
// Update or add the Compas Private Element to the SCL File.
170-
var newSclName = getFilenameFromXML(scl).orElse(currentSclMetaInfo.getName());
188+
var newSclName = newFileName.orElse(currentSclMetaInfo.getName());
171189
setSclCompasPrivateElement(scl, newSclName, type);
172190

173191
var newSclData = converter.convertToString(scl);
@@ -200,6 +218,10 @@ public void delete(SclFileType type, UUID id, Version version) {
200218
repository.delete(type, id, version);
201219
}
202220

221+
private boolean hasDuplicateSclName(SclFileType type, String name) {
222+
return repository.hasDuplicateSclName(type, name);
223+
}
224+
203225
/**
204226
* Retrieve the Header from the SCL Fiel or create one if it doesn't exists and set the ID and
205227
* version on the Header.

0 commit comments

Comments
 (0)