Merge remote-tracking branch 'nayan/workspace'

This commit is contained in:
Keith Woodlock
2013-03-15 10:35:47 +00:00
10 changed files with 138 additions and 96 deletions
@@ -0,0 +1,5 @@
ALTER TABLE m_group MODIFY name VARCHAR(100) NOT NULL;
ALTER TABLE m_group DROP INDEX name;
ALTER TABLE m_group DROP INDEX external_id;
ALTER TABLE m_group ADD UNIQUE INDEX name (name, level_Id);
ALTER TABLE m_group ADD UNIQUE INDEX external_id (external_id, level_Id);
@@ -45,9 +45,13 @@ import org.springframework.stereotype.Component;
@Scope("singleton")
public class GroupsApiResource {
private static final Set<String> GROUP_DATA_PARAMETERS = new HashSet<String>(Arrays.asList("id", "officeId", "name", "externalId",
"clientMembers", "allowedClients", "allowedOffices", "allowedParentGroups", "groupLevel", "allowedStaffs", "childGroups"));
/*
* GROUP_DATA_PARAMETERS is used by ToApiJsonSerializer<E>, make sure E's properties and E_PARAMETERS are in same
*/
private static final Set<String> GROUP_DATA_PARAMETERS = new HashSet<String>(Arrays.asList("id", "name", "externalId", "officeId",
"officeName", "staffId", "staffName", "parentId", "parentName", "hierarchy", "groupSummaryData", "groupLevelData",
"clientMembers", "allowedClients", "allowedOffices", "allowedParentGroups", "allowedStaffs", "childGroups"));
private final PlatformSecurityContext context;
private final GroupReadPlatformService groupReadPlatformService;
private final ToApiJsonSerializer<GroupData> toApiJsonSerializer;
@@ -23,12 +23,11 @@ public class GroupData {
private final String officeName;
private final Long staffId;
private final String staffName;
private final Long groupLevel;
private final Long parentId;
private final String parentName;
private final String hierarchy;
@SuppressWarnings("unused")
private final GroupSummaryData groupSummaryData;
private final GroupSummary groupSummaryData;
@SuppressWarnings("unused")
private final GroupLevelData groupLevelData;
private final Collection<ClientLookup> clientMembers;
@@ -37,21 +36,19 @@ public class GroupData {
@SuppressWarnings("unused")
private final Collection<OfficeData> allowedOffices;
@SuppressWarnings("unused")
private final Collection<GroupLookupData> allowedParentGroups;
private final Collection<GroupLookup> allowedParentGroups;
@SuppressWarnings("unused")
private final Collection<StaffData> allowedStaffs;
@SuppressWarnings("unused")
private final Collection<GroupLookupData> childGroups;
private final Collection<GroupLookup> childGroups;
public GroupData(final Long id, final Long officeId, final String officeName, final String name, final String externalId,
final Long groupLevel, final Long parentId, final String parentName, final Long staffId, final String staffName,
final String hierarchy) {
final Long parentId, final String parentName, final Long staffId, final String staffName, final String hierarchy) {
this.id = id;
this.officeId = officeId;
this.officeName = officeName;
this.name = name;
this.externalId = externalId;
this.groupLevel = groupLevel;
this.parentId = parentId;
this.parentName = parentName;
this.staffId = staffId;
@@ -68,15 +65,14 @@ public class GroupData {
}
public GroupData(final GroupData group, final Collection<ClientLookup> clientMembers, final Collection<ClientLookup> allowedClients,
final Collection<OfficeData> allowedOffices, final Collection<GroupLookupData> allowedParentGroups,
final GroupLevelData groupLevelData, final Collection<StaffData> allowedStaffs, final Collection<GroupLookupData> childGroups,
final GroupSummaryData groupSummaryData) {
final Collection<OfficeData> allowedOffices, final Collection<GroupLookup> allowedParentGroups,
final GroupLevelData groupLevelData, final Collection<StaffData> allowedStaffs, final Collection<GroupLookup> childGroups,
final GroupSummary groupSummaryData) {
this.id = group.getId();
this.officeId = group.getOfficeId();
this.officeName = group.getOfficeName();
this.name = group.getName();
this.externalId = group.getExternalId();
this.groupLevel = group.getGroupLevel();
this.parentId = group.getParentId();
this.parentName = group.getParentName();
this.staffId = group.getStaffId();
@@ -94,7 +90,7 @@ public class GroupData {
}
public GroupData(final Long officeId, final Collection<ClientLookup> allowedClients, final Collection<OfficeData> allowedOffices,
final Collection<GroupLookupData> allowedParentGroups, final GroupLevelData groupLevelData,
final Collection<GroupLookup> allowedParentGroups, final GroupLevelData groupLevelData,
final Collection<StaffData> allowedStaffs) {
this.id = null;
this.officeId = officeId;
@@ -102,7 +98,6 @@ public class GroupData {
this.name = null;
this.externalId = null;
this.clientMembers = null;
this.groupLevel = null;
this.parentId = null;
this.parentName = null;
this.staffId = null;
@@ -137,10 +132,6 @@ public class GroupData {
return this.officeName;
}
public Long getGroupLevel() {
return this.groupLevel;
}
public Long getParentId() {
return this.parentId;
}
@@ -8,12 +8,12 @@ package org.mifosplatform.portfolio.group.data;
/**
* Immutable data object representing groups.
*/
public class GroupLookupData {
public class GroupLookup {
private final Long id;
private final String name;
public GroupLookupData(final Long id, final String name) {
public GroupLookup(final Long id, final String name) {
this.id = id;
this.name = name;
}
@@ -9,14 +9,14 @@ import java.util.Collection;
import org.mifosplatform.organisation.monetary.data.MoneyData;
public class GroupSummaryData {
public class GroupSummary {
private final Long totalActiveClients;
private final Long totalChildGroups;
private final Collection<MoneyData> totalLoanPortfolio;
private final Collection<MoneyData> totalSavings;
public GroupSummaryData(final Long totalActiveClients, final Long totalChildGroups, final Collection<MoneyData> totalLoanPortfolio,
public GroupSummary(final Long totalActiveClients, final Long totalChildGroups, final Collection<MoneyData> totalLoanPortfolio,
final Collection<MoneyData> totalSavings) {
this.totalActiveClients = totalActiveClients;
this.totalChildGroups = totalChildGroups;
@@ -80,6 +80,13 @@ public final class GroupCommandFromApiJsonDeserializer extends AbstractFromApiJs
final List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("group");
/*
* Name is a mandatory field and not exceeding length 100 character
*/
final String nameParameterName = "name";
final String name = this.fromApiJsonHelper.extractStringNamed(nameParameterName, element);
baseDataValidator.reset().parameter(nameParameterName).value(name).notNull().notExceedingLengthOf(100);
final String externalIdParameterName = "externalId";
if (this.fromApiJsonHelper.parameterExists(externalIdParameterName, element)) {
@@ -87,22 +94,24 @@ public final class GroupCommandFromApiJsonDeserializer extends AbstractFromApiJs
baseDataValidator.reset().parameter(externalIdParameterName).value(externalId).ignoreIfNull().notExceedingLengthOf(100);
}
final String officeIdParameterName = "officeId";
if (this.fromApiJsonHelper.parameterExists(officeIdParameterName, element)) {
final Long officeId = this.fromApiJsonHelper.extractLongNamed(officeIdParameterName, element);
baseDataValidator.reset().parameter(officeIdParameterName).value(officeId).notNull().integerGreaterThanZero();
}
/*
* Either OfficeId or ParentId is mandatory to have value
*/
final String parentIdParameterName = "parentId";
if (this.fromApiJsonHelper.parameterExists(parentIdParameterName, element)) {
final Long parentId = this.fromApiJsonHelper.extractLongNamed(parentIdParameterName, element);
baseDataValidator.reset().parameter(parentIdParameterName).value(parentId).notNull().integerGreaterThanZero();
}else{
final String officeIdParameterName = "officeId";
final Long officeId = this.fromApiJsonHelper.extractLongNamed(officeIdParameterName, element);
baseDataValidator.reset().parameter(officeIdParameterName).value(officeId).notNull().integerGreaterThanZero();
}
final String staffIdParameterName = "staffId";
if (this.fromApiJsonHelper.parameterExists(staffIdParameterName, element)) {
final Long staffId = this.fromApiJsonHelper.extractLongNamed(staffIdParameterName, element);
baseDataValidator.reset().parameter(staffIdParameterName).value(staffId).notNull().integerGreaterThanZero();
baseDataValidator.reset().parameter(staffIdParameterName).value(staffId).ignoreIfNull().integerGreaterThanZero();
}
if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist",
@@ -137,13 +146,13 @@ public final class GroupCommandFromApiJsonDeserializer extends AbstractFromApiJs
final String parentIdParameterName = "parentId";
if (this.fromApiJsonHelper.parameterExists(parentIdParameterName, element)) {
final Long parentId = this.fromApiJsonHelper.extractLongNamed(parentIdParameterName, element);
baseDataValidator.reset().parameter(parentIdParameterName).value(parentId).notNull().integerGreaterThanZero();
baseDataValidator.reset().parameter(parentIdParameterName).value(parentId).ignoreIfNull().integerGreaterThanZero();
}
final String staffIdParameterName = "staffId";
if (this.fromApiJsonHelper.parameterExists(staffIdParameterName, element)) {
final Long staffId = this.fromApiJsonHelper.extractLongNamed(staffIdParameterName, element);
baseDataValidator.reset().parameter(staffIdParameterName).value(staffId).notNull().integerGreaterThanZero();
baseDataValidator.reset().parameter(staffIdParameterName).value(staffId).ignoreIfNull().integerGreaterThanZero();
}
if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist",
@@ -155,9 +164,6 @@ public final class GroupCommandFromApiJsonDeserializer extends AbstractFromApiJs
final Type typeOfMap = new TypeToken<Map<String, Object>>() {}.getType();
/*
* Office updated is not supported
*/
final Set<String> supportedParametersUnassignStaff = new HashSet<String>(Arrays.asList("staffId"));
this.fromApiJsonHelper.checkForUnsupportedParameters(typeOfMap, json, supportedParametersUnassignStaff);
@@ -168,10 +174,8 @@ public final class GroupCommandFromApiJsonDeserializer extends AbstractFromApiJs
final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("group");
final String staffIdParameterName = "staffId";
if (this.fromApiJsonHelper.parameterExists(staffIdParameterName, element)) {
final Long staffId = this.fromApiJsonHelper.extractLongNamed(staffIdParameterName, element);
baseDataValidator.reset().parameter(staffIdParameterName).value(staffId).notNull().integerGreaterThanZero();
}
final Long staffId = this.fromApiJsonHelper.extractLongNamed(staffIdParameterName, element);
baseDataValidator.reset().parameter(staffIdParameterName).value(staffId).notNull().integerGreaterThanZero();
if (!dataValidationErrors.isEmpty()) { throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist",
"Validation errors exist.", dataValidationErrors); }
@@ -9,6 +9,7 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import org.mifosplatform.infrastructure.core.domain.JdbcSupport;
import org.mifosplatform.infrastructure.core.service.TenantAwareRoutingDataSource;
import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext;
import org.mifosplatform.portfolio.group.data.GroupLevelData;
@@ -54,9 +55,9 @@ public class GroupLevelReadPlatformServiceImpl implements GroupLevelReadPlatform
final Long levelId = rs.getLong("id");
final String levelName = rs.getString("levelName");
final Long parentLevelId = rs.getLong("parentLevelId");
final Long parentLevelId = JdbcSupport.getLong(rs, "parentLevelId");
final String parentLevelName = rs.getString("parentName");
final Long childLevelId = rs.getLong("childLevelId");
final Long childLevelId = JdbcSupport.getLong(rs, "childLevelId");
final String childLevelName = rs.getString("childLevelName");
final boolean superParent = rs.getBoolean("superParent");
final boolean recursable = rs.getBoolean("recursable");
@@ -14,7 +14,7 @@ import org.mifosplatform.portfolio.group.data.GroupAccountSummaryCollectionData;
import org.mifosplatform.portfolio.group.data.GroupAccountSummaryData;
import org.mifosplatform.portfolio.group.data.GroupData;
import org.mifosplatform.portfolio.group.data.GroupLevelData;
import org.mifosplatform.portfolio.group.data.GroupLookupData;
import org.mifosplatform.portfolio.group.data.GroupLookup;
public interface GroupReadPlatformService {
@@ -30,7 +30,7 @@ public interface GroupReadPlatformService {
Collection<GroupAccountSummaryData> retrieveGroupLoanAccountsByLoanOfficerId(Long groupId, Long loanOfficerId);
Collection<GroupLookupData> retrieveAllGroupsbyOfficeIdAndLevelId(Long officeId, Long levelId);
Collection<GroupLookup> retrieveAllGroupsbyOfficeIdAndLevelId(Long officeId, Long levelId);
GroupLevelData retrieveGroupLevelDetails(Long levelId);
@@ -38,7 +38,7 @@ public interface GroupReadPlatformService {
GroupData retrieveGroupDetails(Long groupId, boolean template);
Collection<GroupLookupData> retrieveChildGroupsbyGroupId(Long groupId);
Collection<GroupLookup> retrieveChildGroupsbyGroupId(Long groupId);
Long retrieveTotalNoOfChildGroups(Long groupId);
@@ -49,4 +49,6 @@ public interface GroupReadPlatformService {
GroupData retrieveNewChildGroupDetails(Long officeId, Long levelId, Long parentGroupId);
StaffData retrieveStaffsbyId(Long staffId);
Long getLevelIdByGroupId(final Long groupId);
}
@@ -28,12 +28,13 @@ import org.mifosplatform.portfolio.group.data.GroupAccountSummaryCollectionData;
import org.mifosplatform.portfolio.group.data.GroupAccountSummaryData;
import org.mifosplatform.portfolio.group.data.GroupData;
import org.mifosplatform.portfolio.group.data.GroupLevelData;
import org.mifosplatform.portfolio.group.data.GroupLookupData;
import org.mifosplatform.portfolio.group.data.GroupSummaryData;
import org.mifosplatform.portfolio.group.data.GroupLookup;
import org.mifosplatform.portfolio.group.data.GroupSummary;
import org.mifosplatform.portfolio.group.domain.Group;
import org.mifosplatform.portfolio.group.domain.GroupRepository;
import org.mifosplatform.portfolio.group.exception.GroupLevelNotFoundException;
import org.mifosplatform.portfolio.group.exception.GroupNotFoundException;
import org.mifosplatform.useradministration.domain.AppUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
@@ -64,16 +65,20 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService {
public Collection<GroupData> retrieveAllGroups(final String extraCriteria) {
this.context.authenticatedUser();
AppUser currentUser = context.authenticatedUser();
String hierarchy = currentUser.getOffice().getHierarchy();
String hierarchySearchString = hierarchy + "%";
final GroupDataMapper rm = new GroupDataMapper();
String sql = "select " + rm.groupSchema() + " where g.is_deleted=0";
String sql = "select " + rm.groupSchema() + " where o.hierarchy like ? and g.is_deleted=0 ";
if (StringUtils.isNotBlank(extraCriteria)) {
sql += " and (" + extraCriteria + ")";
}
return this.jdbcTemplate.query(sql, rm, new Object[] {});
return this.jdbcTemplate.query(sql, rm, new Object[] {hierarchySearchString});
}
@Override
@@ -93,7 +98,7 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService {
}
@Override
public Collection<GroupLookupData> retrieveAllGroupsbyOfficeIdAndLevelId(final Long officeId, final Long levelId) {
public Collection<GroupLookup> retrieveAllGroupsbyOfficeIdAndLevelId(final Long officeId, final Long levelId) {
this.context.authenticatedUser();
final GroupLookupMapper rm = new GroupLookupMapper();
@@ -101,7 +106,7 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService {
return this.jdbcTemplate.query(sql, rm, new Object[] { officeId, levelId });
}
private GroupLookupData retrieveGroupbyId(final Long parentGroupId) {
private GroupLookup retrieveGroupbyId(final Long parentGroupId) {
try {
this.context.authenticatedUser();
final GroupLookupMapper rm = new GroupLookupMapper();
@@ -114,7 +119,7 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService {
}
@Override
public Collection<GroupLookupData> retrieveChildGroupsbyGroupId(final Long groupId) {
public Collection<GroupLookup> retrieveChildGroupsbyGroupId(final Long groupId) {
this.context.authenticatedUser();
final GroupLookupMapper rm = new GroupLookupMapper();
@@ -155,10 +160,10 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService {
final List<OfficeData> allowedOffices = new ArrayList<OfficeData>(this.officeReadPlatformService.retrieveAllOfficesForDropdown());
List<GroupLookupData> allowedParentGroups = null;
List<GroupLookup> allowedParentGroups = null;
if (groupLevelData.getParentLevelId() != null) {
allowedParentGroups = new ArrayList<GroupLookupData>(retrieveAllGroupsbyOfficeIdAndLevelId(officeId,
allowedParentGroups = new ArrayList<GroupLookup>(retrieveAllGroupsbyOfficeIdAndLevelId(officeId,
groupLevelData.getParentLevelId()));
}
@@ -190,7 +195,7 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService {
final List<OfficeData> allowedOffices = new ArrayList<OfficeData>();
allowedOffices.add(this.officeReadPlatformService.retrieveOffice(officeId));
final List<GroupLookupData> allowedParentGroups = new ArrayList<GroupLookupData>();
final List<GroupLookup> allowedParentGroups = new ArrayList<GroupLookup>();
allowedParentGroups.add(retrieveGroupbyId(parentGroupId));
final List<StaffData> allowedStaffs = new ArrayList<StaffData>();
@@ -206,17 +211,17 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService {
final Collection<ClientLookup> clientMembers = retrieveClientMembers(groupId);
Collection<ClientLookup> availableClients = null;
Collection<OfficeData> allowedOffices = null;
Collection<GroupLookupData> allowedParentGroups = null;
final GroupLevelData groupLevelData = retrieveGroupLevelDetails(group.getGroupLevel());
Collection<GroupLookup> allowedParentGroups = null;
final GroupLevelData groupLevelData = retrieveGroupLevelDetails(getLevelIdByGroupId(groupId));
;
Collection<StaffData> allowedStaffs = null;
final Collection<GroupLookupData> childGroups = retrieveChildGroupsbyGroupId(groupId);
final Collection<GroupLookup> childGroups = retrieveChildGroupsbyGroupId(groupId);
final Long totalActiveClients = retrieveTotalClients(group.getHierarchy());
final Long totalChildGroups = retrieveTotalNoOfChildGroups(groupId);
final Collection<MoneyData> totalLoanPortfolio = retrieveGroupLoanPortfolio(group.getHierarchy());
final GroupSummaryData groupSummaryData = new GroupSummaryData(totalActiveClients, totalChildGroups, totalLoanPortfolio, null);
final GroupSummary groupSummaryData = new GroupSummary(totalActiveClients, totalChildGroups, totalLoanPortfolio, null);
group = new GroupData(group, clientMembers, availableClients, allowedOffices, allowedParentGroups, groupLevelData, allowedStaffs,
childGroups, groupSummaryData);
@@ -265,6 +270,15 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService {
}
}
@Override
public Long getLevelIdByGroupId(final Long groupId){
this.context.authenticatedUser();
final String sqlTotalClients = "SELECT g.level_Id as levelId FROM m_group g WHERE g.id = ?";
return this.jdbcTemplate.queryForLong(sqlTotalClients, new Object[] { groupId });
}
@Override
public Long retrieveTotalClients(final String hierarchy) {
@@ -323,10 +337,10 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService {
private static final class GroupDataMapper implements RowMapper<GroupData> {
public String groupSchema() {
return "g.office_id as officeId, g.level_id as groupLevel , g.parent_id as parentId , o.name as officeName,"
return "g.office_id as officeId, g.parent_id as parentId , o.name as officeName,"
+ " g.id as id, g.external_id as externalId, g.name as name , s.display_name as staffName , pg.name as"
+ " parentName , g.staff_id as staffId , g.hierarchy as hierarchy from m_group g join m_office o on o.id = g.office_id left join "
+ "m_staff s on s.id = g.staff_id left join m_group pg on pg.id = g.parent_id";
+ " parentName , g.staff_id as staffId , g.hierarchy as hierarchy from m_group g join m_office o on o.id = g.office_id left join"
+ " m_staff s on s.id = g.staff_id left join m_group pg on pg.id = g.parent_id";
}
@Override
@@ -337,32 +351,31 @@ public class GroupReadPlatformServiceImpl implements GroupReadPlatformService {
final String externalId = rs.getString("externalId");
final Long officeId = rs.getLong("officeId");
final String officeName = rs.getString("officeName");
final Long groupLevel = rs.getLong("groupLevel");
final Long parentId = JdbcSupport.getLong(rs, "parentId");
final String parentName = rs.getString("parentName");
final Long staffId = JdbcSupport.getLong(rs, "staffId");
final String staffName = rs.getString("staffName");
final String hierarchy = rs.getString("hierarchy");
return new GroupData(id, officeId, officeName, name, externalId, groupLevel, parentId, parentName, staffId, staffName,
return new GroupData(id, officeId, officeName, name, externalId, parentId, parentName, staffId, staffName,
hierarchy);
}
}
private static final class GroupLookupMapper implements RowMapper<GroupLookupData> {
private static final class GroupLookupMapper implements RowMapper<GroupLookup> {
public String groupLookupSchema() {
return "g.id as id, g.name as name ";
}
@Override
public GroupLookupData mapRow(final ResultSet rs, @SuppressWarnings("unused") final int rowNum) throws SQLException {
public GroupLookup mapRow(final ResultSet rs, @SuppressWarnings("unused") final int rowNum) throws SQLException {
final Long id = rs.getLong("id");
final String name = rs.getString("name");
return new GroupLookupData(id, name);
return new GroupLookup(id, name);
}
}
@@ -73,6 +73,8 @@ public class GroupWritePlatformServiceJpaRepositoryImpl implements GroupWritePla
@Transactional
@Override
public CommandProcessingResult createGroup(final JsonCommand command) {
GroupLevel groupLevel = null;
try {
this.context.authenticatedUser();
@@ -84,7 +86,7 @@ public class GroupWritePlatformServiceJpaRepositoryImpl implements GroupWritePla
final Long parentId = command.longValueOfParameterNamed("parentId");
Long officeId = null;
GroupLevel groupLevel = null;
Group parentGroup = null;
if (parentId == null) {
@@ -152,7 +154,7 @@ public class GroupWritePlatformServiceJpaRepositoryImpl implements GroupWritePla
.build();
} catch (final DataIntegrityViolationException dve) {
handleGroupDataIntegrityIssues(command, dve);
handleGroupDataIntegrityIssues(command, dve , groupLevel );
return new CommandProcessingResult(Long.valueOf(-1));
}
}
@@ -161,6 +163,8 @@ public class GroupWritePlatformServiceJpaRepositoryImpl implements GroupWritePla
@Override
public CommandProcessingResult updateGroup(final Long grouptId, final JsonCommand command) {
GroupLevel groupLevel = null;
try {
this.context.authenticatedUser();
@@ -197,12 +201,16 @@ public class GroupWritePlatformServiceJpaRepositoryImpl implements GroupWritePla
if (command.isChangeInLongParameterNamed(staffIdParamName, presentStaffId)) {
final Long newValue = command.longValueOfParameterNamed(staffIdParamName);
actualChanges.put(staffIdParamName, newValue);
final Staff newStaff = this.staffRepository.findByOffice(newValue, officeId);
if (newStaff == null) { throw new StaffNotFoundException(newValue); }
Staff newStaff = null;
if(newValue != null){
newStaff = this.staffRepository.findByOffice(newValue, officeId);
if (newStaff == null) { throw new StaffNotFoundException(newValue); }
}
groupForUpdate.setStaff(newStaff);
}
final GroupLevel groupLevel = this.groupLevelRepository.findOne(groupForUpdate.getGroupLevel().getId());
groupLevel = this.groupLevelRepository.findOne(groupForUpdate.getGroupLevel().getId());
/*
* Ignoring parentId param, if group for update is super parent.
@@ -216,27 +224,28 @@ public class GroupWritePlatformServiceJpaRepositoryImpl implements GroupWritePla
final Long newValue = command.longValueOfParameterNamed(parentIdParamName);
actualChanges.put(parentIdParamName, newValue);
final Group newParentGroup = this.groupRepository.findOne(newValue);
Group newParentGroup = null;
if (newValue != null) {
newParentGroup = this.groupRepository.findOne(newValue);
if (newParentGroup == null || newParentGroup.isDeleted()) { throw new StaffNotFoundException(newValue); }
if (newParentGroup == null || newParentGroup.isDeleted()) { throw new StaffNotFoundException(newValue); }
if (!newParentGroup.isOfficeIdentifiedBy(officeId)) {
final String errorMessage = "Group and parent group must have the same office";
throw new InvalidOfficeException("group", "attach.to.parent.group", errorMessage);
}
/*
* If Group is not super parent then validate group
* level's parent level is same as group parent's level
* this check makes sure new group is added at immediate
* next level in hierarchy
*/
if (!newParentGroup.isOfficeIdentifiedBy(officeId)) {
final String errorMessage = "Group and parent group must have the same office";
throw new InvalidOfficeException("group", "attach.to.parent.group", errorMessage);
if (!groupForUpdate.getGroupLevel().isIdentifiedByParentId(newParentGroup.getGroupLevel().getId())) {
final String errorMessage = "Parent group's level is not equal to child level's parent level ";
throw new InvalidGroupLevelException("add", "invalid.level", errorMessage);
}
}
/*
* If Group is not super parent then validate group level's
* parent level is same as group parent's level this check
* makes sure new group is added at immediate next level in
* hierarchy
*/
if (!groupForUpdate.getGroupLevel().isIdentifiedByParentId(newParentGroup.getGroupLevel().getId())) {
final String errorMessage = "Parent group's level is not equal to child level's parent level ";
throw new InvalidGroupLevelException("add", "invalid.level", errorMessage);
}
groupForUpdate.setParent(newParentGroup);
}
}
@@ -262,7 +271,7 @@ public class GroupWritePlatformServiceJpaRepositoryImpl implements GroupWritePla
.build();
} catch (final DataIntegrityViolationException dve) {
handleGroupDataIntegrityIssues(command, dve);
handleGroupDataIntegrityIssues(command, dve , groupLevel);
return new CommandProcessingResult(Long.valueOf(-1));
}
}
@@ -378,14 +387,27 @@ public class GroupWritePlatformServiceJpaRepositoryImpl implements GroupWritePla
* Guaranteed to throw an exception no matter what the data integrity issue
* is.
*/
private void handleGroupDataIntegrityIssues(final JsonCommand command, final DataIntegrityViolationException dve) {
private void handleGroupDataIntegrityIssues(final JsonCommand command, final DataIntegrityViolationException dve, GroupLevel groupLevel) {
final Throwable realCause = dve.getMostSpecificCause();
String errorMessageForUser = null;
String errorMessageForMachine = null;
if (realCause.getMessage().contains("external_id")) {
throw new PlatformDataIntegrityException("error.msg.group.duplicate.externalId", "Group with externalId {0} already exists",
"externalId", command.stringValueOfParameterNamed("externalId"));
} else if (realCause.getMessage().contains("name")) { throw new PlatformDataIntegrityException("error.msg.group.duplicate.name",
"Group with name {0} already exists", "name", command.stringValueOfParameterNamed("name")); }
errorMessageForUser = groupLevel.getLevelName() + " with externalId {0} already exists";
errorMessageForMachine = "error.msg." + groupLevel.getLevelName().toLowerCase() + ".duplicate.externalId";
throw new PlatformDataIntegrityException(errorMessageForMachine, errorMessageForUser, "externalId",
command.stringValueOfParameterNamed("externalId"));
} else if (realCause.getMessage().contains("name")) {
errorMessageForUser = groupLevel.getLevelName() + " with name {0} already exists";
errorMessageForMachine = "error.msg." + groupLevel.getLevelName().toLowerCase() + ".duplicate.name";
throw new PlatformDataIntegrityException(errorMessageForMachine, errorMessageForUser, "name",
command.stringValueOfParameterNamed("name"));
}
logger.error(dve.getMessage(), dve);
throw new PlatformDataIntegrityException("error.msg.group.unknown.data.integrity.issue",