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

net.sf.ahtutils.db.ejb.acl.AclInitViews Maven / Gradle / Ivy

package net.sf.ahtutils.db.ejb.acl;

import java.io.FileNotFoundException;
import java.util.Map;

import net.sf.ahtutils.controller.factory.ejb.status.EjbLangFactory;
import net.sf.ahtutils.controller.interfaces.AhtAclFacade;
import net.sf.ahtutils.db.ejb.AhtDbEjbUpdater;
import net.sf.ahtutils.exception.ejb.UtilsContraintViolationException;
import net.sf.ahtutils.exception.ejb.UtilsIntegrityException;
import net.sf.ahtutils.exception.ejb.UtilsLockingException;
import net.sf.ahtutils.exception.ejb.UtilsNotFoundException;
import net.sf.ahtutils.exception.processing.UtilsConfigurationException;
import net.sf.ahtutils.model.interfaces.acl.UtilsAclCategoryUsecase;
import net.sf.ahtutils.model.interfaces.acl.UtilsAclView;
import net.sf.ahtutils.model.interfaces.status.UtilsDescription;
import net.sf.ahtutils.model.interfaces.status.UtilsLang;
import net.sf.ahtutils.model.interfaces.status.UtilsStatus;
import net.sf.ahtutils.xml.access.Access;
import net.sf.ahtutils.xml.access.Category;
import net.sf.ahtutils.xml.access.View;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AclInitViews ,
							L extends UtilsLang,
							D extends UtilsDescription,
							CU extends UtilsAclCategoryUsecase,
							U extends UtilsAclView>
{
	final static Logger logger = LoggerFactory.getLogger(AclInitViews.class);
	public static enum ExtractId {aclUseCases,aclRoles,aclRoleAutoAssign,aclProjectRoles}
	
	final Class statusClass;
    final Class langClass;
    final Class descriptionClass;
    
    final Class categoryUsecaseClass;
    final Class usecaseClass;
		
	private AhtAclFacade fAcl;
	private EjbLangFactory ejbLangFactory;
	
	public static ,
				   L extends UtilsLang,
				   D extends UtilsDescription,
				   CU extends UtilsAclCategoryUsecase,
				   U extends UtilsAclView>
		AclInitViews
		factory(final Class statusClass,
				final Class langClass,
				final Class descriptionClass,
				final Class categoryUsecaseClass,
				final Class usecaseClass,
				AhtAclFacade fAcl)
	{
		return new AclInitViews(statusClass, langClass, descriptionClass, categoryUsecaseClass, usecaseClass,fAcl);
	}
	
	public AclInitViews(final Class statusClass, final Class langClass, final Class descriptionClass,
			final Class categoryUsecaseClass, final Class usecaseClass,
			AhtAclFacade fAcl)
	{       
		this.statusClass = statusClass;
        this.langClass = langClass;
        this.descriptionClass = descriptionClass;
        this.categoryUsecaseClass = categoryUsecaseClass;
        this.usecaseClass = usecaseClass;
        
        this.fAcl=fAcl;
		
		ejbLangFactory = EjbLangFactory.createFactory(langClass);
	}
	
	public void iuUsecases(Access access) throws FileNotFoundException, UtilsConfigurationException
	{
		logger.debug("i/u "+Category.class.getSimpleName()+" with "+access.getCategory().size()+" categories");
		
		AhtDbEjbUpdater ejbUccUpdate = AhtDbEjbUpdater.createFactory(categoryUsecaseClass);
		AhtDbEjbUpdater ejbUcUpdate = AhtDbEjbUpdater.createFactory(usecaseClass);
		
		ejbUccUpdate.dbEjbs(fAcl.all(categoryUsecaseClass));
		ejbUcUpdate.dbEjbs(fAcl.all(usecaseClass));

		for(Category category : access.getCategory())
		{
			logger.debug("Processing category: "+category.getCode());
			ejbUccUpdate.actualAdd(category.getCode());
			
			CU usecaseCategory;
			try
			{
				usecaseCategory = fAcl.fByCode(categoryUsecaseClass, category.getCode());
				
				logger.trace("removing existing langs: "+category.getCode());
				Map langMap = usecaseCategory.getName();
				Map descMap = usecaseCategory.getDescription();
				usecaseCategory.setName(null);
				usecaseCategory.setDescription(null);
				
				try{usecaseCategory=(CU)fAcl.update(usecaseCategory);}
				catch (UtilsContraintViolationException e) {logger.error("",e);}
				catch (UtilsLockingException e) {logger.error("",e);}
				
				for(L lang : langMap.values())
				{
					try {fAcl.rm(lang);}
					catch (UtilsIntegrityException e) {logger.error("",e);}
				}
				
				for(D desc : descMap.values())
				{
					try {fAcl.rm(desc);}
					catch (UtilsIntegrityException e) {logger.error("",e);}
				}
				logger.trace("removed existing langs: "+category.getCode());
			}
			catch (UtilsNotFoundException e)
			{
				try
				{
					usecaseCategory = categoryUsecaseClass.newInstance();
					usecaseCategory.setCode(category.getCode());
					usecaseCategory = fAcl.persist(usecaseCategory);
				}
				catch (InstantiationException e2) {throw new UtilsConfigurationException(e2.getMessage());}
				catch (IllegalAccessException e2) {throw new UtilsConfigurationException(e2.getMessage());}
				catch (UtilsContraintViolationException e2) {throw new UtilsConfigurationException(e2.getMessage());}	
			}
			
			try
			{
				usecaseCategory.setName(ejbLangFactory.getLangMap(category.getLangs()));
//				usecaseCategory.setDescription(ejbFactory.getDescriptionMap(category.getDescriptions()));
				usecaseCategory=fAcl.update(usecaseCategory);
				
				if(category.isSetViews() && category.getViews().isSetView())
				{
					for(View usecase : category.getViews().getView())
					{
						ejbUcUpdate.actualAdd(usecase.getCode());
						initUpdateUsecase(usecaseCategory, usecase);
					}
				}
			}
			catch (UtilsContraintViolationException e) {logger.error("",e);}
			catch (InstantiationException e) {logger.error("",e);}
			catch (IllegalAccessException e) {logger.error("",e);}
			catch (UtilsIntegrityException e) {logger.error("",e);}
			catch (UtilsLockingException e) {logger.error("",e);}
		}
		
		ejbUcUpdate.remove(fAcl);
		ejbUccUpdate.remove(fAcl);
		
		logger.trace("initUpdateUsecaseCategories finished");
	}
	
	public void initUpdateUsecase(CU usecaseCategory, View usecase) throws UtilsConfigurationException
	{
		U aclUsecase;
		try
		{
			aclUsecase = fAcl.fByCode(usecaseClass, usecase.getCode());
			
			Map langMap = aclUsecase.getName();
			Map descMap = aclUsecase.getDescription();
			aclUsecase.setName(null);
			aclUsecase.setDescription(null);
			
			try{aclUsecase=fAcl.update(aclUsecase);}
			catch (UtilsLockingException e) {logger.error("",e);}
			catch (UtilsContraintViolationException e) {logger.error("",e);}
			
			for(L lang : langMap.values())
			{
				try {fAcl.rm(lang);}
				catch (UtilsIntegrityException e) {logger.error("",e);}
			}
			
			for(D desc : descMap.values())
			{
				try {fAcl.rm(desc);}
				catch (UtilsIntegrityException e) {logger.error("",e);}
			}
		}
		catch (UtilsNotFoundException e)
		{
			try
			{
				aclUsecase = usecaseClass.newInstance();
				aclUsecase.setCode(usecase.getCode());
				aclUsecase = fAcl.persist(aclUsecase);
			}
			catch (InstantiationException e2) {throw new UtilsConfigurationException(e2.getMessage());}
			catch (IllegalAccessException e2) {throw new UtilsConfigurationException(e2.getMessage());}
			catch (UtilsContraintViolationException e2) {throw new UtilsConfigurationException(e2.getMessage());}	
		}
		
		try
		{
			aclUsecase.setName(ejbLangFactory.getLangMap(usecase.getLangs()));
//			aclUsecase.setDescription(ejbFactory.getDescriptionMap(usecase.getDescriptions()));
			aclUsecase.setCategory(usecaseCategory);
			aclUsecase=fAcl.update(aclUsecase);
		}
		catch (UtilsContraintViolationException e) {logger.error("",e);}
		catch (InstantiationException e) {logger.error("",e);}
		catch (IllegalAccessException e) {logger.error("",e);}
		catch (UtilsIntegrityException e) {logger.error("",e);}
		catch (UtilsLockingException e) {logger.error("",e);}
	}
}