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,15 +19,31 @@
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. */
29 34
 	public String label;
30
-	public List<Property> properties;
35
+
36
+	 * User defined extra properties for this resource type. Example: may define a
37
+	 * 'Quality definition' property for a 'Video projector' type.
38
+	public List<Property> properties = Collections.emptyList();
39
+	 * Templates keyed by language tags.<br>
40
+	 * A template uses {@link #properties} names as variables.<br>
41
+	 * <br>
42
+	 * Example: <i>This template uses the property WhiteBoard having the value
43
+	 * ${WhiteBoard} and the property Seats having the value ${Seats}.</i>
44
+	 * 
45
+	 * @see Locale#toLanguageTag()
46
+	public Map<String, String> templates = new HashMap<>();
31 47
 
32 48
 	@BMApi(version = "3")
33 49
 	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;
... ...
@@ -71,9 +72,14 @@ public class ResourceTypeStoreTests {
71 72
 				ResourceTypeDescriptor.Property.create("WhiteBoard", ResourceTypeDescriptor.Property.Type.Boolean,
72 73
 						"whiteboard ?"), //
73 74
 
74
-		ResourceTypeDescriptor.Property.create("Seats", ResourceTypeDescriptor.Property.Type.Number, "number of seats"), //
75
+				ResourceTypeDescriptor.Property.create("Seats", ResourceTypeDescriptor.Property.Type.Number,
76
+						"number of seats"), //
75 77
 				ResourceTypeDescriptor.Property.create("BlaBla", ResourceTypeDescriptor.Property.Type.String,
76 78
 						"blabla"));
79
+		testDescriptor.templates.put("fr",
80
+				"Ce template utilise la propriété WhiteBoard de valeur ${WhiteBoard} et Seats de valeur ${Seats}");
81
+		testDescriptor.templates.put("en",
82
+				"This template uses the property WhiteBoard having the value ${WhiteBoard} and the property Seats having the value ${Seats}");
77 83
 		resourceTypeStore.create("Room/EventRoom", testDescriptor);
78 84
 
79 85
 		ResourceTypeDescriptor get = resourceTypeStore.get("Room/EventRoom");
... ...
@@ -83,8 +89,8 @@ public class ResourceTypeStoreTests {
83 89
 		testDescriptor.properties = Arrays.asList(
84 90
 				ResourceTypeDescriptor.Property.create("TV", ResourceTypeDescriptor.Property.Type.Boolean, "TV ?"), //
85 91
 
86
-		ResourceTypeDescriptor.Property.create("Bottles", ResourceTypeDescriptor.Property.Type.Number,
87
-				"number of bottles"), //
92
+				ResourceTypeDescriptor.Property.create("Bottles", ResourceTypeDescriptor.Property.Type.Number,
93
+						"number of bottles"), //
88 94
 				ResourceTypeDescriptor.Property.create("BlaBla", ResourceTypeDescriptor.Property.Type.String,
89 95
 						"blabla"));
90 96
 		resourceTypeStore.update("Room/EventRoom", testDescriptor);
... ...
@@ -116,6 +122,11 @@ public class ResourceTypeStoreTests {
116 122
 		for (int i = 0; i < get.properties.size(); i++) {
117 123
 			assertPropertyEquals(get.properties.get(i), testDescriptor.properties.get(i));
118 124
 		}
125
+		assertEquals(get.templates.size(), testDescriptor.templates.size());
126
+		testDescriptor.templates.forEach((key, value) -> {
127
+			assertTrue(get.templates.containsKey(key));
128
+			assertEquals(get.templates.get(key), value);
129
+		});
119 130
 	}
120 131
 
121 132
 	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
 		inTransaction(() -> {
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
 		inTransaction(() -> {
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