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

foundation.cmo.api.mls.graphql.security.services.MServiceDefault 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.List;
import java.util.Locale;
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 MServiceDefault {
	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;
		}
	}

	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);
	}

	
	// ---------------------------------------------------------------//
	private final MMultiRegitry> subscribes = new MMultiRegitry<>();
	
	protected  Publisher publish(Class type, String id, T defaultValue) {
		return Flux.create(subs -> subscribes.add(id, 
				subs.onDispose(()-> subscribes.remove(id, subs)).next(defaultValue)), FluxSink.OverflowStrategy.BUFFER);
	}
    
//    public Publisher publish(String id) {
//        return Flux.create(emitter -> {
//            sinks.put(id, emitter);
//            emitter.onDispose(() -> sinks.remove(id));
//        }, FluxSink.OverflowStrategy.BUFFER).share();
//    }
//    
//    public void callPublish(String id, Object value) {
//    	FluxSink sink = sinks.get(id);
//        if (sink != null) {
//            sink.next(value);
//        }
//    }
	
}