Use URL params for sections

pull/20/head
Jens Pelzetter 2022-03-02 19:27:57 +01:00
parent 2c49a4fbc1
commit 496d0ac462
1 changed files with 28 additions and 29 deletions

View File

@ -35,6 +35,7 @@ import org.librecms.contentsection.ContentItem;
import org.librecms.contenttypes.MultiPartArticle; import org.librecms.contenttypes.MultiPartArticle;
import org.librecms.contenttypes.MultiPartArticleSection; import org.librecms.contenttypes.MultiPartArticleSection;
import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
/** /**
@ -45,6 +46,8 @@ import java.util.ArrayList;
@Named("CmsPagesMultiPartArticleModel") @Named("CmsPagesMultiPartArticleModel")
public class MultiPartArticleModel implements ProcessesContentItem { public class MultiPartArticleModel implements ProcessesContentItem {
private static final String MPA_SECTION_QUERY_PARAM = "mpa_section";
@Inject @Inject
private ContentItemModel contentItemModel; private ContentItemModel contentItemModel;
@ -197,12 +200,15 @@ public class MultiPartArticleModel implements ProcessesContentItem {
} }
private int readCurrentSection() { private int readCurrentSection() {
if (pageUrlModel.getPath().matches(".*/@sections/[0-9]*$")) { return Optional
final String[] tokens = pageUrlModel.getPath().split("/"); .ofNullable(
return Integer.valueOf(tokens[tokens.length - 1]) - 1; pageUrlModel.getQueryParameters().get(
} else { MPA_SECTION_QUERY_PARAM
return 0; )
} )
.filter(value -> value.matches("\\d*"))
.map(value -> Integer.parseInt(value))
.orElse(0);
} }
@Transactional(Transactional.TxType.REQUIRED) @Transactional(Transactional.TxType.REQUIRED)
@ -227,38 +233,31 @@ public class MultiPartArticleModel implements ProcessesContentItem {
@Transactional(Transactional.TxType.REQUIRED) @Transactional(Transactional.TxType.REQUIRED)
private List<String> buildSectionLinks() { private List<String> buildSectionLinks() {
final String pathWithoutSections; final int size = sectionTitles.size();
if (pageUrlModel.getPath().matches(".*/@sections/[0-9]*$")) {
final String path = pageUrlModel.getPath();
pathWithoutSections = path.substring(
0,
path.lastIndexOf("/@sections") + 1
);
} else {
pathWithoutSections = pageUrlModel.getPath();
}
final List<String> sectionLinksList = new ArrayList<>(); final List<String> sectionLinksList = new ArrayList<>();
for (int i = 0; i < sectionTitles.size(); i++) { for (int i = 0; i < size; i++) {
sectionLinksList.add( sectionLinksList.add(
String.format( String.format(
"%s/@sections/%d?%s", "?%s&%s=%d",
pathWithoutSections,
i,
pageUrlModel pageUrlModel
.getQueryParameters() .getQueryParameters()
.entrySet() .entrySet()
.stream() .stream()
.filter(
entry
-> !MPA_SECTION_QUERY_PARAM
.equals(
entry.getKey()
)
)
.map( .map(
entry -> String.format( entry -> String.format(
"%s=%s", "%s=%s", entry.getKey(), entry.getValue()
entry.getKey(),
entry.getValue()
) )
) )
.collect( .collect(Collectors.joining("&")),
Collectors.joining("&") MPA_SECTION_QUERY_PARAM,
) i
) )
); );
} }