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

Crops have long lists of taxonomies, load on request

parent 6f01fcee
......@@ -21,14 +21,24 @@ import java.util.List;
import org.genesys2.server.model.genesys.Taxonomy;
import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.model.impl.CropTaxonomy;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
public interface CropTaxonomyRepository extends JpaRepository<CropTaxonomy, Long> {
/**
* Returns all
*
* @param crop
* @return
*/
List<CropTaxonomy> findByCrop(Crop crop);
Page<CropTaxonomy> findByCrop(Crop crop, Pageable pageable);
@Query("select distinct t from CropTaxonomy ct inner join ct.taxonomy t where ct.crop = ?1")
List<Taxonomy> findTaxonomiesByCrop(Crop crop);
......
......@@ -23,6 +23,8 @@ import java.util.Locale;
import org.genesys2.server.model.genesys.Taxonomy;
import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.model.impl.CropTaxonomy;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
public interface CropService {
......@@ -34,5 +36,5 @@ public interface CropService {
void rebuildTaxonomies();
List<CropTaxonomy> getCropTaxonomies(Crop crop);
Page<CropTaxonomy> getCropTaxonomies(Crop crop, Pageable pageable);
}
......@@ -33,6 +33,8 @@ import org.genesys2.server.persistence.domain.CropTaxonomyRepository;
import org.genesys2.server.persistence.domain.TaxonomyRepository;
import org.genesys2.server.service.CropService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
......@@ -186,7 +188,7 @@ public class CropServiceImpl implements CropService {
}
@Override
public List<CropTaxonomy> getCropTaxonomies(Crop crop) {
return cropTaxonomyRepository.findByCrop(crop);
public Page<CropTaxonomy> getCropTaxonomies(Crop crop, Pageable pageable) {
return cropTaxonomyRepository.findByCrop(crop, pageable);
}
}
......@@ -20,6 +20,7 @@ import java.util.HashMap;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.model.impl.CropTaxonomy;
import org.genesys2.server.service.CropService;
import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.TraitService;
......@@ -61,11 +62,27 @@ public class CropController extends BaseController {
throw new ResourceNotFoundException();
}
model.addAttribute("crop", crop);
model.addAttribute("cropTaxonomies", cropService.getCropTaxonomies(crop));
model.addAttribute("cropTaxonomies", cropService.getCropTaxonomies(crop, new PageRequest(0, 20, new Sort("taxonomy.genus", "taxonomy.species"))));
return "/crop/index";
}
@RequestMapping("/{shortName}/ajax/taxonomies")
public String ajaxTaxonomies(ModelMap model, @PathVariable(value = "shortName") String shortName,
@RequestParam(value = "page", required = false, defaultValue = "1") int page) {
Crop crop = cropService.getCrop(shortName);
if (crop == null || page < 1) {
throw new ResourceNotFoundException();
}
Page<CropTaxonomy> res = cropService.getCropTaxonomies(crop, new PageRequest(page - 1, 20, new Sort("taxonomy.genus", "taxonomy.species")));
model.addAttribute("cropTaxonomies", res);
if (res.getNumberOfElements()==0) {
throw new ResourceNotFoundException();
}
return "/crop/ajax.taxonomies";
}
@RequestMapping(value = "/rebuild", method = RequestMethod.POST)
public String rebuild() {
_logger.info("Rebuilding taxonomies");
......
<%@include file="/WEB-INF/jsp/init.jsp"%>
<c:forEach items="${cropTaxonomies.content}" var="cropTaxonomy" varStatus="status">
<li class="${((cropTaxonomies.size * cropTaxonomies.number) + status.count) % 2 == 0 ? 'even' : 'odd'}"><a href="<c:url value="/acn/t/${cropTaxonomy.taxonomy.genus}/${cropTaxonomy.taxonomy.species}" />"><c:out value="${cropTaxonomy.taxonomy.taxonName}" /></a></li>
</c:forEach>
......@@ -43,12 +43,38 @@
<h3><spring:message code="taxonomy-list" /></h3>
<ul class="funny-list">
<c:forEach items="${cropTaxonomies}" var="cropTaxonomy">
<li><a href="<c:url value="/acn/t/${cropTaxonomy.taxonomy.genus}/${cropTaxonomy.taxonomy.species}" />"><c:out value="${cropTaxonomy.taxonomy.taxonName}" /></a></li>
<c:forEach items="${cropTaxonomies.content}" var="cropTaxonomy" varStatus="status">
<li class="${status.count % 2 == 0 ? 'even' : 'odd'}"><a href="<c:url value="/acn/t/${cropTaxonomy.taxonomy.genus}/${cropTaxonomy.taxonomy.species}" />"><c:out value="${cropTaxonomy.taxonomy.taxonName}" /></a></li>
</c:forEach>
<c:if test="${cropTaxonomies.hasNextPage()}">
<li id="loadMoreTaxonomies"><button class="btn">Load more...</button></li>
</c:if>
</ul>
<script type="text/javascript">
jQuery(document).ready(function() {
$("body").on("click", "#loadMoreTaxonomies", function(event) {
event.preventDefault();
var loader=$(this);
var page=loader.attr("page") || 2;
$.ajax({
url : "<c:url value="/c/${crop.shortName}/ajax/taxonomies" />",
type : "GET",
data : {
"page" : page
},
success : function(data) {
loader.before(data);
loader.attr("page", parseInt(page)+1);
},
error: function(err) {
loader.remove();
}
});
});
});
</script>
</body>
</html>
\ No newline at end of file
......@@ -3,4 +3,5 @@
<mapping path="/welcome" decorator="/WEB-INF/decorator/entry.jsp" />
<mapping path="/*" decorator="/WEB-INF/decorator/main.jsp" />
<mapping path="/html/*" exclude="true" />
<mapping path="*/ajax/*" exclude="true" />
</sitemesh>
\ 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