Commit 0ea3cfb1 authored by Matija Obreza's avatar Matija Obreza
Browse files

CMS update

- Always check for default content
- Download content article in JSON
- Paginate articles properly
parent df678a93
......@@ -73,12 +73,12 @@ public class CreateContentListener extends RunAsAdminListener {
}
private void createArticles() throws IOException, JsonProcessingException {
if (!createContent) {
LOG.warn("Skipping content creation on startup.");
return;
}
// if (!createContent) {
// LOG.warn("Skipping content creation on startup.");
// return;
// }
LOG.debug("Checking if default content exists");
LOG.info("Checking if default content exists");
final ClassLoader classLoader = CreateContentListener.class.getClassLoader();
final PathMatchingResourcePatternResolver rpr = new PathMatchingResourcePatternResolver(classLoader);
final String resourcePath = "/default-content/*";
......@@ -90,10 +90,18 @@ public class CreateContentListener extends RunAsAdminListener {
final ObjectMapper mapper = new ObjectMapper();
try (InputStream stream = r.getInputStream()) {
final JsonNode json = mapper.readTree(stream);
JsonNode templateNode = json.get("template");
final boolean isTemplate = templateNode != null && templateNode.booleanValue();
final Iterator<Entry<String, JsonNode>> it = json.fields();
while (it.hasNext()) {
final Entry<String, JsonNode> entry = it.next();
if (entry.getKey().length() != 2) {
// Skip over non-language fields
continue;
}
final Locale locale = new Locale(entry.getKey());
// Load from default locale if exists
......@@ -102,10 +110,10 @@ public class CreateContentListener extends RunAsAdminListener {
// If nothing is found, parse the resource and create content
if (article == null) {
try {
contentService.createGlobalArticle(slug, locale, entry.getValue().get("title").asText(), entry.getValue().get("body").asText(), null, false);
LOG.info("Created article for slug: {} lang={}", slug, locale.getLanguage());
contentService.createGlobalArticle(slug, locale, entry.getValue().get("title").asText(), entry.getValue().get("body").asText(), null, isTemplate);
LOG.warn("Created article for slug: {} lang={}", slug, locale.getLanguage());
} catch (CRMException e) {
LOG.warn("Failed to create global article slug={}.", slug, e);
LOG.error("Failed to create global article slug={}.", slug, e);
}
}
}
......
......@@ -42,6 +42,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
......@@ -50,10 +51,12 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
@Controller
@RequestMapping("/content")
......@@ -346,6 +349,28 @@ public class ArticleController extends BaseController {
return "/content/article-edit";
}
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
@RequestMapping(value = "/edit/{url}/{language}", produces = MediaType.APPLICATION_JSON_VALUE, params = { "json" })
public @ResponseBody Object articleAsJson(ModelMap model, @PathVariable(value = "url") String slug, @PathVariable("language") String language) {
Article article = contentService.getArticleBySlugAndLang(slug, language);
if (article == null) {
article = new Article();
article.setSlug(slug);
article.setLang(language);
}
model.addAttribute("article", article);
ObjectMapper mapper=new ObjectMapper();
ObjectNode node = mapper.createObjectNode();
node.put("template", article.isTemplate());
ObjectNode lang = node.putObject(language);
lang.put("title", article.getTitle());
lang.put("body", article.getBody());
return node;
}
/**
* Edit article in another language
*
......
......@@ -30,7 +30,7 @@
</ul>
</div>
</div>
<local:paginate2 page="${pagedData}"/>
<local:paginate2 action="${language eq null || language eq '' ? '' : '?language='.concat(language)}" page="${pagedData}"/>
</div>
</div>
......@@ -57,6 +57,7 @@
</c:when>
<c:otherwise>
<a href="<c:url value="/content/edit/${article.slug}/${article.lang}" />"><c:out value="${article.slug}" /></a>
<a style="float: right; display: inline-block;" href="<c:url value="/content/edit/${article.slug}/${article.lang}?json" />">JSON</a>
</c:otherwise>
</c:choose>
</td>
......
......@@ -9,6 +9,9 @@
<%--
This is the more civil <nav> version of the pagination
--%>
<c:if test="${action eq null}">
<c:set var="action" value="" />
</c:if>
<nav class="text-center pull-left">
<ul class="pagination pagination-lg">
<li><span><spring:message code="paged.totalElements"
......@@ -30,24 +33,24 @@
<c:if test="${page.number gt 0}">
<c:choose>
<c:when test="${empty q}">
<li><a href="?"
<li><a href="<spring:url value="${action}" />"
aria-label="<spring:message
code="pagination.previous-page" />">
<span aria-hidden="true">&laquo;</span>
</a></li>
<li><a
href="<spring:url value=""><spring:param name="page" value="${page.number eq 0 ? 1 : page.number}" /></spring:url>"
href="<spring:url value="${action}"><spring:param name="page" value="${page.number eq 0 ? 1 : page.number}" /></spring:url>"
aria-label="<spring:message
code="pagination.previous-page" />">&lsaquo;</a></li>
</c:when>
<c:otherwise>
<li><a href="<spring:url value=""><spring:param name="q" value="${q}" /></spring:url>"
<li><a href="<spring:url value="${action}"><spring:param name="q" value="${q}" /></spring:url>"
aria-label="<spring:message
code="pagination.previous-page" />">
<span aria-hidden="true">&laquo;</span>
</a></li>
<li><a
href="<spring:url value=""><spring:param name="page" value="${page.number eq 0 ? 1 : page.number}" /><spring:param name="q" value="${q}" /></spring:url>"
href="<spring:url value="${action}"><spring:param name="page" value="${page.number eq 0 ? 1 : page.number}" /><spring:param name="q" value="${q}" /></spring:url>"
aria-label="<spring:message
code="pagination.previous-page" />">&lsaquo;</a></li>
</c:otherwise>
......@@ -55,7 +58,7 @@
</c:if>
<li><span class="pagination-input">
<form method="get" action="">
<form method="get" action="${action}">
<input class="text-center" type="text" name="page" id="paginate2-input-field"
placeholder="<c:out value="${page.number + 1}" />" value="<c:out value="${page.number + 1}" />" />
</form>
......@@ -69,18 +72,18 @@
<c:choose>
<c:when test="${empty q}">
<li><a
href="<spring:url value=""><spring:param name="page" value="${page.number+2}" /></spring:url>" aria-label="<spring:message
href="<spring:url value="${action}"><spring:param name="page" value="${page.number+2}" /></spring:url>" aria-label="<spring:message
code="pagination.next-page" />">&rsaquo;</a></li>
<li><a
href="<spring:url value=""><spring:param name="page" value="${page.totalPages}" /></spring:url>" aria-label="<spring:message
href="<spring:url value="${action}"><spring:param name="page" value="${page.totalPages}" /></spring:url>" aria-label="<spring:message
code="pagination.next-page" />">&raquo;</a></li>
</c:when>
<c:otherwise>
<li><a
href="<spring:url value=""><spring:param name="page" value="${page.number+2}" /><spring:param name="q" value="${q}" /></spring:url>" aria-label="<spring:message
href="<spring:url value="${action}"><spring:param name="page" value="${page.number+2}" /><spring:param name="q" value="${q}" /></spring:url>" aria-label="<spring:message
code="pagination.next-page" />">&rsaquo;</a></li>
<li><a
href="<spring:url value=""><spring:param name="page" value="${page.totalPages}" /><spring:param name="q" value="${q}" /></spring:url>" aria-label="<spring:message
href="<spring:url value="${action}"><spring:param name="page" value="${page.totalPages}" /><spring:param name="q" value="${q}" /></spring:url>" aria-label="<spring:message
code="pagination.next-page" />">&raquo;</a></li>
</c:otherwise>
</c:choose>
......
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