Skip to content

Commit 033d6fc

Browse files
committed
[Update]: Whitespace suggested change
1 parent 83c5eb9 commit 033d6fc

3 files changed

Lines changed: 65 additions & 13 deletions

File tree

sdm/src/main/java/com/sap/cds/sdm/constants/SDMConstants.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ private SDMConstants() {
6969
public static final String NAMED_USER_FLOW = "TOKEN_EXCHANGE";
7070
public static final String ANNOTATION_IS_MEDIA_DATA = "_is_media_data";
7171
public static final String DRAFT_READONLY_CONTEXT = "DRAFT_READONLY_CONTEXT";
72+
public static final String FILENAME_WHITESPACE_WARNING_MESSAGE =
73+
"The following file(s) could not be updated because the filename(s) cannot be empty.";
7274

7375
public static String nameConstraintMessage(
7476
List<String> fileNameWithRestrictedCharacters, String operation) {
@@ -145,6 +147,21 @@ public static String noSDMRolesMessage(List<String> files, String operation) {
145147
return bulletPoints.toString();
146148
}
147149

150+
public static String removeWhiteSpace(List<String> noWhiteSpace) {
151+
// Create the base message
152+
String prefixMessage = "File name cannot start with Space. \n\n";
153+
154+
// Initialize the StringBuilder with the formatted message prefix
155+
StringBuilder bulletPoints = new StringBuilder(prefixMessage);
156+
157+
// Append each file name and its error message to the StringBuilder
158+
for (String file : noWhiteSpace) {
159+
bulletPoints.append(String.format("\t• %s%n", file));
160+
}
161+
bulletPoints.append(System.lineSeparator());
162+
return bulletPoints.toString();
163+
}
164+
148165
public static String unsupportedPropertiesMessage(List<String> propertiesList) {
149166
// Create the base message
150167
String prefixMessage = "The following secondary properties are not supported.\n\n";

sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/SDMCreateAttachmentsHandler.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public void updateName(CdsCreateEventContext context, List<CdsData> data, String
6565
List<String> filesNotFound = new ArrayList<>();
6666
List<String> filesWithUnsupportedProperties = new ArrayList<>();
6767
Map<String, String> badRequest = new HashMap<>();
68+
List<String> fileWithWhiteSpace = new ArrayList<>();
6869
List<String> noSDMRoles = new ArrayList<>();
6970
for (Map<String, Object> entity : data) {
7071
List<Map<String, Object>> attachments = (List<Map<String, Object>>) entity.get(composition);
@@ -89,7 +90,8 @@ public void updateName(CdsCreateEventContext context, List<CdsData> data, String
8990
composition,
9091
attachmentEntity,
9192
secondaryPropertiesWithInvalidDefinitions,
92-
noSDMRoles);
93+
noSDMRoles,
94+
fileWithWhiteSpace);
9395
handleWarnings(
9496
context,
9597
fileNameWithRestrictedCharacters,
@@ -98,6 +100,7 @@ public void updateName(CdsCreateEventContext context, List<CdsData> data, String
98100
filesWithUnsupportedProperties,
99101
badRequest,
100102
propertyTitles,
103+
fileWithWhiteSpace,
101104
noSDMRoles);
102105
}
103106
}
@@ -124,7 +127,8 @@ private void processEntity(
124127
String composition,
125128
Optional<CdsEntity> attachmentEntity,
126129
Map<String, String> secondaryPropertiesWithInvalidDefinitions,
127-
List<String> noSDMRoles)
130+
List<String> noSDMRoles,
131+
List<String> fileWithWhiteSpace)
128132
throws IOException {
129133
List<Map<String, Object>> attachments = (List<Map<String, Object>>) entity.get(composition);
130134
if (attachments != null) {
@@ -140,7 +144,8 @@ private void processEntity(
140144
composition,
141145
attachmentEntity,
142146
secondaryPropertiesWithInvalidDefinitions,
143-
noSDMRoles);
147+
noSDMRoles,
148+
fileWithWhiteSpace);
144149
}
145150
SecondaryPropertiesKey secondaryPropertiesKey =
146151
new SecondaryPropertiesKey(); // Emptying cache after attachments are updated in loop
@@ -160,6 +165,7 @@ private void processAttachment(
160165
String composition,
161166
Optional<CdsEntity> attachmentEntity,
162167
Map<String, String> secondaryPropertiesWithInvalidDefinitions,
168+
List<String> fileWithWhiteSpace,
163169
List<String> noSDMRoles)
164170
throws IOException {
165171
String id = (String) attachment.get("ID");
@@ -225,8 +231,11 @@ private void processAttachment(
225231
throw new ServiceException("Filename cannot be empty");
226232
}
227233
} else {
228-
if (filenameInRequest == null) {
229-
throw new ServiceException("Filename cannot be empty");
234+
if (filenameInRequest == null || filenameInRequest.trim().length() == 0) {
235+
fileWithWhiteSpace.add(fileNameInDB);
236+
replacePropertiesInAttachment(
237+
attachment, fileNameInDB, propertiesInDB, secondaryTypeProperties);
238+
230239
} else if (!fileNameInDB.equals(
231240
filenameInRequest)) { // If the file name in DB is not equal to the file name in
232241
// request, it means that the file name has been modified
@@ -317,6 +326,7 @@ private void handleWarnings(
317326
List<String> filesWithUnsupportedProperties,
318327
Map<String, String> badRequest,
319328
Map<String, String> propertyTitles,
329+
List<String> fileWithWhiteSpace,
320330
List<String> noSDMRoles) {
321331
if (!fileNameWithRestrictedCharacters.isEmpty()) {
322332
context
@@ -358,6 +368,14 @@ private void handleWarnings(
358368
if (!noSDMRoles.isEmpty()) {
359369
context.getMessages().warn(SDMConstants.noSDMRolesMessage(noSDMRoles, "create"));
360370
}
371+
if (!fileWithWhiteSpace.isEmpty()) {
372+
context
373+
.getMessages()
374+
.warn(
375+
String.format(
376+
SDMConstants.FILENAME_WHITESPACE_WARNING_MESSAGE,
377+
String.join(", ", fileWithWhiteSpace)));
378+
}
361379
}
362380

363381
private List<String> getEntityCompositions(CdsCreateEventContext context) {

sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/SDMUpdateAttachmentsHandler.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ private void renameDocument(
7878
List<String> filesWithUnsupportedProperties = new ArrayList<>();
7979
Map<String, String> badRequest = new HashMap<>();
8080
Map<String, String> propertyTitles = new HashMap<>();
81+
List<String> fileWithWhiteSpace = new ArrayList<>();
8182
List<String> noSDMRoles = new ArrayList<>();
8283
for (Map<String, Object> entity : data) {
8384
List<Map<String, Object>> attachments = (List<Map<String, Object>>) entity.get(composition);
@@ -105,7 +106,8 @@ private void renameDocument(
105106
filesWithUnsupportedProperties,
106107
badRequest,
107108
secondaryPropertiesWithInvalidDefinitions,
108-
noSDMRoles);
109+
noSDMRoles,
110+
fileWithWhiteSpace);
109111
}
110112
}
111113
handleWarnings(
@@ -116,7 +118,8 @@ private void renameDocument(
116118
filesWithUnsupportedProperties,
117119
badRequest,
118120
propertyTitles,
119-
noSDMRoles);
121+
noSDMRoles,
122+
fileWithWhiteSpace);
120123
}
121124

122125
private void processAttachments(
@@ -129,7 +132,8 @@ private void processAttachments(
129132
List<String> filesWithUnsupportedProperties,
130133
Map<String, String> badRequest,
131134
Map<String, String> secondaryPropertiesWithInvalidDefinitions,
132-
List<String> noSDMRoles)
135+
List<String> noSDMRoles,
136+
List<String> fileWithWhiteSpace)
133137
throws IOException {
134138
Iterator<Map<String, Object>> iterator = attachments.iterator();
135139
while (iterator.hasNext()) {
@@ -144,7 +148,8 @@ private void processAttachments(
144148
filesWithUnsupportedProperties,
145149
badRequest,
146150
secondaryPropertiesWithInvalidDefinitions,
147-
noSDMRoles);
151+
noSDMRoles,
152+
fileWithWhiteSpace);
148153
}
149154
SecondaryPropertiesKey secondaryPropertiesKey = new SecondaryPropertiesKey();
150155
secondaryPropertiesKey.setRepositoryId(SDMConstants.REPOSITORY_ID);
@@ -164,7 +169,8 @@ public void processAttachment(
164169
List<String> filesWithUnsupportedProperties,
165170
Map<String, String> badRequest,
166171
Map<String, String> secondaryPropertiesWithInvalidDefinitions,
167-
List<String> noSDMRoles)
172+
List<String> noSDMRoles,
173+
List<String> fileWithWhiteSpace)
168174
throws IOException {
169175
String id = (String) attachment.get("ID");
170176
Map<String, String> secondaryTypeProperties =
@@ -220,8 +226,10 @@ public void processAttachment(
220226
throw new ServiceException("Filename cannot be empty");
221227
}
222228
} else {
223-
if (filenameInRequest == null) {
224-
throw new ServiceException("Filename cannot be empty");
229+
if (filenameInRequest == null || filenameInRequest.trim().length() == 0) {
230+
fileWithWhiteSpace.add(fileNameInDB);
231+
replacePropertiesInAttachment(
232+
attachment, fileNameInDB, propertiesInDB, secondaryTypeProperties);
225233
} else if (!fileNameInDB.equals(filenameInRequest)) {
226234
updatedSecondaryProperties.put("filename", filenameInRequest);
227235
}
@@ -311,7 +319,8 @@ private void handleWarnings(
311319
List<String> filesWithUnsupportedProperties,
312320
Map<String, String> badRequest,
313321
Map<String, String> propertyTitles,
314-
List<String> noSDMRoles) {
322+
List<String> noSDMRoles,
323+
List<String> fileWithWhiteSpace) {
315324
if (!fileNameWithRestrictedCharacters.isEmpty()) {
316325
context
317326
.getMessages()
@@ -350,6 +359,14 @@ private void handleWarnings(
350359
}
351360
if (!noSDMRoles.isEmpty()) {
352361
context.getMessages().warn(SDMConstants.noSDMRolesMessage(noSDMRoles, "update"));
362+
if (!fileWithWhiteSpace.isEmpty()) {
363+
context
364+
.getMessages()
365+
.warn(
366+
String.format(
367+
SDMConstants.FILENAME_WHITESPACE_WARNING_MESSAGE,
368+
String.join(", ", fileWithWhiteSpace)));
369+
}
353370
}
354371
}
355372

0 commit comments

Comments
 (0)