Commit 05ec1688 authored by Matija Obreza's avatar Matija Obreza

Article#template review

parent 8b48d5ea
...@@ -603,7 +603,7 @@ ...@@ -603,7 +603,7 @@
<configuration> <configuration>
<source>${jdk.source}</source> <source>${jdk.source}</source>
<target>${jdk.target}</target> <target>${jdk.target}</target>
<optimize>false</optimize> <optimize>true</optimize>
<showDeprecation>${show.deprecations}</showDeprecation> <showDeprecation>${show.deprecations}</showDeprecation>
<showWarnings>true</showWarnings> <showWarnings>true</showWarnings>
<failOnError>true</failOnError> <failOnError>true</failOnError>
......
...@@ -25,6 +25,7 @@ import java.util.Map.Entry; ...@@ -25,6 +25,7 @@ import java.util.Map.Entry;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.genesys2.server.listener.RunAsAdminListener; import org.genesys2.server.listener.RunAsAdminListener;
import org.genesys2.server.model.impl.Article; import org.genesys2.server.model.impl.Article;
import org.genesys2.server.service.CRMException;
import org.genesys2.server.service.ContentService; import org.genesys2.server.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
...@@ -101,8 +102,12 @@ public class CreateContentListener extends RunAsAdminListener { ...@@ -101,8 +102,12 @@ public class CreateContentListener extends RunAsAdminListener {
// If nothing is found, parse the resource and create content // If nothing is found, parse the resource and create content
if (article == null) { if (article == null) {
contentService.updateGlobalArticle(slug, locale, entry.getValue().get("title").asText(), entry.getValue().get("body").asText(), null); try {
_logger.info("Created article for slug: " + slug + " lang=" + locale.getLanguage()); contentService.createGlobalArticle(slug, locale, entry.getValue().get("title").asText(), entry.getValue().get("body").asText(), null, false);
_logger.info("Created article for slug: " + slug + " lang=" + locale.getLanguage());
} catch (CRMException e) {
_logger.warn("Failed to create global article slug=" + slug + ".", e);
}
} }
} }
} }
......
...@@ -53,20 +53,9 @@ public class FullTextDocument { ...@@ -53,20 +53,9 @@ public class FullTextDocument {
@Field(index = FieldIndex.not_analyzed, type = FieldType.Date) @Field(index = FieldIndex.not_analyzed, type = FieldType.Date)
private Date lastModifiedDate; private Date lastModifiedDate;
@Field(type = FieldType.Boolean)
private Boolean template;
/** The score here will boost result relevance */ /** The score here will boost result relevance */
private Float score = 0.5f; private Float score = 0.5f;
public Boolean getTemplate() {
return template;
}
public void setTemplate(Boolean template) {
this.template = template;
}
public Long getId() { public Long getId() {
return this.id; return this.id;
} }
......
/** /*
* Copyright 2014 Global Crop Diversity Trust * Copyright 2017 Global Crop Diversity Trust
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -12,47 +12,18 @@ ...@@ -12,47 +12,18 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
**/ */
package org.genesys2.server.service.impl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.model.impl.Article;
import org.genesys2.server.service.ContentService;
import org.genesys2.server.service.HtmlSanitizer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;
@Component
public class ContentSanitizer {
public static final Log LOG = LogFactory.getLog(ContentSanitizer.class);
@Autowired package org.genesys2.server.service;
private ContentService contentService;
@Autowired /**
private HtmlSanitizer htmlSanitizer; * Content Service related exceptions
*/
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')") public class CRMException extends Exception {
public void sanitizeAll() {
LOG.info("Sanitizing articles");
Page<Article> articles;
int page = 0;
do {
articles = contentService.listArticles(new PageRequest(page++, 10));
for (final Article a : articles) {
a.setBody(htmlSanitizer.sanitize(a.getBody()));
}
contentService.save(articles.getContent()); private static final long serialVersionUID = 1L;
} while (articles.hasNext()); public CRMException(String message) {
super(message);
} }
} }
...@@ -37,14 +37,14 @@ public interface ContentService { ...@@ -37,14 +37,14 @@ public interface ContentService {
Page<ActivityPost> allNews(int page); Page<ActivityPost> allNews(int page);
ClassPK ensureClassPK(Class<?> clazz); ClassPK ensureClassPK(Class<?> clazz);
ClassPK getClassPk(String shortName); ClassPK getClassPk(String shortName);
ClassPK getClassPk(Class<?> clazz); ClassPK getClassPk(Class<?> clazz);
/** /**
* Load article with {@link ClassPK} of clazz with specified id and the * Load article with {@link ClassPK} of clazz with specified id and the "slug"
* "slug" for the locale * for the locale
* *
* @param slug * @param slug
* @param locale * @param locale
...@@ -54,7 +54,7 @@ public interface ContentService { ...@@ -54,7 +54,7 @@ public interface ContentService {
Article getArticle(EntityId entity, String slug, Locale locale); Article getArticle(EntityId entity, String slug, Locale locale);
Article getArticleBySlugAndLang(String slug, String lang); Article getArticleBySlugAndLang(String slug, String lang);
Article getArticleBySlugLangTargetIdClassPk(String slug, String lang, Long targetId, String classPkShortName); Article getArticleBySlugLangTargetIdClassPk(String slug, String lang, Long targetId, String classPkShortName);
...@@ -63,15 +63,14 @@ public interface ContentService { ...@@ -63,15 +63,14 @@ public interface ContentService {
* *
* @param slug * @param slug
* @param locale * @param locale
* @param useDefault * @param useDefault Load article from default language
* Load article from default language
* @return * @return
*/ */
Article getGlobalArticle(String slug, Locale locale, boolean useDefault); Article getGlobalArticle(String slug, Locale locale, boolean useDefault);
/** /**
* Loads a global article in the specified locale, or when not found the * Loads a global article in the specified locale, or when not found the default
* default locale * locale
* *
* @param string * @param string
* @param locale * @param locale
...@@ -81,9 +80,9 @@ public interface ContentService { ...@@ -81,9 +80,9 @@ public interface ContentService {
Page<Article> listArticles(Pageable pageable); Page<Article> listArticles(Pageable pageable);
Page<Article> listArticlesByLang(String lang, Pageable pageable); Page<Article> listArticlesByLang(String lang, Pageable pageable);
void save(Iterable<Article> articles); // void save(Iterable<Article> articles);
/** /**
* Create new activity post * Create new activity post
...@@ -94,14 +93,16 @@ public interface ContentService { ...@@ -94,14 +93,16 @@ public interface ContentService {
*/ */
ActivityPost createActivityPost(String title, String body); ActivityPost createActivityPost(String title, String body);
Article updateArticle(EntityId entity, String slug, String title, String body, String summary, Locale locale, Boolean isTemplate); Article updateArticle(EntityId entity, String slug, String title, String body, String summary, Locale locale) throws CRMException;
Article updateArticle(Class<?> clazz, Long id, String slug, String title, String body, String summary, Locale locale, Boolean isTemplate ); Article updateArticle(Class<?> clazz, Long id, String slug, String title, String body, String summary, Locale locale) throws CRMException;
Article updateArticle(long id, String slug, String title, String body, String summary, Boolean isTemplate); Article updateArticle(long id, String slug, String title, String body, String summary);
Article updateGlobalArticle(String slug, Locale locale, String title, String body, String summary); Article createGlobalArticle(String slug, Locale locale, String title, String body, String summary, boolean isTemplate) throws CRMException;
Article updateGlobalArticle(String slug, Locale locale, String title, String body, String summary) throws CRMException;
ActivityPost getActivityPost(long id); ActivityPost getActivityPost(long id);
ActivityPost updateActivityPost(long id, String title, String body); ActivityPost updateActivityPost(long id, String title, String body);
...@@ -124,4 +125,6 @@ public interface ContentService { ...@@ -124,4 +125,6 @@ public interface ContentService {
Menu updateMenu(String key, List<MenuItem> menuItems); Menu updateMenu(String key, List<MenuItem> menuItems);
MenuItem ensureMenuItem(String menuKey, String url, String text); MenuItem ensureMenuItem(String menuKey, String url, String text);
void sanitizeAll();
} }
...@@ -38,7 +38,7 @@ public interface CropService { ...@@ -38,7 +38,7 @@ public interface CropService {
List<Crop> getCrops(Taxonomy2 taxonomy2); List<Crop> getCrops(Taxonomy2 taxonomy2);
void updateBlurp(Crop crop, String textBody, String summary, Locale locale); void updateBlurp(Crop crop, String textBody, String summary, Locale locale) throws CRMException;
void rebuildTaxonomies(); void rebuildTaxonomies();
......
...@@ -61,7 +61,7 @@ public interface GeoService { ...@@ -61,7 +61,7 @@ public interface GeoService {
List<Country> listAll(); List<Country> listAll();
void updateBlurp(Country country, String blurp, Locale locale); void updateBlurp(Country country, String blurp, Locale locale) throws CRMException;
List<Country> listAll(Locale locale); List<Country> listAll(Locale locale);
......
...@@ -47,7 +47,7 @@ public interface InstituteService { ...@@ -47,7 +47,7 @@ public interface InstituteService {
List<FaoInstitute> update(Collection<FaoInstitute> institutes); List<FaoInstitute> update(Collection<FaoInstitute> institutes);
void updateAbout(FaoInstitute faoInstitute, String body, String summary, Locale locale); void updateAbout(FaoInstitute faoInstitute, String body, String summary, Locale locale) throws CRMException;
void updateCountryRefs(); void updateCountryRefs();
......
...@@ -31,7 +31,7 @@ public interface OrganizationService { ...@@ -31,7 +31,7 @@ public interface OrganizationService {
Organization getOrganization(String slug); Organization getOrganization(String slug);
Article updateAbout(Organization organization, String body, String summary, Locale locale); Article updateAbout(Organization organization, String body, String summary, Locale locale) throws CRMException;
Organization update(long id, String newSlug, String title); Organization update(long id, String newSlug, String title);
......
...@@ -39,5 +39,5 @@ public interface ProjectService { ...@@ -39,5 +39,5 @@ public interface ProjectService {
void deleteProject(Project project); void deleteProject(Project project);
void updateBlurp(Project project, String textBody, String summary, Locale locale); void updateBlurp(Project project, String textBody, String summary, Locale locale) throws CRMException;
} }
...@@ -37,9 +37,6 @@ public interface UserService extends BasicUserService<UserRole, User> { ...@@ -37,9 +37,6 @@ public interface UserService extends BasicUserService<UserRole, User> {
@PreAuthorize("hasRole('ADMINISTRATOR')") @PreAuthorize("hasRole('ADMINISTRATOR')")
User addUser(User user) throws UserException, PasswordPolicyException; User addUser(User user) throws UserException, PasswordPolicyException;
@PreAuthorize("hasRole('ADMINISTRATOR')")
void updateUser(User user) throws UserException;
@PreAuthorize("hasRole('ADMINISTRATOR')") @PreAuthorize("hasRole('ADMINISTRATOR')")
void removeUserById(long userId) throws UserException; void removeUserById(long userId) throws UserException;
......
/** /*
* Copyright 2014 Global Crop Diversity Trust * Copyright 2017 Global Crop Diversity Trust
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
**/ */
package org.genesys2.server.service.impl; package org.genesys2.server.service.impl;
...@@ -41,6 +41,7 @@ import org.genesys2.server.persistence.domain.ActivityPostRepository; ...@@ -41,6 +41,7 @@ import org.genesys2.server.persistence.domain.ActivityPostRepository;
import org.genesys2.server.persistence.domain.ArticleRepository; import org.genesys2.server.persistence.domain.ArticleRepository;
import org.genesys2.server.persistence.domain.MenuItemRepository; import org.genesys2.server.persistence.domain.MenuItemRepository;
import org.genesys2.server.persistence.domain.MenuRepository; import org.genesys2.server.persistence.domain.MenuRepository;
import org.genesys2.server.service.CRMException;
import org.genesys2.server.service.ContentService; import org.genesys2.server.service.ContentService;
import org.genesys2.server.service.HtmlSanitizer; import org.genesys2.server.service.HtmlSanitizer;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -111,14 +112,6 @@ public class ContentServiceImpl implements ContentService { ...@@ -111,14 +112,6 @@ public class ContentServiceImpl implements ContentService {
return articleRepository.getAllByLang(lang, pageable); return articleRepository.getAllByLang(lang, pageable);
} }
@Override
@Transactional(readOnly = false)
// @PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
@CacheEvict(value = "contentcache", allEntries = true)
public void save(Iterable<Article> articles) {
articleRepository.save(articles);
}
@Override @Override
@Cacheable(value = "contentcache", key = "'globalarticle-' + #slug + '-' + #locale.language + '-' + #useDefault") @Cacheable(value = "contentcache", key = "'globalarticle-' + #slug + '-' + #locale.language + '-' + #useDefault")
public Article getGlobalArticle(String slug, Locale locale, boolean useDefault) { public Article getGlobalArticle(String slug, Locale locale, boolean useDefault) {
...@@ -165,41 +158,46 @@ public class ContentServiceImpl implements ContentService { ...@@ -165,41 +158,46 @@ public class ContentServiceImpl implements ContentService {
@Transactional(readOnly = false) @Transactional(readOnly = false)
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')") @PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
@CacheEvict(value = "contentcache", allEntries = true) @CacheEvict(value = "contentcache", allEntries = true)
public Article updateArticle(long id, String slug, String title, String body, String summary, Boolean isTemplate) { public Article updateArticle(long id, String slug, String title, String body, String summary) {
final Article article = articleRepository.findOne(id); final Article article = articleRepository.findOne(id);
updateArticleContent(article, slug, title, body, summary);
articleRepository.save(article);
return article;
}
private void updateArticleContent(final Article article, String slug, String title, String body, String summary) {
article.setPostDate(Calendar.getInstance());
article.setSlug(slug); article.setSlug(slug);
article.setTitle(htmlSanitizer.sanitize(title)); article.setTitle(htmlSanitizer.sanitize(title));
article.setSummary(htmlSanitizer.sanitize(summary)); article.setSummary(htmlSanitizer.sanitize(summary));
article.setTemplate(isTemplate);
if (isTemplate) { if (article.isTemplate()) {
article.setBody(body); article.setBody(body);
} else { } else {
article.setBody(htmlSanitizer.sanitize(body)); article.setBody(htmlSanitizer.sanitize(body));
} }
articleRepository.save(article);
return article;
} }
@Override @Override
@Transactional(readOnly = false) @Transactional(readOnly = false)
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')") @PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
@CacheEvict(value = "contentcache", allEntries = true) @CacheEvict(value = "contentcache", allEntries = true)
public Article updateGlobalArticle(String slug, Locale locale, String title, String body, String summary) { public Article updateGlobalArticle(String slug, Locale locale, String title, String body, String summary) throws CRMException {
Article article = getGlobalArticle(slug, locale, false); Article article = getGlobalArticle(slug, locale, false);
if (article == null) { if (article == null) {
article = new Article(); article = new Article();
Article sourceArticle = getGlobalArticle(slug, locale, true);
if (sourceArticle==null) {
throw new CRMException("Article " + slug + " must exist in original language!");
}
article.setTemplate(sourceArticle.isTemplate());
article.setClassPk(sourceArticle.getClassPk());
} }
article.setClassPk(ensureClassPK(Article.class));
article.setLang(locale.getLanguage()); article.setLang(locale.getLanguage());
article.setSlug(slug); updateArticleContent(article, slug, title, body, summary);
article.setTitle(htmlSanitizer.sanitize(title));
article.setSummary(htmlSanitizer.sanitize(summary));
article.setBody(htmlSanitizer.sanitize(body));
article.setPostDate(Calendar.getInstance());
articleRepository.save(article); articleRepository.save(article);
return article; return article;
...@@ -212,42 +210,33 @@ public class ContentServiceImpl implements ContentService { ...@@ -212,42 +210,33 @@ public class ContentServiceImpl implements ContentService {
* @param body * @param body
* @param locale * @param locale
* @return * @return
* @throws CRMException
*/ */
@Override @Override
@Transactional(readOnly = false) @Transactional(readOnly = false)
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER') or hasPermission(#entity, 'ADMINISTRATION')") @PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER') or hasPermission(#entity, 'ADMINISTRATION')")
@CacheEvict(value = "contentcache", allEntries = true) @CacheEvict(value = "contentcache", allEntries = true)
public Article updateArticle(EntityId entity, String slug, String title, String body, String summary, Locale locale, Boolean isTemplate) { public Article updateArticle(EntityId entity, String slug, String title, String body, String summary, Locale locale) throws CRMException {
// return return updateArticle(entity.getClass(), entity.getId(), slug, title, body, summary, locale);
// articleRepository.findByClassPkAndTargetIdAndSlugAndLang(getClassPk(clazz),
// id, slug, locale.getLanguage());
return updateArticle(entity.getClass(), entity.getId(), slug, title, body, summary, locale, isTemplate);
} }
@Override @Override
@Transactional(readOnly = false) @Transactional(readOnly = false)
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')") @PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
@CacheEvict(value = "contentcache", allEntries = true) @CacheEvict(value = "contentcache", allEntries = true)
public Article updateArticle(Class<?> clazz, Long id, String slug, String title, String body, String summary, Locale locale, Boolean isTemplate) { public Article updateArticle(Class<?> clazz, Long id, String slug, String title, String body, String summary, Locale locale) throws CRMException {
Article article = getArticle(clazz, id, slug, locale, false); Article article = getArticle(clazz, id, slug, locale, true);
if (article == null || !article.getLang().equals(locale.getLanguage())) { if (article == null) {
article = new Article(); article = new Article();
article.setClassPk(ensureClassPK(clazz)); article.setClassPk(ensureClassPK(clazz));
article.setTargetId(id); article.setTargetId(id);
article.setLang(locale.getLanguage()); article.setLang(locale.getLanguage());
article.setPostDate(Calendar.getInstance()); article.setTemplate(false);
article.setSlug(slug);
}
if (isTemplate) {
article.setBody(body);
} else {
article.setBody(htmlSanitizer.sanitize(body));
} }
article.setSummary(htmlSanitizer.sanitize(summary));
article.setTitle(htmlSanitizer.sanitize(title));
articleRepository.save(article); updateArticleContent(article, slug, title, body, summary);
return article;
return articleRepository.save(article);
} }
@Override @Override
...@@ -261,7 +250,7 @@ public class ContentServiceImpl implements ContentService { ...@@ -261,7 +250,7 @@ public class ContentServiceImpl implements ContentService {
} }
@Override @Override
@Transactional(readOnly = false) @Transactional
public ClassPK ensureClassPK(Class<?> clazz) { public ClassPK ensureClassPK(Class<?> clazz) {
ClassPK classPk = classPkRepository.getByClassname(clazz.getName()); ClassPK classPk = classPkRepository.getByClassname(clazz.getName());
if (classPk == null) { if (classPk == null) {
...@@ -475,5 +464,42 @@ public class ContentServiceImpl implements ContentService { ...@@ -475,5 +464,42 @@ public class ContentServiceImpl implements ContentService {
return menu; return menu;
} }
@Override
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
@Transactional
public void sanitizeAll() {
LOG.info("Sanitizing articles");
Page<Article> articles;
int page = 0;
do {
articles = listArticles(new PageRequest(page++, 10));