From 3bd6d03b4c730cdae7289bff0f1814055a02ba06 Mon Sep 17 00:00:00 2001 From: nishadi Date: Wed, 17 Jan 2018 15:13:22 +0530 Subject: [PATCH] Modified CommentManagerImpl and CommentManager wrapped & thrown the sql and db exceptions with CommentManagementException and ApplicationManagementException. --- .../mgt/common/services/CommentsManager.java | 9 +-- .../mgt/core/impl/CommentsManagerImpl.java | 60 ++++++++++--------- 2 files changed, 38 insertions(+), 31 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/CommentsManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/CommentsManager.java index 693198b683e..d2793ed904a 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/CommentsManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/CommentsManager.java @@ -24,6 +24,7 @@ import org.wso2.carbon.device.application.mgt.common.PaginationResult; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.exception.CommentManagementException; import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; +import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; import java.sql.SQLException; import java.util.List; @@ -65,7 +66,7 @@ public interface CommentsManager { * @throws CommentManagementException Exceptions of the comment management. * @throws SQLException SQL Exception */ - List getAllComments(PaginationRequest request, String uuid) throws CommentManagementException, SQLException; + List getAllComments(PaginationRequest request, String uuid) throws CommentManagementException; /** @@ -96,7 +97,7 @@ public interface CommentsManager { * @throws SQLException SQL Exception * @throws DBConnectionException Database connection Exception */ - Comment updateComment(Comment comment,int CommentId) throws CommentManagementException, SQLException, DBConnectionException; + Comment updateComment(Comment comment,int CommentId) throws CommentManagementException; /** * To get number of rated users @@ -105,7 +106,7 @@ public interface CommentsManager { * @return number of rated users * @throws SQLException sql exception */ - int getRatedUser(String uuid)throws SQLException; + int getRatedUser(String uuid) throws SQLException, CommentManagementException, ApplicationManagementException; /** * To get the average of stars @@ -114,7 +115,7 @@ public interface CommentsManager { * @return value of the stars of an application * @throws SQLException sql exception */ - int getStars(String uuid)throws SQLException; + int getStars(String uuid) throws SQLException, CommentManagementException, ApplicationManagementException; /** * To update rating stars diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/CommentsManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/CommentsManagerImpl.java index 74ea9e57d4f..f25a420d7e8 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/CommentsManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/CommentsManagerImpl.java @@ -68,13 +68,15 @@ public class CommentsManagerImpl implements CommentsManager { commentDAO.addComment(tenantId,comment, comment.getCreatedBy(),comment.getParent(),uuid); ConnectionManagerUtil.commitDBTransaction(); return comment; - } catch (Exception e) { - log.error("Exception occurs.", e); + } catch (DBConnectionException e) { ConnectionManagerUtil.rollbackDBTransaction(); + throw new CommentManagementException("DB Connection Exception occurs.",e); + } catch (SQLException | TransactionManagementException e){ + throw new CommentManagementException("SQL Exception occurs.",e); } finally { ConnectionManagerUtil.closeDBConnection(); } - return comment; + } /** @@ -102,8 +104,7 @@ public class CommentsManagerImpl implements CommentsManager { @Override - public List getAllComments(PaginationRequest request,String uuid) throws CommentManagementException, - SQLException { + public List getAllComments(PaginationRequest request,String uuid) throws CommentManagementException { PaginationResult paginationResult = new PaginationResult(); List comments; @@ -124,11 +125,12 @@ public class CommentsManagerImpl implements CommentsManager { return comments; } catch (DBConnectionException e) { - log.error("DB Connection Exception occurs.", e); - } finally { + throw new CommentManagementException("DB Connection Exception occurs.", e); + } catch (SQLException e){ + throw new CommentManagementException("SQL Exception occurs.",e); + }finally { ConnectionManagerUtil.closeDBConnection(); } - return null; } @Override @@ -145,9 +147,9 @@ public class CommentsManagerImpl implements CommentsManager { ConnectionManagerUtil.openDBConnection(); comment=commentDAO.getComment(CommentId); } catch (DBConnectionException e) { - log.error("DB Connection Exception occurs.", e); + throw new CommentManagementException("DB Connection Exception occurs.", e); } catch (SQLException e) { - log.error("SQL Exception occurs.", e); + throw new CommentManagementException("SQL Exception occurs.", e); } finally { ConnectionManagerUtil.closeDBConnection(); } @@ -169,19 +171,18 @@ public class CommentsManagerImpl implements CommentsManager { commentDAO.deleteComment(CommentId); ConnectionManagerUtil.commitDBTransaction(); } catch (DBConnectionException e) { - log.error("DB Connection Exception occurs.", e); + throw new CommentManagementException("DB Connection Exception occurs.", e); } catch (SQLException e) { - log.error("SQL Exception occurs.", e); + throw new CommentManagementException("SQL Exception occurs.", e); } catch (TransactionManagementException e) { - log.error("Transaction Management Exception occurs.", e); + throw new CommentManagementException("Transaction Management Exception occurs.", e); } finally { ConnectionManagerUtil.closeDBConnection(); } } @Override - public Comment updateComment(Comment comment,int CommentId) throws CommentManagementException, SQLException, - DBConnectionException { + public Comment updateComment(Comment comment,int CommentId) throws CommentManagementException { PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); validateComment(CommentId,comment.getComment()); @@ -193,13 +194,18 @@ public class CommentsManagerImpl implements CommentsManager { try { ConnectionManagerUtil.openDBConnection(); commentDAO.getComment(CommentId); - return commentDAO.updateComment(CommentId, comment.getComment(),comment.getModifiedBy(),comment.getModifiedAt()); - } finally { + return commentDAO + .updateComment(CommentId, comment.getComment(), comment.getModifiedBy(), comment.getModifiedAt()); + } catch (SQLException e){ + throw new CommentManagementException("SQL Exception occurs.",e); + }catch (DBConnectionException e){ + throw new CommentManagementException("DB Connection occurs.",e); + }finally { ConnectionManagerUtil.closeDBConnection(); } } - public int getRatedUser(String uuid){ + public int getRatedUser(String uuid) throws ApplicationManagementException { int ratedUsers=0; if (log.isDebugEnabled()) { @@ -209,9 +215,9 @@ public class CommentsManagerImpl implements CommentsManager { ConnectionManagerUtil.openDBConnection(); ratedUsers =commentDAO.getRatedUser(uuid); } catch (DBConnectionException e) { - log.error("DB Connection Exception occurs.", e); + throw new ApplicationManagementException("DB Connection Exception occurs",e); } catch (ApplicationManagementDAOException e) { - log.error("Application Management Exception occurs.", e); + throw new ApplicationManagementException("Application Management Exception occurs.", e); } finally { ConnectionManagerUtil.closeDBConnection(); } @@ -219,7 +225,7 @@ public class CommentsManagerImpl implements CommentsManager { } @Override - public int getStars(String uuid) throws SQLException { + public int getStars(String uuid) throws ApplicationManagementException { int stars=0; if (log.isDebugEnabled()) { @@ -229,10 +235,10 @@ public class CommentsManagerImpl implements CommentsManager { ConnectionManagerUtil.openDBConnection(); stars= commentDAO.getStars(uuid); } catch (DBConnectionException e) { - log.error("DB Connection Exception occurs.", e); + throw new ApplicationManagementException("DB Connection Exception occurs",e); } catch (ApplicationManagementDAOException e) { - log.error("Application Management Exception occurs.", e); - } finally { + throw new ApplicationManagementException("Application Management Exception occurs.", e); + } finally { ConnectionManagerUtil.closeDBConnection(); } return stars; @@ -261,14 +267,14 @@ public class CommentsManagerImpl implements CommentsManager { } } catch (ApplicationManagementDAOException e) { ConnectionManagerUtil.rollbackDBTransaction(); - log.error("Application Management Exception occurs.", e); + throw new ApplicationManagementException("Application Management Exception occurs.", e); } catch (DBConnectionException e) { - log.error("DB Connection Exception occurs.", e); + throw new ApplicationManagementException("DB Connection Exception occurs.", e); } finally { ConnectionManagerUtil.closeDBConnection(); } - return newStars; + }