| /** |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| */ |
| package org.apache.fineract.organisation.staff.service; |
| |
| import java.util.Map; |
| |
| import org.apache.commons.lang.StringUtils; |
| import org.apache.fineract.infrastructure.core.api.JsonCommand; |
| import org.apache.fineract.infrastructure.core.data.CommandProcessingResult; |
| import org.apache.fineract.infrastructure.core.data.CommandProcessingResultBuilder; |
| import org.apache.fineract.infrastructure.core.exception.PlatformDataIntegrityException; |
| import org.apache.fineract.organisation.office.domain.Office; |
| import org.apache.fineract.organisation.office.domain.OfficeRepository; |
| import org.apache.fineract.organisation.office.exception.OfficeNotFoundException; |
| import org.apache.fineract.organisation.staff.domain.Staff; |
| import org.apache.fineract.organisation.staff.domain.StaffRepository; |
| import org.apache.fineract.organisation.staff.exception.StaffNotFoundException; |
| import org.apache.fineract.organisation.staff.serialization.StaffCommandFromApiJsonDeserializer; |
| 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 StaffCommandFromApiJsonDeserializer fromApiJsonDeserializer; |
| private final StaffRepository staffRepository; |
| private final OfficeRepository officeRepository; |
| |
| @Autowired |
| public StaffWritePlatformServiceJpaRepositoryImpl(final StaffCommandFromApiJsonDeserializer fromApiJsonDeserializer, |
| final StaffRepository staffRepository, final OfficeRepository officeRepository) { |
| this.fromApiJsonDeserializer = fromApiJsonDeserializer; |
| this.staffRepository = staffRepository; |
| this.officeRepository = officeRepository; |
| } |
| |
| @Transactional |
| @Override |
| public CommandProcessingResult createStaff(final JsonCommand command) { |
| |
| try { |
| this.fromApiJsonDeserializer.validateForCreate(command.json()); |
| |
| final Long officeId = command.longValueOfParameterNamed("officeId"); |
| |
| final Office staffOffice = this.officeRepository.findOne(officeId); |
| if (staffOffice == null) { throw new OfficeNotFoundException(officeId); } |
| |
| final Staff staff = Staff.fromJson(staffOffice, command); |
| |
| this.staffRepository.save(staff); |
| |
| return new CommandProcessingResultBuilder() // |
| .withCommandId(command.commandId()) // |
| .withEntityId(staff.getId()).withOfficeId(officeId) // |
| .build(); |
| } catch (final DataIntegrityViolationException dve) { |
| handleStaffDataIntegrityIssues(command, dve); |
| return CommandProcessingResult.empty(); |
| } |
| } |
| |
| @Transactional |
| @Override |
| public CommandProcessingResult updateStaff(final Long staffId, final JsonCommand command) { |
| |
| try { |
| this.fromApiJsonDeserializer.validateForUpdate(command.json(), staffId); |
| |
| final Staff staffForUpdate = this.staffRepository.findOne(staffId); |
| if (staffForUpdate == null) { throw new StaffNotFoundException(staffId); } |
| |
| final Map<String, Object> changesOnly = staffForUpdate.update(command); |
| |
| if (changesOnly.containsKey("officeId")) { |
| final Long officeId = (Long) changesOnly.get("officeId"); |
| final Office newOffice = this.officeRepository.findOne(officeId); |
| if (newOffice == null) { throw new OfficeNotFoundException(officeId); } |
| |
| staffForUpdate.changeOffice(newOffice); |
| } |
| |
| if (!changesOnly.isEmpty()) { |
| this.staffRepository.saveAndFlush(staffForUpdate); |
| } |
| |
| return new CommandProcessingResultBuilder().withCommandId(command.commandId()).withEntityId(staffId) |
| .withOfficeId(staffForUpdate.officeId()).with(changesOnly).build(); |
| } catch (final DataIntegrityViolationException dve) { |
| handleStaffDataIntegrityIssues(command, dve); |
| return CommandProcessingResult.empty(); |
| } |
| } |
| |
| /* |
| * Guaranteed to throw an exception no matter what the data integrity issue |
| * is. |
| */ |
| private void handleStaffDataIntegrityIssues(final JsonCommand command, final DataIntegrityViolationException dve) { |
| final Throwable realCause = dve.getMostSpecificCause(); |
| |
| if (realCause.getMessage().contains("external_id")) { |
| |
| final String externalId = command.stringValueOfParameterNamed("externalId"); |
| throw new PlatformDataIntegrityException("error.msg.staff.duplicate.externalId", "Staff with externalId `" + externalId |
| + "` already exists", "externalId", externalId); |
| } else if (realCause.getMessage().contains("display_name")) { |
| final String lastname = command.stringValueOfParameterNamed("lastname"); |
| String displayName = lastname; |
| if (!StringUtils.isBlank(displayName)) { |
| final String firstname = command.stringValueOfParameterNamed("firstname"); |
| displayName = lastname + ", " + firstname; |
| } |
| 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()); |
| } |
| } |