WebUserApiServiceImpl.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 org.gringlobal.api.model.WebCooperatorDTO;
import org.gringlobal.api.model.WebUserDTO;
import org.gringlobal.api.v2.facade.WebUserApiService;
import org.gringlobal.api.v2.mapper.MapstructMapper;
import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.model.WebUser;
import org.gringlobal.service.EMailVerificationService;
import org.gringlobal.service.LanguageService;
import org.gringlobal.service.WebCooperatorService;
import org.gringlobal.service.WebUserService;
import org.gringlobal.service.filter.WebUserFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
public class WebUserApiServiceImpl implements WebUserApiService {

	@Autowired
	private WebUserService service;

	@Autowired
	private MapstructMapper mapper;

	@Autowired
	private LanguageService languageService;

	@Autowired
	private WebCooperatorService webCooperatorService;

	@Autowired
	private EMailVerificationService emailVerificationService;

	@Override
	@Transactional
	public WebUserDTO create(String username, String pass, Long webCooperatorId) {
		WebUser source = new WebUser();
		source.setIsEnabled("N");
		source.setUsername(username);
		source.setPassword(pass);
		source.setSysLang(languageService.getLanguage(LocaleContextHolder.getLocale()));
		if (webCooperatorId != null) {
			source.setWebCooperator(webCooperatorService.get(webCooperatorId));
		}

		return mapper.map(service.create(source));
	}

	@Override
	@Transactional
	public WebUserDTO upsertWebCooperator(Long webUserId, WebCooperatorDTO webCooperator) {
		return mapper.map(service.upsertWebCooperator(service.get(webUserId), mapper.map(webCooperator)));
	}

	@Override
	@Transactional
	public WebUserDTO update(WebUserDTO entity) {
		return mapper.map(service.update(mapper.map(entity)));
	}

	@Override
	public Page<WebUserDTO> list(Pageable page, WebUserFilter filter) throws SearchException {
		return service.list(filter, page).map(mapper::map);
	}

	@Override
	public WebUserDTO load(long id) {
		return mapper.map(service.load(id));
	}

	@Override
	@Transactional
	public WebUserDTO remove(long id) {
		return mapper.map(service.remove(service.load(id)));
	}

	@Override
	@Transactional
	public void setPassword(Long id, String pass) {
		service.setPassword(id, pass);
	}

	@Override
	@Transactional
	public WebUserDTO enable(Long id) {
		service.setAccountActive(id, true);
		return mapper.map(service.load(id));
	}

	@Override
	@Transactional
	public WebUserDTO disable(Long id) {
		service.setAccountActive(id, false);
		return mapper.map(service.load(id));
	}

	@Override
	public WebUserDTO loadUserByUsername(String username) {
		return mapper.map((WebUser) service.loadUserByUsername(username));
	}

	@Override
	@Transactional
	public WebUserDTO registerUser(String email, String password) {
		WebUser source = new WebUser();
		source.setIsEnabled("N");
		source.setUsername(email);
		source.setPassword(password);
		source.setSysLang(languageService.getLanguage(LocaleContextHolder.getLocale()));
		WebUser savedWebUser = service.create(source);

		emailVerificationService.sendVerificationEmail(savedWebUser);

		return mapper.map(savedWebUser);
	}

}