Correct add space and improve space form validation message
authorymh <ymh.work@gmail.com>
Tue, 02 Apr 2013 14:05:56 +0200
changeset 128 93a1fbe6a848
parent 127 906fed13c1e1
child 129 01c862ada33c
Correct add space and improve space form validation message
server/src/main/java/org/iri_research/renkan/controller/AdminController.java
server/src/main/java/org/iri_research/renkan/forms/SpaceForm.java
server/src/main/webapp/WEB-INF/templates/admin/spaceNew.html
server/src/main/webapp/WEB-INF/templates/admin/spacesList.html
server/src/main/webapp/WEB-INF/templates/fragment/spaceForm.html
server/src/test/java/org/iri_research/renkan/test/controller/AdminControllerTest.java
--- a/server/src/main/java/org/iri_research/renkan/controller/AdminController.java	Tue Apr 02 01:56:11 2013 +0200
+++ b/server/src/main/java/org/iri_research/renkan/controller/AdminController.java	Tue Apr 02 14:05:56 2013 +0200
@@ -63,19 +63,28 @@
 		return "admin/spacesList";
 	}
 	
-	@RequestMapping(value="/spaces/{space_id}", method = RequestMethod.GET, produces={"text/html;charset=UTF-8"})
-	public String editSpace(Model model, @PathVariable(value="space_id") String spaceId) {
+	@RequestMapping(value="/spaces/edit/", method = RequestMethod.GET, produces={"text/html;charset=UTF-8"})
+	public String editSpace(Model model) {
+		return editSpace(model, null);
+	}
+	
+	@RequestMapping(value="/spaces/edit/{spaceId}", method = RequestMethod.GET, produces={"text/html;charset=UTF-8"})
+	public String editSpace(Model model, @PathVariable(value="spaceId") String spaceId) {
+		
+		SpaceForm spaceForm = null;
 		
-		if(spaceId == null || spaceId.length() == 0) {
-			throw new IllegalArgumentException("AdminContoller.editSpace: space id is null or empty.");
+		if(spaceId == null || spaceId.length() == 0 || "_".equals(spaceId)) {
+			spaceForm = new SpaceForm();
+		}
+		else {
+			Space space = this.spacesRepository.findOne(spaceId);		
+			if(space == null) {
+				throw new HttpClientErrorException(HttpStatus.NOT_FOUND, "space " + spaceId + " not found");			
+			}
+			spaceForm = new SpaceForm(space);
 		}
 		
-		Space space = this.spacesRepository.findOne(spaceId);		
-		if(space == null) {
-			throw new HttpClientErrorException(HttpStatus.NOT_FOUND, "space " + spaceId + " not found");			
-		}
-		
-		model.addAttribute("space", new SpaceForm(space));
+		model.addAttribute("space", spaceForm);
 		
 		return "admin/spaceEdit";
 	}
@@ -88,12 +97,7 @@
 		logger.debug("space description " + spaceForm.getDescription());
 		
 		if(bindingResult.hasErrors()) {
-			if(spaceForm.getId() != null && spaceForm.getId().length() > 0) {
-				return "admin/spaceEdit";
-			}
-			else {
-				return "admin/spaceNew";
-			}
+			return "admin/spaceEdit";
 		}
 		
 		spaceForm.setSpacesRepository(spacesRepository);
--- a/server/src/main/java/org/iri_research/renkan/forms/SpaceForm.java	Tue Apr 02 01:56:11 2013 +0200
+++ b/server/src/main/java/org/iri_research/renkan/forms/SpaceForm.java	Tue Apr 02 14:05:56 2013 +0200
@@ -1,5 +1,8 @@
 package org.iri_research.renkan.forms;
 
+import java.util.Date;
+import java.util.UUID;
+
 import org.iri_research.renkan.models.Space;
 import org.iri_research.renkan.repositories.IRenkanRepository;
 import org.iri_research.renkan.repositories.SpacesRepository;
@@ -40,6 +43,10 @@
 
 	@Override
 	protected void saveToModel() {
+		if(this.getId() == null || this.getId().length() == 0) {
+			this.model.setId(UUID.randomUUID().toString());
+			this.model.setCreated(new Date());
+		}
 		this.model.setBinConfig(binConfig);
 		this.model.setImage(image);
 	}
--- a/server/src/main/webapp/WEB-INF/templates/admin/spaceNew.html	Tue Apr 02 01:56:11 2013 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,30 +0,0 @@
-<!DOCTYPE html>
-<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" >
-  <head>
-    <title>Renkan Admin - new space</title>
-    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
-    <meta charset="utf-8"/>
-    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"/>
-
-    <link rel="shortcut icon" href="../../../static/img/favicon.ico" th:href="@{/static/img/favicon.ico}" />
-
-    <link href="../../../static/css/style.css" rel="stylesheet" th:href="@{/static/css/style.css}"/>
-    <link href="../../../static/css/index.css" rel="stylesheet" th:href="@{/static/css/index.css}"/>
-    
-    <script th:remove="all" type="text/javascript" src="../../../static/lib/jquery.min.js" th:src="@{/static/lib/jquery.min.js}" ></script>
-    <script th:remove="all" type="text/javascript" src="../../../static/js/thymol.js"></script> 
-  </head>
-  <body>
-    <div id="container">
-      <div id="wrapper">
-        <h1><a href="renkanIndex.html" th:href="@{/admin}" th:text="#{renkanAdmin.renkan_admin}" id="home-link">Renkan administration</a></h1>
-        <h2><a href="spacesList.html" th:href="@{/admin/spaces}" th:text="#{renkanAdmin.object_list(#{renkanAdmin.space_objects_name})}">Spaces List</a>&nbsp;/&nbsp;<span th:text="#{renkanAdmin.space_edit}">Edit space</span></h2>
-        <div th:include="fragment/spaceForm::spaceFormFragment" id="space-form-container">
-        </div>
-      </div>
-      <footer id="footer" th:substituteby="fragment/pageFragment::footerFragment">
-        <div id="version">© <span class="version-date">2013</span> <a href="http://www.iri.centrepompidou.fr" target="_blanck">IRI</a> - Version <span class="version-version">0.0</span></div>
-      </footer>      
-    </div>
-  </body>
-</html>
\ No newline at end of file
--- a/server/src/main/webapp/WEB-INF/templates/admin/spacesList.html	Tue Apr 02 01:56:11 2013 +0200
+++ b/server/src/main/webapp/WEB-INF/templates/admin/spacesList.html	Tue Apr 02 14:05:56 2013 +0200
@@ -36,7 +36,7 @@
         </div>
         <div id="objects-content">
           <ul class="object-tools">
-            <li><a href="spacesAdd.html" th:href="@{/admin/spaces/add}" th:text="#{renkanAdmin.space_add}">Add space</a></li>
+            <li><a href="spacesAdd.html" th:href="@{/admin/spaces/edit/}" th:text="#{renkanAdmin.space_add}">Add space</a></li>
           </ul>
           <table>
             <thead>
@@ -50,8 +50,8 @@
             <tbody>
               <tr th:each="object: ${page.content}">
                 <td th:text="${object.title}" class="spaces-table-title" >title</td>
-                <td th:text="${#dates.format(object.created, #messages.msg('date.format'))}" class="spaces-table-created">created</td>
-                <td><a href="spaceEdit.html" th:href="@{'/admin/spaces/'+${object.id}}" th:text="#{renkanAdmin.object_edit_link}" class="spaces-table-actions">Edit</a></td>
+                <td th:text="${object.created}?${#dates.format(object.created, #messages.msg('date.format'))}:'n/a'" class="spaces-table-created">created</td>
+                <td><a href="spaceEdit.html" th:href="@{'/admin/spaces/edit/'+${object.id}}" th:text="#{renkanAdmin.object_edit_link}" class="spaces-table-actions">Edit</a></td>
                 <td><a href="#" th:href="@{'/s/'+${object.id}}" th:text="#{renkanAdmin.object_delete_link}" class="spaces-table-actions">Delete</a></td>
               </tr>
             </tbody>
--- a/server/src/main/webapp/WEB-INF/templates/fragment/spaceForm.html	Tue Apr 02 01:56:11 2013 +0200
+++ b/server/src/main/webapp/WEB-INF/templates/fragment/spaceForm.html	Tue Apr 02 14:05:56 2013 +0200
@@ -20,7 +20,7 @@
         }
               
         function spaceFormSubmit() {
-        	
+
         	var errors = {};
         	var valid = true;
         	
@@ -29,16 +29,16 @@
         		valid = false;
         	}
             
-            try
-            {
-               var json = JSON.parse($('#binConfig').val());
-            }
-            catch(e)
-            {
-               console.log(e);
-               errors['binConfigDiv'] = /*[[#{renkan.error.bin_config.json}]]*/"renkan.error.bin_config.json";
-               valid = false;
-            }
+        	if($('#binConfig').val()) {
+        		
+        		var editor = ace.edit("binConfigDiv");
+        		var annotations = editor.getSession().getAnnotations();
+        		if(annotations.length>0) {
+                    var error_message = /*[[#{renkan.error.bin_config.json}]]*/"renkan.error.bin_config.json";
+                    errors['binConfigDiv'] = error_message + ". "+ annotations[0].type + ": (" +(annotations[0].row+1)+","+annotations[0].column+") " + annotations[0].text; 
+        			valid = false;        			
+        		}
+        	}
             
             showformErrors(errors);
                         
--- a/server/src/test/java/org/iri_research/renkan/test/controller/AdminControllerTest.java	Tue Apr 02 01:56:11 2013 +0200
+++ b/server/src/test/java/org/iri_research/renkan/test/controller/AdminControllerTest.java	Tue Apr 02 14:05:56 2013 +0200
@@ -135,6 +135,8 @@
 				Assert.assertEquals("Uri equals", "http://ldt.iri.centrepompidou.fr/new/uri", sp.getUri());
 				Assert.assertEquals("Color equals", "#ffffff", sp.getColor());
 				Assert.assertEquals("BinConfig equals", "{}", sp.getBinConfig());
+				Assert.assertTrue("id sould match uuid regex", sp.getId().matches("[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}"));
+				Assert.assertNotNull("Date created should be not null", sp.getCreated());
 			}
 		}
 	}
@@ -177,7 +179,7 @@
 		
 		this.mvc.perform(post)
 				.andExpect(MockMvcResultMatchers.status().isOk())
-				.andExpect(MockMvcResultMatchers.view().name("admin/spaceNew"))
+				.andExpect(MockMvcResultMatchers.view().name("admin/spaceEdit"))
 				.andExpect(MockMvcResultMatchers.model().hasErrors())
 				.andExpect(MockMvcResultMatchers.model().errorCount(1))
 				.andExpect(MockMvcResultMatchers.model().attributeHasErrors("space"))
@@ -225,7 +227,7 @@
 		
 		this.mvc.perform(post)
 				.andExpect(MockMvcResultMatchers.status().isOk())
-				.andExpect(MockMvcResultMatchers.view().name("admin/spaceNew"))
+				.andExpect(MockMvcResultMatchers.view().name("admin/spaceEdit"))
 				.andExpect(MockMvcResultMatchers.model().hasErrors())
 				.andExpect(MockMvcResultMatchers.model().errorCount(1))
 				.andExpect(MockMvcResultMatchers.model().attributeHasErrors("space"))
@@ -275,7 +277,7 @@
 		
 		this.mvc.perform(post)
 				.andExpect(MockMvcResultMatchers.status().isOk())
-				.andExpect(MockMvcResultMatchers.view().name("admin/spaceNew"))
+				.andExpect(MockMvcResultMatchers.view().name("admin/spaceEdit"))
 				.andExpect(MockMvcResultMatchers.model().hasErrors())
 				.andExpect(MockMvcResultMatchers.model().errorCount(2))
 				.andExpect(MockMvcResultMatchers.model().attributeHasErrors("space"))