Merge pull request #924 from GDLMadushanka/master

Logging errors in devmgt and groupmgt services
revert-70aa11f8
Harshan Liyanage 7 years ago committed by GitHub
commit ec78ba09f2

@ -65,4 +65,10 @@ public class GroupPaginationRequest {
this.groupName = groupName; this.groupName = groupName;
} }
@Override
public String toString() {
return "Group Name '" + this.groupName + "' num of rows: " + this.rowCount + " start index: " + this.startIndex
+ " owner' " + this.owner + "'";
}
} }

@ -129,4 +129,12 @@ public class PaginationRequest {
public void setOwnerPattern(String ownerPattern) { public void setOwnerPattern(String ownerPattern) {
this.ownerPattern = ownerPattern; this.ownerPattern = ownerPattern;
} }
@Override
public String toString() {
return "Device type '" + this.deviceType + "' Device Name '" + this.deviceName + "' row count: " + this.rowCount
+ " Owner role '" + this.ownerRole + "' owner pattern '" + this.ownerPattern + "' ownership "
+ this.ownership + "' Status '" + this.status + "' owner '" + this.owner + "' groupId: " + this.groupId
+ " start index: " + this.startIndex;
}
} }

@ -65,6 +65,14 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
@Override @Override
public void createGroup(DeviceGroup deviceGroup, String defaultRole, String[] defaultPermissions) public void createGroup(DeviceGroup deviceGroup, String defaultRole, String[] defaultPermissions)
throws GroupManagementException, GroupAlreadyExistException { throws GroupManagementException, GroupAlreadyExistException {
if (deviceGroup == null) {
String msg = "Received incomplete data for createGroup";
log.error(msg);
throw new GroupManagementException(msg);
}
if (log.isDebugEnabled()) {
log.debug("Creating group '" + deviceGroup.getName() + "'");
}
if (deviceGroup == null) { if (deviceGroup == null) {
throw new GroupManagementException("DeviceGroup cannot be null.", new NullPointerException()); throw new GroupManagementException("DeviceGroup cannot be null.", new NullPointerException());
} }
@ -80,10 +88,17 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
} }
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction(); GroupManagementDAOFactory.rollbackTransaction();
throw new GroupManagementException("Error occurred while adding deviceGroup " + String msg = "Error occurred while adding deviceGroup '" + deviceGroup.getName() + "' to database.";
"'" + deviceGroup.getName() + "' to database.", e); log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
throw new GroupManagementException("Error occurred while initiating transaction.", e); String msg = "Error occurred while initiating transaction.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (Exception e) {
String msg = "Error occurred in creating group '" + deviceGroup.getName() + "'";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} finally { } finally {
GroupManagementDAOFactory.closeConnection(); GroupManagementDAOFactory.closeConnection();
} }
@ -99,6 +114,14 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
@Override @Override
public void updateGroup(DeviceGroup deviceGroup, int groupId) public void updateGroup(DeviceGroup deviceGroup, int groupId)
throws GroupManagementException, GroupAlreadyExistException { throws GroupManagementException, GroupAlreadyExistException {
if (deviceGroup == null) {
String msg = "Received incomplete data for updateGroup";
log.error(msg);
throw new GroupManagementException(msg);
}
if (log.isDebugEnabled()) {
log.debug("update group '" + deviceGroup.getName() + "'");
}
if (deviceGroup == null) { if (deviceGroup == null) {
throw new GroupManagementException("DeviceGroup cannot be null.", new NullPointerException()); throw new GroupManagementException("DeviceGroup cannot be null.", new NullPointerException());
} }
@ -114,10 +137,17 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
} }
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction(); GroupManagementDAOFactory.rollbackTransaction();
throw new GroupManagementException("Error occurred while modifying deviceGroup " + String msg = "Error occurred while modifying deviceGroup '" + deviceGroup.getName() + "'.";
"'" + deviceGroup.getName() + "'.", e); log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
throw new GroupManagementException("Error occurred while initiating transaction.", e); String msg = "Error occurred while initiating transaction.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (Exception e) {
String msg = "Error occurred in updating group '" + deviceGroup.getName() + "'";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} finally { } finally {
GroupManagementDAOFactory.closeConnection(); GroupManagementDAOFactory.closeConnection();
} }
@ -128,6 +158,9 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
*/ */
@Override @Override
public boolean deleteGroup(int groupId) throws GroupManagementException { public boolean deleteGroup(int groupId) throws GroupManagementException {
if (log.isDebugEnabled()) {
log.debug("Delete group: " + groupId);
}
DeviceGroup deviceGroup = getGroup(groupId); DeviceGroup deviceGroup = getGroup(groupId);
if (deviceGroup == null) { if (deviceGroup == null) {
return false; return false;
@ -142,9 +175,17 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
return true; return true;
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction(); GroupManagementDAOFactory.rollbackTransaction();
throw new GroupManagementException("Error occurred while removing group data.", e); String msg = "Error occurred while removing group data.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
throw new GroupManagementException("Error occurred while initiating transaction.", e); String msg = "Error occurred while initiating transaction.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (Exception e) {
String msg = "Error occurred in deleting group: " + groupId;
log.error(msg, e);
throw new GroupManagementException(msg, e);
} finally { } finally {
GroupManagementDAOFactory.closeConnection(); GroupManagementDAOFactory.closeConnection();
} }
@ -155,14 +196,25 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
*/ */
@Override @Override
public DeviceGroup getGroup(int groupId) throws GroupManagementException { public DeviceGroup getGroup(int groupId) throws GroupManagementException {
if (log.isDebugEnabled()) {
log.debug("Get group by id: " + groupId);
}
DeviceGroup deviceGroup; DeviceGroup deviceGroup;
try { try {
GroupManagementDAOFactory.openConnection(); GroupManagementDAOFactory.openConnection();
deviceGroup = this.groupDAO.getGroup(groupId, CarbonContext.getThreadLocalCarbonContext().getTenantId()); deviceGroup = this.groupDAO.getGroup(groupId, CarbonContext.getThreadLocalCarbonContext().getTenantId());
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {
throw new GroupManagementException("Error occurred while obtaining group '" + groupId + "'", e); String msg = "Error occurred while obtaining group '" + groupId + "'";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new GroupManagementException("Error occurred while opening a connection to the data source.", e); String msg = "Error occurred while opening a connection to the data source.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (Exception e) {
String msg = "Error occurred in getGroup for groupId: " + groupId;
log.error(msg, e);
throw new GroupManagementException(msg, e);
} finally { } finally {
GroupManagementDAOFactory.closeConnection(); GroupManagementDAOFactory.closeConnection();
} }
@ -174,14 +226,30 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
*/ */
@Override @Override
public DeviceGroup getGroup(String groupName) throws GroupManagementException { public DeviceGroup getGroup(String groupName) throws GroupManagementException {
if (groupName == null) {
String msg = "Received empty groupName for getGroup";
log.error(msg);
throw new GroupManagementException(msg);
}
if (log.isDebugEnabled()) {
log.debug("Get group by name '" + groupName + "'");
}
DeviceGroup deviceGroup; DeviceGroup deviceGroup;
try { try {
GroupManagementDAOFactory.openConnection(); GroupManagementDAOFactory.openConnection();
deviceGroup = this.groupDAO.getGroup(groupName, CarbonContext.getThreadLocalCarbonContext().getTenantId()); deviceGroup = this.groupDAO.getGroup(groupName, CarbonContext.getThreadLocalCarbonContext().getTenantId());
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {
throw new GroupManagementException("Error occurred while obtaining group with name: '" + groupName + "'", e); String msg = "Error occurred while obtaining group with name: '" + groupName + "'";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new GroupManagementException("Error occurred while opening a connection to the data source.", e); String msg = "Error occurred while opening a connection to the data source.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (Exception e) {
String msg = "Error occurred in getGroup with name " + groupName;
log.error(msg, e);
throw new GroupManagementException(msg, e);
} finally { } finally {
GroupManagementDAOFactory.closeConnection(); GroupManagementDAOFactory.closeConnection();
} }
@ -190,15 +258,26 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
@Override @Override
public List<DeviceGroup> getGroups() throws GroupManagementException { public List<DeviceGroup> getGroups() throws GroupManagementException {
if (log.isDebugEnabled()) {
log.debug("Get groups");
}
List<DeviceGroup> deviceGroups = new ArrayList<>(); List<DeviceGroup> deviceGroups = new ArrayList<>();
try { try {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
GroupManagementDAOFactory.openConnection(); GroupManagementDAOFactory.openConnection();
deviceGroups = this.groupDAO.getGroups(tenantId); deviceGroups = this.groupDAO.getGroups(tenantId);
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {
throw new GroupManagementException("Error occurred while retrieving all groups in tenant", e); String msg = "Error occurred while retrieving all groups in tenant";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new GroupManagementException("Error occurred while opening a connection to the data source.", e); String msg = "Error occurred while opening a connection to the data source.";
log.error(msg);
throw new GroupManagementException(msg, e);
} catch (Exception e) {
String msg = "Error occurred in getGroups";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} finally { } finally {
GroupManagementDAOFactory.closeConnection(); GroupManagementDAOFactory.closeConnection();
} }
@ -207,6 +286,14 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
@Override @Override
public PaginationResult getGroups(GroupPaginationRequest request) throws GroupManagementException { public PaginationResult getGroups(GroupPaginationRequest request) throws GroupManagementException {
if (request == null) {
String msg = "Received incomplete data for getGroup";
log.error(msg);
throw new GroupManagementException(msg);
}
if (log.isDebugEnabled()) {
log.debug("Get groups with pagination " + request.toString());
}
request = DeviceManagerUtil.validateGroupListPageSize(request); request = DeviceManagerUtil.validateGroupListPageSize(request);
List<DeviceGroup> deviceGroups = new ArrayList<>(); List<DeviceGroup> deviceGroups = new ArrayList<>();
try { try {
@ -214,9 +301,17 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
GroupManagementDAOFactory.openConnection(); GroupManagementDAOFactory.openConnection();
deviceGroups = this.groupDAO.getGroups(request, tenantId); deviceGroups = this.groupDAO.getGroups(request, tenantId);
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {
throw new GroupManagementException("Error occurred while retrieving all groups in tenant", e); String msg = "Error occurred while retrieving all groups in tenant";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new GroupManagementException("Error occurred while opening a connection to the data source.", e); String msg = "Error occurred while opening a connection to the data source.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (Exception e) {
String msg = "Error occurred in getGroups";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} finally { } finally {
GroupManagementDAOFactory.closeConnection(); GroupManagementDAOFactory.closeConnection();
} }
@ -228,6 +323,14 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
@Override @Override
public List<DeviceGroup> getGroups(String username) throws GroupManagementException { public List<DeviceGroup> getGroups(String username) throws GroupManagementException {
if (username == null || username.isEmpty()) {
String msg = "Received null user name for getGroups";
log.error(msg);
throw new GroupManagementException(msg);
}
if (log.isDebugEnabled()) {
log.debug("Get groups of owner '" + username + "'");
}
Map<Integer, DeviceGroup> groups = new HashMap<>(); Map<Integer, DeviceGroup> groups = new HashMap<>();
UserStoreManager userStoreManager; UserStoreManager userStoreManager;
try { try {
@ -245,11 +348,20 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
groups.put(deviceGroup.getGroupId(), deviceGroup); groups.put(deviceGroup.getGroupId(), deviceGroup);
} }
} catch (UserStoreException e) { } catch (UserStoreException e) {
throw new GroupManagementException("Error occurred while getting user store manager.", e); String msg = "Error occurred while getting user store manager.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new GroupManagementException("Error occurred while retrieving all groups accessible to user.", e); String msg = "Error occurred while retrieving all groups accessible to user.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {
log.error(e);
throw new GroupManagementException(e); throw new GroupManagementException(e);
} catch (Exception e) {
String msg = "Error occurred in getGroups for " + username;
log.error(msg, e);
throw new GroupManagementException(msg, e);
} finally { } finally {
GroupManagementDAOFactory.closeConnection(); GroupManagementDAOFactory.closeConnection();
} }
@ -257,6 +369,14 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
} }
private List<Integer> getGroupIds(String username) throws GroupManagementException { private List<Integer> getGroupIds(String username) throws GroupManagementException {
if (username == null || username.isEmpty()) {
String msg = "Received empty user name for getGroupIds";
log.error(msg);
throw new GroupManagementException(msg);
}
if (log.isDebugEnabled()) {
log.debug("Get groups Ids of owner '" + username + "'");
}
UserStoreManager userStoreManager; UserStoreManager userStoreManager;
List<Integer> deviceGroupIds = new ArrayList<>(); List<Integer> deviceGroupIds = new ArrayList<>();
try { try {
@ -268,11 +388,20 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
deviceGroupIds = this.groupDAO.getOwnGroupIds(username, tenantId); deviceGroupIds = this.groupDAO.getOwnGroupIds(username, tenantId);
deviceGroupIds.addAll(this.groupDAO.getGroupIds(roleList, tenantId)); deviceGroupIds.addAll(this.groupDAO.getGroupIds(roleList, tenantId));
} catch (UserStoreException e) { } catch (UserStoreException e) {
throw new GroupManagementException("Error occurred while getting user store manager.", e); String msg = "Error occurred while getting user store manager.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new GroupManagementException("Error occurred while retrieving all groups accessible to user.", e); String msg = "Error occurred while retrieving all groups accessible to user.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {
log.error(e);
throw new GroupManagementException(e); throw new GroupManagementException(e);
} catch (Exception e) {
String msg = "Error occurred in getGroups for username '" + username + "'";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} finally { } finally {
GroupManagementDAOFactory.closeConnection(); GroupManagementDAOFactory.closeConnection();
} }
@ -282,6 +411,14 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
@Override @Override
public PaginationResult getGroups(String currentUser, GroupPaginationRequest request) public PaginationResult getGroups(String currentUser, GroupPaginationRequest request)
throws GroupManagementException { throws GroupManagementException {
if (currentUser == null || request == null) {
String msg = "Received incomplete date for getGroups";
log.error(msg);
throw new GroupManagementException(msg);
}
if (log.isDebugEnabled()) {
log.debug("Get all groups of user '" + currentUser + "' pagination request " + request.toString());
}
request = DeviceManagerUtil.validateGroupListPageSize(request); request = DeviceManagerUtil.validateGroupListPageSize(request);
List<Integer> allDeviceGroupIdsOfUser = getGroupIds(currentUser); List<Integer> allDeviceGroupIdsOfUser = getGroupIds(currentUser);
List<DeviceGroup> allMatchingGroups = new ArrayList<>(); List<DeviceGroup> allMatchingGroups = new ArrayList<>();
@ -290,9 +427,17 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
GroupManagementDAOFactory.openConnection(); GroupManagementDAOFactory.openConnection();
allMatchingGroups = this.groupDAO.getGroups(request, allDeviceGroupIdsOfUser, tenantId); allMatchingGroups = this.groupDAO.getGroups(request, allDeviceGroupIdsOfUser, tenantId);
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {
throw new GroupManagementException("Error occurred while retrieving all groups in tenant", e); String msg = "Error occurred while retrieving all groups in tenant";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new GroupManagementException("Error occurred while opening a connection to the data source.", e); String msg = "Error occurred while opening a connection to the data source.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (Exception e) {
String msg = "Error occurred in getGroups";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} finally { } finally {
GroupManagementDAOFactory.closeConnection(); GroupManagementDAOFactory.closeConnection();
} }
@ -304,28 +449,55 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
@Override @Override
public int getGroupCount() throws GroupManagementException { public int getGroupCount() throws GroupManagementException {
if (log.isDebugEnabled()) {
log.debug("Get groups count");
}
try { try {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
GroupManagementDAOFactory.openConnection(); GroupManagementDAOFactory.openConnection();
return groupDAO.getGroupCount(tenantId); return groupDAO.getGroupCount(tenantId);
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {
throw new GroupManagementException("Error occurred while retrieving all groups in tenant", e); String msg = "Error occurred while retrieving all groups in tenant";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new GroupManagementException("Error occurred while opening a connection to the data source.", e); String msg = "Error occurred while opening a connection to the data source.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (Exception e) {
String msg = "Error occurred";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} finally { } finally {
GroupManagementDAOFactory.closeConnection(); GroupManagementDAOFactory.closeConnection();
} }
} }
private int getGroupCount(GroupPaginationRequest request) throws GroupManagementException { private int getGroupCount(GroupPaginationRequest request) throws GroupManagementException {
if (request == null) {
String msg = "Received empty request for getGroupCount";
log.error(msg);
throw new GroupManagementException(msg);
}
if (log.isDebugEnabled()) {
log.debug("Get groups count, pagination request " + request.toString());
}
try { try {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
GroupManagementDAOFactory.openConnection(); GroupManagementDAOFactory.openConnection();
return groupDAO.getGroupCount(request, tenantId); return groupDAO.getGroupCount(request, tenantId);
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {
throw new GroupManagementException("Error occurred while retrieving all groups in tenant", e); String msg = "Error occurred while retrieving all groups in tenant";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new GroupManagementException("Error occurred while opening a connection to the data source.", e); String msg = "Error occurred while opening a connection to the data source.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (Exception e) {
String msg = "Error occurred in getGroupCount";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} finally { } finally {
GroupManagementDAOFactory.closeConnection(); GroupManagementDAOFactory.closeConnection();
} }
@ -336,6 +508,14 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
*/ */
@Override @Override
public int getGroupCount(String username) throws GroupManagementException { public int getGroupCount(String username) throws GroupManagementException {
if (username == null || username.isEmpty()) {
String msg = "Received empty user name for getGroupCount";
log.error(msg);
throw new GroupManagementException(msg);
}
if (log.isDebugEnabled()) {
log.debug("Get groups count of '" + username + "'");
}
UserStoreManager userStoreManager; UserStoreManager userStoreManager;
int count; int count;
try { try {
@ -348,11 +528,21 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
count += groupDAO.getGroupsCount(roleList, tenantId); count += groupDAO.getGroupsCount(roleList, tenantId);
return count; return count;
} catch (UserStoreException e) { } catch (UserStoreException e) {
throw new GroupManagementException("Error occurred while getting user store manager.", e); String msg = "Error occurred while getting user store manager.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {
throw new GroupManagementException("Error occurred while retrieving group count of user '" + username + "'", e); String msg = "Error occurred while retrieving group count of user '" + username + "'";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new GroupManagementException("Error occurred while opening a connection to the data source.", e); String msg = "Error occurred while opening a connection to the data source.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (Exception e) {
String msg = "Error occurred in getGroupCount for username '" + username + "'";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} finally { } finally {
GroupManagementDAOFactory.closeConnection(); GroupManagementDAOFactory.closeConnection();
} }
@ -364,41 +554,52 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
@Override @Override
public void manageGroupSharing(int groupId, List<String> newRoles) public void manageGroupSharing(int groupId, List<String> newRoles)
throws GroupManagementException, RoleDoesNotExistException { throws GroupManagementException, RoleDoesNotExistException {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); if (log.isDebugEnabled()) {
UserStoreManager userStoreManager; log.debug("Manage group sharing for group: " + groupId);
try { }
userStoreManager = int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm( UserStoreManager userStoreManager;
tenantId).getUserStoreManager(); try {
List<String> currentUserRoles = getRoles(groupId); userStoreManager =
GroupManagementDAOFactory.beginTransaction(); DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm(
if (newRoles != null) { tenantId).getUserStoreManager();
for (String role : newRoles) { List<String> currentUserRoles = getRoles(groupId);
if (!userStoreManager.isExistingRole(role)) { GroupManagementDAOFactory.beginTransaction();
throw new RoleDoesNotExistException("Role '" + role + "' does not exists in the user store."); if (newRoles != null) {
} for (String role : newRoles) {
// Removing role from current user roles of the group will return true if role exist. if (!userStoreManager.isExistingRole(role)) {
// So we don't need to add it to the db again. throw new RoleDoesNotExistException("Role '" + role + "' does not exists in the user store.");
if (!currentUserRoles.remove(role)) { }
// If group doesn't have the role, it is adding to the db. // Removing role from current user roles of the group will return true if role exist.
groupDAO.addRole(groupId, role, tenantId); // So we don't need to add it to the db again.
} if (!currentUserRoles.remove(role)) {
// If group doesn't have the role, it is adding to the db.
groupDAO.addRole(groupId, role, tenantId);
} }
} }
for (String role : currentUserRoles) { }
// Removing old roles from db which are not available in the new roles list. for (String role : currentUserRoles) {
groupDAO.removeRole(groupId, role, tenantId); // Removing old roles from db which are not available in the new roles list.
} groupDAO.removeRole(groupId, role, tenantId);
GroupManagementDAOFactory.commitTransaction(); }
} catch (GroupManagementDAOException e) { GroupManagementDAOFactory.commitTransaction();
GroupManagementDAOFactory.rollbackTransaction(); } catch (GroupManagementDAOException e) {
throw new GroupManagementException(e); GroupManagementDAOFactory.rollbackTransaction();
} catch (UserStoreException e) { log.error(e);
throw new GroupManagementException("User store error in updating sharing roles.", e); throw new GroupManagementException(e);
} catch (TransactionManagementException e) { } catch (UserStoreException e) {
throw new GroupManagementException(e); String msg = "User store error in updating sharing roles.";
} finally { log.error(msg, e);
GroupManagementDAOFactory.closeConnection(); throw new GroupManagementException(msg, e);
} catch (TransactionManagementException e) {
log.error(e);
throw new GroupManagementException(e);
} catch (Exception e) {
String msg = "Error occurred in manageGroupSharing for groupId: " + groupId;
log.error(msg, e);
throw new GroupManagementException(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
} }
} }
@ -407,14 +608,25 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
*/ */
@Override @Override
public List<String> getRoles(int groupId) throws GroupManagementException { public List<String> getRoles(int groupId) throws GroupManagementException {
if (log.isDebugEnabled()) {
log.debug("Group roles for group: " + groupId);
}
try { try {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
GroupManagementDAOFactory.openConnection(); GroupManagementDAOFactory.openConnection();
return groupDAO.getRoles(groupId, tenantId); return groupDAO.getRoles(groupId, tenantId);
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {
throw new GroupManagementException("Error occurred while retrieving all groups in tenant", e); String msg = "Error occurred while retrieving all groups in tenant";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new GroupManagementException("Error occurred while opening a connection to the data source.", e); String msg = "Error occurred while opening a connection to the data source.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (Exception e) {
String msg = "Error occurred in getRoles for groupId: " + groupId;
log.error(msg, e);
throw new GroupManagementException(msg, e);
} finally { } finally {
GroupManagementDAOFactory.closeConnection(); GroupManagementDAOFactory.closeConnection();
} }
@ -426,6 +638,9 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
@Override @Override
public List<Device> getDevices(int groupId, int startIndex, int rowCount) public List<Device> getDevices(int groupId, int startIndex, int rowCount)
throws GroupManagementException { throws GroupManagementException {
if (log.isDebugEnabled()) {
log.debug("Group devices of group: " + groupId + " start index " + startIndex + " row count " + rowCount);
}
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
List<Device> devices; List<Device> devices;
try { try {
@ -433,11 +648,21 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
GroupManagementDAOFactory.openConnection(); GroupManagementDAOFactory.openConnection();
devices = this.groupDAO.getDevices(groupId, startIndex, rowCount, tenantId); devices = this.groupDAO.getDevices(groupId, startIndex, rowCount, tenantId);
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {
throw new GroupManagementException("Error occurred while getting devices in group.", e); String msg = "Error occurred while getting devices in group.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new GroupManagementException("Error occurred while opening a connection to the data source.", e); String msg = "Error occurred while opening a connection to the data source.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
throw new GroupManagementException("Error occurred while validating the limit of the devices to be returned", e); String msg = "Error occurred while validating the limit of the devices to be returned";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (Exception e) {
String msg = "Error occurred in getDevices for groupId: " + groupId;
log.error(msg, e);
throw new GroupManagementException(msg, e);
} finally { } finally {
GroupManagementDAOFactory.closeConnection(); GroupManagementDAOFactory.closeConnection();
} }
@ -449,13 +674,24 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
*/ */
@Override @Override
public int getDeviceCount(int groupId) throws GroupManagementException { public int getDeviceCount(int groupId) throws GroupManagementException {
if (log.isDebugEnabled()) {
log.debug("Group devices count of group: " + groupId);
}
try { try {
GroupManagementDAOFactory.openConnection(); GroupManagementDAOFactory.openConnection();
return groupDAO.getDeviceCount(groupId, CarbonContext.getThreadLocalCarbonContext().getTenantId()); return groupDAO.getDeviceCount(groupId, CarbonContext.getThreadLocalCarbonContext().getTenantId());
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {
throw new GroupManagementException("Error occurred while retrieving all groups in tenant", e); String msg = "Error occurred while retrieving all groups in tenant";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new GroupManagementException("Error occurred while opening a connection to the data source.", e); String msg = "Error occurred while opening a connection to the data source.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (Exception e) {
String msg = "Error occurred in getDeviceCount for groupId: " + groupId;
log.error(msg, e);
throw new GroupManagementException(msg, e);
} finally { } finally {
GroupManagementDAOFactory.closeConnection(); GroupManagementDAOFactory.closeConnection();
} }
@ -467,6 +703,9 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
@Override @Override
public void addDevices(int groupId, List<DeviceIdentifier> deviceIdentifiers) public void addDevices(int groupId, List<DeviceIdentifier> deviceIdentifiers)
throws GroupManagementException, DeviceNotFoundException { throws GroupManagementException, DeviceNotFoundException {
if (log.isDebugEnabled()) {
log.debug("Group devices to the group: " + groupId);
}
Device device; Device device;
try { try {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
@ -483,12 +722,22 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
} }
GroupManagementDAOFactory.commitTransaction(); GroupManagementDAOFactory.commitTransaction();
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
throw new GroupManagementException("Error occurred while retrieving device.", e); String msg = "Error occurred while retrieving device.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction(); GroupManagementDAOFactory.rollbackTransaction();
throw new GroupManagementException("Error occurred while adding device to group.", e); String msg = "Error occurred while adding device to group.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
throw new GroupManagementException("Error occurred while initiating transaction.", e); String msg = "Error occurred while initiating transaction.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (Exception e) {
String msg = "Error occurred in addDevices for groupId " + groupId;
log.error(msg, e);
throw new GroupManagementException(msg, e);
} finally { } finally {
GroupManagementDAOFactory.closeConnection(); GroupManagementDAOFactory.closeConnection();
} }
@ -500,6 +749,9 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
@Override @Override
public void removeDevice(int groupId, List<DeviceIdentifier> deviceIdentifiers) public void removeDevice(int groupId, List<DeviceIdentifier> deviceIdentifiers)
throws GroupManagementException, DeviceNotFoundException { throws GroupManagementException, DeviceNotFoundException {
if (log.isDebugEnabled()) {
log.debug("Remove devices from the group: " + groupId);
}
Device device; Device device;
try { try {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
@ -514,12 +766,22 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
} }
GroupManagementDAOFactory.commitTransaction(); GroupManagementDAOFactory.commitTransaction();
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
throw new GroupManagementException("Error occurred while retrieving device.", e); String msg = "Error occurred while retrieving device.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
throw new GroupManagementException("Error occurred while initiating transaction.", e); String msg = "Error occurred while initiating transaction.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction(); GroupManagementDAOFactory.rollbackTransaction();
throw new GroupManagementException("Error occurred while adding device to group.", e); String msg = "Error occurred while adding device to group.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (Exception e) {
String msg = "Error occurred in removeDevice for groupId: " + groupId;
log.error(msg, e);
throw new GroupManagementException(msg, e);
} finally { } finally {
GroupManagementDAOFactory.closeConnection(); GroupManagementDAOFactory.closeConnection();
} }
@ -530,6 +792,9 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
*/ */
@Override @Override
public List<DeviceGroup> getGroups(String username, String permission) throws GroupManagementException { public List<DeviceGroup> getGroups(String username, String permission) throws GroupManagementException {
if (log.isDebugEnabled()) {
log.debug("Get groups of user '" + username + "'");
}
List<DeviceGroup> deviceGroups = getGroups(username); List<DeviceGroup> deviceGroups = getGroups(username);
Map<Integer, DeviceGroup> permittedDeviceGroups = new HashMap<>(); Map<Integer, DeviceGroup> permittedDeviceGroups = new HashMap<>();
UserRealm userRealm; UserRealm userRealm;
@ -546,13 +811,27 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
} }
} }
} catch (UserStoreException e) { } catch (UserStoreException e) {
throw new GroupManagementException("Error occurred while getting user realm.", e); String msg = "Error occurred while getting user realm.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (Exception e) {
String msg = "Error occurred in getGroups for username '" + username + "'";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} }
return new ArrayList<>(permittedDeviceGroups.values()); return new ArrayList<>(permittedDeviceGroups.values());
} }
@Override @Override
public List<DeviceGroup> getGroups(DeviceIdentifier deviceIdentifier) throws GroupManagementException { public List<DeviceGroup> getGroups(DeviceIdentifier deviceIdentifier) throws GroupManagementException {
if (deviceIdentifier == null) {
String msg = "Received empty device identifier for getGroups";
log.error(msg);
throw new GroupManagementException(msg);
}
if (log.isDebugEnabled()) {
log.debug("Get groups of device " + deviceIdentifier.getId());
}
DeviceManagementProviderService managementProviderService = new DeviceManagementProviderServiceImpl(); DeviceManagementProviderService managementProviderService = new DeviceManagementProviderServiceImpl();
try { try {
Device device = managementProviderService.getDevice(deviceIdentifier, false); Device device = managementProviderService.getDevice(deviceIdentifier, false);
@ -560,11 +839,21 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
return groupDAO.getGroups(device.getId(), return groupDAO.getGroups(device.getId(),
PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId());
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
throw new GroupManagementException("Error occurred while retrieving the device details.", e); String msg = "Error occurred while retrieving the device details.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (GroupManagementDAOException e) { } catch (GroupManagementDAOException e) {
throw new GroupManagementException("Error occurred while retrieving device groups.", e); String msg = "Error occurred while retrieving device groups.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (SQLException e) { } catch (SQLException e) {
throw new GroupManagementException("Error occurred while opening database connection.", e); String msg = "Error occurred while opening database connection.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} catch (Exception e) {
String msg = "Error occurred in getGroups";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} finally { } finally {
GroupManagementDAOFactory.closeConnection(); GroupManagementDAOFactory.closeConnection();
} }
@ -575,7 +864,9 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
*/ */
@Override @Override
public DeviceGroup createDefaultGroup(String groupName) throws GroupManagementException { public DeviceGroup createDefaultGroup(String groupName) throws GroupManagementException {
if (log.isDebugEnabled()) {
log.debug("Create default group " + groupName);
}
DeviceGroup defaultGroup = this.getGroup(groupName); DeviceGroup defaultGroup = this.getGroup(groupName);
if (defaultGroup == null) { if (defaultGroup == null) {
defaultGroup = new DeviceGroup(groupName); defaultGroup = new DeviceGroup(groupName);
@ -586,10 +877,13 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
this.createGroup(defaultGroup, DeviceGroupConstants.Roles.DEFAULT_ADMIN_ROLE, this.createGroup(defaultGroup, DeviceGroupConstants.Roles.DEFAULT_ADMIN_ROLE,
DeviceGroupConstants.Permissions.DEFAULT_ADMIN_PERMISSIONS); DeviceGroupConstants.Permissions.DEFAULT_ADMIN_PERMISSIONS);
} catch (GroupAlreadyExistException e) { } catch (GroupAlreadyExistException e) {
if (log.isDebugEnabled()) { String msg = "Default group: " + defaultGroup.getName() + " already exists. Skipping group creation.";
log.debug("Default group: " + defaultGroup.getName() + " already exists. Skipping group creation.", log.error(msg, e);
e); throw new GroupManagementException(msg, e);
} } catch (Exception e) {
String msg = "Error occurred in createDefaultGroup for groupName '" + groupName + "'";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} }
return this.getGroup(groupName); return this.getGroup(groupName);
} else { } else {

Loading…
Cancel
Save