Commit efeb54f7 authored by Matija Obreza's avatar Matija Obreza

ClassPK entity and Articles based on classPk, targetId, slug and

language
parent a928c690
......@@ -14,14 +14,15 @@
* limitations under the License.
**/
package org.genesys2.server.model.impl;
import java.util.Calendar;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
......@@ -38,15 +39,28 @@ public class Article extends BusinessModel {
private static final long serialVersionUID = 8690395020204070378L;
@ManyToOne(cascade = {}, optional = false)
@JoinColumn(name = "classPk")
private ClassPK classPk;
@Column
private Long targetId;
/**
* A descriptive unique URL to the article
*/
@Column(nullable = false, length = 150)
private String slug;
@Column(nullable = false, length = 6)
private String lang;
@Lob
@Field(name="title", store=Store.NO)
@Field(name = "title", store = Store.NO)
private String title;
@Lob
@Field(name="body", store=Store.NO)
@Field(name = "body", store = Store.NO)
private String body;
@Temporal(TemporalType.TIMESTAMP)
......@@ -55,11 +69,11 @@ public class Article extends BusinessModel {
public String getSlug() {
return slug;
}
public void setSlug(String slug) {
this.slug = slug;
}
public String getTitle() {
return title;
}
......@@ -84,4 +98,27 @@ public class Article extends BusinessModel {
this.postDate = postDate;
}
public ClassPK getClassPk() {
return classPk;
}
public void setClassPk(ClassPK classPk) {
this.classPk = classPk;
}
public Long getTargetId() {
return targetId;
}
public void setTargetId(Long targetId) {
this.targetId = targetId;
}
public String getLang() {
return lang;
}
public void setLang(String lang) {
this.lang = lang;
}
}
/**
* Copyright 2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* 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.model.impl;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import org.genesys2.server.model.BusinessModel;
/**
* ClassPK serves as a classifier for content elements ({@link Article}).
*
* @author mobreza
*
*/
@Entity
@Table(name = "classname")
public class ClassPK extends BusinessModel {
private static final long serialVersionUID = 838906032026597957L;
@Column(length = 250, unique = true, nullable = false)
private String className;
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
@Override
public String toString() {
return "ClassPK " + this.className + " id=" + id;
}
}
......@@ -20,12 +20,9 @@ import java.text.MessageFormat;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.search.annotations.DocumentId;
import org.genesys2.server.model.BusinessModel;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Store;
......@@ -33,10 +30,9 @@ import org.hibernate.search.annotations.Store;
@Entity
@Table(name = "country")
@Indexed
public class Country implements java.io.Serializable {
public class Country extends BusinessModel {
private static final long serialVersionUID = -1688723909298769804L;
private Long id;
private String code3;
private String code2;
private boolean current;
......@@ -47,17 +43,6 @@ public class Country implements java.io.Serializable {
public Country() {
}
@Id
@DocumentId
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(unique = true, nullable = false)
public Long getId() {
return this.id;
}
public void setId(final Long id) {
this.id = id;
}
@Column(nullable = false, unique = true, length = 3)
@Field(name="title")
......
......@@ -17,10 +17,11 @@
package org.genesys2.server.persistence.domain;
import org.genesys2.server.model.impl.Article;
import org.genesys2.server.model.impl.ClassPK;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ArticleRepository extends JpaRepository<Article, Long> {
Article findBySlug(String slug);
Article findByClassPkAndTargetIdAndSlugAndLang(ClassPK classPk, Long id, String slug, String lang);
}
/**
* Copyright 2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* 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.persistence.domain;
import org.genesys2.server.model.impl.ClassPK;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ClassPKRepository extends JpaRepository<ClassPK, Long> {
ClassPK findByClassName(String className);
}
......@@ -14,18 +14,42 @@
* limitations under the License.
**/
package org.genesys2.server.service;
import java.util.List;
import java.util.Locale;
import org.genesys2.server.model.BusinessModel;
import org.genesys2.server.model.impl.ActivityPost;
import org.genesys2.server.model.impl.Article;
import org.genesys2.server.model.impl.ClassPK;
public interface ContentService {
List<ActivityPost> lastNews();
Article getArticle(String slug);
ClassPK ensureClassPK(Class<?> clazz);
/**
* Load article with {@link ClassPK} of clazz with specified id and the
* "slug" for the locale
*
* @param target
* @param slug
* @param locale
* @return
*/
Article getArticle(Class<?> clazz, Long id, String slug, Locale locale);
Article getArticle(BusinessModel businessEntity, String slug, Locale locale);
/**
* Global articles have ClassPK of Article.class and targetId of null
*
* @param slug
* @param locale
* @return
*/
Article getGlobalArticle(String slug, Locale locale);
}
......@@ -14,15 +14,20 @@
* limitations under the License.
**/
package org.genesys2.server.service.impl;
import java.util.List;
import java.util.Locale;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.model.BusinessModel;
import org.genesys2.server.model.impl.ActivityPost;
import org.genesys2.server.model.impl.Article;
import org.genesys2.server.model.impl.ClassPK;
import org.genesys2.server.persistence.domain.ActivityPostRepository;
import org.genesys2.server.persistence.domain.ArticleRepository;
import org.genesys2.server.persistence.domain.ClassPKRepository;
import org.genesys2.server.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
......@@ -33,13 +38,17 @@ import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional(readOnly = true)
public class ContentServiceImpl implements ContentService {
public static final Log LOG = LogFactory.getLog(ContentServiceImpl.class);
@Autowired
private ActivityPostRepository postRepository;
@Autowired
private ArticleRepository articleRepository;
@Autowired
private ClassPKRepository classPkRepository;
@Override
public List<ActivityPost> lastNews() {
PageRequest page = new PageRequest(0, 10, Direction.DESC, "postDate");
......@@ -47,7 +56,43 @@ public class ContentServiceImpl implements ContentService {
}
@Override
public Article getArticle(String slug) {
return articleRepository.findBySlug(slug);
public Article getGlobalArticle(String slug, Locale locale) {
return getArticle(Article.class, null, slug, locale);
}
@Override
public Article getArticle(BusinessModel businessEntity, String slug, Locale locale) {
return getArticle(businessEntity.getClass(), businessEntity.getId(), slug, locale);
}
@Override
public Article getArticle(Class<?> clazz, Long id, String slug, Locale locale) {
return articleRepository.findByClassPkAndTargetIdAndSlugAndLang(getClassPk(clazz), id, slug, locale.getLanguage());
}
public ClassPK getClassPk(Class<?> clazz) {
return classPkRepository.findByClassName(clazz.getName());
}
@Override
@Transactional(readOnly = false)
public ClassPK ensureClassPK(Class<?> clazz) {
ClassPK classPk = classPkRepository.findByClassName(clazz.getName());
if (classPk == null) {
classPk = ensureClassPKInternal(clazz);
}
return classPk;
}
private synchronized ClassPK ensureClassPKInternal(Class<?> clazz) {
String className = clazz.getName();
ClassPK classPk = classPkRepository.findByClassName(className);
if (classPk == null) {
LOG.info("Registering new ClassPK for " + className);
classPk = new ClassPK();
classPk.setClassName(className);
classPkRepository.save(classPk);
}
return classPk;
}
}
......@@ -36,7 +36,8 @@ public class ArticleController extends BaseController {
@RequestMapping("{url}")
public String view(ModelMap model, @PathVariable(value = "url") String slug) {
_logger.debug("Viewing article " + slug);
Article article = contentService.getArticle(slug);
Article article = contentService.getGlobalArticle(slug, getLocale());
if (article == null) {
throw new ResourceNotFoundException();
}
......
......@@ -14,12 +14,12 @@
* limitations under the License.
**/
package org.genesys2.server.servlet.controller;
import java.util.HashMap;
import org.genesys2.server.model.impl.Country;
import org.genesys2.server.service.ContentService;
import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.GeoService;
import org.genesys2.server.service.InstituteService;
......@@ -41,32 +41,36 @@ public class CountryController extends BaseController {
@Autowired
private SelectionBean selectionBean;
@Autowired
private GeoService geoService;
@Autowired
private InstituteService instituteService;
@Autowired
private GenesysService genesysService;
@Autowired
private ContentService contentService;
@RequestMapping
public String view(ModelMap model) {
model.addAttribute("countries", geoService.listAll());
return "/country/index";
}
@RequestMapping("/{country}")
public String view(ModelMap model, @PathVariable(value = "country") String countryStr) {
_logger.debug("Viewing country " + countryStr);
Country country = geoService.getCountry(countryStr);
if (country==null) {
if (country == null) {
throw new ResourceNotFoundException();
}
model.addAttribute("country", country);
model.addAttribute("blurp", contentService.getArticle(country, "blurp", getLocale()));
// All institutes
model.addAttribute("faoInstitutes", instituteService.listByCountry(country));
// Active ones
......@@ -74,22 +78,22 @@ public class CountryController extends BaseController {
model.addAttribute("countByOrigin", genesysService.countByOrigin(country.getCode3()));
model.addAttribute("countByLocation", genesysService.countByLocation(country.getCode3()));
return "/country/details";
}
@RequestMapping("/{country}/data")
public String viewData(ModelMap model, @PathVariable(value = "country") String countryStr, @RequestParam(value = "page", required = false, defaultValue = "1") int page) {
public String viewData(ModelMap model, @PathVariable(value = "country") String countryStr,
@RequestParam(value = "page", required = false, defaultValue = "1") int page) {
_logger.debug("Viewing country " + countryStr);
Country country = geoService.getCountry(countryStr);
if (country==null) {
if (country == null) {
throw new ResourceNotFoundException();
}
model.addAttribute("pagedData", genesysService.listAccessionsByOrigin(country, new PageRequest(page - 1, 50, new Sort("accessionName"))));
model.addAttribute("selection", selectionBean);
HashMap<Object, Object> filters = new HashMap<Object, Object>();
filters.put("filter.countryOfOrigin", country);
......
......@@ -7,41 +7,48 @@
<title><spring:message code="country.page.profile.title" arguments="${country.name}" argumentSeparator="|" /></title>
</head>
<body>
<c:if test="${country eq null}">
<div class="alert alert-error"><spring:message code="data.error.404"/></div>
</c:if>
<h1>
<c:out value="${country.name}" />
<img class="country-flag bigger" src="http://genesys-pgr.org/images/flags/${country.code3.toUpperCase()}.png" />
<img class="country-flag bigger" src="http://genesys-pgr.org/images/flags/${country.code3.toUpperCase()}.png" />
</h1>
<c:if test="${not country.current}">
<div class="alert"><spring:message code="country.page.not-current"/></div>
<div class="alert">
<spring:message code="country.page.not-current" />
</div>
</c:if>
<c:if test="${blurp ne null}">
<div class="free-text">
<c:out value="${blurp.body}" escapeXml="false" />
</div>
</c:if>
<div>
<c:out value="${country.code3}" />
</div>
<div>
<c:out value="${country.code2}" />
ISO-3166 3-alpha: <c:out value="${country.code3}" /><br />
ISO-3166 2-alpha: <c:out value="${country.code2}" />
</div>
<h3><spring:message code="country.statistics" /></h3>
<h3>
<spring:message code="country.statistics" />
</h3>
<div>
<spring:message code="country.stat.countByOrigin" arguments="${countByOrigin}" />
<a href="<c:url value="/geo/${country.code3.toLowerCase()}/data" />"><spring:message code="view.accessions" /></a>
</div>
<h3><spring:message code="country.stat.countByLocation" arguments="${countByLocation}" /></h3>
<h3>
<spring:message code="country.stat.countByLocation" arguments="${countByLocation}" />
</h3>
<ul class="funny-list">
<c:forEach items="${genesysInstitutes}" var="faoInstitute" varStatus="status">
<li class="clearfix ${status.count % 2 == 0 ? 'even' : 'odd'}">
<a class="show pull-left" href="<c:url value="/wiews/${faoInstitute.code.toLowerCase()}" />"><b><c:out value="${faoInstitute.code}" /></b> <c:out value="${faoInstitute.fullName}" /></a>
<div class="pull-right"><spring:message code="faoInstitute.accessionCount" arguments="${faoInstitute.accessionCount}" /></div></li>
<li class="clearfix ${status.count % 2 == 0 ? 'even' : 'odd'}"><a class="show pull-left" href="<c:url value="/wiews/${faoInstitute.code.toLowerCase()}" />"><b><c:out value="${faoInstitute.code}" /></b> <c:out value="${faoInstitute.fullName}" /></a>
<div class="pull-right">
<spring:message code="faoInstitute.accessionCount" arguments="${faoInstitute.accessionCount}" />
</div></li>
</c:forEach>
</ul>
<h4>
<spring:message code="country.page.faoInstitutes" arguments="${faoInstitutes.size()}" />
</h4>
......@@ -51,6 +58,6 @@
<li class="${status.count % 2 == 0 ? 'even' : 'odd'}"><a class="show" href="<c:url value="/wiews/${faoInstitute.code.toLowerCase()}" />"><b><c:out value="${faoInstitute.code}" /></b> <c:out value="${faoInstitute.fullName}" /></a></li>
</c:forEach>
</ul>
</body>
</html>
\ No newline at end of file
......@@ -420,4 +420,9 @@ tr.acn .sel.picked {
select, textarea, input[type="text"], input[type="password"], input[type="datetime"], input[type="datetime-local"], input[type="date"], input[type="month"], input[type="time"], input[type="week"], input[type="number"], input[type="email"], input[type="url"], input[type="search"], input[type="tel"], input[type="color"], .uneditable-input {
height: auto;
}
.free-text {
font-size: 14px;
line-height: 20px;
}
\ No newline at end of file
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