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

com.venky.swf.db.extensions.ParticipantExtension Maven / Gradle / Ivy

The newest version!
package com.venky.swf.db.extensions;

import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.Map;

import com.venky.cache.Cache;
import com.venky.core.collections.SequenceSet;
import com.venky.extension.Extension;
import com.venky.extension.Registry;
import com.venky.swf.db.annotations.column.pm.PARTICIPANT;
import com.venky.swf.db.model.Model;
import com.venky.swf.db.model.User;
import com.venky.swf.db.model.reflection.ModelReflector;

public abstract class ParticipantExtension implements Extension{

	protected static  void registerExtension(ParticipantExtension instance){
		Class modelClass = getModelClass(instance);
		instance.modelClass = modelClass;
		instance.ref = ModelReflector.instance(modelClass);
		Registry.instance().registerExtension(User.GET_PARTICIPATION_OPTION + "." + modelClass.getSimpleName() , instance);
	}
    protected static  void deregisterExtension(ParticipantExtension instance){
        Class modelClass = getModelClass(instance);
        instance.modelClass = modelClass;
        instance.ref = ModelReflector.instance(modelClass);
        Registry.instance().deregisterExtension(User.GET_PARTICIPATION_OPTION + "." + modelClass.getSimpleName() , instance);
    }
	
	@SuppressWarnings("unchecked")
	protected static  Class getModelClass(ParticipantExtension instance){
		ParameterizedType pt = (ParameterizedType)instance.getClass().getGenericSuperclass();
		return (Class) pt.getActualTypeArguments()[0];
	}

	
	private Class modelClass; 
	private ModelReflector ref ; 
	protected ParticipantExtension(){
		
	}
	
	public ModelReflector getReflector(){
		return ref;
	}
	
	public Class getModelClass(){
		return modelClass;
	}
	
	@SuppressWarnings("unchecked")
	public void invoke(Object... context) {
		User user = (User)context[0];
		M model = (M) context[1];
		String fieldName =  (String)context[2];
		Cache>> participatingGroupOptions = (Cache>>)context[3];

		PARTICIPANT participant = getReflector().getAnnotation(getReflector().getFieldGetter(fieldName), PARTICIPANT.class);
		
		Map> participatingOptions = participatingGroupOptions.get(participant.value());
		List allowedValues = getAllowedFieldValues(user,model,fieldName);

		if (allowedValues != null){
			List ret = new SequenceSet<>();
			ret.addAll(allowedValues);
			allowedValues = ret;

			if (getReflector().getColumnDescriptor(fieldName).isNullable()){
				allowedValues.add(null);
			}
			List existing = participatingOptions.get(fieldName);
			if (existing == null && !participatingOptions.containsKey(fieldName)){
				existing = new SequenceSet();
				participatingOptions.put(fieldName, existing);
			}
			if (existing != null) {
				existing.addAll(allowedValues);
			}
		}else {
			participatingOptions.put(fieldName, null);
		}
	}

	
	/**
	 * @param user
	 * @param fieldName
	 * @return Allowed field values for passed used. null implies all values are allowed. 
	 */
	protected abstract List getAllowedFieldValues(User user, M partiallyFilledModel, String fieldName);
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy