Staff CRUD services : MIFOSX-52
This commit is contained in:
committed by
Keith Woodlock
parent
d56c86c9af
commit
85df179c9b
@@ -0,0 +1,111 @@
|
|||||||
|
package org.mifosng.platform.api;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import javax.ws.rs.Consumes;
|
||||||
|
import javax.ws.rs.GET;
|
||||||
|
import javax.ws.rs.POST;
|
||||||
|
import javax.ws.rs.PUT;
|
||||||
|
import javax.ws.rs.Path;
|
||||||
|
import javax.ws.rs.PathParam;
|
||||||
|
import javax.ws.rs.Produces;
|
||||||
|
import javax.ws.rs.core.Context;
|
||||||
|
import javax.ws.rs.core.MediaType;
|
||||||
|
import javax.ws.rs.core.Response;
|
||||||
|
import javax.ws.rs.core.UriInfo;
|
||||||
|
|
||||||
|
import org.mifosng.platform.api.commands.StaffCommand;
|
||||||
|
import org.mifosng.platform.api.data.EntityIdentifier;
|
||||||
|
import org.mifosng.platform.api.data.StaffData;
|
||||||
|
import org.mifosng.platform.api.infrastructure.ApiDataConversionService;
|
||||||
|
import org.mifosng.platform.api.infrastructure.ApiJsonSerializerService;
|
||||||
|
import org.mifosng.platform.api.infrastructure.ApiParameterHelper;
|
||||||
|
import org.mifosng.platform.staff.service.StaffReadPlatformService;
|
||||||
|
import org.mifosng.platform.staff.service.StaffWritePlatformService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.Scope;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Path("/staff")
|
||||||
|
@Component
|
||||||
|
@Scope("singleton")
|
||||||
|
public class StaffApiResource {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private StaffReadPlatformService readPlatformService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private StaffWritePlatformService writePlatformService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ApiDataConversionService apiDataConversionService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ApiJsonSerializerService apiJsonSerializerService;
|
||||||
|
|
||||||
|
@GET
|
||||||
|
@Consumes({ MediaType.APPLICATION_JSON })
|
||||||
|
@Produces({ MediaType.APPLICATION_JSON })
|
||||||
|
public String retrieveStaffs(@Context final UriInfo uriInfo) {
|
||||||
|
|
||||||
|
Set<String> responseParameters = ApiParameterHelper
|
||||||
|
.extractFieldsForResponseIfProvided(uriInfo
|
||||||
|
.getQueryParameters());
|
||||||
|
boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo
|
||||||
|
.getQueryParameters());
|
||||||
|
|
||||||
|
final Collection<StaffData> staff = this.readPlatformService
|
||||||
|
.retrieveAllStaff();
|
||||||
|
|
||||||
|
return this.apiJsonSerializerService.serializeStaffDataToJson(
|
||||||
|
prettyPrint, responseParameters, staff);
|
||||||
|
}
|
||||||
|
|
||||||
|
@POST
|
||||||
|
@Consumes({ MediaType.APPLICATION_JSON })
|
||||||
|
@Produces({ MediaType.APPLICATION_JSON })
|
||||||
|
public Response createStaff(final String jsonRequestBody) {
|
||||||
|
|
||||||
|
final StaffCommand command = this.apiDataConversionService
|
||||||
|
.convertJsonToStaffCommand(null, jsonRequestBody);
|
||||||
|
|
||||||
|
final Long staffId = this.writePlatformService.createStaff(command);
|
||||||
|
|
||||||
|
return Response.ok().entity(new EntityIdentifier(staffId)).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GET
|
||||||
|
@Path("{staffId}")
|
||||||
|
@Consumes({ MediaType.APPLICATION_JSON })
|
||||||
|
@Produces({ MediaType.APPLICATION_JSON })
|
||||||
|
public String retreiveStaff(@PathParam("staffId") final Long staffId,
|
||||||
|
@Context final UriInfo uriInfo) {
|
||||||
|
|
||||||
|
final Set<String> responseParameters = ApiParameterHelper
|
||||||
|
.extractFieldsForResponseIfProvided(uriInfo
|
||||||
|
.getQueryParameters());
|
||||||
|
final boolean prettyPrint = ApiParameterHelper.prettyPrint(uriInfo
|
||||||
|
.getQueryParameters());
|
||||||
|
|
||||||
|
final StaffData staff = this.readPlatformService.retrieveStaff(staffId);
|
||||||
|
|
||||||
|
return this.apiJsonSerializerService.serializeStaffDataToJson(
|
||||||
|
prettyPrint, responseParameters, staff);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PUT
|
||||||
|
@Path("{staffId}")
|
||||||
|
@Consumes({ MediaType.APPLICATION_JSON })
|
||||||
|
@Produces({ MediaType.APPLICATION_JSON })
|
||||||
|
public Response updateStaff(@PathParam("staffId") final Long staffId,
|
||||||
|
final String jsonRequestBody) {
|
||||||
|
|
||||||
|
final StaffCommand command = this.apiDataConversionService
|
||||||
|
.convertJsonToStaffCommand(staffId, jsonRequestBody);
|
||||||
|
|
||||||
|
final Long entityId = this.writePlatformService.updateStaff(command);
|
||||||
|
|
||||||
|
return Response.ok().entity(new EntityIdentifier(entityId)).build();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
package org.mifosng.platform.api.commands;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Immutable command for creating or updating details of a staff member.
|
||||||
|
*/
|
||||||
|
public class StaffCommand {
|
||||||
|
|
||||||
|
private final Long id;
|
||||||
|
private final String firstName;
|
||||||
|
private final String lastName;
|
||||||
|
|
||||||
|
private final Set<String> modifiedParameters;
|
||||||
|
|
||||||
|
public StaffCommand(final Set<String> modifiedParameters, final Long id,
|
||||||
|
final String firstName, final String lastName) {
|
||||||
|
this.modifiedParameters = modifiedParameters;
|
||||||
|
this.id = id;
|
||||||
|
this.firstName = firstName;
|
||||||
|
this.lastName = lastName;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFirstName() {
|
||||||
|
return firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLastName() {
|
||||||
|
return lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isFirstNameChanged() {
|
||||||
|
return this.modifiedParameters.contains("firstname");
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isLastNameChanged() {
|
||||||
|
return this.modifiedParameters.contains("lastname");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package org.mifosng.platform.api.data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
public class StaffData implements Serializable {
|
||||||
|
|
||||||
|
private Long id;
|
||||||
|
private String firstname;
|
||||||
|
private String lastname;
|
||||||
|
private String displayName;
|
||||||
|
|
||||||
|
public StaffData() {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
public StaffData(final Long id, final String firstname,
|
||||||
|
final String lastname,final String displayName) {
|
||||||
|
this.id = id;
|
||||||
|
this.firstname = firstname;
|
||||||
|
this.lastname = lastname;
|
||||||
|
this.displayName= displayName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFirstname() {
|
||||||
|
return firstname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFirstname(String firstname) {
|
||||||
|
this.firstname = firstname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLastname() {
|
||||||
|
return lastname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLastname(String lastname) {
|
||||||
|
this.lastname = lastname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDisplayName() {
|
||||||
|
return displayName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDisplayName(String displayName) {
|
||||||
|
this.displayName = displayName;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
+3
@@ -18,6 +18,7 @@ import org.mifosng.platform.api.commands.OfficeCommand;
|
|||||||
import org.mifosng.platform.api.commands.OrganisationCurrencyCommand;
|
import org.mifosng.platform.api.commands.OrganisationCurrencyCommand;
|
||||||
import org.mifosng.platform.api.commands.RoleCommand;
|
import org.mifosng.platform.api.commands.RoleCommand;
|
||||||
import org.mifosng.platform.api.commands.SavingProductCommand;
|
import org.mifosng.platform.api.commands.SavingProductCommand;
|
||||||
|
import org.mifosng.platform.api.commands.StaffCommand;
|
||||||
import org.mifosng.platform.api.commands.SubmitLoanApplicationCommand;
|
import org.mifosng.platform.api.commands.SubmitLoanApplicationCommand;
|
||||||
import org.mifosng.platform.api.commands.UserCommand;
|
import org.mifosng.platform.api.commands.UserCommand;
|
||||||
|
|
||||||
@@ -63,4 +64,6 @@ public interface ApiDataConversionService {
|
|||||||
DepositStateTransitionCommand convertJsonToDepositStateTransitionCommand(Long resourceIdentifier, String jsonRequestBody);
|
DepositStateTransitionCommand convertJsonToDepositStateTransitionCommand(Long resourceIdentifier, String jsonRequestBody);
|
||||||
|
|
||||||
DepositStateTransitionApprovalCommand convertJsonToDepositStateTransitionApprovalCommand(Long resourceIdentifier, String jsonRequestBody);
|
DepositStateTransitionApprovalCommand convertJsonToDepositStateTransitionApprovalCommand(Long resourceIdentifier, String jsonRequestBody);
|
||||||
|
|
||||||
|
StaffCommand convertJsonToStaffCommand(Long resourceIdentifier, String json);
|
||||||
}
|
}
|
||||||
+29
@@ -35,6 +35,7 @@ import org.mifosng.platform.api.commands.OfficeCommand;
|
|||||||
import org.mifosng.platform.api.commands.OrganisationCurrencyCommand;
|
import org.mifosng.platform.api.commands.OrganisationCurrencyCommand;
|
||||||
import org.mifosng.platform.api.commands.RoleCommand;
|
import org.mifosng.platform.api.commands.RoleCommand;
|
||||||
import org.mifosng.platform.api.commands.SavingProductCommand;
|
import org.mifosng.platform.api.commands.SavingProductCommand;
|
||||||
|
import org.mifosng.platform.api.commands.StaffCommand;
|
||||||
import org.mifosng.platform.api.commands.SubmitLoanApplicationCommand;
|
import org.mifosng.platform.api.commands.SubmitLoanApplicationCommand;
|
||||||
import org.mifosng.platform.api.commands.UserCommand;
|
import org.mifosng.platform.api.commands.UserCommand;
|
||||||
import org.mifosng.platform.api.data.ApiParameterError;
|
import org.mifosng.platform.api.data.ApiParameterError;
|
||||||
@@ -119,6 +120,32 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService {
|
|||||||
return new FundCommand(modifiedParameters, resourceIdentifier, name, externalId);
|
return new FundCommand(modifiedParameters, resourceIdentifier, name, externalId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public StaffCommand convertJsonToStaffCommand(Long resourceIdentifier,
|
||||||
|
String json) {
|
||||||
|
|
||||||
|
if (StringUtils.isBlank(json)) {
|
||||||
|
throw new InvalidJsonException();
|
||||||
|
}
|
||||||
|
|
||||||
|
Type typeOfMap = new TypeToken<Map<String, String>>(){}.getType();
|
||||||
|
Map<String, String> requestMap = gsonConverter.fromJson(json, typeOfMap);
|
||||||
|
|
||||||
|
|
||||||
|
Set<String> supportedParams = new HashSet<String>(
|
||||||
|
Arrays.asList("firstname", "lastname")
|
||||||
|
);
|
||||||
|
|
||||||
|
checkForUnsupportedParameters(requestMap, supportedParams);
|
||||||
|
|
||||||
|
Set<String> modifiedParameters = new HashSet<String>();
|
||||||
|
|
||||||
|
String firstname = extractStringParameter("firstname", requestMap, modifiedParameters);
|
||||||
|
String lastname = extractStringParameter("lastname", requestMap, modifiedParameters);
|
||||||
|
|
||||||
|
return new StaffCommand(modifiedParameters, resourceIdentifier, firstname, lastname);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public OfficeCommand convertJsonToOfficeCommand(final Long resourceIdentifier, final String json) {
|
public OfficeCommand convertJsonToOfficeCommand(final Long resourceIdentifier, final String json) {
|
||||||
if (StringUtils.isBlank(json)) {
|
if (StringUtils.isBlank(json)) {
|
||||||
@@ -1074,4 +1101,6 @@ public class ApiDataConversionServiceImpl implements ApiDataConversionService {
|
|||||||
|
|
||||||
return new DepositStateTransitionApprovalCommand(resourceIdentifier, productId, eventDate, tenureInMonths, depositAmount, interestCompoundedEveryPeriodType,interestCompoundedEvery);
|
return new DepositStateTransitionApprovalCommand(resourceIdentifier, productId, eventDate, tenureInMonths, depositAmount, interestCompoundedEveryPeriodType,interestCompoundedEvery);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
+5
@@ -25,6 +25,7 @@ import org.mifosng.platform.api.data.OfficeTransactionData;
|
|||||||
import org.mifosng.platform.api.data.PermissionData;
|
import org.mifosng.platform.api.data.PermissionData;
|
||||||
import org.mifosng.platform.api.data.RoleData;
|
import org.mifosng.platform.api.data.RoleData;
|
||||||
import org.mifosng.platform.api.data.SavingProductData;
|
import org.mifosng.platform.api.data.SavingProductData;
|
||||||
|
import org.mifosng.platform.api.data.StaffData;
|
||||||
|
|
||||||
public interface ApiJsonSerializerService {
|
public interface ApiJsonSerializerService {
|
||||||
|
|
||||||
@@ -97,4 +98,8 @@ public interface ApiJsonSerializerService {
|
|||||||
String serializeChargeDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<ChargeData> charges);
|
String serializeChargeDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<ChargeData> charges);
|
||||||
|
|
||||||
String serializeChargeDataToJson(boolean prettyPrint, Set<String> responseParameters, ChargeData charge);
|
String serializeChargeDataToJson(boolean prettyPrint, Set<String> responseParameters, ChargeData charge);
|
||||||
|
|
||||||
|
String serializeStaffDataToJson(boolean prettyPrint, Set<String> responseParameters, StaffData staff);
|
||||||
|
|
||||||
|
String serializeStaffDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<StaffData> staff);
|
||||||
}
|
}
|
||||||
+22
-1
@@ -27,6 +27,7 @@ import org.mifosng.platform.api.data.OfficeTransactionData;
|
|||||||
import org.mifosng.platform.api.data.PermissionData;
|
import org.mifosng.platform.api.data.PermissionData;
|
||||||
import org.mifosng.platform.api.data.RoleData;
|
import org.mifosng.platform.api.data.RoleData;
|
||||||
import org.mifosng.platform.api.data.SavingProductData;
|
import org.mifosng.platform.api.data.SavingProductData;
|
||||||
|
import org.mifosng.platform.api.data.StaffData;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@@ -62,7 +63,8 @@ public class GoogleGsonApiJsonSerializerService implements
|
|||||||
Arrays.asList("selectedCurrencyOptions", "currencyOptions"));
|
Arrays.asList("selectedCurrencyOptions", "currencyOptions"));
|
||||||
private static final Set<String> FUND_DATA_PARAMETERS = new HashSet<String>(
|
private static final Set<String> FUND_DATA_PARAMETERS = new HashSet<String>(
|
||||||
Arrays.asList("id", "name", "externalId"));
|
Arrays.asList("id", "name", "externalId"));
|
||||||
|
private static final Set<String> STAFF_DATA_PARAMETERS = new HashSet<String>(
|
||||||
|
Arrays.asList("id", "firstname", "lastname","displayName"));
|
||||||
private static final Set<String> LOAN_PRODUCT_DATA_PARAMETERS = new HashSet<String>(
|
private static final Set<String> LOAN_PRODUCT_DATA_PARAMETERS = new HashSet<String>(
|
||||||
Arrays.asList("id", "name", "description", "fundId", "fundName",
|
Arrays.asList("id", "name", "description", "fundId", "fundName",
|
||||||
"transactionProcessingStrategyId",
|
"transactionProcessingStrategyId",
|
||||||
@@ -535,4 +537,23 @@ public class GoogleGsonApiJsonSerializerService implements
|
|||||||
responseParameters);
|
responseParameters);
|
||||||
return helper.serializedJsonFrom(gsonDeserializer, charge);
|
return helper.serializedJsonFrom(gsonDeserializer, charge);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String serializeStaffDataToJson(boolean prettyPrint,
|
||||||
|
Set<String> responseParameters, StaffData staff) {
|
||||||
|
final Gson gsonDeserializer = helper
|
||||||
|
.createGsonBuilderWithParameterExclusionSerializationStrategy(
|
||||||
|
STAFF_DATA_PARAMETERS, prettyPrint, responseParameters);
|
||||||
|
return helper.serializedJsonFrom(gsonDeserializer, staff);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String serializeStaffDataToJson(boolean prettyPrint,
|
||||||
|
Set<String> responseParameters, Collection<StaffData> staff) {
|
||||||
|
final Gson gsonDeserializer = helper
|
||||||
|
.createGsonBuilderWithParameterExclusionSerializationStrategy(
|
||||||
|
STAFF_DATA_PARAMETERS, prettyPrint, responseParameters);
|
||||||
|
return helper.serializedJsonFrom(gsonDeserializer,
|
||||||
|
staff.toArray(new StaffData[staff.size()]));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
package org.mifosng.platform.exceptions;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A {@link RuntimeException} thrown when staff resources are not found.
|
||||||
|
*/
|
||||||
|
public class StaffNotFoundException extends
|
||||||
|
AbstractPlatformResourceNotFoundException {
|
||||||
|
|
||||||
|
public StaffNotFoundException(final Long id) {
|
||||||
|
super("error.msg.fund.id.invalid", "Staff with identifier " + id
|
||||||
|
+ " does not exist", id);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package org.mifosng.platform.staff.domain;
|
||||||
|
|
||||||
|
import javax.persistence.Column;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.Table;
|
||||||
|
import javax.persistence.UniqueConstraint;
|
||||||
|
|
||||||
|
import org.apache.commons.lang.StringUtils;
|
||||||
|
import org.mifosng.platform.api.commands.StaffCommand;
|
||||||
|
import org.mifosng.platform.infrastructure.AbstractAuditableCustom;
|
||||||
|
import org.mifosng.platform.user.domain.AppUser;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "m_staff", uniqueConstraints = {
|
||||||
|
@UniqueConstraint(columnNames = { "display_name" }, name = "display_name")})
|
||||||
|
public class Staff extends AbstractAuditableCustom<AppUser, Long> {
|
||||||
|
|
||||||
|
@Column(name = "firstname", length = 50)
|
||||||
|
private String firstname;
|
||||||
|
|
||||||
|
@Column(name = "lastname", length = 50)
|
||||||
|
private String lastname;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
@Column(name = "display_name", length = 100)
|
||||||
|
private String displayName;
|
||||||
|
|
||||||
|
public static Staff createNew(final String firstname, final String lastname) {
|
||||||
|
return new Staff(firstname, lastname);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Staff() {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
private Staff(final String firstname, final String lastname) {
|
||||||
|
this.firstname = StringUtils.defaultIfEmpty(firstname, null);
|
||||||
|
this.lastname = StringUtils.defaultIfEmpty(lastname, null);
|
||||||
|
deriveDisplayName(firstname);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void update(final StaffCommand command) {
|
||||||
|
|
||||||
|
if (command.isFirstNameChanged()) {
|
||||||
|
this.firstname = StringUtils.defaultIfEmpty(command.getFirstName(),
|
||||||
|
null);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (command.isLastNameChanged()) {
|
||||||
|
this.lastname = StringUtils.defaultIfEmpty(command.getLastName(),
|
||||||
|
null);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (command.isLastNameChanged() || command.isFirstNameChanged()) {
|
||||||
|
deriveDisplayName(firstname);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void deriveDisplayName(final String firstname) {
|
||||||
|
if (!StringUtils.isBlank(firstname)) {
|
||||||
|
this.displayName = this.lastname + ", " + this.firstname;
|
||||||
|
} else {
|
||||||
|
this.displayName = this.lastname;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package org.mifosng.platform.staff.domain;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||||
|
|
||||||
|
public interface StaffRepository extends JpaRepository<Staff, Long>,
|
||||||
|
JpaSpecificationExecutor<Staff> {
|
||||||
|
// no added behaviour
|
||||||
|
}
|
||||||
+64
@@ -0,0 +1,64 @@
|
|||||||
|
package org.mifosng.platform.staff.service;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.mifosng.platform.DataValidatorBuilder;
|
||||||
|
import org.mifosng.platform.api.commands.StaffCommand;
|
||||||
|
import org.mifosng.platform.api.data.ApiParameterError;
|
||||||
|
import org.mifosng.platform.exceptions.PlatformApiDataValidationException;
|
||||||
|
|
||||||
|
public class StaffCommandValidator {
|
||||||
|
|
||||||
|
private final StaffCommand command;
|
||||||
|
|
||||||
|
public StaffCommandValidator(StaffCommand command) {
|
||||||
|
this.command = command;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void validateForCreate() {
|
||||||
|
|
||||||
|
List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
|
||||||
|
|
||||||
|
DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(
|
||||||
|
dataValidationErrors).resource("staff");
|
||||||
|
|
||||||
|
baseDataValidator.reset().parameter("firstname")
|
||||||
|
.value(command.getFirstName()).ignoreIfNull()
|
||||||
|
.notExceedingLengthOf(50);
|
||||||
|
baseDataValidator.reset().parameter("lastname")
|
||||||
|
.value(command.getLastName()).notBlank()
|
||||||
|
.notExceedingLengthOf(50);
|
||||||
|
|
||||||
|
if (!dataValidationErrors.isEmpty()) {
|
||||||
|
throw new PlatformApiDataValidationException(
|
||||||
|
"validation.msg.validation.errors.exist",
|
||||||
|
"Validation errors exist.", dataValidationErrors);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void validateForUpdate() {
|
||||||
|
List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();
|
||||||
|
|
||||||
|
DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(
|
||||||
|
dataValidationErrors).resource("staff");
|
||||||
|
|
||||||
|
baseDataValidator.reset().parameter("id").value(command.getId())
|
||||||
|
.notNull();
|
||||||
|
baseDataValidator.reset().parameter("firstname")
|
||||||
|
.value(command.getFirstName()).ignoreIfNull()
|
||||||
|
.notExceedingLengthOf(50);
|
||||||
|
baseDataValidator.reset().parameter("lastname")
|
||||||
|
.value(command.getFirstName()).ignoreIfNull().notBlank()
|
||||||
|
.notExceedingLengthOf(50);
|
||||||
|
|
||||||
|
baseDataValidator.reset().anyOfNotNull(command.getFirstName(),
|
||||||
|
command.getLastName());
|
||||||
|
|
||||||
|
if (!dataValidationErrors.isEmpty()) {
|
||||||
|
throw new PlatformApiDataValidationException(
|
||||||
|
"validation.msg.validation.errors.exist",
|
||||||
|
"Validation errors exist.", dataValidationErrors);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+12
@@ -0,0 +1,12 @@
|
|||||||
|
package org.mifosng.platform.staff.service;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import org.mifosng.platform.api.data.StaffData;
|
||||||
|
|
||||||
|
public interface StaffReadPlatformService {
|
||||||
|
|
||||||
|
StaffData retrieveStaff(Long staffId);
|
||||||
|
|
||||||
|
Collection<StaffData> retrieveAllStaff();
|
||||||
|
}
|
||||||
+77
@@ -0,0 +1,77 @@
|
|||||||
|
package org.mifosng.platform.staff.service;
|
||||||
|
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import org.mifosng.platform.api.data.StaffData;
|
||||||
|
import org.mifosng.platform.exceptions.StaffNotFoundException;
|
||||||
|
import org.mifosng.platform.infrastructure.TenantAwareRoutingDataSource;
|
||||||
|
import org.mifosng.platform.security.PlatformSecurityContext;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.dao.EmptyResultDataAccessException;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
import org.springframework.jdbc.core.RowMapper;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class StaffReadPlatformServiceImpl implements StaffReadPlatformService {
|
||||||
|
|
||||||
|
private final JdbcTemplate jdbcTemplate;
|
||||||
|
private final PlatformSecurityContext context;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public StaffReadPlatformServiceImpl(final PlatformSecurityContext context,
|
||||||
|
final TenantAwareRoutingDataSource dataSource) {
|
||||||
|
this.context = context;
|
||||||
|
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final class StaffMapper implements RowMapper<StaffData> {
|
||||||
|
|
||||||
|
public String schema() {
|
||||||
|
return " s.id as id, s.firstname as firstname, s.lastname as lastname, s.display_name as displayName from m_staff s ";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public StaffData mapRow(final ResultSet rs, final int rowNum)
|
||||||
|
throws SQLException {
|
||||||
|
|
||||||
|
Long id = rs.getLong("id");
|
||||||
|
String firstname = rs.getString("firstname");
|
||||||
|
String lastname = rs.getString("lastname");
|
||||||
|
String displayName = rs.getString("displayName");
|
||||||
|
|
||||||
|
return new StaffData(id, firstname, lastname,displayName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<StaffData> retrieveAllStaff() {
|
||||||
|
|
||||||
|
context.authenticatedUser();
|
||||||
|
|
||||||
|
StaffMapper rm = new StaffMapper();
|
||||||
|
String sql = "select " + rm.schema() + " order by s.lastname";
|
||||||
|
|
||||||
|
return this.jdbcTemplate.query(sql, rm, new Object[] {});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public StaffData retrieveStaff(final Long staffId) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
context.authenticatedUser();
|
||||||
|
|
||||||
|
StaffMapper rm = new StaffMapper();
|
||||||
|
String sql = "select " + rm.schema() + " where s.id = ?";
|
||||||
|
|
||||||
|
StaffData selectedStaff = this.jdbcTemplate.queryForObject(sql, rm,
|
||||||
|
new Object[] { staffId });
|
||||||
|
|
||||||
|
return selectedStaff;
|
||||||
|
} catch (EmptyResultDataAccessException e) {
|
||||||
|
throw new StaffNotFoundException(staffId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
package org.mifosng.platform.staff.service;
|
||||||
|
|
||||||
|
import org.mifosng.platform.api.commands.StaffCommand;
|
||||||
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
|
|
||||||
|
public interface StaffWritePlatformService {
|
||||||
|
|
||||||
|
@PreAuthorize(value = "hasRole('ORGANISATION_ADMINISTRATION_SUPER_USER_ROLE')")
|
||||||
|
Long createStaff(final StaffCommand command);
|
||||||
|
|
||||||
|
@PreAuthorize(value = "hasRole('ORGANISATION_ADMINISTRATION_SUPER_USER_ROLE')")
|
||||||
|
Long updateStaff(final StaffCommand command);
|
||||||
|
}
|
||||||
+108
@@ -0,0 +1,108 @@
|
|||||||
|
package org.mifosng.platform.staff.service;
|
||||||
|
|
||||||
|
import org.apache.commons.lang.StringUtils;
|
||||||
|
import org.mifosng.platform.api.commands.StaffCommand;
|
||||||
|
import org.mifosng.platform.exceptions.StaffNotFoundException;
|
||||||
|
import org.mifosng.platform.exceptions.PlatformDataIntegrityException;
|
||||||
|
import org.mifosng.platform.staff.domain.Staff;
|
||||||
|
import org.mifosng.platform.staff.domain.StaffRepository;
|
||||||
|
import org.mifosng.platform.security.PlatformSecurityContext;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.dao.DataIntegrityViolationException;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class StaffWritePlatformServiceJpaRepositoryImpl implements
|
||||||
|
StaffWritePlatformService {
|
||||||
|
|
||||||
|
private final static Logger logger = LoggerFactory
|
||||||
|
.getLogger(StaffWritePlatformServiceJpaRepositoryImpl.class);
|
||||||
|
|
||||||
|
private final PlatformSecurityContext context;
|
||||||
|
private final StaffRepository staffRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public StaffWritePlatformServiceJpaRepositoryImpl(
|
||||||
|
final PlatformSecurityContext context,
|
||||||
|
final StaffRepository staffRepository) {
|
||||||
|
this.context = context;
|
||||||
|
this.staffRepository = staffRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
@Override
|
||||||
|
public Long createStaff(final StaffCommand command) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
context.authenticatedUser();
|
||||||
|
|
||||||
|
StaffCommandValidator validator = new StaffCommandValidator(command);
|
||||||
|
validator.validateForCreate();
|
||||||
|
|
||||||
|
Staff staff = Staff.createNew(command.getFirstName(),
|
||||||
|
command.getLastName());
|
||||||
|
|
||||||
|
this.staffRepository.saveAndFlush(staff);
|
||||||
|
|
||||||
|
return staff.getId();
|
||||||
|
} catch (DataIntegrityViolationException dve) {
|
||||||
|
handleStaffDataIntegrityIssues(command, dve);
|
||||||
|
return Long.valueOf(-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
@Override
|
||||||
|
public Long updateStaff(final StaffCommand command) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
context.authenticatedUser();
|
||||||
|
|
||||||
|
StaffCommandValidator validator = new StaffCommandValidator(command);
|
||||||
|
validator.validateForUpdate();
|
||||||
|
|
||||||
|
final Long staffId = command.getId();
|
||||||
|
Staff staff = this.staffRepository.findOne(staffId);
|
||||||
|
if (staff == null) {
|
||||||
|
throw new StaffNotFoundException(staffId);
|
||||||
|
}
|
||||||
|
staff.update(command);
|
||||||
|
|
||||||
|
this.staffRepository.saveAndFlush(staff);
|
||||||
|
|
||||||
|
return staff.getId();
|
||||||
|
} catch (DataIntegrityViolationException dve) {
|
||||||
|
handleStaffDataIntegrityIssues(command, dve);
|
||||||
|
return Long.valueOf(-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Guaranteed to throw an exception no matter what the data integrity issue
|
||||||
|
* is.
|
||||||
|
*/
|
||||||
|
private void handleStaffDataIntegrityIssues(final StaffCommand command,
|
||||||
|
DataIntegrityViolationException dve) {
|
||||||
|
Throwable realCause = dve.getMostSpecificCause();
|
||||||
|
if (realCause.getMessage().contains("display_name")) {
|
||||||
|
String displayName = command.getLastName();
|
||||||
|
if (!StringUtils.isBlank(command.getFirstName())) {
|
||||||
|
displayName = command.getLastName() + ", "
|
||||||
|
+ command.getFirstName();
|
||||||
|
}
|
||||||
|
throw new PlatformDataIntegrityException(
|
||||||
|
"error.msg.staff.duplicate.displayName",
|
||||||
|
"A staff with the given display name '" + displayName
|
||||||
|
+ "' already exists", "displayName", displayName);
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.error(dve.getMessage(), dve);
|
||||||
|
throw new PlatformDataIntegrityException(
|
||||||
|
"error.msg.staff.unknown.data.integrity.issue",
|
||||||
|
"Unknown data integrity issue with resource: "
|
||||||
|
+ realCause.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user