Skip to content

Commit 4a616b6

Browse files
Merge pull request BroadleafCommerce#73 from BroadleafCommerce/qa-925-detailed-error-on-promotion
Show detailed validation error message on promotion failure
2 parents f3b8312 + 2676004 commit 4a616b6

File tree

3 files changed

+74
-17
lines changed

3 files changed

+74
-17
lines changed

admin/broadleaf-open-admin-platform/src/main/java/org/broadleafcommerce/openadmin/server/service/persistence/PersistenceManagerImpl.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.broadleafcommerce.common.exception.ServiceException;
3131
import org.broadleafcommerce.common.money.Money;
3232
import org.broadleafcommerce.common.presentation.client.OperationType;
33+
import org.broadleafcommerce.common.util.ValidationUtil;
3334
import org.broadleafcommerce.openadmin.dto.BasicFieldMetadata;
3435
import org.broadleafcommerce.openadmin.dto.ClassMetadata;
3536
import org.broadleafcommerce.openadmin.dto.CriteriaTransferObject;
@@ -438,7 +439,9 @@ public PersistenceResponse add(PersistencePackage persistencePackage) throws Ser
438439

439440
if (response.isValidationFailure()) {
440441
PersistenceResponse validationResponse = executeValidationProcessors(persistencePackage, new PersistenceResponse().withEntity(response));
441-
throw new ValidationException(validationResponse.getEntity(), "The entity has failed validation");
442+
Entity entity = validationResponse.getEntity();
443+
String message = ValidationUtil.buildErrorMessage(entity.getPropertyValidationErrors(), entity.getGlobalValidationErrors());
444+
throw new ValidationException(entity, message);
442445
}
443446

444447
return executePostAddHandlers(persistencePackage, new PersistenceResponse().withEntity(response));
@@ -577,7 +580,9 @@ public PersistenceResponse update(PersistencePackage persistencePackage) throws
577580

578581
if (response.isValidationFailure()) {
579582
PersistenceResponse validationResponse = executeValidationProcessors(persistencePackage, new PersistenceResponse().withEntity(response));
580-
throw new ValidationException(validationResponse.getEntity(), "The entity has failed validation");
583+
Entity entity = validationResponse.getEntity();
584+
String message = ValidationUtil.buildErrorMessage(entity.getPropertyValidationErrors(), entity.getGlobalValidationErrors());
585+
throw new ValidationException(entity, message);
581586
}
582587

583588
return executePostUpdateHandlers(persistencePackage, new PersistenceResponse().withEntity(response));

admin/broadleaf-open-admin-platform/src/main/java/org/broadleafcommerce/openadmin/server/service/persistence/module/BasicPersistenceModule.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.broadleafcommerce.common.presentation.client.SupportedFieldType;
4141
import org.broadleafcommerce.common.presentation.client.VisibilityEnum;
4242
import org.broadleafcommerce.common.util.FormatUtil;
43+
import org.broadleafcommerce.common.util.ValidationUtil;
4344
import org.broadleafcommerce.common.util.dao.TQJoin;
4445
import org.broadleafcommerce.common.util.dao.TQOrder;
4546
import org.broadleafcommerce.common.util.dao.TQRestriction;
@@ -110,7 +111,6 @@
110111
import java.util.Comparator;
111112
import java.util.Date;
112113
import java.util.HashMap;
113-
import java.util.Iterator;
114114
import java.util.List;
115115
import java.util.Locale;
116116
import java.util.Map;
@@ -411,20 +411,8 @@ public int compare(Property o1, Property o2) {
411411
invalid.setPropertyValidationErrors(entity.getPropertyValidationErrors());
412412
invalid.overridePropertyValues(entity);
413413

414-
StringBuilder sb = new StringBuilder();
415-
for (Map.Entry<String, List<String>> entry : invalid.getPropertyValidationErrors().entrySet()) {
416-
Iterator<String> itr = entry.getValue().iterator();
417-
while (itr.hasNext()) {
418-
sb.append(entry.getKey());
419-
sb.append(" : ");
420-
sb.append(itr.next());
421-
if (itr.hasNext()) {
422-
sb.append(" / ");
423-
}
424-
}
425-
}
426-
427-
throw new ValidationException(invalid, "The entity has failed validation - " + sb.toString());
414+
String message = ValidationUtil.buildErrorMessage(invalid.getPropertyValidationErrors(), invalid.getGlobalValidationErrors());
415+
throw new ValidationException(invalid, message);
428416
} else if (entityPersistenceException != null) {
429417
throw ExceptionHelper.refineException(entityPersistenceException.getCause());
430418
} else {
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* #%L
3+
* BroadleafCommerce Common Libraries
4+
* %%
5+
* Copyright (C) 2009 - 2015 Broadleaf Commerce
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package org.broadleafcommerce.common.util;
21+
22+
import java.util.Iterator;
23+
import java.util.List;
24+
import java.util.Map;
25+
26+
/**
27+
* @author Chad Harchar (charchar)
28+
*/
29+
public class ValidationUtil {
30+
31+
public static String buildErrorMessage(Map<String, List<String>> propertyErrors, List<String> globalErrors) {
32+
StringBuilder sb = new StringBuilder();
33+
for (Map.Entry<String, List<String>> entry : propertyErrors.entrySet()) {
34+
Iterator<String> itr = entry.getValue().iterator();
35+
while (itr.hasNext()) {
36+
sb.append(entry.getKey());
37+
sb.append(" : ");
38+
39+
String errorMessage = itr.next();
40+
sb.append(processMessage(errorMessage));
41+
42+
if (itr.hasNext()) {
43+
sb.append(" / ");
44+
}
45+
}
46+
sb.append("; ");
47+
}
48+
49+
for (String globalError : globalErrors) {
50+
sb.append(processMessage(globalError));
51+
sb.append("; ");
52+
}
53+
54+
return "The entity has failed validation - " + sb.toString();
55+
}
56+
57+
public static String processMessage(String globalError) {
58+
String friendlyName = BLCMessageUtils.getMessage(globalError);
59+
if (friendlyName != null) {
60+
return friendlyName;
61+
}
62+
return globalError;
63+
}
64+
}

0 commit comments

Comments
 (0)