class IdentityEmailGenerator extends AbstractIdentityValueGenerator {

	@Override
	public IdmIdentityDto generate(IdmIdentityDto dto, IdmGenerateValueDto valueGenerator) {
		Assert.notNull(dto, "DTO is required.");
		Assert.notNull(valueGenerator, "Value generator is required.");
		//		
		// if exists email and configuration doesn't allow regenerate return dto
		if (!valueGenerator.isRegenerateValue() && StringUtils.isNotEmpty(dto.getEmail())) {
			return dto;
		}
		
		String transformedUsername = null;
		if (isGenerateFromUsername(valueGenerator)) {
			transformedUsername = StringUtils.stripAccents(dto.getUsername());
			transformedUsername = replaceAllWhiteSpaces(valueGenerator, transformedUsername);
		} else {
			transformedUsername = super.generateUsername(dto, valueGenerator);
		}
		//
		if (StringUtils.isEmpty(transformedUsername) ) {
			// transformed username is required
			return dto;
		}
		//
		if (StringUtils.contains(transformedUsername, AT_CONSTANT)) {
			// username contains @ invalid mail return whole dto
			return dto;
		}
		String emailSuffix = getEmailSuffix(valueGenerator);
		if (StringUtils.isEmpty(emailSuffix)) {
			// email suffix is empty
			return dto;
		}
		//
		StringBuilder result = new StringBuilder();
		result.append(transformedUsername);
		result.append(getTransformedSuffix(valueGenerator, emailSuffix));
		
		dto.setEmail(result.toString());
		//
		return dto;
	}

}