# HG changeset patch # User ymh # Date 1363186028 -3600 # Node ID 523f0647513e5aaa2f3b3f3d1eee79e07c4b54a8 # Parent 7adef9ce92aab3199cc748522ba72253eb5ba4f0 add the count of project by spaces, add pagination, update libraries and add some more unit tests. diff -r 7adef9ce92aa -r 523f0647513e .classpath --- a/.classpath Wed Mar 13 15:37:31 2013 +0100 +++ b/.classpath Wed Mar 13 15:47:08 2013 +0100 @@ -20,8 +20,8 @@ - - + + diff -r 7adef9ce92aa -r 523f0647513e server/pom.xml --- a/server/pom.xml Wed Mar 13 15:37:31 2013 +0100 +++ b/server/pom.xml Wed Mar 13 15:47:08 2013 +0100 @@ -7,7 +7,8 @@ 0.1 war - + + 2.14 1.0 1.0 1.17 @@ -16,7 +17,7 @@ 8.1.9.v20130131 4.10 - 2.0.15 + 2.0.16 2.5.1 2.1.4 2.1 @@ -100,6 +101,11 @@ + + org.apache.maven.plugins + maven-surefire-plugin + ${surefire-version} + @@ -257,18 +263,18 @@ org.springframework.data - spring-data-commons-core - 1.4.0.RELEASE + spring-data-commons + 1.5.0.RELEASE org.springframework.data spring-data-jpa - 1.2.0.RELEASE + 1.3.0.RELEASE org.springframework.data spring-data-mongodb - 1.1.1.RELEASE + 1.2.0.RELEASE javax.persistence diff -r 7adef9ce92aa -r 523f0647513e server/src/main/java/org/iri_research/renkan/Constants.java --- a/server/src/main/java/org/iri_research/renkan/Constants.java Wed Mar 13 15:37:31 2013 +0100 +++ b/server/src/main/java/org/iri_research/renkan/Constants.java Wed Mar 13 15:47:08 2013 +0100 @@ -38,5 +38,6 @@ public final static String KEYHEX = "f2338d2299ac28ef64f82956fde37337b87a2b9e8fc03e28fa0768cac37d838113c7d0fc78c60fce1e23b1b3e03ac7db4676b3189c267f26baaab10f72544441"; public final static int SALT_LENGTH = 24; + public final static int PAGINATION_SIZE = 10; } diff -r 7adef9ce92aa -r 523f0647513e server/src/main/java/org/iri_research/renkan/controller/RenkanRootController.java --- a/server/src/main/java/org/iri_research/renkan/controller/RenkanRootController.java Wed Mar 13 15:37:31 2013 +0100 +++ b/server/src/main/java/org/iri_research/renkan/controller/RenkanRootController.java Wed Mar 13 15:47:08 2013 +0100 @@ -1,9 +1,12 @@ package org.iri_research.renkan.controller; +import java.util.Enumeration; import java.util.HashMap; -import java.util.List; import java.util.Map; +import javax.servlet.http.HttpServletRequest; + +import org.iri_research.renkan.Constants; import org.iri_research.renkan.models.Project; import org.iri_research.renkan.models.Space; import org.iri_research.renkan.repositories.ProjectsRepository; @@ -11,8 +14,13 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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.Direction; +import org.springframework.data.web.PageableDefaults; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @@ -31,21 +39,48 @@ @Autowired private SpacesRepository spacesRepository; + + private String buildBaseUrl(HttpServletRequest request) { + StringBuffer baseUrl = request.getRequestURL(); + boolean firstParam = true; + Enumeration namesEnum = request.getParameterNames(); + while(namesEnum.hasMoreElements()) { + String paramName = + (String)namesEnum.nextElement(); + + if("p.page".equals(paramName)) { + continue; + } + for(String val:request.getParameterValues(paramName)) { + if(firstParam) { + baseUrl.append('?'); + firstParam = false; + } + else { + baseUrl.append('&'); + } + baseUrl.append(paramName).append('=').append(val); + } + } + return baseUrl.toString(); + } @RequestMapping(value="", method = RequestMethod.GET, produces={"text/html;charset=UTF-8"}) - public ModelAndView renkanIndex() { - - Map model = new HashMap(); + public String renkanIndex(Model model, @PageableDefaults(sort={"created"}, sortDir=Direction.DESC, value=Constants.PAGINATION_SIZE) Pageable p, HttpServletRequest request) { + + Page page = this.spacesRepository.findAll(p); + //Iterable spaces = this.spacesRepository.findAll(); - Iterable spaces = this.spacesRepository.findAll(); + + model.addAttribute("page", page); + model.addAttribute("baseUrl", this.buildBaseUrl(request)); + model.addAttribute("projectsCount", this.projectsRepository.getCountBySpace()); - model.put("spaces", spaces); - - return new ModelAndView("renkanIndex", model); + return "renkanIndex"; } @RequestMapping(value="/s/{space_id}", method = RequestMethod.GET, produces={"text/html;charset=UTF-8"}) - public ModelAndView spaceIndex(@PathVariable("space_id") String spaceId) { + public ModelAndView spaceIndex(@PathVariable("space_id") String spaceId, @PageableDefaults(sort={"created"}, sortDir=Direction.DESC, value=Constants.PAGINATION_SIZE) Pageable p, HttpServletRequest request) { logger.debug("SpaceId : " + (spaceId== null ? "null" : spaceId)); @@ -63,9 +98,10 @@ model.put("space", space); - List projects = this.projectsRepository.findBySpaceId(spaceId); + Page page = this.projectsRepository.findBySpaceId(spaceId, p); - model.put("projects", projects); + model.put("page", page); + model.put("baseUrl", this.buildBaseUrl(request)); return new ModelAndView("projectIndex", model); } diff -r 7adef9ce92aa -r 523f0647513e server/src/main/java/org/iri_research/renkan/repositories/IRenkanRepository.java --- a/server/src/main/java/org/iri_research/renkan/repositories/IRenkanRepository.java Wed Mar 13 15:37:31 2013 +0100 +++ b/server/src/main/java/org/iri_research/renkan/repositories/IRenkanRepository.java Wed Mar 13 15:47:08 2013 +0100 @@ -2,12 +2,12 @@ import java.io.Serializable; -import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.PagingAndSortingRepository; import com.mongodb.DBCollection; public interface IRenkanRepository extends - CrudRepository { + PagingAndSortingRepository { public DBCollection getCollection(); } diff -r 7adef9ce92aa -r 523f0647513e server/src/main/java/org/iri_research/renkan/repositories/ProjectsRepository.java --- a/server/src/main/java/org/iri_research/renkan/repositories/ProjectsRepository.java Wed Mar 13 15:37:31 2013 +0100 +++ b/server/src/main/java/org/iri_research/renkan/repositories/ProjectsRepository.java Wed Mar 13 15:47:08 2013 +0100 @@ -3,9 +3,11 @@ import java.util.List; import org.iri_research.renkan.models.Project; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; public interface ProjectsRepository extends IRenkanRepository, ProjectsRepositoryCustom { List findBySpaceId(String spaceId); - + Page findBySpaceId(String spaceId, Pageable p); } diff -r 7adef9ce92aa -r 523f0647513e server/src/main/java/org/iri_research/renkan/repositories/ProjectsRepositoryCustom.java --- a/server/src/main/java/org/iri_research/renkan/repositories/ProjectsRepositoryCustom.java Wed Mar 13 15:37:31 2013 +0100 +++ b/server/src/main/java/org/iri_research/renkan/repositories/ProjectsRepositoryCustom.java Wed Mar 13 15:47:08 2013 +0100 @@ -1,5 +1,11 @@ package org.iri_research.renkan.repositories; +import java.util.Map; + public interface ProjectsRepositoryCustom { + public int getRevCounter(String projectId); + + public Map getCountBySpace(); + } diff -r 7adef9ce92aa -r 523f0647513e server/src/main/java/org/iri_research/renkan/repositories/ProjectsRepositoryImpl.java --- a/server/src/main/java/org/iri_research/renkan/repositories/ProjectsRepositoryImpl.java Wed Mar 13 15:37:31 2013 +0100 +++ b/server/src/main/java/org/iri_research/renkan/repositories/ProjectsRepositoryImpl.java Wed Mar 13 15:47:08 2013 +0100 @@ -1,15 +1,26 @@ package org.iri_research.renkan.repositories; +import java.util.HashMap; +import java.util.Map; + import org.iri_research.renkan.models.Project; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.stereotype.Component; import static org.springframework.data.mongodb.core.query.Query.query; import static org.springframework.data.mongodb.core.query.Criteria.where; + +import org.springframework.data.mongodb.core.mapreduce.GroupBy; +import org.springframework.data.mongodb.core.mapreduce.GroupByResults; import org.springframework.data.mongodb.core.query.Update; @Component -public class ProjectsRepositoryImpl implements ProjectsRepositoryCustom { +public class ProjectsRepositoryImpl implements ProjectsRepositoryCustom { + + private class GroupResult { + public String space_id; + public int count; + } @Autowired private MongoTemplate mongoTemplate; @@ -24,4 +35,21 @@ return p.getRevCounter(); } + @Override + public Map getCountBySpace() { + + GroupByResults groupResult = this.mongoTemplate.group( + this.mongoTemplate.getCollectionName(Project.class), + GroupBy.key("space_id").initialDocument("{ count: 0 }").reduceFunction("function(doc, prev) { prev.count += 1; }"), + GroupResult.class); + + HashMap res = new HashMap<>(); + for (GroupResult gr : groupResult) { + res.put(gr.space_id, new Integer(gr.count)); + } + + return res; + + } + } diff -r 7adef9ce92aa -r 523f0647513e server/src/main/resources/log4j.xml --- a/server/src/main/resources/log4j.xml Wed Mar 13 15:37:31 2013 +0100 +++ b/server/src/main/resources/log4j.xml Wed Mar 13 15:47:08 2013 +0100 @@ -1,5 +1,5 @@ - - + + diff -r 7adef9ce92aa -r 523f0647513e server/src/main/webapp/WEB-INF/i18n/messages_en.properties --- a/server/src/main/webapp/WEB-INF/i18n/messages_en.properties Wed Mar 13 15:37:31 2013 +0100 +++ b/server/src/main/webapp/WEB-INF/i18n/messages_en.properties Wed Mar 13 15:47:08 2013 +0100 @@ -3,19 +3,21 @@ renkanIndex.new_renkan = New Renkan renkanIndex.new_space = New Space -renkanIndex.renkan_title = or create/open a Renkan with the title +renkanIndex.renkan_exp = or create/open a Renkan with the title renkanIndex.project_list = Project list renkanIndex.project_name = Name renkanIndex.project_creation = Creation renkanIndex.project_edit = Edit renkanIndex.project_edit_link = Edit project +renkanIndex.space_exp = or create/open a space with the title renkanIndex.space_list = Spaces list renkanIndex.space_name = Name renkanIndex.space_title = Title renkanIndex.space_creation = Creation renkanIndex.space_open = Open renkanIndex.space_open_link = Open space +renkanIndex.space_proj_count = Proj. count renkanIndex.js.empty_name_error = Please enter a title \ No newline at end of file diff -r 7adef9ce92aa -r 523f0647513e server/src/main/webapp/WEB-INF/i18n/messages_fr.properties --- a/server/src/main/webapp/WEB-INF/i18n/messages_fr.properties Wed Mar 13 15:37:31 2013 +0100 +++ b/server/src/main/webapp/WEB-INF/i18n/messages_fr.properties Wed Mar 13 15:47:08 2013 +0100 @@ -3,7 +3,7 @@ renkanIndex.new_renkan = Nouveau Renkan renkanIndex.new_space = Nouvel Espace -renkanIndex.renkan_titre = ou bien créer un Renkan avec le titre +renkanIndex.renkan_exp = ou bien créer un Renkan avec le titre renkanIndex.project_list = Liste des projets renkanIndex.project_title = Titre @@ -11,13 +11,14 @@ renkanIndex.project_edit = Edition renkanIndex.project_edit_link = Editer proj. +renkanIndex.space_exp = ou bien créer un espace avec le titre renkanIndex.space_list = Liste des espace renkanIndex.space_name = Nom renkanIndex.space_title = Titre renkanIndex.space_creation = Creation renkanIndex.space_open = Ouvrir renkanIndex.space_open_link = Ouvrir esp. - +renkanIndex.space_proj_count = Nb. proj. renkanIndex.js.empty_name_error = Veuillez entrer un titre diff -r 7adef9ce92aa -r 523f0647513e server/src/main/webapp/WEB-INF/mongo-config.xml --- a/server/src/main/webapp/WEB-INF/mongo-config.xml Wed Mar 13 15:37:31 2013 +0100 +++ b/server/src/main/webapp/WEB-INF/mongo-config.xml Wed Mar 13 15:47:08 2013 +0100 @@ -24,5 +24,5 @@ - + diff -r 7adef9ce92aa -r 523f0647513e server/src/main/webapp/WEB-INF/spring-servlet.xml --- a/server/src/main/webapp/WEB-INF/spring-servlet.xml Wed Mar 13 15:37:31 2013 +0100 +++ b/server/src/main/webapp/WEB-INF/spring-servlet.xml Wed Mar 13 15:47:08 2013 +0100 @@ -5,11 +5,11 @@ xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans - http://www.springframework.org/schema/beans/spring-beans-3.0.xsd + http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context - http://www.springframework.org/schema/context/spring-context-3.0.xsd + http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc - http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> + http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> @@ -23,7 +23,13 @@ - + + + + + + + diff -r 7adef9ce92aa -r 523f0647513e server/src/main/webapp/WEB-INF/templates/fragment/paginationFragment.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/server/src/main/webapp/WEB-INF/templates/fragment/paginationFragment.html Wed Mar 13 15:47:08 2013 +0100 @@ -0,0 +1,23 @@ + + + + +pagination fragment + + +
+
+ << + < + ... + + + + ... + > + >> +
+
+ + \ No newline at end of file diff -r 7adef9ce92aa -r 523f0647513e server/src/main/webapp/WEB-INF/templates/projectIndex.html --- a/server/src/main/webapp/WEB-INF/templates/projectIndex.html Wed Mar 13 15:37:31 2013 +0100 +++ b/server/src/main/webapp/WEB-INF/templates/projectIndex.html Wed Mar 13 15:47:08 2013 +0100 @@ -1,5 +1,6 @@ + Renkan @@ -123,17 +124,19 @@ /*]]>*/ - + +
New Renkan
-
Create a Renkan with the title
+
Create a Renkan with the title

Project list

+
@@ -141,7 +144,7 @@ - + @@ -184,5 +187,5 @@ /*]]>*/ - + diff -r 7adef9ce92aa -r 523f0647513e server/src/main/webapp/WEB-INF/templates/renkanIndex.html --- a/server/src/main/webapp/WEB-INF/templates/renkanIndex.html Wed Mar 13 15:37:31 2013 +0100 +++ b/server/src/main/webapp/WEB-INF/templates/renkanIndex.html Wed Mar 13 15:47:08 2013 +0100 @@ -1,5 +1,6 @@ + Renkan @@ -123,27 +124,33 @@ /*]]>*/ - + +
New Space
-
Create a Renkan with the title
+
Create a Space with the title
- +

Space list

+
title date Edit projet
- + + + + - + + @@ -161,7 +168,7 @@ return false; } - new_renkan = { + new_space = { title: renkantitle, description: "(empty description)", uri: null @@ -169,7 +176,7 @@ var post_url = /*[[@{/rest/spaces/}]]*/"/rest/spaces/"; $.ajax(post_url, { - data:JSON.stringify(new_renkan), + data:JSON.stringify(new_space), type: "POST", dataType: "json", contentType: "application/json; charset=UTF-8" @@ -183,5 +190,5 @@ /*]]>*/ - + diff -r 7adef9ce92aa -r 523f0647513e server/src/test/java/org/iri_research/renkan/test/repositories/ProjectsRepositoryTest.java --- a/server/src/test/java/org/iri_research/renkan/test/repositories/ProjectsRepositoryTest.java Wed Mar 13 15:37:31 2013 +0100 +++ b/server/src/test/java/org/iri_research/renkan/test/repositories/ProjectsRepositoryTest.java Wed Mar 13 15:47:08 2013 +0100 @@ -3,6 +3,8 @@ import java.util.ArrayList; import java.util.Date; +import java.util.List; +import java.util.Map; import java.util.UUID; import org.iri_research.renkan.models.Project; @@ -25,6 +27,8 @@ @ContextConfiguration("repositories-context.xml") public class ProjectsRepositoryTest { + private final static int SPACE_NB = 3; + private Logger logger = LoggerFactory.getLogger(ProjectsRepositoryTest.class); @Autowired @@ -37,28 +41,36 @@ private Date creationDate = new Date(); + private List spaceIds = new ArrayList<>(); + public ProjectsRepositoryTest() { } @Before public void setup() { - + logger.debug("Setup"); - Space testSpace = new Space(UUID.randomUUID().toString(), "test", "Test space", null, null, null, "test_user", null, this.creationDate); - testSpace = spacesRepository.save(testSpace); ArrayList pl = new ArrayList(); - pl.add(new Project(testSpace.getId(), UUID.randomUUID().toString(), "test1", "desc1", "http://localhost:8080/rest/projects/id1", this.creationDate)); - pl.add(new Project(testSpace.getId(), UUID.randomUUID().toString(), "test2", "desc2", "http://localhost:8080/rest/projects/id2", this.creationDate)); - logger.debug("Setup : new Project "); + Space testSpace = null; + for(int i=0; i groupRes = projectsRepository.getCountBySpace(); + + Assert.assertNotNull("GroupRes not null", groupRes); + Assert.assertEquals("Group res size", SPACE_NB-1, groupRes.size()); + + for(int i=0;i<(SPACE_NB-1);i++) { + Integer count = groupRes.get(this.spaceIds.get(i)); + Assert.assertNotNull("count not null", count); + Assert.assertEquals("Nb of project/space", 2-i, count.intValue()); + } + + Assert.assertNull("Last space id has no project i.e count is null", groupRes.get(this.spaceIds.get(SPACE_NB-1))); + } + } diff -r 7adef9ce92aa -r 523f0647513e server/src/test/java/org/iri_research/renkan/test/repositories/SpacesRepositoryTest.java --- a/server/src/test/java/org/iri_research/renkan/test/repositories/SpacesRepositoryTest.java Wed Mar 13 15:37:31 2013 +0100 +++ b/server/src/test/java/org/iri_research/renkan/test/repositories/SpacesRepositoryTest.java Wed Mar 13 15:47:08 2013 +0100 @@ -1,7 +1,9 @@ package org.iri_research.renkan.test.repositories; +import java.util.ArrayList; import java.util.Date; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.UUID; @@ -15,6 +17,11 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.data.domain.Sort.Direction; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -26,6 +33,8 @@ @ContextConfiguration("repositories-context.xml") public class SpacesRepositoryTest { + private final static int SPACE_NB = 3; + private Logger logger = LoggerFactory.getLogger(SpacesRepositoryTest.class); @Autowired @@ -34,19 +43,27 @@ @Autowired private MongoTemplate mongoTemplate; - private Map spacesList = new HashMap(); - + private Map spacesList = new HashMap(SPACE_NB); + private List spacesUuids = new ArrayList<>(SPACE_NB); @Before public void setup() { logger.debug("Setup"); spacesRepository.deleteAll(); - Date creationDate = new Date(); - String uuid = UUID.randomUUID().toString(); - Space testSpace = new Space(uuid, "test", "Test space", "{}", "http://ldt.iri.centrepompidou.fr", "#ababab", "test_user", "http://ldt.iri.centrepompidou.fr", creationDate); - testSpace = spacesRepository.save(testSpace); - this.spacesList.put(uuid, testSpace); + for(int i=0;i page = this.spacesRepository.findAll(p); + + Assert.assertEquals("page content length must be two", 2, page.getNumberOfElements()); + Assert.assertEquals("id must be id of last created space", this.spacesUuids.get(SPACE_NB-1), page.getContent().get(0).getId()); + Assert.assertEquals("id must be id of last created space", this.spacesUuids.get(SPACE_NB-2), page.getContent().get(1).getId()); + + p = new PageRequest(1, 2, s); + page = this.spacesRepository.findAll(p); + Assert.assertEquals("page content length must be one", 1, page.getNumberOfElements()); + Assert.assertEquals("id must be id of first created space", this.spacesUuids.get(0), page.getContent().get(0).getId()); } diff -r 7adef9ce92aa -r 523f0647513e server/src/test/resources/log4j.xml --- a/server/src/test/resources/log4j.xml Wed Mar 13 15:37:31 2013 +0100 +++ b/server/src/test/resources/log4j.xml Wed Mar 13 15:47:08 2013 +0100 @@ -1,4 +1,4 @@ - +
NameCreationEditNameCreationProj. countEdit
title datenb. proj Open space