1 | package clarin.cmdi.componentregistry.impl.database; |
---|
2 | |
---|
3 | import org.springframework.jdbc.core.JdbcTemplate; |
---|
4 | |
---|
5 | /** |
---|
6 | * |
---|
7 | * @author Twan Goosen <twan.goosen@mpi.nl> |
---|
8 | */ |
---|
9 | public final class ComponentRegistryTestDatabase { |
---|
10 | |
---|
11 | private ComponentRegistryTestDatabase() { |
---|
12 | } |
---|
13 | |
---|
14 | public static void resetDatabase(JdbcTemplate jdbcTemplate) { |
---|
15 | jdbcTemplate.execute("DROP SCHEMA PUBLIC CASCADE"); |
---|
16 | } |
---|
17 | |
---|
18 | |
---|
19 | |
---|
20 | public static void createTablePersistentComponents(JdbcTemplate jdbcTemplate) { |
---|
21 | jdbcTemplate.execute("create sequence basedescription_id_seq start with 1 increment by 1"); |
---|
22 | |
---|
23 | jdbcTemplate.execute("CREATE TABLE basedescription (" |
---|
24 | + "id IDENTITY NOT NULL," |
---|
25 | + " user_id integer," |
---|
26 | + " is_public boolean NOT NULL," |
---|
27 | + " is_deleted boolean DEFAULT false NOT NULL," |
---|
28 | + " component_id VARCHAR(255) NOT NULL," |
---|
29 | + " name VARCHAR(255) NOT NULL," |
---|
30 | + " description VARCHAR(255) NOT NULL," |
---|
31 | + " registration_date timestamp,"// with timezone," |
---|
32 | + " href VARCHAR(255)," |
---|
33 | + " creator_name VARCHAR(255)," |
---|
34 | + " domain_name VARCHAR(255)," |
---|
35 | + " group_name VARCHAR(255), " |
---|
36 | + " content VARCHAR(10240) DEFAULT '' NOT NULL, " |
---|
37 | + " show_in_editor boolean DEFAULT true NOT NULL, " |
---|
38 | + " CONSTRAINT UNIQUE_PROFILE_ID UNIQUE (component_id));"); |
---|
39 | } |
---|
40 | |
---|
41 | |
---|
42 | public static void createTableRegistryUser(JdbcTemplate jdbcTemplate) { |
---|
43 | jdbcTemplate.execute("create sequence registry_user_id_seq start with 1 increment by 1"); |
---|
44 | jdbcTemplate.execute("CREATE TABLE registry_user (" |
---|
45 | + " id IDENTITY NOT NULL," |
---|
46 | + " name VARCHAR(255)," |
---|
47 | + " principal_name VARCHAR(255));"); |
---|
48 | } |
---|
49 | |
---|
50 | public static void createTableComments(JdbcTemplate jdbcTemplate) { |
---|
51 | jdbcTemplate.execute("create sequence comments_id_seq start with 1 increment by 1"); |
---|
52 | jdbcTemplate.execute("CREATE TABLE comments (" |
---|
53 | + "id IDENTITY NOT NULL," |
---|
54 | + "user_id integer," |
---|
55 | + " component_id VARCHAR(255)," |
---|
56 | + " comments VARCHAR(255) NOT NULL," |
---|
57 | + " comment_date timestamp," |
---|
58 | + " user_name VARCHAR(255)" |
---|
59 | + " );"); |
---|
60 | } |
---|
61 | |
---|
62 | public static void createTableGroup(JdbcTemplate jdbcTemplate) { |
---|
63 | jdbcTemplate.execute("CREATE TABLE usergroup (" |
---|
64 | + "id int GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1) not null," |
---|
65 | + "ownerId integer NOT NULL," |
---|
66 | + " name VARCHAR(255) NOT NULL," |
---|
67 | + "constraint usergroup_ID primary key (id)" |
---|
68 | + ");"); |
---|
69 | } |
---|
70 | |
---|
71 | public static void createTableOwnership(JdbcTemplate jdbcTemplate) { |
---|
72 | jdbcTemplate.execute("CREATE TABLE ownership (" |
---|
73 | + "id int GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1) not null," |
---|
74 | + "componentId VARCHAR(255)," |
---|
75 | + "groupId int," |
---|
76 | + "userId int," |
---|
77 | + "constraint ownership_id primary key (id)" |
---|
78 | + ");"); |
---|
79 | } |
---|
80 | |
---|
81 | public static void createTableGroupMembership(JdbcTemplate jdbcTemplate) { |
---|
82 | jdbcTemplate.execute("CREATE TABLE groupmembership (" |
---|
83 | + "id int GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1) not null," |
---|
84 | + "groupId integer," |
---|
85 | + "userId integer," |
---|
86 | + "constraint groupmembership_id primary key (id)" |
---|
87 | + ");"); |
---|
88 | } |
---|
89 | |
---|
90 | public static void resetAndCreateAllTables(JdbcTemplate jdbcTemplate) { |
---|
91 | resetDatabase(jdbcTemplate); |
---|
92 | createTablePersistentComponents(jdbcTemplate); |
---|
93 | createTableRegistryUser(jdbcTemplate); |
---|
94 | createTableComments(jdbcTemplate); |
---|
95 | createTableGroup(jdbcTemplate); |
---|
96 | createTableOwnership(jdbcTemplate); |
---|
97 | createTableGroupMembership(jdbcTemplate); |
---|
98 | |
---|
99 | } |
---|
100 | } |
---|