Staff CRUD services : MIFOSX-52

This commit is contained in:
Vishwas Babu A J
2012-09-03 16:19:28 +01:00
committed by Keith Woodlock
parent d56c86c9af
commit 85df179c9b
15 changed files with 2411 additions and 1777 deletions
@@ -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;
}
}
@@ -1,66 +1,69 @@
package org.mifosng.platform.api.infrastructure;
import org.mifosng.platform.api.commands.AdjustLoanTransactionCommand;
import org.mifosng.platform.api.commands.BranchMoneyTransferCommand;
import org.mifosng.platform.api.commands.ChargeCommand;
import org.mifosng.platform.api.commands.ClientCommand;
import org.mifosng.platform.api.commands.DepositAccountCommand;
import org.mifosng.platform.api.commands.DepositProductCommand;
import org.mifosng.platform.api.commands.DepositStateTransitionApprovalCommand;
import org.mifosng.platform.api.commands.DepositStateTransitionCommand;
import org.mifosng.platform.api.commands.FundCommand;
import org.mifosng.platform.api.commands.GroupCommand;
import org.mifosng.platform.api.commands.LoanProductCommand;
import org.mifosng.platform.api.commands.LoanStateTransitionCommand;
import org.mifosng.platform.api.commands.LoanTransactionCommand;
import org.mifosng.platform.api.commands.NoteCommand;
import org.mifosng.platform.api.commands.OfficeCommand;
import org.mifosng.platform.api.commands.OrganisationCurrencyCommand;
import org.mifosng.platform.api.commands.RoleCommand;
import org.mifosng.platform.api.commands.SavingProductCommand;
import org.mifosng.platform.api.commands.SubmitLoanApplicationCommand;
import org.mifosng.platform.api.commands.UserCommand;
public interface ApiDataConversionService {
ChargeCommand convertJsonToChargeCommand(Long resourceIdentifier, String json);
FundCommand convertJsonToFundCommand(Long resourceIdentifier, String json);
OfficeCommand convertJsonToOfficeCommand(Long resourceIdentifier, String json);
RoleCommand convertJsonToRoleCommand(Long resourceIdentifier, String json);
UserCommand convertJsonToUserCommand(Long resourceIdentifier, String json);
BranchMoneyTransferCommand convertJsonToBranchMoneyTransferCommand(String jsonRequestBody);
LoanProductCommand convertJsonToLoanProductCommand(Long resourceIdentifier, String json);
SavingProductCommand convertJsonToSavingProductCommand(Long resourceIdentifier, String json);
DepositProductCommand convertJsonToDepositProductCommand(Long resourceIdentifier, String json);
ClientCommand convertJsonToClientCommand(Long resourceIdentifier, String jsonRequestBody);
GroupCommand convertJsonToGroupCommand(Long resourceIdentifier, String jsonRequestBody);
SubmitLoanApplicationCommand convertJsonToSubmitLoanApplicationCommand(String jsonRequestBody);
LoanStateTransitionCommand convertJsonToLoanStateTransitionCommand(Long resourceIdentifier, String jsonRequestBody);
LoanTransactionCommand convertJsonToLoanTransactionCommand(Long resourceIdentifier, String jsonRequestBody);
AdjustLoanTransactionCommand convertJsonToAdjustLoanTransactionCommand(
Long loanId, Long transactionId, String jsonRequestBody);
OrganisationCurrencyCommand convertJsonToOrganisationCurrencyCommand(String jsonRequestBody);
NoteCommand convertJsonToNoteCommand(Long resourceIdentifier, Long clientId, String jsonRequestBody);
DepositAccountCommand convertJsonToDepositAccountCommand(Long resourceIdentifier, String jsonRequestBody);
DepositStateTransitionCommand convertJsonToDepositStateTransitionCommand(Long resourceIdentifier, String jsonRequestBody);
DepositStateTransitionApprovalCommand convertJsonToDepositStateTransitionApprovalCommand(Long resourceIdentifier, String jsonRequestBody);
package org.mifosng.platform.api.infrastructure;
import org.mifosng.platform.api.commands.AdjustLoanTransactionCommand;
import org.mifosng.platform.api.commands.BranchMoneyTransferCommand;
import org.mifosng.platform.api.commands.ChargeCommand;
import org.mifosng.platform.api.commands.ClientCommand;
import org.mifosng.platform.api.commands.DepositAccountCommand;
import org.mifosng.platform.api.commands.DepositProductCommand;
import org.mifosng.platform.api.commands.DepositStateTransitionApprovalCommand;
import org.mifosng.platform.api.commands.DepositStateTransitionCommand;
import org.mifosng.platform.api.commands.FundCommand;
import org.mifosng.platform.api.commands.GroupCommand;
import org.mifosng.platform.api.commands.LoanProductCommand;
import org.mifosng.platform.api.commands.LoanStateTransitionCommand;
import org.mifosng.platform.api.commands.LoanTransactionCommand;
import org.mifosng.platform.api.commands.NoteCommand;
import org.mifosng.platform.api.commands.OfficeCommand;
import org.mifosng.platform.api.commands.OrganisationCurrencyCommand;
import org.mifosng.platform.api.commands.RoleCommand;
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.UserCommand;
public interface ApiDataConversionService {
ChargeCommand convertJsonToChargeCommand(Long resourceIdentifier, String json);
FundCommand convertJsonToFundCommand(Long resourceIdentifier, String json);
OfficeCommand convertJsonToOfficeCommand(Long resourceIdentifier, String json);
RoleCommand convertJsonToRoleCommand(Long resourceIdentifier, String json);
UserCommand convertJsonToUserCommand(Long resourceIdentifier, String json);
BranchMoneyTransferCommand convertJsonToBranchMoneyTransferCommand(String jsonRequestBody);
LoanProductCommand convertJsonToLoanProductCommand(Long resourceIdentifier, String json);
SavingProductCommand convertJsonToSavingProductCommand(Long resourceIdentifier, String json);
DepositProductCommand convertJsonToDepositProductCommand(Long resourceIdentifier, String json);
ClientCommand convertJsonToClientCommand(Long resourceIdentifier, String jsonRequestBody);
GroupCommand convertJsonToGroupCommand(Long resourceIdentifier, String jsonRequestBody);
SubmitLoanApplicationCommand convertJsonToSubmitLoanApplicationCommand(String jsonRequestBody);
LoanStateTransitionCommand convertJsonToLoanStateTransitionCommand(Long resourceIdentifier, String jsonRequestBody);
LoanTransactionCommand convertJsonToLoanTransactionCommand(Long resourceIdentifier, String jsonRequestBody);
AdjustLoanTransactionCommand convertJsonToAdjustLoanTransactionCommand(
Long loanId, Long transactionId, String jsonRequestBody);
OrganisationCurrencyCommand convertJsonToOrganisationCurrencyCommand(String jsonRequestBody);
NoteCommand convertJsonToNoteCommand(Long resourceIdentifier, Long clientId, String jsonRequestBody);
DepositAccountCommand convertJsonToDepositAccountCommand(Long resourceIdentifier, String jsonRequestBody);
DepositStateTransitionCommand convertJsonToDepositStateTransitionCommand(Long resourceIdentifier, String jsonRequestBody);
DepositStateTransitionApprovalCommand convertJsonToDepositStateTransitionApprovalCommand(Long resourceIdentifier, String jsonRequestBody);
StaffCommand convertJsonToStaffCommand(Long resourceIdentifier, String json);
}
@@ -1,100 +1,105 @@
package org.mifosng.platform.api.infrastructure;
import java.util.Collection;
import java.util.Set;
import org.mifosng.platform.api.data.AdditionalFieldsSetData;
import org.mifosng.platform.api.data.AppUserData;
import org.mifosng.platform.api.data.AuthenticatedUserData;
import org.mifosng.platform.api.data.ChargeData;
import org.mifosng.platform.api.data.ClientData;
import org.mifosng.platform.api.data.ClientAccountSummaryCollectionData;
import org.mifosng.platform.api.data.ConfigurationData;
import org.mifosng.platform.api.data.DepositAccountData;
import org.mifosng.platform.api.data.DepositProductData;
import org.mifosng.platform.api.data.FundData;
import org.mifosng.platform.api.data.GenericResultsetData;
import org.mifosng.platform.api.data.GroupData;
import org.mifosng.platform.api.data.LoanAccountData;
import org.mifosng.platform.api.data.LoanProductData;
import org.mifosng.platform.api.data.LoanTransactionData;
import org.mifosng.platform.api.data.NewLoanData;
import org.mifosng.platform.api.data.NoteData;
import org.mifosng.platform.api.data.OfficeData;
import org.mifosng.platform.api.data.OfficeTransactionData;
import org.mifosng.platform.api.data.PermissionData;
import org.mifosng.platform.api.data.RoleData;
import org.mifosng.platform.api.data.SavingProductData;
public interface ApiJsonSerializerService {
String serializeAuthenticatedUserDataToJson(boolean prettyPrint, AuthenticatedUserData authenticatedUserData);
String serializeGenericResultsetDataToJson(boolean prettyPrint, GenericResultsetData result);
String serializeAdditionalFieldsSetDataToJson(boolean prettyPrint, Collection<AdditionalFieldsSetData> result);
String serializePermissionDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<PermissionData> permissions);
String serializeRoleDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<RoleData> roles);
String serializeRoleDataToJson(boolean prettyPrint, Set<String> responseParameters, RoleData role);
String serializeAppUserDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<AppUserData> users);
String serializeAppUserDataToJson(boolean prettyPrint, Set<String> responseParameters, AppUserData user);
String serializeOfficeDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<OfficeData> offices);
String serializeOfficeDataToJson(boolean prettyPrint, Set<String> responseParameters, OfficeData office);
String serializeOfficeTransactionDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<OfficeTransactionData> officeTransactions);
String serializeOfficeTransactionDataToJson(boolean prettyPrint, Set<String> responseParameters, OfficeTransactionData officeTransaction);
String serializeConfigurationDataToJson(boolean prettyPrint, Set<String> responseParameters, ConfigurationData configuration);
String serializeFundDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<FundData> funds);
String serializeFundDataToJson(boolean prettyPrint, Set<String> responseParameters, FundData fund);
String serializeLoanProductDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<LoanProductData> products);
String serializeLoanProductDataToJson(boolean prettyPrint, Set<String> responseParameters, LoanProductData loanProduct);
String serializeSavingProductDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<SavingProductData> products);
String serializeSavingProductDataToJson(boolean prettyPrint, Set<String> responseParameters, SavingProductData savingProduct);
String serializeDepositProductDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<DepositProductData> products);
String serializeDepositProductDataToJson(boolean prettyPrint, Set<String> responseParameters, DepositProductData depositProduct);
String serializeDepositAccountDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<DepositAccountData> accounts);
String serializeDepositAccountDataToJson(boolean prettyPrint, Set<String> responseParameters, DepositAccountData account);
String serializeClientDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<ClientData> clients);
String serializeClientDataToJson(boolean prettyPrint, Set<String> responseParameters, ClientData clientData);
String serializeClientAccountSummaryCollectionDataToJson(boolean prettyPrint, Set<String> responseParameters, ClientAccountSummaryCollectionData clientAccount);
String serializeGroupDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<GroupData> groups);
String serializeGroupDataToJson(boolean prettyPrint, Set<String> responseParameters, GroupData group);
String serializeNoteDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<NoteData> notes);
String serializeNoteDataToJson(boolean prettyPrint, Set<String> responseParameters, NoteData note);
String serializeNewLoanDataToJson(boolean prettyPrint, Set<String> responseParameters, NewLoanData newLoan);
String serialzieLoanAccountDataToJson(boolean prettyPrint, Set<String> responseParameters, LoanAccountData loanAccount);
String serializeLoanTransactionDataToJson(boolean prettyPrint, Set<String> responseParameters, LoanTransactionData transaction);
String serializeChargeDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<ChargeData> charges);
String serializeChargeDataToJson(boolean prettyPrint, Set<String> responseParameters, ChargeData charge);
package org.mifosng.platform.api.infrastructure;
import java.util.Collection;
import java.util.Set;
import org.mifosng.platform.api.data.AdditionalFieldsSetData;
import org.mifosng.platform.api.data.AppUserData;
import org.mifosng.platform.api.data.AuthenticatedUserData;
import org.mifosng.platform.api.data.ChargeData;
import org.mifosng.platform.api.data.ClientData;
import org.mifosng.platform.api.data.ClientAccountSummaryCollectionData;
import org.mifosng.platform.api.data.ConfigurationData;
import org.mifosng.platform.api.data.DepositAccountData;
import org.mifosng.platform.api.data.DepositProductData;
import org.mifosng.platform.api.data.FundData;
import org.mifosng.platform.api.data.GenericResultsetData;
import org.mifosng.platform.api.data.GroupData;
import org.mifosng.platform.api.data.LoanAccountData;
import org.mifosng.platform.api.data.LoanProductData;
import org.mifosng.platform.api.data.LoanTransactionData;
import org.mifosng.platform.api.data.NewLoanData;
import org.mifosng.platform.api.data.NoteData;
import org.mifosng.platform.api.data.OfficeData;
import org.mifosng.platform.api.data.OfficeTransactionData;
import org.mifosng.platform.api.data.PermissionData;
import org.mifosng.platform.api.data.RoleData;
import org.mifosng.platform.api.data.SavingProductData;
import org.mifosng.platform.api.data.StaffData;
public interface ApiJsonSerializerService {
String serializeAuthenticatedUserDataToJson(boolean prettyPrint, AuthenticatedUserData authenticatedUserData);
String serializeGenericResultsetDataToJson(boolean prettyPrint, GenericResultsetData result);
String serializeAdditionalFieldsSetDataToJson(boolean prettyPrint, Collection<AdditionalFieldsSetData> result);
String serializePermissionDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<PermissionData> permissions);
String serializeRoleDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<RoleData> roles);
String serializeRoleDataToJson(boolean prettyPrint, Set<String> responseParameters, RoleData role);
String serializeAppUserDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<AppUserData> users);
String serializeAppUserDataToJson(boolean prettyPrint, Set<String> responseParameters, AppUserData user);
String serializeOfficeDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<OfficeData> offices);
String serializeOfficeDataToJson(boolean prettyPrint, Set<String> responseParameters, OfficeData office);
String serializeOfficeTransactionDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<OfficeTransactionData> officeTransactions);
String serializeOfficeTransactionDataToJson(boolean prettyPrint, Set<String> responseParameters, OfficeTransactionData officeTransaction);
String serializeConfigurationDataToJson(boolean prettyPrint, Set<String> responseParameters, ConfigurationData configuration);
String serializeFundDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<FundData> funds);
String serializeFundDataToJson(boolean prettyPrint, Set<String> responseParameters, FundData fund);
String serializeLoanProductDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<LoanProductData> products);
String serializeLoanProductDataToJson(boolean prettyPrint, Set<String> responseParameters, LoanProductData loanProduct);
String serializeSavingProductDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<SavingProductData> products);
String serializeSavingProductDataToJson(boolean prettyPrint, Set<String> responseParameters, SavingProductData savingProduct);
String serializeDepositProductDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<DepositProductData> products);
String serializeDepositProductDataToJson(boolean prettyPrint, Set<String> responseParameters, DepositProductData depositProduct);
String serializeDepositAccountDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<DepositAccountData> accounts);
String serializeDepositAccountDataToJson(boolean prettyPrint, Set<String> responseParameters, DepositAccountData account);
String serializeClientDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<ClientData> clients);
String serializeClientDataToJson(boolean prettyPrint, Set<String> responseParameters, ClientData clientData);
String serializeClientAccountSummaryCollectionDataToJson(boolean prettyPrint, Set<String> responseParameters, ClientAccountSummaryCollectionData clientAccount);
String serializeGroupDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<GroupData> groups);
String serializeGroupDataToJson(boolean prettyPrint, Set<String> responseParameters, GroupData group);
String serializeNoteDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<NoteData> notes);
String serializeNoteDataToJson(boolean prettyPrint, Set<String> responseParameters, NoteData note);
String serializeNewLoanDataToJson(boolean prettyPrint, Set<String> responseParameters, NewLoanData newLoan);
String serialzieLoanAccountDataToJson(boolean prettyPrint, Set<String> responseParameters, LoanAccountData loanAccount);
String serializeLoanTransactionDataToJson(boolean prettyPrint, Set<String> responseParameters, LoanTransactionData transaction);
String serializeChargeDataToJson(boolean prettyPrint, Set<String> responseParameters, Collection<ChargeData> charges);
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);
}
@@ -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
}
@@ -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);
}
}
}
@@ -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();
}
@@ -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);
}
}
}
@@ -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);
}
@@ -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());
}
}