AppUserGuiSettingApiServiceImpl.java

/*
 * Copyright 2025 Global Crop Diversity Trust
 *
 * Licensed 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.gringlobal.api.v2.facade.impl;

import java.util.List;
import javax.validation.constraints.NotNull;

import org.gringlobal.api.model.AppUserGuiSettingDTO;
import org.gringlobal.api.v2.facade.AppUserGuiSettingApiService;
import org.gringlobal.model.AppUserGuiSetting;
import org.gringlobal.service.AppUserGuiSettingService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
public class AppUserGuiSettingApiServiceImpl extends APIServiceFacadeImpl<AppUserGuiSettingService, AppUserGuiSettingDTO, AppUserGuiSetting> 
	implements AppUserGuiSettingApiService {

	@Override
	protected AppUserGuiSetting convert(AppUserGuiSettingDTO source) {
		return mapper.map(source);
	}

	@Override
	protected AppUserGuiSettingDTO convert(AppUserGuiSetting source) {
		return mapper.map(source);
	}

	@Override
	public AppUserGuiSettingDTO getSetting(@NotNull String appName, String formName, @NotNull String resourceName, @NotNull String resourceKey) {
		return mapper.map(service.getSetting(appName, formName, resourceName, resourceKey));
	}

	@Override
	public List<AppUserGuiSettingDTO> listForApp(String appName) {
		return mapper.map(service.listForApp(appName), mapper::map);
	}

	@Override
	@Transactional
	public AppUserGuiSettingDTO upsertSetting(AppUserGuiSettingDTO setting) {
		var existing = service.getSetting(setting.getAppName(), setting.getFormName(), setting.getResourceName(), setting.getResourceKey());
		if (existing == null) {
			return convert(service.createFast(convert(setting)));
		} else {
			return convert(service.updateFast(convert(setting), existing));
		}
	}

	@Override
	@Transactional
	public AppUserGuiSetting update(AppUserGuiSettingDTO updated) {
		return service.updateFast(convert(updated), service.get(updated.getId()));
	}
}