/** * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ package org.mifosplatform.infrastructure.configuration.service; import java.util.Map; import org.mifosplatform.infrastructure.configuration.data.GlobalConfigurationDataValidator; import org.mifosplatform.infrastructure.configuration.domain.GlobalConfigurationProperty; import org.mifosplatform.infrastructure.configuration.domain.GlobalConfigurationRepositoryWrapper; import org.mifosplatform.infrastructure.core.api.JsonCommand; import org.mifosplatform.infrastructure.core.data.CommandProcessingResult; import org.mifosplatform.infrastructure.core.data.CommandProcessingResultBuilder; import org.mifosplatform.infrastructure.core.exception.PlatformDataIntegrityException; import org.mifosplatform.infrastructure.security.service.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 GlobalConfigurationWritePlatformServiceJpaRepositoryImpl implements GlobalConfigurationWritePlatformService { private final static Logger logger = LoggerFactory.getLogger(GlobalConfigurationWritePlatformServiceJpaRepositoryImpl.class); private final PlatformSecurityContext context; private final GlobalConfigurationRepositoryWrapper repository; private final GlobalConfigurationDataValidator globalConfigurationDataValidator; @Autowired public GlobalConfigurationWritePlatformServiceJpaRepositoryImpl(final PlatformSecurityContext context, final GlobalConfigurationRepositoryWrapper codeRepository, final GlobalConfigurationDataValidator dataValidator) { this.context = context; this.repository = codeRepository; this.globalConfigurationDataValidator = dataValidator; } @Transactional @Override public CommandProcessingResult update(final Long configId, final JsonCommand command) { this.context.authenticatedUser(); try { this.globalConfigurationDataValidator.validateForUpdate(command); final GlobalConfigurationProperty configItemForUpdate = this.repository.findOneWithNotFoundDetection(configId); final Map<String, Object> changes = configItemForUpdate.update(command); if (!changes.isEmpty()) { this.repository.save(configItemForUpdate); } return new CommandProcessingResultBuilder().withCommandId(command.commandId()).withEntityId(configId).with(changes).build(); } catch (final DataIntegrityViolationException dve) { handleDataIntegrityIssues(dve); return CommandProcessingResult.empty(); } } @Transactional @Override public void addSurveyConfig(final String name) { try{ final GlobalConfigurationProperty ppi = GlobalConfigurationProperty.newSurveyConfiguration(name); this.repository.save(ppi); } catch (final DataIntegrityViolationException dve) { handleDataIntegrityIssues(dve); } } /* * Guaranteed to throw an exception no matter what the data integrity issue * is. */ private void handleDataIntegrityIssues(final DataIntegrityViolationException dve) { final Throwable realCause = dve.getMostSpecificCause(); logger.error(dve.getMessage(), dve); throw new PlatformDataIntegrityException("error.msg.globalConfiguration.unknown.data.integrity.issue", "Unknown data integrity issue with resource: " + realCause.getMessage()); } }