<commit_before>package com.zfgc.services.users;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.CharBuffer;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.mail.internet.InternetAddress;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.time.DateUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import com.zfgc.config.ZfgcGeneralConfig;
import com.zfgc.dao.LookupDao;
import com.zfgc.dataprovider.EmailAddressDataProvider;
import com.zfgc.dataprovider.IpDataProvider;
import com.zfgc.dataprovider.UserConnectionDataProvider;
import com.zfgc.dataprovider.UserCurrentActionDataProvider;
import com.zfgc.dataprovider.UserEmailViewDataProvider;
import com.zfgc.dataprovider.UsersDataProvider;
import com.zfgc.exception.ZfgcNotFoundException;
import com.zfgc.exception.ZfgcValidationException;
import com.zfgc.model.users.AuthCredentials;
import com.zfgc.model.users.AuthToken;
import com.zfgc.model.users.EmailAddress;
import com.zfgc.model.users.IpAddress;
import com.zfgc.model.users.MemberListingView;
import com.zfgc.model.users.MembersView;
import com.zfgc.model.users.UserConnection;
import com.zfgc.model.users.UserContactInfo;
import com.zfgc.model.users.UserCurrentAction;
import com.zfgc.model.users.UserEmailView;
import com.zfgc.model.users.UserSecurityInfo;
import com.zfgc.model.users.Users;
import com.zfgc.model.users.profile.PersonalInfo;
import com.zfgc.requiredfields.users.UsersRequiredFieldsChecker;
import com.zfgc.rules.users.UsersRuleChecker;
import com.zfgc.services.AbstractService;
import com.zfgc.services.RuleRunService;
import com.zfgc.services.authentication.AuthenticationService;
import com.zfgc.services.avatar.AvatarService;
import com.zfgc.services.ip.IpAddressService;
import com.zfgc.services.lookups.LookupService;
import com.zfgc.services.pm.PmService;
import com.zfgc.util.ZfgcEmailUtils;
import com.zfgc.util.security.RsaKeyPair;
import com.zfgc.util.security.ZfgcSecurityUtils;
import com.zfgc.util.time.ZfgcTimeUtils;
import com.zfgc.validation.uservalidation.UserValidator;
import com.zfgc.model.avatar.Avatar;
import com.zfgc.model.online.OnlineUser;

@Service
public class UsersService extends AbstractService {
	@Autowired 
	AuthenticationService authenticationService;
	
	@Autowired
	UsersDataProvider usersDataProvider;
	
	@Autowired
	IpAddressService ipAddressService;
	
	@Autowired
	EmailAddressDataProvider emailAddressDataProvider;
	
	@Autowired
	private ZfgcEmailUtils zfgcEmailUtils;
	
	@Autowired 
	UsersRequiredFieldsChecker requiredFieldsChecker;
	
	@Autowired
	UserValidator validator;
	
	@Autowired
	UsersRuleChecker ruleChecker;
	
	@Autowired
	PmService pmService;
	
	@Autowired
	RuleRunService<Users> ruleRunner;
	
	@Autowired
	UserConnectionDataProvider userConnectionDataProvider;
	
	@Autowired
	UserCurrentActionDataProvider userCurrentActionDataProvider;
	
	@Autowired
	ZfgcGeneralConfig zfgcGeneralConfig;
	
	@Autowired
	private UserEmailViewDataProvider userEmailViewDataProvider;
	
	@Autowired
	private AvatarService avatarService;
	
	@Value("${clausius.client}")
	private String clientId;
	
	@Value("${clausius.password}")
	private String clientSecret;
	
	@Value("${clausius.authEndpoint}")
	private String authEndpoint;

	private Logger LOGGER = LogManager.getLogger(UsersService.class);

	private void getUserInternal(Users user) {
		user.setPrimaryIpAddress(ipAddressService.getIpAddress(user.getPrimaryIp()));
		user.setPersonalInfo(usersDataProvider.getPersonalInfo(user.getUsersId()));
		user.getPersonalInfo().setAvatar(avatarService.getAvatarRecord(user.getPersonalInfo().getAvatarId()));
	}
	
	public Users getUser(Integer usersId) {
		Users user = usersDataProvider.getUser(usersId);
		getUserInternal(user);
		
		return user;
	}
	
	public Users getUser(String username) {
		Users user = usersDataProvider.getUser(username);
		getUserInternal(user);
		
		return user;
	}
	
	public Users getUserByEmail(String email) {
		UserEmailView emailView = userEmailViewDataProvider.getActiveUserEmailReverse(email);
		
		return getUser(emailView.getLoginName());
	}
	
	public List<Integer> getUsersByConversation(Integer conversationId) throws RuntimeException{
		List<Users> result = null;

		result = usersDataProvider.getUsersByConversation(conversationId);
		
		List<Integer> Ids = new ArrayList<>(result.size());
		for(Users user : result) {
			Ids.add(user.getUsersId());
		}
		
		return Ids;
	}
	
	private void registerUserAtIdentity(Users user) {
		RestTemplate template = new RestTemplate();
		HttpHeaders headers = new HttpHeaders();
		headers.setBasicAuth(clientId, clientSecret);
		headers.setContentType(MediaType.APPLICATION_JSON);
		
		HttpEntity ent = new HttpEntity("{ 'username' : '" + user.getUserContactInfo().getEmail().getEmailAddress() + "', 'password' : '" + user.getPassword() + "' }", headers);

		template.exchange(authEndpoint + "/users/register", HttpMethod.POST, ent, String.class);
	}
	
	@Transactional
	public Users createNewUser(Users user, HttpServletRequest requestHeader) throws RuntimeException{
		try {
			user.setTimeOffsetLkup(lookupService.getLkupValue(LookupService.TIMEZONE, user.getTimeOffset()));
			ruleRunner.runRules(validator, requiredFieldsChecker, ruleChecker, user, user);
		} 
		catch(ZfgcValidationException ex){<commit_msg>fix password issue<commit_after>package com.zfgc.services.users;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.CharBuffer;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.mail.internet.InternetAddress;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.time.DateUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import com.zfgc.config.ZfgcGeneralConfig;
import com.zfgc.dao.LookupDao;
import com.zfgc.dataprovider.EmailAddressDataProvider;
import com.zfgc.dataprovider.IpDataProvider;
import com.zfgc.dataprovider.UserConnectionDataProvider;
import com.zfgc.dataprovider.UserCurrentActionDataProvider;
import com.zfgc.dataprovider.UserEmailViewDataProvider;
import com.zfgc.dataprovider.UsersDataProvider;
import com.zfgc.exception.ZfgcNotFoundException;
import com.zfgc.exception.ZfgcValidationException;
import com.zfgc.model.users.AuthCredentials;
import com.zfgc.model.users.AuthToken;
import com.zfgc.model.users.EmailAddress;
import com.zfgc.model.users.IpAddress;
import com.zfgc.model.users.MemberListingView;
import com.zfgc.model.users.MembersView;
import com.zfgc.model.users.UserConnection;
import com.zfgc.model.users.UserContactInfo;
import com.zfgc.model.users.UserCurrentAction;
import com.zfgc.model.users.UserEmailView;
import com.zfgc.model.users.UserSecurityInfo;
import com.zfgc.model.users.Users;
import com.zfgc.model.users.profile.PersonalInfo;
import com.zfgc.requiredfields.users.UsersRequiredFieldsChecker;
import com.zfgc.rules.users.UsersRuleChecker;
import com.zfgc.services.AbstractService;
import com.zfgc.services.RuleRunService;
import com.zfgc.services.authentication.AuthenticationService;
import com.zfgc.services.avatar.AvatarService;
import com.zfgc.services.ip.IpAddressService;
import com.zfgc.services.lookups.LookupService;
import com.zfgc.services.pm.PmService;
import com.zfgc.util.ZfgcEmailUtils;
import com.zfgc.util.security.RsaKeyPair;
import com.zfgc.util.security.ZfgcSecurityUtils;
import com.zfgc.util.time.ZfgcTimeUtils;
import com.zfgc.validation.uservalidation.UserValidator;
import com.zfgc.model.avatar.Avatar;
import com.zfgc.model.online.OnlineUser;

@Service
public class UsersService extends AbstractService {
	@Autowired 
	AuthenticationService authenticationService;
	
	@Autowired
	UsersDataProvider usersDataProvider;
	
	@Autowired
	IpAddressService ipAddressService;
	
	@Autowired
	EmailAddressDataProvider emailAddressDataProvider;
	
	@Autowired
	private ZfgcEmailUtils zfgcEmailUtils;
	
	@Autowired 
	UsersRequiredFieldsChecker requiredFieldsChecker;
	
	@Autowired
	UserValidator validator;
	
	@Autowired
	UsersRuleChecker ruleChecker;
	
	@Autowired
	PmService pmService;
	
	@Autowired
	RuleRunService<Users> ruleRunner;
	
	@Autowired
	UserConnectionDataProvider userConnectionDataProvider;
	
	@Autowired
	UserCurrentActionDataProvider userCurrentActionDataProvider;
	
	@Autowired
	ZfgcGeneralConfig zfgcGeneralConfig;
	
	@Autowired
	private UserEmailViewDataProvider userEmailViewDataProvider;
	
	@Autowired
	private AvatarService avatarService;
	
	@Value("${clausius.client}")
	private String clientId;
	
	@Value("${clausius.password}")
	private String clientSecret;
	
	@Value("${clausius.authEndpoint}")
	private String authEndpoint;

	private Logger LOGGER = LogManager.getLogger(UsersService.class);

	private void getUserInternal(Users user) {
		user.setPrimaryIpAddress(ipAddressService.getIpAddress(user.getPrimaryIp()));
		user.setPersonalInfo(usersDataProvider.getPersonalInfo(user.getUsersId()));
		user.getPersonalInfo().setAvatar(avatarService.getAvatarRecord(user.getPersonalInfo().getAvatarId()));
	}
	
	public Users getUser(Integer usersId) {
		Users user = usersDataProvider.getUser(usersId);
		getUserInternal(user);
		
		return user;
	}
	
	public Users getUser(String username) {
		Users user = usersDataProvider.getUser(username);
		getUserInternal(user);
		
		return user;
	}
	
	public Users getUserByEmail(String email) {
		UserEmailView emailView = userEmailViewDataProvider.getActiveUserEmailReverse(email);
		
		return getUser(emailView.getLoginName());
	}
	
	public List<Integer> getUsersByConversation(Integer conversationId) throws RuntimeException{
		List<Users> result = null;

		result = usersDataProvider.getUsersByConversation(conversationId);
		
		List<Integer> Ids = new ArrayList<>(result.size());
		for(Users user : result) {
			Ids.add(user.getUsersId());
		}
		
		return Ids;
	}
	
	private void registerUserAtIdentity(Users user) {
		RestTemplate template = new RestTemplate();
		HttpHeaders headers = new HttpHeaders();
		headers.setBasicAuth(clientId, clientSecret);
		headers.setContentType(MediaType.APPLICATION_JSON);
		
		HttpEntity ent = new HttpEntity("{ 'username' : '" + user.getUserContactInfo().getEmail().getEmailAddress() + "', 'password' : '" + user.getUserSecurityInfo().getNewPassword() + "' }", headers);

		template.exchange(authEndpoint + "/users/register", HttpMethod.POST, ent, String.class);
	}
	
	@Transactional
	public Users createNewUser(Users user, HttpServletRequest requestHeader) throws RuntimeException{
		try {
			user.setTimeOffsetLkup(lookupService.getLkupValue(LookupService.TIMEZONE, user.getTimeOffset()));
			ruleRunner.runRules(validator, requiredFieldsChecker, ruleChecker, user, user);
		} 
		catch(ZfgcValidationException ex){
{'fraction_non_alphanumeric': 0.09757486979166667, 'fraction_numerical': 0.00048828125, 'mean_word_length': 6.567118226600985, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6537400', 'n_tokens_mistral': 3903, 'n_tokens_neox': 3774, 'n_words': 627}