Updated dbException. Fixed the resource update service.

Change-Id: Ie3762953927411b8db121f460fed59661df6f659
diff --git a/src/main/java/de/ids_mannheim/korap/handlers/EntityDao.java b/src/main/java/de/ids_mannheim/korap/handlers/EntityDao.java
index 3ef97bb..0c0468e 100644
--- a/src/main/java/de/ids_mannheim/korap/handlers/EntityDao.java
+++ b/src/main/java/de/ids_mannheim/korap/handlers/EntityDao.java
@@ -17,6 +17,7 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.dao.DataAccessException;
+import org.springframework.dao.DuplicateKeyException;
 import org.springframework.dao.EmptyResultDataAccessException;
 import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
 import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
@@ -38,6 +39,7 @@
     private static Logger jlog = LoggerFactory.getLogger(EntityDao.class);
     private NamedParameterJdbcTemplate jdbcTemplate;
 
+
     public EntityDao (PersistenceClient client) {
         this.jdbcTemplate = (NamedParameterJdbcTemplate) client.getSource();
     }
@@ -46,8 +48,8 @@
     // usersettings are fetched plus basic account info, no details, since i rarely use them anyway!
     @Override
     public User getAccount (String username) throws KustvaktException {
-        Map<String, String> namedParameters = Collections.singletonMap(
-                "username", username);
+        Map<String, String> namedParameters = Collections
+                .singletonMap("username", username);
         final String sql = "select a.* from korap_users as a where a.username=:username;";
         User user;
         try {
@@ -61,7 +63,9 @@
         catch (DataAccessException e) {
             jlog.error("Could not retrieve user for name: " + username, e);
             throw new dbException(username, "korap_users",
-                    StatusCodes.DB_GET_FAILED, username);
+                    StatusCodes.DB_GET_FAILED,
+                    "Could not retrieve the user with username: " + username,
+                    username);
         }
         return user;
     }
@@ -117,7 +121,9 @@
                     "Could not update user account for user: " + user.getId(),
                     e);
             throw new dbException(user.getId(), "korap_users",
-                    StatusCodes.DB_UPDATE_FAILED, user.toString());
+                    StatusCodes.DB_UPDATE_FAILED,
+                    "Could not update user account for user: " + user.getId(),
+                    user.toString());
         }
     }
 
@@ -199,13 +205,19 @@
                     new String[] { "id" });
             user.setId(holder.getKey().intValue());
         }
-        catch (DataAccessException e) {
+        catch (DuplicateKeyException e) {
             jlog.error("Could not create user account with username: {}",
                     user.getUsername());
             throw new dbException(user.getUsername(), "korap_users",
-                    StatusCodes.ENTRY_EXISTS, user.getUsername());
+                    StatusCodes.ENTRY_EXISTS, "Username exists.",
+                    user.getUsername());
         }
-        
+        catch (DataAccessException e) {
+            throw new dbException(user.getUsername(), "korap_users",
+                    StatusCodes.ENTRY_EXISTS, "Username exists.",
+                    user.getUsername());
+        }
+
         return r;
     }
 
@@ -217,8 +229,8 @@
 
         try {
             int r;
-            r = this.jdbcTemplate.update(
-                    "DELETE FROM korap_users WHERE id=:user", s);
+            r = this.jdbcTemplate
+                    .update("DELETE FROM korap_users WHERE id=:user", s);
             //            if (user instanceof KorAPUser)
             //                r = this.jdbcTemplate
             //                        .update("DELETE FROM korap_users WHERE username=:user",
@@ -235,7 +247,9 @@
             jlog.error("Could not delete account for user: " + userid, e);
             //            throw new KorAPException(e, StatusCodes.CONNECTION_ERROR);
             throw new dbException(userid, "korap_users",
-                    StatusCodes.DB_DELETE_FAILED, userid.toString());
+                    StatusCodes.DB_DELETE_FAILED,
+                    "Could not delete account for user: " + userid,
+                    userid.toString());
         }
 
     }
@@ -270,8 +284,9 @@
         catch (DataAccessException e) {
             jlog.error("Could not reset password for name: " + username, e);
             throw new dbException(username, "korap_users",
-                    StatusCodes.DB_UPDATE_FAILED, username, uriToken,
-                    passphrase);
+                    StatusCodes.DB_UPDATE_FAILED,
+                    "Could not reset password for username: " + username,
+                    username, uriToken, passphrase);
         }
     }
 
@@ -291,9 +306,12 @@
             return this.jdbcTemplate.update(query, np);
         }
         catch (DataAccessException e) {
-            jlog.error("Could not confirm registration for name " + username, e);
+            jlog.error("Could not confirm registration for name " + username,
+                    e);
             throw new dbException(username, "korap_users",
-                    StatusCodes.DB_UPDATE_FAILED, username, uriToken);
+                    StatusCodes.DB_UPDATE_FAILED,
+                    "Could not confirm registration for username " + username,
+                    username, uriToken);
         }
     }
 
diff --git a/src/main/java/de/ids_mannheim/korap/handlers/ResourceDao.java b/src/main/java/de/ids_mannheim/korap/handlers/ResourceDao.java
index 6abc72b..3f0f7e1 100644
--- a/src/main/java/de/ids_mannheim/korap/handlers/ResourceDao.java
+++ b/src/main/java/de/ids_mannheim/korap/handlers/ResourceDao.java
@@ -28,8 +28,8 @@
  * Created by hanl on 7/21/14.
  */
 //todo: auditing // testing
-public class ResourceDao<T extends KustvaktResource> implements
-        ResourceOperationIface<T> {
+public class ResourceDao<T extends KustvaktResource>
+        implements ResourceOperationIface<T> {
 
     private static Logger log = LoggerFactory.getLogger(ResourceDao.class);
     protected final NamedParameterJdbcTemplate jdbcTemplate;
@@ -59,10 +59,13 @@
                     new RowMapperFactory.ResourceMapper());
         }
         catch (DataAccessException e) {
-            log.error("Exception during database retrieval for ids '" + ids
-                    + "'", e);
+            log.error(
+                    "Exception during database retrieval for ids '" + ids + "'",
+                    e);
             throw new dbException(user.getId(), "resource_store",
-                    StatusCodes.DB_GET_FAILED, ids.toString());
+                    StatusCodes.DB_GET_FAILED,
+                    "Exception during database retrieval for ids '" + ids,
+                    ids.toString());
         }
 
     }
@@ -74,17 +77,17 @@
         source.addValue("id", resource.getPersistentID());
         source.addValue("name", resource.getName());
         source.addValue("desc", resource.getDescription());
-        source.addValue("data", resource.getData());
+        source.addValue("data", resource.getStringData());
         final String sql = "UPDATE resource_store set name=:name, data=:data, description=:desc where persistent_id=:id;";
         try {
             return this.jdbcTemplate.update(sql, source);
         }
         catch (DataAccessException e) {
-            log.error(
-                    "Exception during database update for id '"
-                            + resource.getPersistentID() + "'", e);
+            log.error("Exception during database update for id '"
+                    + resource.getPersistentID() + "'", e);
             throw new dbException(user.getId(), "resource_store",
-                    StatusCodes.DB_UPDATE_FAILED, resource.toString());
+                    StatusCodes.DB_UPDATE_FAILED, "Exception during database update for id '"
+                            + resource.getPersistentID(), resource.toString());
         }
     }
 
@@ -109,7 +112,7 @@
                     new RowMapperFactory.ResourceMapper());
         }
         catch (DataAccessException e) {
-        	// empty results
+            // empty results
             return null;
         }
     }
@@ -170,7 +173,8 @@
         b.insert(Attributes.NAME, Attributes.PARENT_ID,
                 Attributes.PERSISTENT_ID, Attributes.DESCRIPTION,
                 Attributes.CREATOR, Attributes.TYPE, Attributes.CREATED);
-        b.params(":name, :parent, :pid, :desc, :ow, :type, :created, :dtype, :data");
+        b.params(
+                ":name, :parent, :pid, :desc, :ow, :type, :created, :dtype, :data");
 
         if (resource.getParentID() == null) {
             sql = "INSERT INTO resource_store (name, parent_id, persistent_id, description, creator, type, created, data) "
@@ -195,15 +199,17 @@
         source.addValue("data", resource.getStringData());
 
         try {
-            this.jdbcTemplate
-                    .update(sql, source, holder, new String[] { "id" });
+            this.jdbcTemplate.update(sql, source, holder,
+                    new String[] { "id" });
         }
         catch (DataAccessException e) {
-            log.error(
-                    "Exception during database store for id '"
-                            + resource.getPersistentID() + "'", e);
+            log.error("Exception during database store for id '"
+                    + resource.getPersistentID() + "'", e);
             throw new dbException(user.getId(), "resource_store",
-                    StatusCodes.DB_INSERT_FAILED, resource.toString());
+                    StatusCodes.DB_INSERT_FAILED,
+                    "Exception during database store for id '"
+                            + resource.getPersistentID(),
+                    resource.toString());
         }
         resource.setId(holder.getKey().intValue());
         return resource.getId();
@@ -220,7 +226,8 @@
         }
         catch (DataAccessException e) {
             throw new dbException(user.getId(), "resource_store",
-                    StatusCodes.DB_DELETE_FAILED, id);
+                    StatusCodes.DB_DELETE_FAILED, "Operation DELETE failed.",
+                    id);
         }
     }
 
diff --git a/src/main/java/de/ids_mannheim/korap/handlers/UserDetailsDao.java b/src/main/java/de/ids_mannheim/korap/handlers/UserDetailsDao.java
index ddfe851..ec704d5 100644
--- a/src/main/java/de/ids_mannheim/korap/handlers/UserDetailsDao.java
+++ b/src/main/java/de/ids_mannheim/korap/handlers/UserDetailsDao.java
@@ -96,7 +96,8 @@
         }
         catch (DataAccessException e) {
             throw new dbException(-1, "userDetails",
-                    StatusCodes.REQUEST_INVALID, String.valueOf(id));
+                    StatusCodes.REQUEST_INVALID, "The request is invalid.",
+                    String.valueOf(id));
         }
     }
 
@@ -127,7 +128,7 @@
         }
         catch (DataAccessException e) {
             throw new dbException(user.getId(), "userDetails",
-                    StatusCodes.REQUEST_INVALID);
+                    StatusCodes.REQUEST_INVALID, "The request is invalid.");
         }
     }
 
diff --git a/src/main/java/de/ids_mannheim/korap/handlers/UserSettingsDao.java b/src/main/java/de/ids_mannheim/korap/handlers/UserSettingsDao.java
index fdc6ba5..a1d29fe 100644
--- a/src/main/java/de/ids_mannheim/korap/handlers/UserSettingsDao.java
+++ b/src/main/java/de/ids_mannheim/korap/handlers/UserSettingsDao.java
@@ -88,8 +88,8 @@
                         @Override
                         public UserSettings mapRow (ResultSet rs, int rowNum)
                                 throws SQLException {
-                            UserSettings details = new UserSettings(rs
-                                    .getInt("user_id"));
+                            UserSettings details = new UserSettings(
+                                    rs.getInt("user_id"));
                             details.setId(rs.getInt("id"));
                             details.setData(rs.getString("data"));
                             return details;
@@ -102,7 +102,8 @@
         }
         catch (DataAccessException e) {
             throw new dbException(-1, "userSettings",
-                    StatusCodes.REQUEST_INVALID, String.valueOf(id));
+                    StatusCodes.REQUEST_INVALID, "The request is invalid.",
+                    String.valueOf(id));
         }
     }
 
@@ -120,8 +121,8 @@
                         @Override
                         public UserSettings mapRow (ResultSet rs, int rowNum)
                                 throws SQLException {
-                            UserSettings details = new UserSettings(rs
-                                    .getInt("user_id"));
+                            UserSettings details = new UserSettings(
+                                    rs.getInt("user_id"));
                             details.setId(rs.getInt("id"));
                             details.setData(rs.getString("data"));
                             return details;
@@ -134,7 +135,7 @@
         }
         catch (DataAccessException e) {
             throw new dbException(-1, "userSettings",
-                    StatusCodes.REQUEST_INVALID);
+                    StatusCodes.REQUEST_INVALID, "The request is invalid.");
         }
     }