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

Article#template review

parent 8b48d5ea
......@@ -603,7 +603,7 @@
<configuration>
<source>${jdk.source}</source>
<target>${jdk.target}</target>
<optimize>false</optimize>
<optimize>true</optimize>
<showDeprecation>${show.deprecations}</showDeprecation>
<showWarnings>true</showWarnings>
<failOnError>true</failOnError>
......
......@@ -25,6 +25,7 @@ import java.util.Map.Entry;
import org.apache.commons.io.IOUtils;
import org.genesys2.server.listener.RunAsAdminListener;
import org.genesys2.server.model.impl.Article;
import org.genesys2.server.service.CRMException;
import org.genesys2.server.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
......@@ -101,8 +102,12 @@ public class CreateContentListener extends RunAsAdminListener {
// If nothing is found, parse the resource and create content
if (article == null) {
contentService.updateGlobalArticle(slug, locale, entry.getValue().get("title").asText(), entry.getValue().get("body").asText(), null);
_logger.info("Created article for slug: " + slug + " lang=" + locale.getLanguage());
try {
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 {
@Field(index = FieldIndex.not_analyzed, type = FieldType.Date)
private Date lastModifiedDate;
@Field(type = FieldType.Boolean)
private Boolean template;
/** The score here will boost result relevance */
private Float score = 0.5f;
public Boolean getTemplate() {
return template;
}
public void setTemplate(Boolean template) {
this.template = template;
}
public Long getId() {
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");
* you may not use this file except in compliance with the License.
......@@ -12,47 +12,18 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* 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
private ContentService contentService;
package org.genesys2.server.service;
@Autowired
private HtmlSanitizer htmlSanitizer;
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
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()));
}
/**
* Content Service related exceptions
*/
public class CRMException extends Exception {
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 {
Page<ActivityPost> allNews(int page);
ClassPK ensureClassPK(Class<?> clazz);
ClassPK getClassPk(String shortName);
ClassPK getClassPk(Class<?> clazz);
/**
* Load article with {@link ClassPK} of clazz with specified id and the
* "slug" for the locale
* Load article with {@link ClassPK} of clazz with specified id and the "slug"
* for the locale
*
* @param slug
* @param locale
......@@ -54,7 +54,7 @@ public interface ContentService {
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);
......@@ -63,15 +63,14 @@ public interface ContentService {
*
* @param slug
* @param locale
* @param useDefault
* Load article from default language
* @param useDefault Load article from default language
* @return
*/
Article getGlobalArticle(String slug, Locale locale, boolean useDefault);
/**
* Loads a global article in the specified locale, or when not found the
* default locale
* Loads a global article in the specified locale, or when not found the default
* locale
*
* @param string
* @param locale
......@@ -81,9 +80,9 @@ public interface ContentService {
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
......@@ -94,14 +93,16 @@ public interface ContentService {
*/
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 updateActivityPost(long id, String title, String body);
......@@ -124,4 +125,6 @@ public interface ContentService {
Menu updateMenu(String key, List<MenuItem> menuItems);
MenuItem ensureMenuItem(String menuKey, String url, String text);
void sanitizeAll();
}
......@@ -38,7 +38,7 @@ public interface CropService {
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();
......
......@@ -61,7 +61,7 @@ public interface GeoService {
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);
......
......@@ -47,7 +47,7 @@ public interface InstituteService {
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();
......
......@@ -31,7 +31,7 @@ public interface OrganizationService {
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);
......
......@@ -39,5 +39,5 @@ public interface ProjectService {
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> {
@PreAuthorize("hasRole('ADMINISTRATOR')")
User addUser(User user) throws UserException, PasswordPolicyException;
@PreAuthorize("hasRole('ADMINISTRATOR')")
void updateUser(User user) throws UserException;
@PreAuthorize("hasRole('ADMINISTRATOR')")
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");
* you may not use this file except in compliance with the License.
......@@ -12,7 +12,7 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
*/
package org.genesys2.server.service.impl;
......@@ -41,6 +41,7 @@ import org.genesys2.server.persistence.domain.ActivityPostRepository;
import org.genesys2.server.persistence.domain.ArticleRepository;
import org.genesys2.server.persistence.domain.MenuItemRepository;
import org.genesys2.server.persistence.domain.MenuRepository;
import org.genesys2.server.service.CRMException;
import org.genesys2.server.service.ContentService;
import org.genesys2.server.service.HtmlSanitizer;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -111,14 +112,6 @@ public class ContentServiceImpl implements ContentService {
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
@Cacheable(value = "contentcache", key = "'globalarticle-' + #slug + '-' + #locale.language + '-' + #useDefault")
public Article getGlobalArticle(String slug, Locale locale, boolean useDefault) {
......@@ -165,41 +158,46 @@ public class ContentServiceImpl implements ContentService {
@Transactional(readOnly = false)
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
@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);
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.setTitle(htmlSanitizer.sanitize(title));
article.setSummary(htmlSanitizer.sanitize(summary));
article.setTemplate(isTemplate);
if (isTemplate) {
if (article.isTemplate()) {
article.setBody(body);
} else {
article.setBody(htmlSanitizer.sanitize(body));
}
articleRepository.save(article);
return article;
}
@Override
@Transactional(readOnly = false)
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
@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);
if (article == null) {
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.setSlug(slug);
article.setTitle(htmlSanitizer.sanitize(title));
article.setSummary(htmlSanitizer.sanitize(summary));
article.setBody(htmlSanitizer.sanitize(body));
article.setPostDate(Calendar.getInstance());
updateArticleContent(article, slug, title, body, summary);
articleRepository.save(article);
return article;
......@@ -212,42 +210,33 @@ public class ContentServiceImpl implements ContentService {
* @param body
* @param locale
* @return
* @throws CRMException
*/
@Override
@Transactional(readOnly = false)
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER') or hasPermission(#entity, 'ADMINISTRATION')")
@CacheEvict(value = "contentcache", allEntries = true)
public Article updateArticle(EntityId entity, String slug, String title, String body, String summary, Locale locale, Boolean isTemplate) {
// return
// articleRepository.findByClassPkAndTargetIdAndSlugAndLang(getClassPk(clazz),
// id, slug, locale.getLanguage());
return updateArticle(entity.getClass(), entity.getId(), slug, title, body, summary, locale, isTemplate);
public Article updateArticle(EntityId entity, String slug, String title, String body, String summary, Locale locale) throws CRMException {
return updateArticle(entity.getClass(), entity.getId(), slug, title, body, summary, locale);
}
@Override
@Transactional(readOnly = false)
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER')")
@CacheEvict(value = "contentcache", allEntries = true)
public Article updateArticle(Class<?> clazz, Long id, String slug, String title, String body, String summary, Locale locale, Boolean isTemplate) {
Article article = getArticle(clazz, id, slug, locale, false);
if (article == null || !article.getLang().equals(locale.getLanguage())) {
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, true);
if (article == null) {
article = new Article();
article.setClassPk(ensureClassPK(clazz));
article.setTargetId(id);
article.setLang(locale.getLanguage());
article.setPostDate(Calendar.getInstance());
article.setSlug(slug);
}
if (isTemplate) {
article.setBody(body);
} else {
article.setBody(htmlSanitizer.sanitize(body));
article.setTemplate(false);
}
article.setSummary(htmlSanitizer.sanitize(summary));
article.setTitle(htmlSanitizer.sanitize(title));
articleRepository.save(article);
return article;
updateArticleContent(article, slug, title, body, summary);
return articleRepository.save(article);
}
@Override
......@@ -261,7 +250,7 @@ public class ContentServiceImpl implements ContentService {
}
@Override
@Transactional(readOnly = false)
@Transactional
public ClassPK ensureClassPK(Class<?> clazz) {
ClassPK classPk = classPkRepository.getByClassname(clazz.getName());
if (classPk == null) {
......@@ -475,5 +464,42 @@ public class ContentServiceImpl implements ContentService {
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));
for (final Article a : articles) {
a.setBody(htmlSanitizer.sanitize(a.getBody()));
}
articleRepository.save(articles.getContent());
} while (articles.hasNext());
}
@Override
@Transactional
public Article createGlobalArticle(String slug, Locale locale, String title, String body, String summary, boolean isTemplate) throws CRMException {
if (! getDefaultLocale().getLanguage().equals(locale.getLanguage())) {
throw new CRMException("Global articles must first be created in primary language=" + getDefaultLocale().getLanguage());
}
Article article=new Article();
article.setTemplate(isTemplate);
article.setLang(locale.getLanguage());
article.setClassPk(ensureClassPK(Article.class));
article.setTargetId(null);
updateArticleContent(article, slug, title, body, summary);
return articleRepository.save(article);
}
}
......@@ -38,6 +38,7 @@ import org.genesys2.server.persistence.domain.CropRepository;
import org.genesys2.server.persistence.domain.CropRuleRepository;
import org.genesys2.server.persistence.domain.CropTaxonomyRepository;
import org.genesys2.server.persistence.domain.Taxonomy2Repository;
import org.genesys2.server.service.CRMException;
import org.genesys2.server.service.ContentService;
import org.genesys2.server.service.CropService;
import org.genesys2.server.service.HtmlSanitizer;
......@@ -112,9 +113,9 @@ public class CropServiceImpl implements CropService {
@Override
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER') or hasPermission(#crop, 'ADMINISTRATION')")
@Transactional(readOnly = false)
public void updateBlurp(Crop crop, String textBody, String summary, Locale locale) {
public void updateBlurp(Crop crop, String textBody, String summary, Locale locale) throws CRMException {
//isTemplate = false because it's crop
contentService.updateArticle(crop, "blurp", null, textBody, summary, locale, false);
contentService.updateArticle(crop, "blurp", null, textBody, summary, locale);
}
@Override
......
......@@ -24,7 +24,7 @@ import org.genesys2.server.service.EasySMTA;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
//@Profile({ "dev" })
@Profile({ "dev" })
@Component
public class EasySMTAMockConnector implements EasySMTA {
private static final Log LOG = LogFactory.getLog(EasySMTAMockConnector.class);
......
......@@ -302,7 +302,6 @@ public class FullTextSearchServiceImpl implements FullTextSearchService, Initial
// countries
final Article article = (Article) model;
document.setBody(article.getBody());
document.setTemplate(article.isTemplate());
document.setSummary(article.getSummary());
document.setLanguage(article.getLang());
document.setCreatedDate(article.getCreatedDate());
......
......@@ -36,6 +36,7 @@ import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.impl.ITPGRFAStatus;
import org.genesys2.server.persistence.domain.CountryRepository;
import org.genesys2.server.persistence.domain.ITPGRFAStatusRepository;
import org.genesys2.server.service.CRMException;
import org.genesys2.server.service.ContentService;
import org.genesys2.server.service.GenesysFilterService;
import org.genesys2.server.service.GeoService;
......@@ -359,10 +360,9 @@ public class GeoServiceImpl implements GeoService {
@Override
@PreAuthorize("hasRole('ADMINISTRATOR')")
@Transactional(readOnly = false)
public void updateBlurp(Country country, String blurp, Locale locale) {
public void updateBlurp(Country country, String blurp, Locale locale) throws CRMException {
// TODO Should we provide summary?
//isTemplate = false because it's country.
contentService.updateArticle(country, "blurp", null, blurp, null, locale, false);
contentService.updateArticle(country, "blurp", null, blurp, null, locale);
}
@Override
......
......@@ -34,6 +34,7 @@ import org.genesys2.server.model.impl.FaoInstituteSetting;
import org.genesys2.server.persistence.domain.FaoInstituteRepository;
import org.genesys2.server.persistence.domain.FaoInstituteSettingRepository;
import org.genesys2.server.persistence.domain.GenesysLowlevelRepository;
import org.genesys2.server.service.CRMException;
import org.genesys2.server.service.ContentService;
import org.genesys2.server.service.InstituteService;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -138,9 +139,8 @@ public class InstituteServiceImpl implements InstituteService {
@Override
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#faoInstitute, 'ADMINISTRATION')")
@Transactional(readOnly = false)
public void updateAbout(FaoInstitute faoInstitute, String body, String summary, Locale locale) {
//isTemplate = false because it's institute.
contentService.updateArticle(faoInstitute, "blurp", null, body, summary, locale, false);
public void updateAbout(FaoInstitute faoInstitute, String body, String summary, Locale locale) throws CRMException {
contentService.updateArticle(faoInstitute, "blurp", null, body, summary, locale);
}
@Override
......
......@@ -29,6 +29,7 @@ import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.impl.Organization;
import org.genesys2.server.persistence.domain.FaoInstituteRepository;
import org.genesys2.server.persistence.domain.OrganizationRepository;
import org.genesys2.server.service.CRMException;
import org.genesys2.server.service.ContentService;
import org.genesys2.server.service.OrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -85,9 +86,8 @@ public class OrganizationServiceImpl implements OrganizationService {
@Override
@PreAuthorize("hasRole('ADMINISTRATOR')")
@Transactional(readOnly = false)
public Article updateAbout(Organization organization, String body, String summary, Locale locale) {
//isTemplate = false because it's organization.
return contentService.updateArticle(organization, "blurp", null, body, summary, locale,false);
public Article updateAbout(Organization organization, String body, String summary, Locale locale) throws CRMException {
return contentService.updateArticle(organization, "blurp", null, body, summary, locale);
}
@Override
......
......@@ -18,6 +18,7 @@ package org.genesys2.server.service.impl;
import org.genesys2.server.model.impl.Project;
import org.genesys2.server.persistence.domain.ProjectRepository;
import org.genesys2.server.service.CRMException;
import org.genesys2.server.service.ContentService;
import org.genesys2.server.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -84,8 +85,8 @@ public class ProjectServiceImpl implements ProjectService {
@Override
@PreAuthorize("hasRole('ADMINISTRATOR') or hasRole('CONTENTMANAGER') or hasPermission(#project, 'ADMINISTRATION')")
@Transactional
public void updateBlurp(Project project, String textBody, String summary, Locale locale) {
public void updateBlurp(Project project, String textBody, String summary, Locale locale) throws CRMException {
//isTemplate = false because it's project.
contentService.updateArticle(project, "blurp", null, textBody, summary, locale, false);
contentService.updateArticle(project, "blurp", null, textBody, summary, locale);
}
}
......@@ -89,7 +89,7 @@ public class UserServiceImpl extends BasicUserServiceImpl<UserRole, User> implem
user.setFullName(fullName);
user.setAccountType(accountType);
user.getRoles().add(UserRole.USER);
user.setPassword(password);
super.setPassword(user, password);
return addUser(user);
}