diff --git a/ccm-cms/src/main/java/org/librecms/ui/contenttypes/MvcArticleTextBodyStep.java b/ccm-cms/src/main/java/org/librecms/ui/contenttypes/MvcArticleTextBodyStep.java index b174489bc..c0c9b6c44 100644 --- a/ccm-cms/src/main/java/org/librecms/ui/contenttypes/MvcArticleTextBodyStep.java +++ b/ccm-cms/src/main/java/org/librecms/ui/contenttypes/MvcArticleTextBodyStep.java @@ -152,7 +152,7 @@ public class MvcArticleTextBodyStep extends AbstractMvcAuthoringStep { } public List getVariants() { - return variants; + return Collections.unmodifiableList(variants); } /** @@ -178,7 +178,6 @@ public class MvcArticleTextBodyStep extends AbstractMvcAuthoringStep { * @return A redirect to this authoring step. */ @POST -// @Path("/@add") @Path("/add") @Transactional(Transactional.TxType.REQUIRED) public String addTextValue( @@ -212,11 +211,7 @@ public class MvcArticleTextBodyStep extends AbstractMvcAuthoringStep { getArticle().getText().addValue(locale, value); itemRepo.save(getArticle()); - return String.format( - "%s/%s/@edit", - buildRedirectPathForStep(), - locale.toString() - ); + return buildRedirectPathForStep(); } else { return documentUi.showAccessDenied( getContentSection(), @@ -261,7 +256,6 @@ public class MvcArticleTextBodyStep extends AbstractMvcAuthoringStep { @GET -// @Path("/{locale}/@edit") @Path("/edit/{locale}") @Transactional(Transactional.TxType.REQUIRED) public String editTextValue( @@ -303,7 +297,6 @@ public class MvcArticleTextBodyStep extends AbstractMvcAuthoringStep { * @return A redirect to this authoring step. */ @POST -// @Path("/{locale}/@edit") @Path("/edit/{locale}") @Transactional(Transactional.TxType.REQUIRED) public String editTextValue( @@ -428,7 +421,6 @@ public class MvcArticleTextBodyStep extends AbstractMvcAuthoringStep { final CmsEditorLocaleVariantRow variant = new CmsEditorLocaleVariantRow(); variant.setLocale(entry.getKey().toString()); final Document document = Jsoup.parseBodyFragment(entry.getValue()); - document.body().text(); variant.setWordCount( new StringTokenizer(document.body().text()).countTokens() ); diff --git a/ccm-cms/src/main/java/org/librecms/ui/contenttypes/MvcArticleTextBodyStepResources.java b/ccm-cms/src/main/java/org/librecms/ui/contenttypes/MvcArticleTextBodyStepResources.java index 1443ffde7..29d0b6bdf 100644 --- a/ccm-cms/src/main/java/org/librecms/ui/contenttypes/MvcArticleTextBodyStepResources.java +++ b/ccm-cms/src/main/java/org/librecms/ui/contenttypes/MvcArticleTextBodyStepResources.java @@ -18,6 +18,8 @@ */ package org.librecms.ui.contenttypes; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; import org.librecms.contentsection.ContentItem; import org.librecms.contentsection.ContentItemRepository; import org.librecms.contentsection.ContentSection; @@ -27,6 +29,7 @@ import org.librecms.ui.contentsections.ItemPermissionChecker; import org.librecms.ui.contentsections.documents.MvcAuthoringSteps; import java.util.Locale; +import java.util.StringTokenizer; import javax.enterprise.context.RequestScoped; import javax.inject.Inject; @@ -36,6 +39,8 @@ import javax.ws.rs.GET; import javax.ws.rs.NotFoundException; import javax.ws.rs.Path; import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; /** * @@ -58,8 +63,47 @@ public class MvcArticleTextBodyStepResources { private ItemPermissionChecker itemPermissionChecker; @GET -// @Path("/{locale}/@view") + @Path("/variants/{locale}/wordcount") + @Produces(MediaType.TEXT_HTML) + @Transactional(Transactional.TxType.REQUIRED) + public long getWordCount( + @PathParam(MvcAuthoringSteps.SECTION_IDENTIFIER_PATH_PARAM) + final String sectionIdentifier, + @PathParam(MvcAuthoringSteps.DOCUMENT_PATH_PATH_PARAM_NAME) + final String documentPathParam, + @PathParam("locale") final String localeParam + ) { + final ContentSection contentSection = sectionsUi + .findContentSection(sectionIdentifier) + .orElseThrow( + () -> new NotFoundException() + ); + + final ContentItem document = itemRepo + .findByPath(contentSection, documentPathParam) + .orElseThrow( + () -> new NotFoundException() + ); + + if (!(document instanceof Article)) { + throw new NotFoundException(); + } + + final Article article = (Article) document; + if (itemPermissionChecker.canEditItem(article)) { + final String text = article + .getText() + .getValue(new Locale(localeParam)); + final Document jsoupDoc = Jsoup.parseBodyFragment(text); + return new StringTokenizer(jsoupDoc.body().text()).countTokens(); + } else { + throw new ForbiddenException(); + } + } + + @GET @Path("/variants/{locale}") + @Produces(MediaType.TEXT_HTML) @Transactional(Transactional.TxType.REQUIRED) public String viewTextValue( @PathParam(MvcAuthoringSteps.SECTION_IDENTIFIER_PATH_PARAM) @@ -68,14 +112,6 @@ public class MvcArticleTextBodyStepResources { final String documentPathParam, @PathParam("locale") final String localeParam ) { -// try { -// init(); -// } catch (ContentSectionNotFoundException ex) { -// return ex.showErrorMessage(); -// } catch (DocumentNotFoundException ex) { -// return ex.showErrorMessage(); -// } - final ContentSection contentSection = sectionsUi .findContentSection(sectionIdentifier) .orElseThrow( diff --git a/ccm-cms/src/main/resources/META-INF/resources/components/librecms/cmsEditor.xhtml b/ccm-cms/src/main/resources/META-INF/resources/components/librecms/cmsEditor.xhtml index 0d2776dd6..124f1e58e 100644 --- a/ccm-cms/src/main/resources/META-INF/resources/components/librecms/cmsEditor.xhtml +++ b/ccm-cms/src/main/resources/META-INF/resources/components/librecms/cmsEditor.xhtml @@ -14,7 +14,7 @@ shortDescription="Label for the add button" type="String" /> @@ -24,17 +24,17 @@ shortDescription="Help text for the locale select field" type="String" /> @@ -44,17 +44,17 @@ shortDescription="Can the current user edit the text?" type="boolean" /> @@ -69,7 +69,7 @@ shortDescription="Label for the value field" type="String" /> @@ -91,7 +91,7 @@ shortDescription="ID for the editor. Also used as prefix to generate IDs for some subcomponents" type="String" /> @@ -337,9 +337,11 @@ - + #{variant.locale} - #{CmsAdminMessages.getMessage('cms_editor.variants.wordcount', [variant.wordCount])} + + #{variant.wordCount} #{CmsAdminMessages['cms_editor.variants.wordcount']} +