Browse code

FEATBL-805 Feat: add a template to ResourceType

Vincent Vignaud authored on 11/06/2019 13:22:10
Showing 5 changed files
... ...
@@ -19,22 +19,36 @@
19 19
 package net.bluemind.resource.api.type;
20 20
 
21 21
 import java.util.Arrays;
22
+import java.util.Collections;
23
+import java.util.HashMap;
22 24
 import java.util.List;
25
+import java.util.Locale;
26
+import java.util.Map;
23 27
 
24 28
 import net.bluemind.core.api.BMApi;
25 29
 
26 30
 @BMApi(version = "3")
27 31
 public class ResourceTypeDescriptor {
28 32
 
33
+	/** Name. */
34
+	public String label;
35
+
29 36
 	/**
30
-	 * Name
37
+	 * User defined extra properties for this resource type. Example: may define a
38
+	 * 'Quality definition' property for a 'Video projector' type.
31 39
 	 */
32
-	public String label;
40
+	public List<Property> properties = Collections.emptyList();
33 41
 
34 42
 	/**
35
-	 * Custom properties
43
+	 * Templates keyed by language tags.<br>
44
+	 * A template uses {@link #properties} names as variables.<br>
45
+	 * <br>
46
+	 * Example: <i>This template uses the property WhiteBoard having the value
47
+	 * ${WhiteBoard} and the property Seats having the value ${Seats}.</i>
48
+	 * 
49
+	 * @see Locale#toLanguageTag()
36 50
 	 */
37
-	public List<Property> properties;
51
+	public Map<String, String> templates = new HashMap<>();
38 52
 
39 53
 	@BMApi(version = "3")
40 54
 	public static class Property {
... ...
@@ -21,6 +21,7 @@ package net.bluemind.resource.persistance.tests;
21 21
 import static org.junit.Assert.assertEquals;
22 22
 import static org.junit.Assert.assertNotNull;
23 23
 import static org.junit.Assert.assertNull;
24
+import static org.junit.Assert.assertTrue;
24 25
 
25 26
 import java.util.Arrays;
26 27
 import java.util.List;
... ...
@@ -46,7 +47,6 @@ public class ResourceTypeStoreTests {
46 47
 	public void before() throws Exception {
47 48
 		JdbcTestHelper.getInstance().beforeTest();
48 49
 
49
-		
50 50
 		SecurityContext securityContext = SecurityContext.ANONYMOUS;
51 51
 
52 52
 		ContainerStore containerStore = new ContainerStore(JdbcTestHelper.getInstance().getDataSource(),
... ...
@@ -71,9 +71,14 @@ public class ResourceTypeStoreTests {
71 71
 				ResourceTypeDescriptor.Property.create("WhiteBoard", ResourceTypeDescriptor.Property.Type.Boolean,
72 72
 						"whiteboard ?"), //
73 73
 
74
-		ResourceTypeDescriptor.Property.create("Seats", ResourceTypeDescriptor.Property.Type.Number, "number of seats"), //
74
+				ResourceTypeDescriptor.Property.create("Seats", ResourceTypeDescriptor.Property.Type.Number,
75
+						"number of seats"), //
75 76
 				ResourceTypeDescriptor.Property.create("BlaBla", ResourceTypeDescriptor.Property.Type.String,
76 77
 						"blabla"));
78
+		testDescriptor.templates.put("fr",
79
+				"Ce template utilise la propriété WhiteBoard de valeur ${WhiteBoard} et Seats de valeur ${Seats}");
80
+		testDescriptor.templates.put("en",
81
+				"This template uses the property WhiteBoard having the value ${WhiteBoard} and the property Seats having the value ${Seats}");
77 82
 		resourceTypeStore.create("Room/EventRoom", testDescriptor);
78 83
 
79 84
 		ResourceTypeDescriptor get = resourceTypeStore.get("Room/EventRoom");
... ...
@@ -83,8 +88,8 @@ public class ResourceTypeStoreTests {
83 88
 		testDescriptor.properties = Arrays.asList(
84 89
 				ResourceTypeDescriptor.Property.create("TV", ResourceTypeDescriptor.Property.Type.Boolean, "TV ?"), //
85 90
 
86
-		ResourceTypeDescriptor.Property.create("Bottles", ResourceTypeDescriptor.Property.Type.Number,
87
-				"number of bottles"), //
91
+				ResourceTypeDescriptor.Property.create("Bottles", ResourceTypeDescriptor.Property.Type.Number,
92
+						"number of bottles"), //
88 93
 				ResourceTypeDescriptor.Property.create("BlaBla", ResourceTypeDescriptor.Property.Type.String,
89 94
 						"blabla"));
90 95
 		resourceTypeStore.update("Room/EventRoom", testDescriptor);
... ...
@@ -116,6 +121,11 @@ public class ResourceTypeStoreTests {
116 121
 		for (int i = 0; i < get.properties.size(); i++) {
117 122
 			assertPropertyEquals(get.properties.get(i), testDescriptor.properties.get(i));
118 123
 		}
124
+		assertEquals(get.templates.size(), testDescriptor.templates.size());
125
+		testDescriptor.templates.forEach((key, value) -> {
126
+			assertTrue(get.templates.containsKey(key));
127
+			assertEquals(get.templates.get(key), value);
128
+		});
119 129
 	}
120 130
 
121 131
 	private void assertPropertyEquals(Property property, Property property2) {
... ...
@@ -22,6 +22,8 @@ import java.sql.Connection;
22 22
 import java.sql.PreparedStatement;
23 23
 import java.sql.ResultSet;
24 24
 import java.sql.SQLException;
25
+import java.util.HashMap;
26
+import java.util.Map;
25 27
 
26 28
 import net.bluemind.core.container.model.Container;
27 29
 import net.bluemind.core.jdbc.Columns;
... ...
@@ -34,7 +36,8 @@ public class ResourceTypeColumns {
34 36
 
35 37
 	public static Columns cols = Columns.create() //
36 38
 			.col("label")//
37
-			.col("id");
39
+			.col("templates")//
40
+			.col("id");//
38 41
 
39 42
 	public static Columns propCols = Columns.create() //
40 43
 			.col("id")//
... ...
@@ -49,8 +52,10 @@ public class ResourceTypeColumns {
49 52
 					ResourceTypeDescriptor desc) throws SQLException {
50 53
 
51 54
 				statement.setString(index++, desc.label);
55
+				statement.setObject(index++, desc.templates);
52 56
 				statement.setString(index++, identifier);
53 57
 				statement.setLong(index++, container.id);
58
+
54 59
 				return index;
55 60
 			}
56 61
 		};
... ...
@@ -79,8 +84,16 @@ public class ResourceTypeColumns {
79 84
 			@Override
80 85
 			public int populate(ResultSet rs, int index, ResourceTypeDescriptor value) throws SQLException {
81 86
 				value.label = rs.getString(index++);
87
+				value.templates = new HashMap<>();
88
+				@SuppressWarnings("unchecked")
89
+				final Map<String, String> optionals = (Map<String, String>) rs.getObject(index++);
90
+				if (optionals != null) {
91
+					value.templates.putAll(optionals);
92
+				}
93
+
82 94
 				// skip id
83 95
 				index++;
96
+
84 97
 				return index;
85 98
 			}
86 99
 
... ...
@@ -47,6 +47,7 @@ public class ResourceTypeStore extends JdbcAbstractStore {
47 47
 		logger.debug("create resourcetype {}", identifier);
48 48
 
49 49
 		doOrFail(() -> {
50
+
50 51
 			String query = "INSERT INTO t_resource_type ( " + ResourceTypeColumns.cols.names()
51 52
 					+ ", resource_container_id)" + " VALUES ( " + ResourceTypeColumns.cols.values() + ", ?)";
52 53
 
... ...
@@ -84,8 +85,7 @@ public class ResourceTypeStore extends JdbcAbstractStore {
84 85
 	public void update(String identifier, ResourceTypeDescriptor descriptor) throws ServerFault {
85 86
 		doOrFail(() -> {
86 87
 
87
-			String query = "UPDATE t_resource_type set label = ? WHERE id = ? and resource_container_id = ?";
88
-
88
+			String query = "UPDATE t_resource_type set label = ?, templates = ? WHERE id = ? and resource_container_id = ?";
89 89
 			update(query, descriptor, ResourceTypeColumns.statementValues(identifier, container));
90 90
 
91 91
 			delete("DELETE FROM t_resource_type_prop WHERE " + "type_id = ? and resource_container_id = ?",
... ...
@@ -4,6 +4,7 @@ create table t_resource_type (
4 4
   id   varchar(255) NOT NULL,
5 5
   resource_container_id   int4 references t_container(id),
6 6
   label text NOT NULL,
7
+  templates hstore,
7 8
    PRIMARY KEY(id, resource_container_id)
8 9
 );
9 10