wip: group and center API changes

This commit is contained in:
ashok-conflux
2013-04-16 20:28:05 +05:30
parent 436f77c3a8
commit a6e655761f
5 changed files with 74 additions and 24 deletions
@@ -109,6 +109,7 @@ public class CentersApiResource {
public String retrieveOne(@Context final UriInfo uriInfo, @PathParam("centerId") final Long centerId) { public String retrieveOne(@Context final UriInfo uriInfo, @PathParam("centerId") final Long centerId) {
this.context.authenticatedUser().validateHasReadPermission(GroupingTypesApiConstants.CENTER_RESOURCE_NAME); this.context.authenticatedUser().validateHasReadPermission(GroupingTypesApiConstants.CENTER_RESOURCE_NAME);
final Set<String> associationParameters = ApiParameterHelper.extractAssociationsForResponseIfProvided(uriInfo.getQueryParameters());
CenterData center = this.centerReadPlatformService.retrieveOne(centerId); CenterData center = this.centerReadPlatformService.retrieveOne(centerId);
@@ -118,6 +119,12 @@ public class CentersApiResource {
center = CenterData.withTemplate(templateCenter, center); center = CenterData.withTemplate(templateCenter, center);
} }
if (!associationParameters.isEmpty()) {
if (associationParameters.contains("groups")) {
center = CenterData.setGroups(center, this.centerReadPlatformService.retriveAssociatedGroups(centerId));
}
}
final ApiRequestJsonSerializationSettings settings = this.apiRequestParameterHelper.process(uriInfo.getQueryParameters()); final ApiRequestJsonSerializationSettings settings = this.apiRequestParameterHelper.process(uriInfo.getQueryParameters());
return this.centerApiJsonSerializer.serialize(settings, center, GroupingTypesApiConstants.CENTER_RESPONSE_DATA_PARAMETERS); return this.centerApiJsonSerializer.serialize(settings, center, GroupingTypesApiConstants.CENTER_RESPONSE_DATA_PARAMETERS);
} }
@@ -34,12 +34,12 @@ public class CenterData {
public static CenterData template(final Long officeId, final Collection<OfficeData> officeOptions, public static CenterData template(final Long officeId, final Collection<OfficeData> officeOptions,
final Collection<StaffData> staffOptions) { final Collection<StaffData> staffOptions) {
return new CenterData(null, null, null, officeId, null, null, null, null, officeOptions, staffOptions); return new CenterData(null, null, null, officeId, null, null, null, null, officeOptions, staffOptions, null);
} }
public static CenterData withTemplate(final CenterData templateCenter, final CenterData center) { public static CenterData withTemplate(final CenterData templateCenter, final CenterData center) {
return new CenterData(center.id, center.name, center.externalId, center.officeId, center.officeName, center.staffId, return new CenterData(center.id, center.name, center.externalId, center.officeId, center.officeName, center.staffId,
center.staffName, center.hierarchy, templateCenter.officeOptions, templateCenter.staffOptions); center.staffName, center.hierarchy, templateCenter.officeOptions, templateCenter.staffOptions, null);
} }
public static CenterData instance(final Long id, final String name, final String externalId, final Long officeId, public static CenterData instance(final Long id, final String name, final String externalId, final Long officeId,
@@ -48,12 +48,16 @@ public class CenterData {
final Collection<OfficeData> officeOptions = null; final Collection<OfficeData> officeOptions = null;
final Collection<StaffData> staffOptions = null; final Collection<StaffData> staffOptions = null;
return new CenterData(id, name, externalId, officeId, officeName, staffId, staffName, hierarchy, officeOptions, staffOptions); return new CenterData(id, name, externalId, officeId, officeName, staffId, staffName, hierarchy, officeOptions, staffOptions, null);
}
public static CenterData setGroups(final CenterData centerData, final Collection<GroupGeneralData> groups){
return new CenterData(centerData.id, centerData.name, centerData.externalId, centerData.officeId, centerData.officeName, centerData.staffId, centerData.staffName, centerData.hierarchy, centerData.officeOptions, centerData.staffOptions, groups);
} }
private CenterData(final Long id, final String name, final String externalId, final Long officeId, final String officeName, private CenterData(final Long id, final String name, final String externalId, final Long officeId, final String officeName,
final Long staffId, final String staffName, final String hierarchy, final Collection<OfficeData> officeOptions, final Long staffId, final String staffName, final String hierarchy, final Collection<OfficeData> officeOptions,
final Collection<StaffData> staffOptions) { final Collection<StaffData> staffOptions, final Collection<GroupGeneralData> groups) {
this.id = id; this.id = id;
this.name = name; this.name = name;
this.externalId = externalId; this.externalId = externalId;
@@ -65,7 +69,7 @@ public class CenterData {
this.officeOptions = officeOptions; this.officeOptions = officeOptions;
this.staffOptions = staffOptions; this.staffOptions = staffOptions;
this.groups = null; this.groups = groups;
} }
public Long officeId() { public Long officeId() {
@@ -21,4 +21,6 @@ public interface CenterReadPlatformService {
Collection<CenterData> retrieveAll(SearchParameters searchCriteria); Collection<CenterData> retrieveAll(SearchParameters searchCriteria);
GroupGeneralData retrieveCenterGroupTemplate(Long centerId); GroupGeneralData retrieveCenterGroupTemplate(Long centerId);
Collection<GroupGeneralData> retriveAssociatedGroups(Long centerId);
} }
@@ -42,6 +42,7 @@ public class CenterReadPlatformServiceImpl implements CenterReadPlatformService
private final OfficeReadPlatformService officeReadPlatformService; private final OfficeReadPlatformService officeReadPlatformService;
private final StaffReadPlatformService staffReadPlatformService; private final StaffReadPlatformService staffReadPlatformService;
private final CenterDataMapper centerMapper = new CenterDataMapper(); private final CenterDataMapper centerMapper = new CenterDataMapper();
private final GroupDataMapper groupDataMapper = new GroupDataMapper();
@Autowired @Autowired
public CenterReadPlatformServiceImpl(final PlatformSecurityContext context, final TenantAwareRoutingDataSource dataSource, public CenterReadPlatformServiceImpl(final PlatformSecurityContext context, final TenantAwareRoutingDataSource dataSource,
@@ -95,22 +96,21 @@ public class CenterReadPlatformServiceImpl implements CenterReadPlatformService
return extraCriteria; return extraCriteria;
} }
private static final String sqlQuery = "g.id as id, g.external_id as externalId, g.name as name, " +
"g.office_id as officeId, o.name as officeName, " +
"g.staff_id as staffId, s.display_name as staffName, " +
"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 ";
private static final class CenterDataMapper implements RowMapper<CenterData> { private static final class CenterDataMapper implements RowMapper<CenterData> {
private final String schemaSql; private final String schemaSql;
public CenterDataMapper() { public CenterDataMapper() {
final StringBuilder sqlBuilder = new StringBuilder(400);
sqlBuilder.append("g.id as id, g.external_id as externalId, g.name as name, "); this.schemaSql = sqlQuery;
sqlBuilder.append("g.office_id as officeId, o.name as officeName, ");
sqlBuilder.append("g.staff_id as staffId, s.display_name as staffName, ");
sqlBuilder.append("g.hierarchy as hierarchy ");
sqlBuilder.append("from m_group g ");
sqlBuilder.append("join m_office o on o.id = g.office_id ");
sqlBuilder.append("left join m_staff s on s.id = g.staff_id ");
this.schemaSql = sqlBuilder.toString();
} }
public String schema() { public String schema() {
@@ -133,6 +133,35 @@ public class CenterReadPlatformServiceImpl implements CenterReadPlatformService
} }
} }
private static final class GroupDataMapper implements RowMapper<GroupGeneralData> {
private final String schemaSql;
public GroupDataMapper() {
this.schemaSql = sqlQuery;
}
public String schema() {
return this.schemaSql;
}
@Override
public GroupGeneralData mapRow(final ResultSet rs, @SuppressWarnings("unused") final int rowNum) throws SQLException {
final Long id = rs.getLong("id");
final String name = rs.getString("name");
final String externalId = rs.getString("externalId");
final Long officeId = rs.getLong("officeId");
final String officeName = rs.getString("officeName");
final Long staffId = JdbcSupport.getLong(rs, "staffId");
final String staffName = rs.getString("staffName");
final String hierarchy = rs.getString("hierarchy");
return GroupGeneralData.instance(id, name, externalId, officeId, officeName, null, null, staffId, staffName, hierarchy);
}
}
@Override @Override
public Collection<CenterData> retrieveAll(final SearchParameters searchCriteria) { public Collection<CenterData> retrieveAll(final SearchParameters searchCriteria) {
@@ -231,4 +260,10 @@ public class CenterReadPlatformServiceImpl implements CenterReadPlatformService
return GroupGeneralData.template(centerOfficeId, center.getId(), center.getName(), staffId, staffName, centerOptions, return GroupGeneralData.template(centerOfficeId, center.getId(), center.getName(), staffId, staffName, centerOptions,
officeOptions, staffOptions, clientOptions); officeOptions, staffOptions, clientOptions);
} }
@Override
public Collection<GroupGeneralData> retriveAssociatedGroups(Long centerId) {
String sql = "select " + this.groupDataMapper.schema() + " where g.parent_id = ? ";
return this.jdbcTemplate.query(sql, groupDataMapper, new Object[] { centerId });
}
} }
@@ -115,13 +115,14 @@ public class GroupWritePlatformServiceJpaRepositoryImpl implements GroupWritePla
clientMembers = assembleSetOfClients(officeId, command); clientMembers = assembleSetOfClients(officeId, command);
} }
Set<Group> childGroups = null; //TODO -AA: Not sure about children are added at the time of group creation
/*Set<Group> childGroups = null;
if (groupLevel.isRecursable()) { if (groupLevel.isRecursable()) {
childGroups = assembleSetOfChildGroups(officeId, command); childGroups = assembleSetOfChildGroups(officeId, command);
} }*/
final Group newGroup = Group final Group newGroup = Group
.newGroup(groupOffice, staff, parentGroup, groupLevel, name, externalId, clientMembers, childGroups); .newGroup(groupOffice, staff, parentGroup, groupLevel, name, externalId, clientMembers, null);
// pre-save to generate id for use in group hierarchy // pre-save to generate id for use in group hierarchy
this.groupRepository.save(newGroup); this.groupRepository.save(newGroup);
@@ -261,7 +262,8 @@ public class GroupWritePlatformServiceJpaRepositoryImpl implements GroupWritePla
return new CommandProcessingResultBuilder() // return new CommandProcessingResultBuilder() //
.withCommandId(command.commandId()) // .withCommandId(command.commandId()) //
.withOfficeId(groupForUpdate.getId()) // .withOfficeId(groupForUpdate.getId()) //
.withGroupId(groupForUpdate.getOfficeId()) // //apart from group context group id does not make much sense
.withGroupId(groupForUpdate.getId())
.withEntityId(groupForUpdate.getId()) // .withEntityId(groupForUpdate.getId()) //
.with(actualChanges) // .with(actualChanges) //
.build(); .build();
@@ -358,7 +360,7 @@ public class GroupWritePlatformServiceJpaRepositoryImpl implements GroupWritePla
return clientMembers; return clientMembers;
} }
private Set<Group> assembleSetOfChildGroups(final Long officeId, final JsonCommand command) { /*private Set<Group> assembleSetOfChildGroups(final Long officeId, final JsonCommand command) {
final Set<Group> childGroups = new HashSet<Group>(); final Set<Group> childGroups = new HashSet<Group>();
final String[] childGroupsArray = command.arrayValueOfParameterNamed("childGroups"); final String[] childGroupsArray = command.arrayValueOfParameterNamed("childGroups");
@@ -377,7 +379,7 @@ public class GroupWritePlatformServiceJpaRepositoryImpl implements GroupWritePla
} }
return childGroups; return childGroups;
} }*/
private String[] getClientIds(final Set<Client> clients) { private String[] getClientIds(final Set<Client> clients) {