Commit 3f73b186 by Anton Sudak

Replace various utility method with better analogs

parent 6bb271ea
@import org.apache.commons.lang3.StringUtils
@import ch.insign.commons.search._
@import ch.insign.cms.views.html._
@(
data: ch.insign.cms.blocks.searchresultblock.SearchResultBlock,
query: String,
results: List[ch.insign.commons.search.SearchResult])
@import play.mvc.Controller
@import ch.insign.commons.search._
@import ch.insign.cms.views.html._
@import ch.insign.cms.models._
@import play.api.i18n.Messages;
results: List[ch.insign.commons.search.SearchResult]
)
@contentBlockForSearchBlock(data = data){
<p>
@if(Template.nonEmpty(query)){
@Messages("searchresult.searchfor", query, String.valueOf(results.size))
}else{
@Messages("searchresult.noquery")
}
@Option(query).filter(_.nonEmpty).map { q =>
@Messages("searchresult.searchfor", q, results.size)
}.getOrElse {
@Messages("searchresult.noquery")
}
</p>
<ol>
@for(result <- results){
@for(result <- results) {
<li>
<a href="@result.getUrl">@Template.nonEmpty(result.getTitle, result.getUrl)</a>
<br />@Template.html(result.getSnippet)
<a href="@result.getUrl">@Seq(result.getTitle, result.getUrl).filter(StringUtils.isNotBlank(_)).headOption</a>
<br />@Html(result.getSnippet)
</li>
}
</ol>
......
......@@ -37,7 +37,7 @@
}
@if(isNonEmpty(data.getText)) {
<p class="lead slider-text">@Template.html(data.getText)</p>
<p class="lead slider-text">@Html(data.getText.get)</p>
}
@if(isNonEmpty(data.getLink) && isNonEmpty(data.getLinkText)) {
......
......@@ -312,10 +312,14 @@ public class FrontendController extends Controller {
StringBuilder sb = new StringBuilder();
sb.append("<html>\n<body>\n");
sb.append(String.valueOf(results.size())).append(" results found for '").append(keywords).append("'<br><br>\n");
sb.append(results.size()).append(" results found for '").append(keywords).append("'<br><br>\n");
for (SearchResult result : results) {
sb.append("<p>\n");
sb.append(String.format("<a href='%s'>%s</a>\n", result.getUrl(), Template.nonEmpty(result.getTitle(), result.getUrl(), "...")));
sb.append(String.format("<a href='%s'>%s</a>\n",
result.getUrl(),
Stream.of(result.getTitle(), result.getUrl(), "...")
.filter(StringUtils::isNotBlank)
.head()));
sb.append("<br />\n...");
sb.append(result.getSnippet());
sb.append("...</p>\n");
......
......@@ -220,10 +220,10 @@ public class DefaultBootstrapper implements Bootstrapper {
page3.getPageTitle().set("en", "Page 2.1");
// Add a sub block to the main pane collection
CollectionBlock mainPane = (CollectionBlock) Template.addBlockToSlot(CollectionBlock.class, homepage, "main");
CollectionBlock mainPane = Template.addBlockToSlot(CollectionBlock.class, homepage, "main");
ContentBlock block, block2, block3, block4;
try {
block = (ContentBlock)mainPane.addSubBlock(ContentBlock.class);
block = mainPane.addSubBlock(ContentBlock.class);
block.getTitle().set("en", "Play! CMS");
block.getContent().set("en", "The insign Play CMS is meant as a solid platform for custom Play projects " +
"that require solid cms features at their base. Play CMS does not try to be a full-fledged major " +
......@@ -231,7 +231,7 @@ public class DefaultBootstrapper implements Bootstrapper {
"requirements on content editing.");
block.save();
block2 = (ContentBlock)mainPane.addSubBlock(ContentBlock.class);
block2 = mainPane.addSubBlock(ContentBlock.class);
block2.getTitle().set("en", "Search");
block2.getContent().set("en", "<p>The CMS offers a flexible interface for fulltext searching. " +
"Implementing projects and define their own SearchProviders, or use an existing one " +
......@@ -239,14 +239,14 @@ public class DefaultBootstrapper implements Bootstrapper {
"<a href=\"https://confluence.insign.ch/display/PLAY/Search\">Learn more about search</a>.</p>");
block2.save();
block3 = (ContentBlock)mainPane.addSubBlock(ContentBlock.class);
block3 = mainPane.addSubBlock(ContentBlock.class);
block3.getTitle().set("en", "Caching");
block3.getContent().set("en", "Play-CMS supports powerful " +
"<a href=\"https://confluence.insign.ch/display/PLAY/Caching\">caching mechanisms</a> at the block level. " +
"Caching is disabled by default, it can be enabled globally via configuration and refined per block class.");
block3.save();
block4 = (ContentBlock)mainPane.addSubBlock(ContentBlock.class);
block4 = mainPane.addSubBlock(ContentBlock.class);
block4.getTitle().set("en", "Play Auth");
block4.getContent().set("en", "<p>The insign <a href=\"https://confluence.insign.ch/display/PLAY/Play+Auth\">Play Auth</a> " +
"provides functionality to manage essential information about parties (people and organizations) and " +
......@@ -259,10 +259,10 @@ public class DefaultBootstrapper implements Bootstrapper {
}
// Add a sub block to the sidebar collection
CollectionBlock sidebarPane = (CollectionBlock) Template.addBlockToSlot(CollectionBlock.class, homepage, "sidebar");
CollectionBlock sidebarPane = Template.addBlockToSlot(CollectionBlock.class, homepage, "sidebar");
ContentBlock sidebarBlock;
try {
sidebarBlock = (ContentBlock)sidebarPane.addSubBlock(ContentBlock.class);
sidebarBlock = sidebarPane.addSubBlock(ContentBlock.class);
sidebarBlock.getTitle().set("en", "Multi-language");
sidebarBlock.getContent().set("en", "The cms is " +
"<a href=\"https://confluence.insign.ch/display/PLAY/Languages\">multi-language capable</a>. " +
......@@ -290,7 +290,7 @@ public class DefaultBootstrapper implements Bootstrapper {
}
try {
SliderEntryBlock slide1 = (SliderEntryBlock)sliderCollection.addSubBlock(SliderEntryBlock.class);
SliderEntryBlock slide1 = sliderCollection.addSubBlock(SliderEntryBlock.class);
slide1.setSite(site.key);
slide1.getTitle().set("en", "Play! CMS");
slide1.getText().set("en", "The Play Framework 2 CMS");
......
......@@ -39,6 +39,7 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.persistence.*;
import java.util.*;
import java.util.stream.Stream;
@Entity
@Table(name = "cms_block_page")
......@@ -427,7 +428,15 @@ public class PageBlock extends AbstractBlock implements Page {
}
public String toString() {
return String.format(getClass().getSimpleName() + " (id: %s, title: %s, key: %s)", getId(), Template.nonEmpty(getPageTitle(), getNavTitle()), getKey());
return String.format(
getClass().getSimpleName() + " (id: %s, title: %s, key: %s)",
getId(),
Stream.of(getPageTitle(), getNavTitle())
.map(MString::get)
.filter(StringUtils::isNotBlank)
.findFirst()
.orElse(""),
getKey());
}
/**
......
......@@ -20,7 +20,6 @@ import ch.insign.cms.blocks.ResponsiveBlock;
import ch.insign.cms.blocks.ResponsiveCollection;
import ch.insign.cms.blocks.backendlinkblock.BackendLinkBlock;
import ch.insign.cms.blocks.backendlinkblock.BackendMenuItem;
import ch.insign.cms.blocks.horizontalcollection.HorizontalCollectionBlock;
import ch.insign.cms.permissions.BlockPermission;
import ch.insign.cms.blocks.groupingblock.GroupingBlock;
import ch.insign.commons.db.MString;
......@@ -39,7 +38,7 @@ import play.mvc.Controller;
import play.mvc.Http;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* This is a helper class for templates to work with cms objects.
......@@ -57,15 +56,13 @@ public class Template {
* @param slot
* @return block
*/
public static AbstractBlock addBlockToSlot(Class<? extends AbstractBlock> blockClass, AbstractBlock parentBlock, String slot) {
AbstractBlock newBlock = AbstractBlock.find.byParentAndSlot(parentBlock, slot);
public static <T extends AbstractBlock> T addBlockToSlot(Class<T> blockClass, AbstractBlock parentBlock, String slot) {
if (parentBlock == null) {
throw new RuntimeException("Cannot add new block to slot: Parent is null!");
}
T newBlock = (T) AbstractBlock.find.byParentAndSlot(parentBlock, slot);
if (newBlock == null) {
try {
logger.info("Creating a new " + blockClass.getSimpleName() + " for parent " + parentBlock.getId() + " and slot " + slot);
newBlock = blockClass.newInstance();
......@@ -180,12 +177,12 @@ public class Template {
* Returns "" if none was found.
*/
public static String nonEmpty(Object... args) {
for (int i = 0; i < args.length; i++) {
if (args[i] != null && !args[i].toString().equals("")) {
return args[i].toString();
}
}
return "";
return Stream.of(args)
.filter(o -> o != null)
.map(Object::toString)
.filter(StringUtils::isNotBlank)
.findFirst()
.orElse("");
}
/**
......@@ -237,7 +234,7 @@ public class Template {
/**
* Get the time restriction state of a block.
*/
public static TR hasTimeRestriction (AbstractBlock block) {
public static TR hasTimeRestriction(AbstractBlock block) {
Date now = new Date();
Date from = block.getDisplayFrom();
......@@ -259,7 +256,7 @@ public class Template {
* Returns {@code true} if {@code block} is NOT accessible for unauthenticated users
* and users without {@code AbstractBlock.PERMISSION_READ} permission.
*/
public static boolean hasAccessRestriction (AbstractBlock block) {
public static boolean hasAccessRestriction(AbstractBlock block) {
return PlayAuth.isRestricted(BlockPermission.READ, block);
}
......
......@@ -73,7 +73,7 @@
<div class="checkout_container">
<div class="row" id="template-@emailTemplate.getId">
<div class="col-md-12">
@Template.html(emailTemplate.getContent.get(lang))
@Html(emailTemplate.getContent.get(lang))
</div>
</div>
</div>
......
......@@ -16,17 +16,14 @@
package ch.insign.commons.search;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
/**
* Search results interface
*/
public interface SearchResult {
public String getTitle();
public String getSnippet();
public String getUrl();
public String getImage();
public String getAuthor();
public String getLanguage();
String getTitle();
String getSnippet();
String getUrl();
String getImage();
String getAuthor();
String getLanguage();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment