All Downloads are FREE. Search and download functionalities are using the official Maven repository.

foundation.cmo.api.mls.graphql.security.services.MService Maven / Gradle / Ivy

There is a newer version: 1.0.21
Show newest version
package foundation.cmo.api.mls.graphql.security.services;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;

import org.reactivestreams.Publisher;
import org.springframework.context.MessageSource;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

import foundation.cmo.api.mls.graphql.security.dto.MUser;
import foundation.cmo.api.mls.graphql.security.dto.MUserDetails;
import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxSink;

public class MService {

	protected final Map> registry2 = new HashMap<>();
	protected final MMultiRegitry> registry = new MMultiRegitry<>();

	protected  void cloneObject(String sfield, String jpaMethod, Object repository, T value) {
	}

	protected  void cloneObject(String jpaMethod, Object repository, T value) {
		try {

			String smethod = jpaMethod.substring(jpaMethod.indexOf("By"));
			smethod = smethod.replace("By", "");
			smethod = smethod.substring(0, 1).toLowerCase() + smethod.substring(1);

			Object obj = getField(smethod, value).get(value);
			if (obj == null) {
				return;
			}

			Class repositoryType = repository.getClass();

			Method method = CopyFieldsUtils.getMethod(repositoryType, jpaMethod);
			method.setAccessible(true);

			obj = method.invoke(repository, obj);
			if (obj instanceof Optional) {
				Optional optional = (Optional) obj;
				if (optional.isPresent()) {
					CopyFieldsUtils.copyAtoB(optional.get(), value);
				}
			} else {
				CopyFieldsUtils.copyAtoB(obj, value);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected  Field getField(String name, T value) {
		try {
			for (Field field : getAllFields(value)) {
				if (name.equalsIgnoreCase(field.getName())) {
					field.setAccessible(true);
					return field;
				}
			}
			return null;
		} catch (Exception e) {
			return null;
		}
	}

	protected List getAllFields(Object value) {
		try {
			List list = new ArrayList<>();
			Class type = value.getClass();
			list.addAll(Arrays.asList(type.getFields()));
			list.addAll(Arrays.asList(type.getDeclaredFields()));

			while (type.getSuperclass() != null) {
				type = type.getSuperclass();
				list.addAll(Arrays.asList(type.getFields()));
				list.addAll(Arrays.asList(type.getDeclaredFields()));
			}

			return list;
		} catch (Exception e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	protected  Publisher publish(Class type, Object key, T defaultValue) {
		String skey = makeId(type, key);
		return (Publisher) Flux.create(
				fs -> registry.add(skey, fs.onDispose(() -> registry.remove(skey, fs)).next(defaultValue)),
				FluxSink.OverflowStrategy.BUFFER);
	}
	
	@SuppressWarnings("unchecked")
	protected  Publisher publish(Object key, T defaultValue) {
		try {
			Class type = defaultValue.getClass();
			String skey = makeId(type, key);
			return (Publisher) Flux.create(
					fs -> registry.add(skey, fs.onDispose(() -> registry.remove(skey, fs)).next(defaultValue)),
					FluxSink.OverflowStrategy.BUFFER);
		} catch (Exception e) {
			return null;
		}
	}

	protected boolean inPublish(Class type, Object key) {
		String skey = makeId(type, key);
		return registry.contains(skey);
	}

	protected void callPublish(Object key, Object value) throws Exception {
		String skey = makeId(value.getClass(), key);
		registry.get(skey).forEach(sub -> sub.next(value));
	}

	protected void callPublish(Class type, Object key, Object value) throws Exception {
		String skey = makeId(type, key);
		registry.get(skey).forEach(sub -> sub.next(value));
	}

	protected void assertNotTrue(boolean expression, String message, Object... args) throws Exception {
		if (expression) {
			message = getString(message, args);
			throw new Exception(message);
		}
	}

	public String getString(String pattern, Object... args) {
		try {
			return messageSource().getMessage(pattern, args, Locale.forLanguageTag("pt-BR"));
		} catch (Exception e) {
			return pattern;
		}
	}

	// @Bean("messageSource")
	MessageSource messageSource() {
		ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
		messageSource.setBasenames("messages/message");
		return messageSource;
	}

	private String makeId(Class type, Object key) {
		return String.format("%s-%s", type.getSimpleName(), key);
	}

	protected MUser getUser() {
		try {
			SecurityContext context = SecurityContextHolder.getContext();
			MUserDetails userDetails = (MUserDetails) context.getAuthentication().getPrincipal();
			return userDetails.getUser();
		} catch (Exception e) {
			return null;
		}
	}

	protected Exception getException(String message) {
		return new Exception(message);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy