From 40865050c9d787eddd8d51a0328cc78f9c4708f4 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Thu, 27 Oct 2016 16:17:33 +0200
Subject: [PATCH] issue #12 PrivilegeType in db is mapped by privilege name not
 id of enum

---
 .../mapviewer/model/user/BasicPrivilege.java  | 341 +++++++++---------
 persist/src/db/10.0.3/fix_db_20161027.sql     |  17 +
 .../persist/dao/user/UserDaoTest.java         |   5 +-
 3 files changed, 193 insertions(+), 170 deletions(-)

diff --git a/model/src/main/java/lcsb/mapviewer/model/user/BasicPrivilege.java b/model/src/main/java/lcsb/mapviewer/model/user/BasicPrivilege.java
index 5241793154..8a11ebcfe7 100644
--- a/model/src/main/java/lcsb/mapviewer/model/user/BasicPrivilege.java
+++ b/model/src/main/java/lcsb/mapviewer/model/user/BasicPrivilege.java
@@ -1,169 +1,172 @@
-package lcsb.mapviewer.model.user;
-
-import java.io.Serializable;
-
-import javax.persistence.Column;
-import javax.persistence.DiscriminatorColumn;
-import javax.persistence.DiscriminatorType;
-import javax.persistence.DiscriminatorValue;
-import javax.persistence.Entity;
-import javax.persistence.FetchType;
-import javax.persistence.GeneratedValue;
-import javax.persistence.GenerationType;
-import javax.persistence.Id;
-import javax.persistence.Inheritance;
-import javax.persistence.InheritanceType;
-import javax.persistence.ManyToOne;
-import javax.persistence.Table;
-
-/**
- * Class that defines user privilege. Privilege has an access level (typicaly 0,
- * 1 value), type of the privilege and user reference.
- * 
- * @author Piotr Gawron
- * 
- */
-@Entity
-@Table(name = "privilege_table")
-@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
-@DiscriminatorColumn(name = "privilege_class_type_db", discriminatorType = DiscriminatorType.STRING)
-@DiscriminatorValue("BASIC_PRIVILEGE")
-public class BasicPrivilege implements Serializable {
-
-	/**
-	 * 
-	 */
-	private static final long	serialVersionUID	= 1L;
-
-	/**
-	 * Unique identifier in the database.
-	 */
-	@Id
-	@GeneratedValue(strategy = GenerationType.IDENTITY)
-	@Column(name = "idDb", unique = true, nullable = false)
-	private int								id;
-
-	/**
-	 * Which user this privilege concerns.
-	 */
-	@ManyToOne(fetch = FetchType.LAZY)
-	private User							user;
-
-	/**
-	 * Type of the privilege.
-	 */
-	private PrivilegeType			type;
-
-	/**
-	 * Access level of the privilege. By default 0 means that user doesn't have
-	 * privilege and value greater than 0 means that user has privilege. It's
-	 * possible to implement different behaviour for different level values.
-	 */
-	private int								level;
-
-	/**
-	 * Constructor that initialize the privilege with given data.
-	 * 
-	 * @param level
-	 *          level on which the privilege is set
-	 * @param type
-	 *          type of the privilege
-	 * @param user
-	 *          user for which this privilege is set
-	 */
-	public BasicPrivilege(int level, PrivilegeType type, User user) {
-		this.level = level;
-		this.type = type;
-		this.user = user;
-	}
-
-	/**
-	 * Default constructor.
-	 */
-	public BasicPrivilege() {
-	}
-
-	/**
-	 * Checks if the privilege is with the same type as privilege given in the
-	 * parameter.
-	 * 
-	 * @param privilege
-	 *          other privilege to compare
-	 * @return <code>true</code> if privilege is of the same type as an argument,
-	 *         <code>false</code> otherwise
-	 */
-	public boolean equalsPrivilege(BasicPrivilege privilege) {
-		if (privilege == null) {
-			return false;
-		}
-		return type.equals(privilege.getType());
-	}
-
-	/**
-	 * @return the id
-	 * @see #id
-	 */
-	public int getId() {
-		return id;
-	}
-
-	/**
-	 * @param id
-	 *          the id to set
-	 * @see #id
-	 */
-	public void setId(int id) {
-		this.id = id;
-	}
-
-	/**
-	 * @return the user
-	 * @see #user
-	 */
-	public User getUser() {
-		return user;
-	}
-
-	/**
-	 * @param user
-	 *          the user to set
-	 * @see #user
-	 */
-	public void setUser(User user) {
-		this.user = user;
-	}
-
-	/**
-	 * @return the type
-	 * @see #type
-	 */
-	public PrivilegeType getType() {
-		return type;
-	}
-
-	/**
-	 * @param type
-	 *          the type to set
-	 * @see #type
-	 */
-	public void setType(PrivilegeType type) {
-		this.type = type;
-	}
-
-	/**
-	 * @return the level
-	 * @see #level
-	 */
-	public int getLevel() {
-		return level;
-	}
-
-	/**
-	 * @param level
-	 *          the level to set
-	 * @see #level
-	 */
-	public void setLevel(int level) {
-		this.level = level;
-	}
-}
+package lcsb.mapviewer.model.user;
+
+import java.io.Serializable;
+
+import javax.persistence.Column;
+import javax.persistence.DiscriminatorColumn;
+import javax.persistence.DiscriminatorType;
+import javax.persistence.DiscriminatorValue;
+import javax.persistence.Entity;
+import javax.persistence.EnumType;
+import javax.persistence.Enumerated;
+import javax.persistence.FetchType;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Inheritance;
+import javax.persistence.InheritanceType;
+import javax.persistence.ManyToOne;
+import javax.persistence.Table;
+
+/**
+ * Class that defines user privilege. Privilege has an access level (typicaly 0,
+ * 1 value), type of the privilege and user reference.
+ * 
+ * @author Piotr Gawron
+ * 
+ */
+@Entity
+@Table(name = "privilege_table")
+@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
+@DiscriminatorColumn(name = "privilege_class_type_db", discriminatorType = DiscriminatorType.STRING)
+@DiscriminatorValue("BASIC_PRIVILEGE")
+public class BasicPrivilege implements Serializable {
+
+	/**
+	 * 
+	 */
+	private static final long	serialVersionUID	= 1L;
+
+	/**
+	 * Unique identifier in the database.
+	 */
+	@Id
+	@GeneratedValue(strategy = GenerationType.IDENTITY)
+	@Column(name = "idDb", unique = true, nullable = false)
+	private int								id;
+
+	/**
+	 * Which user this privilege concerns.
+	 */
+	@ManyToOne(fetch = FetchType.LAZY)
+	private User							user;
+
+	/**
+	 * Type of the privilege.
+	 */
+	@Enumerated(EnumType.STRING)
+	private PrivilegeType			type;
+
+	/**
+	 * Access level of the privilege. By default 0 means that user doesn't have
+	 * privilege and value greater than 0 means that user has privilege. It's
+	 * possible to implement different behaviour for different level values.
+	 */
+	private int								level;
+
+	/**
+	 * Constructor that initialize the privilege with given data.
+	 * 
+	 * @param level
+	 *          level on which the privilege is set
+	 * @param type
+	 *          type of the privilege
+	 * @param user
+	 *          user for which this privilege is set
+	 */
+	public BasicPrivilege(int level, PrivilegeType type, User user) {
+		this.level = level;
+		this.type = type;
+		this.user = user;
+	}
+
+	/**
+	 * Default constructor.
+	 */
+	public BasicPrivilege() {
+	}
+
+	/**
+	 * Checks if the privilege is with the same type as privilege given in the
+	 * parameter.
+	 * 
+	 * @param privilege
+	 *          other privilege to compare
+	 * @return <code>true</code> if privilege is of the same type as an argument,
+	 *         <code>false</code> otherwise
+	 */
+	public boolean equalsPrivilege(BasicPrivilege privilege) {
+		if (privilege == null) {
+			return false;
+		}
+		return type.equals(privilege.getType());
+	}
+
+	/**
+	 * @return the id
+	 * @see #id
+	 */
+	public int getId() {
+		return id;
+	}
+
+	/**
+	 * @param id
+	 *          the id to set
+	 * @see #id
+	 */
+	public void setId(int id) {
+		this.id = id;
+	}
+
+	/**
+	 * @return the user
+	 * @see #user
+	 */
+	public User getUser() {
+		return user;
+	}
+
+	/**
+	 * @param user
+	 *          the user to set
+	 * @see #user
+	 */
+	public void setUser(User user) {
+		this.user = user;
+	}
+
+	/**
+	 * @return the type
+	 * @see #type
+	 */
+	public PrivilegeType getType() {
+		return type;
+	}
+
+	/**
+	 * @param type
+	 *          the type to set
+	 * @see #type
+	 */
+	public void setType(PrivilegeType type) {
+		this.type = type;
+	}
+
+	/**
+	 * @return the level
+	 * @see #level
+	 */
+	public int getLevel() {
+		return level;
+	}
+
+	/**
+	 * @param level
+	 *          the level to set
+	 * @see #level
+	 */
+	public void setLevel(int level) {
+		this.level = level;
+	}
+}
diff --git a/persist/src/db/10.0.3/fix_db_20161027.sql b/persist/src/db/10.0.3/fix_db_20161027.sql
index 0980e2be8c..69c68b7090 100644
--- a/persist/src/db/10.0.3/fix_db_20161027.sql
+++ b/persist/src/db/10.0.3/fix_db_20161027.sql
@@ -3,3 +3,20 @@ alter table project_table add column sbgnformat boolean default false ;
 update project_table set sbgnformat = (select sbgnformat from model_table  where project_iddb = project_table.iddb);
 
 alter table model_table drop column sbgnformat;
+
+--PrivilegeType should be mapped in database by name not by index
+alter table privilege_table add column type_string character varying;
+update privilege_table set type_string = 'VIEW_PROJECT' where type = 0;
+update privilege_table set type_string = 'ADD_MAP' where type = 1;
+update privilege_table set type_string = 'EDIT_MISSING_CONNECTIONS_PROJECT' where type = 2;
+update privilege_table set type_string = 'EDIT_COMMENTS_PROJECT' where type = 3;
+update privilege_table set type_string = 'DRUG_TARGETING_ADVANCED_VIEW_PROJECT' where type = 4;
+update privilege_table set type_string = 'PROJECT_MANAGEMENT' where type = 5;
+update privilege_table set type_string = 'USER_MANAGEMENT' where type = 6;
+update privilege_table set type_string = 'CUSTOM_LAYOUTS' where type = 7;
+update privilege_table set type_string = 'LAYOUT_VIEW' where type = 8;
+update privilege_table set type_string = 'CONFIGURATION_MANAGE' where type = 9;
+update privilege_table set type_string = 'LAYOUT_MANAGEMENT' where type = 10;
+update privilege_table set type_string = 'MANAGE_GENOMES' where type = 11;
+alter table privilege_table drop column type;
+alter table privilege_table rename type_string   TO type;
diff --git a/persist/src/test/java/lcsb/mapviewer/persist/dao/user/UserDaoTest.java b/persist/src/test/java/lcsb/mapviewer/persist/dao/user/UserDaoTest.java
index c36edb9b85..d960d1d65c 100644
--- a/persist/src/test/java/lcsb/mapviewer/persist/dao/user/UserDaoTest.java
+++ b/persist/src/test/java/lcsb/mapviewer/persist/dao/user/UserDaoTest.java
@@ -20,6 +20,8 @@ import org.springframework.security.crypto.password.PasswordEncoder;
 import lcsb.mapviewer.model.map.MiriamType;
 import lcsb.mapviewer.model.map.reaction.Reaction;
 import lcsb.mapviewer.model.map.species.Species;
+import lcsb.mapviewer.model.user.BasicPrivilege;
+import lcsb.mapviewer.model.user.PrivilegeType;
 import lcsb.mapviewer.model.user.User;
 import lcsb.mapviewer.model.user.UserAnnotationSchema;
 import lcsb.mapviewer.model.user.UserClassAnnotators;
@@ -61,6 +63,7 @@ public class UserDaoTest extends PersistTestFunctions {
 
 			user = new User();
 			user.setLogin(testLogin);
+			user.addPrivilege(new BasicPrivilege(0, PrivilegeType.ADD_MAP, user));
 			userDao.add(user);
 
 			long counter2 = userDao.getCount();
@@ -81,7 +84,7 @@ public class UserDaoTest extends PersistTestFunctions {
 
 		} catch (Exception e) {
 			e.printStackTrace();
-			fail("Unexpected exception occured");
+			throw e;
 		}
 	}
 
-- 
GitLab