Skip to content

Commit 433b3f9

Browse files
authored
Ccs 4519 xref validation errors (#626)
* UI fixes * fix xref validation issues when bulk publish * remove debugging code * code clean up * update UI on unpublishing * refactor code
1 parent e728530 commit 433b3f9

File tree

11 files changed

+107
-86
lines changed

11 files changed

+107
-86
lines changed

pantheon-bundle/frontend/src/app/BulkOperationPublish.tsx

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -203,32 +203,19 @@ class BulkOperationPublish extends React.Component<IBulkOperationPublishProps, a
203203
let hrefPart = href.slice(0, href.indexOf("?"))
204204
let modulePath = hrefPart.slice(hrefPart.indexOf("/repositories"))
205205
const backend = "/content" + modulePath + `/en_US/variants/${variant}/draft`
206-
let exist = this.props.isBulkPublish ? Utils.draftExist(backend) : false
206+
if (this.props.isBulkUnpublish) {
207+
this.processDocuments(modulePath, formData, hdrs)
207208

208-
if (exist || this.props.isBulkUnpublish) {
209-
fetch("/content" + modulePath, {
210-
body: formData,
211-
method: "post",
212-
headers: hdrs
213-
}).then(response => {
214-
if (response.status === 201 || response.status === 200) {
215-
this.setState({
216-
documentsSucceeded: [...this.state.documentsSucceeded, modulePath],
217-
bulkUpdateSuccess: this.state.bulkUpdateSuccess + 1,
218-
}, () => {
219-
this.calculateSuccessProgress(this.state.bulkUpdateSuccess)
220-
}
221-
)
209+
} else {
210+
Utils.draftExist(backend).then((exist) => {
211+
if (exist) {
212+
this.processDocuments(modulePath, formData, hdrs)
222213
} else {
223-
this.setState({ bulkUpdateFailure: this.state.bulkUpdateFailure + 1, documentsFailed: [...this.state.documentsFailed, modulePath] }, () => {
224-
this.calculateFailureProgress(this.state.bulkUpdateFailure)
214+
this.setState({ bulkUpdateWarning: this.state.bulkUpdateWarning + 1, documentsIgnored: [...this.state.documentsIgnored, modulePath] }, () => {
215+
this.calculateWarningProgress(this.state.bulkUpdateWarning)
225216
})
226217
}
227218
})
228-
} else {
229-
this.setState({ bulkUpdateWarning: this.state.bulkUpdateWarning + 1, documentsIgnored: [...this.state.documentsIgnored, modulePath] }, () => {
230-
this.calculateWarningProgress(this.state.bulkUpdateWarning)
231-
})
232219
}
233220
}
234221

@@ -290,5 +277,27 @@ class BulkOperationPublish extends React.Component<IBulkOperationPublishProps, a
290277
this.setState({ confirmationFailed: failed })
291278
}
292279
}
280+
281+
private processDocuments = (modulePath, formData, hdrs) => {
282+
fetch("/content" + modulePath, {
283+
body: formData,
284+
method: "post",
285+
headers: hdrs
286+
}).then(response => {
287+
if (response.status === 201 || response.status === 200) {
288+
this.setState({
289+
documentsSucceeded: [...this.state.documentsSucceeded, modulePath],
290+
bulkUpdateSuccess: this.state.bulkUpdateSuccess + 1,
291+
}, () => {
292+
this.calculateSuccessProgress(this.state.bulkUpdateSuccess)
293+
}
294+
)
295+
} else {
296+
this.setState({ bulkUpdateFailure: this.state.bulkUpdateFailure + 1, documentsFailed: [...this.state.documentsFailed, modulePath] }, () => {
297+
this.calculateFailureProgress(this.state.bulkUpdateFailure)
298+
})
299+
}
300+
})
301+
}
293302
}
294303
export { BulkOperationPublish }

pantheon-bundle/frontend/src/app/Utils.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1+
import { resolve } from "path"
2+
13
export class Utils {
2-
4+
35
/**
46
* fetchHelper
57
* @param endpoint: string
68
* @param options: object
79
*/
810

911
static fetchHelper = (endpoint: string, options: object) => {
10-
12+
1113
if (options !== null) {
1214
return fetch(endpoint, options)
13-
.then(Utils.handleErrors)
14-
.then(response => response.json())
15+
.then(Utils.handleErrors)
16+
.then(response => response.json())
1517
} else {
1618
return fetch(endpoint)
17-
.then(Utils.handleErrors)
18-
.then(response => response.json())
19+
.then(Utils.handleErrors)
20+
.then(response => response.json())
1921
}
2022
}
2123

@@ -51,4 +53,3 @@ export class Utils {
5153
})
5254
}
5355
}
54-

pantheon-bundle/frontend/src/app/bulkOperationMetadata.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ class BulkOperationMetadata extends React.Component<IBulkOperationMetadataProps,
6161

6262
public componentDidMount() {
6363
// fetch products and label for metadata Modal
64-
this.fetchProducts()
64+
if (this.props.isEditMetadata) {
65+
this.fetchProducts()
66+
}
6567
}
6668

6769
public render() {

pantheon-bundle/src/main/java/com/redhat/pantheon/asciidoctor/AsciidoctorService.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,7 @@ private String buildDocument(@Nonnull Document base, @Nonnull Locale locale, @No
288288
String html = "";
289289
try {
290290
TableOfContents tableOfContents = new TableOfContents();
291-
PantheonXrefProcessor xrefProcessor = new PantheonXrefProcessor(documentVariant, tableOfContents
292-
);
291+
PantheonXrefProcessor xrefProcessor = new PantheonXrefProcessor(documentVariant, tableOfContents);
293292
// extensions needed to generate a module's html
294293
asciidoctor.javaExtensionRegistry().includeProcessor(
295294
new SlingResourceIncludeProcessor(base, tableOfContents, xrefProcessor));

pantheon-bundle/src/main/java/com/redhat/pantheon/asciidoctor/extension/PantheonXrefProcessor.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.apache.sling.api.resource.Resource;
1515
import org.asciidoctor.ast.ContentNode;
1616
import org.asciidoctor.extension.InlineMacroProcessor;
17+
import org.osgi.service.component.annotations.Reference;
1718
import org.slf4j.Logger;
1819
import org.slf4j.LoggerFactory;
1920

@@ -47,18 +48,25 @@ public class PantheonXrefProcessor extends InlineMacroProcessor {
4748
* @param documentVariant
4849
* @param tableOfContents Doesn't necessarily need to be populated when this constructor is called.
4950
*/
50-
public PantheonXrefProcessor(DocumentVariant documentVariant, TableOfContents tableOfContents) {
51+
public PantheonXrefProcessor(@Reference DocumentVariant documentVariant, @Reference TableOfContents tableOfContents) {
5152
this.documentVariant = documentVariant;
5253
this.toc = tableOfContents;
5354
}
5455

5556
public String preprocess(String content) {
5657
List<String> urlList = new ArrayList<>();
58+
HashMap<String, ArrayList<String>> xrefTargetsMap = new HashMap<>();
59+
5760
if (!documentVariant.getPath().startsWith("/content/docs/")) {
5861
content = preprocessWithPattern(content, XREF_PATTERN, urlList);
5962
content = preprocessWithPattern(content, TRIANGLE_PATTERN, urlList);
6063
}
61-
XrefValidationHelper.getInstance().setObjectsToValidate(urlList);
64+
65+
if (!urlList.isEmpty()) {
66+
xrefTargetsMap.put(documentVariant.uuid().get(), (ArrayList<String>) urlList);
67+
XrefValidationHelper.setObjectsToValidate(xrefTargetsMap);
68+
}
69+
6270
return content;
6371
}
6472

pantheon-bundle/src/main/java/com/redhat/pantheon/servlet/DocumentVariantRenderServlet.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class DocumentVariantRenderServlet extends SlingSafeMethodsServlet {
5050
private final Logger log = LoggerFactory.getLogger(DocumentVariantRenderServlet.class);
5151

5252
private AsciidoctorService asciidoctorService;
53+
private XrefValidationHelper xrefValidationHelper;
5354

5455
@Activate
5556
public DocumentVariantRenderServlet(
@@ -94,7 +95,7 @@ protected void doGet(SlingHttpServletRequest request,
9495

9596
// only allow forced rerendering if this is a draft version. Released and historical revs are written in stone.
9697
boolean draft = latest && variant.hasDraft();
97-
XrefValidationHelper.getInstance().initList();
98+
XrefValidationHelper.initList();
9899
String html = asciidoctorService.getDocumentHtml(
99100
variant.getParentLocale().getParent(),
100101
LocaleUtils.toLocale(variant.getParentLocale().getName()),

pantheon-bundle/src/main/java/com/redhat/pantheon/servlet/PublishDraftVersion.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public void run(SlingHttpServletRequest request, PostResponse response, SlingPos
8787
return;
8888
}
8989
long startTime = System.currentTimeMillis();
90+
XrefValidationHelper.initList();
9091
super.run(request, response, processors);
9192
try {
9293
if (response.getError() == null) {
@@ -101,13 +102,12 @@ public void run(SlingHttpServletRequest request, PostResponse response, SlingPos
101102
.released().get();
102103

103104
// Regenerate the document once more
104-
XrefValidationHelper.getInstance().initList();
105105
asciidoctorService.getDocumentHtml(document, locale, variant, false, new HashMap(),true);
106106
events.fireEvent(new DocumentVersionPublishedEvent(documentVersion), 15);
107107
ServletUtils.getCustomerPortalUrl(request, response);
108108
}
109109
}catch (RepositoryException ex){
110-
logger.error("An error has occured ", ex.getMessage());
110+
logger.error("An error has occurred ", ex.getMessage());
111111
}
112112
log.debug("Operation Publishing draft version, completed");
113113
long elapseTime = System.currentTimeMillis() - startTime;

pantheon-bundle/src/main/java/com/redhat/pantheon/servlet/util/VersionUploadOperation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ protected void versionUpload(SlingHttpServletRequest request,
127127
resolver.commit();
128128

129129
Map<String, Object> context = asciidoctorService.buildContextFromRequest(request);
130-
XrefValidationHelper.getInstance().initList();
130+
XrefValidationHelper.initList();
131131
asciidoctorService.getDocumentHtml(document, localeObj, document.getWorkspace().getCanonicalVariantName(),
132132
true, context, true);
133133

pantheon-bundle/src/main/java/com/redhat/pantheon/validation/helper/ValidationHelper.java

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import org.slf4j.Logger;
99
import org.slf4j.LoggerFactory;
1010

11+
import java.util.ArrayList;
12+
import java.util.HashMap;
1113
import java.util.Optional;
1214

1315
/**
@@ -17,26 +19,32 @@ public class ValidationHelper {
1719
Logger logger = LoggerFactory.getLogger(ValidationHelper.class);
1820
public void createXrefValidationNode(DocumentVersion documentVersion, String content) throws PersistenceException {
1921
Violations violations = new XrefValidator(documentVersion.getParent(), content).validate();
20-
Validations validations = documentVersion.validations().getOrCreate();
21-
if(null != validations.validationType(PantheonConstants.TYPE_XREF).get()){
22-
try {
23-
validations.validationType(PantheonConstants.TYPE_XREF).get().delete();
24-
} catch (Exception e) {
25-
logger.error("error while validation node creation",e);
26-
}
27-
}
22+
// Get xrefTargetMap
23+
HashMap<String, ArrayList<String>> xrefTargetsMap = XrefValidationHelper.getObjectsToValidate();
2824

29-
if(violations.hasViolations()) {
30-
Validation validation;
31-
ErrorDetails errorDetails = violations.get(PantheonConstants.TYPE_XREF);
32-
if(null == errorDetails || errorDetails.length() ==0){
33-
return;
25+
if (xrefTargetsMap != null && xrefTargetsMap.containsKey(documentVersion.getParent().uuid().get())) {
26+
Validations validations = documentVersion.validations().getOrCreate();
27+
if(null != validations.validationType(PantheonConstants.TYPE_XREF).get()){
28+
try {
29+
validations.validationType(PantheonConstants.TYPE_XREF).get().delete();
30+
} catch (Exception e) {
31+
logger.error("error while validation node creation",e);
32+
}
3433
}
35-
ValidationType validationType = validations.validationType(PantheonConstants.TYPE_XREF).getOrCreate();
36-
for(int ind=0; ind< errorDetails.length();ind++) {
37-
validation = validationType.page(ind+1).getOrCreate();
38-
validation.setValidation(violations, ind);
34+
35+
if(violations.hasViolations()) {
36+
Validation validation;
37+
ErrorDetails errorDetails = violations.get(PantheonConstants.TYPE_XREF);
38+
if(null == errorDetails || errorDetails.length() ==0){
39+
return;
40+
}
41+
ValidationType validationType = validations.validationType(PantheonConstants.TYPE_XREF).getOrCreate();
42+
for(int ind=0; ind< errorDetails.length();ind++) {
43+
validation = validationType.page(ind+1).getOrCreate();
44+
validation.setValidation(violations, ind);
45+
}
3946
}
4047
}
48+
4149
}
4250
}

pantheon-bundle/src/main/java/com/redhat/pantheon/validation/helper/XrefValidationHelper.java

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,25 @@
22

33
import java.util.ArrayList;
44
import java.util.HashMap;
5-
import java.util.List;
6-
import java.util.Map;
75

86

97
public class XrefValidationHelper {
108

11-
private List<String> xRefs;
9+
private static HashMap<String, ArrayList<String>> xRefs;
1210

13-
private XrefValidationHelper() {
11+
public XrefValidationHelper() {
12+
xRefs = new HashMap<String, ArrayList<String>>();
1413
}
1514

16-
private static class SingletonHelper{
17-
private static final XrefValidationHelper INSTANCE = new XrefValidationHelper();
18-
}
19-
20-
public static XrefValidationHelper getInstance(){
21-
return SingletonHelper.INSTANCE;
22-
}
23-
public List<String> getObjectsToValidate() {
15+
public static HashMap<String, ArrayList<String>> getObjectsToValidate() {
2416
return xRefs;
2517
}
2618

27-
public void initList() {
28-
xRefs = new ArrayList<>();
19+
public static void initList() {
20+
XrefValidationHelper.xRefs = new HashMap<String, ArrayList<String>>();
2921
}
3022

31-
public void setObjectsToValidate(List<String> objectsToValidate) {
32-
if(null == xRefs || objectsToValidate.isEmpty()){
33-
return;
34-
}
35-
xRefs.addAll(objectsToValidate);
23+
public static void setObjectsToValidate(HashMap<String, ArrayList<String>> objectsToValidate) {
24+
XrefValidationHelper.xRefs = objectsToValidate;
3625
}
3726
}

0 commit comments

Comments
 (0)