<commit_before>package edu.duke.cabig.c3pr.infrastructure.interceptor;

import java.io.Serializable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.EmptyInterceptor;
import org.hibernate.type.Type;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import edu.duke.cabig.c3pr.constants.NotificationEventType;
import edu.duke.cabig.c3pr.dao.OrganizationDao;
import edu.duke.cabig.c3pr.domain.CoordinatingCenterStudyStatus;
import edu.duke.cabig.c3pr.domain.Organization;
import edu.duke.cabig.c3pr.domain.PlannedNotification;
import edu.duke.cabig.c3pr.domain.SiteStudyStatus;
import edu.duke.cabig.c3pr.domain.Study;
import edu.duke.cabig.c3pr.domain.StudySite;
import edu.duke.cabig.c3pr.service.impl.RulesDelegationServiceImpl;
import edu.duke.cabig.c3pr.tools.Configuration;

public class NotificationInterceptor extends EmptyInterceptor implements ApplicationContextAware {

	private static final Log log = LogFactory.getLog(NotificationInterceptor.class);
	
	RulesDelegationServiceImpl rulesDelegationService;
	
	private ApplicationContext applicationContext;
	
	private OrganizationDao organizationDao;
	
	private Configuration configuration; 
	
	public ApplicationContext getApplicationContext() {
		return applicationContext;
	}
	
	public void setApplicationContext(ApplicationContext applicationContext) {
		this.applicationContext = applicationContext;
	}
	
	public Organization getHostingOrganization(){
		Organization org = organizationDao.getByNciIdentifier(configuration.get(Configuration.LOCAL_NCI_INSTITUTE_CODE)).get(0);
		return org;
	}
	
	
	/*
	 *  Return true only if the state is modified in anyway.
	 */
	@Override
	public boolean onSave(Object entity, Serializable id, Object[] state,
		String[] propertyNames, Type[] types) {
		log.debug(this.getClass().getName() + ": Entering onSave()");
		//SessionFactory sessionFactory = (SessionFactory)applicationContext.getBean("sessionFactory");
		//Session session  = sessionFactory.openSession();	
		//Serializable persistedObjectId = getObjectId(entity);
		//Object preUpdateState = session.get(entity.getClass(),  persistedObjectId);
		if(entity instanceof Study){			
			handleNewStudySaved((Study)entity);
		}
		if(entity instanceof StudySite){
			handleNewStudySiteSaved((StudySite)entity);
		}
		log.debug(this.getClass().getName() + ": Exiting onSave()");
		return false;
	}

/*	private Serializable getObjectId(Object obj) {
        
        Class objectClass = obj.getClass();
        Method[] methods = objectClass.getMethods();

        Serializable persistedObjectId = null;
        for (int ii = 0; ii < methods.length; ii++) {
            // If the method name equals 'getId' then invoke it to get the id of the object.
            if (methods[ii].getName().equals("getId")) {
                try {
                    persistedObjectId = (Serializable)methods[ii].invoke(obj, null);
                    break;      
                } catch (Exception e) {
                    log.warn("Audit Log Failed - Could not get persisted object id: " + e.getMessage());
                }
            }
        }
        return persistedObjectId;
    }*/

	/*
	 * This call back intercepts all updates to the database.
	 * return true only if the current state is modified in anyway. Do NOT modify the previousState.
	 */
	@Override
	public boolean onFlushDirty(final Object entity, Serializable id, final Object[] currentState,
					            final Object[] previousState, String[] propertyNames, Type[] types) {
		
		log.debug(this.getClass().getName() + ": Entering onFlushDirty()");
		//Study related property updates are spotted here for activating the corresponding rules.
		if(entity instanceof Study){
			for (int i = 0; i < propertyNames.length; i++) {
				if (propertyNames[i].equals("coordinatingCenterStudyStatus")){					
					handleStudyStatusChange(previousState[i], currentState[i], entity);										
				}
			}
		}
		
		//Study Site related property updates are spotted here for activating the corresponding rules.
		if(entity instanceof StudySite){
			for (int i = 0; i < propertyNames.length; i++) {
				if (propertyNames[i].equals("siteStudyStatus")){
					handleStudySiteStatusChange(previousState[i], currentState[i], entity);
				}
			}
		}		
		log.debug(this.getClass().getName() + ": Exiting onFlushDirty()");
		return false;
	}
	<commit_msg>Added a null check<commit_after>package edu.duke.cabig.c3pr.infrastructure.interceptor;

import java.io.Serializable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.EmptyInterceptor;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.type.Type;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import edu.duke.cabig.c3pr.constants.NotificationEventType;
import edu.duke.cabig.c3pr.dao.OrganizationDao;
import edu.duke.cabig.c3pr.domain.CoordinatingCenterStudyStatus;
import edu.duke.cabig.c3pr.domain.Organization;
import edu.duke.cabig.c3pr.domain.PlannedNotification;
import edu.duke.cabig.c3pr.domain.SiteStudyStatus;
import edu.duke.cabig.c3pr.domain.Study;
import edu.duke.cabig.c3pr.domain.StudySite;
import edu.duke.cabig.c3pr.service.impl.RulesDelegationServiceImpl;
import edu.duke.cabig.c3pr.tools.Configuration;

public class NotificationInterceptor extends EmptyInterceptor implements ApplicationContextAware {

	private static final Log log = LogFactory.getLog(NotificationInterceptor.class);
	
	RulesDelegationServiceImpl rulesDelegationService;
	
	private ApplicationContext applicationContext;
	
	private OrganizationDao organizationDao;
	
	private Configuration configuration; 
	
	public ApplicationContext getApplicationContext() {
		return applicationContext;
	}
	
	public void setApplicationContext(ApplicationContext applicationContext) {
		this.applicationContext = applicationContext;
	}
	
	public Organization getHostingOrganization(){
		return organizationDao.getByNciIdentifierForInterceptor(configuration.get(Configuration.LOCAL_NCI_INSTITUTE_CODE)).get(0);
	}
	
	
	/*
	 *  Return true only if the state is modified in anyway.
	 */
	@Override
	public boolean onSave(Object entity, Serializable id, Object[] state,
		String[] propertyNames, Type[] types) {
		log.debug(this.getClass().getName() + ": Entering onSave()");
		//SessionFactory sessionFactory = (SessionFactory)applicationContext.getBean("sessionFactory");
		//Session session  = sessionFactory.openSession();	
		//Serializable persistedObjectId = getObjectId(entity);
		//Object preUpdateState = session.get(entity.getClass(),  persistedObjectId);
		if(entity instanceof Study){			
			handleNewStudySaved((Study)entity);
		}
		if(entity instanceof StudySite){
			handleNewStudySiteSaved((StudySite)entity);
		}
		log.debug(this.getClass().getName() + ": Exiting onSave()");
		return false;
	}

/*	private Serializable getObjectId(Object obj) {
        
        Class objectClass = obj.getClass();
        Method[] methods = objectClass.getMethods();

        Serializable persistedObjectId = null;
        for (int ii = 0; ii < methods.length; ii++) {
            // If the method name equals 'getId' then invoke it to get the id of the object.
            if (methods[ii].getName().equals("getId")) {
                try {
                    persistedObjectId = (Serializable)methods[ii].invoke(obj, null);
                    break;      
                } catch (Exception e) {
                    log.warn("Audit Log Failed - Could not get persisted object id: " + e.getMessage());
                }
            }
        }
        return persistedObjectId;
    }*/

	/*
	 * This call back intercepts all updates to the database.
	 * return true only if the current state is modified in anyway. Do NOT modify the previousState.
	 */
	@Override
	public boolean onFlushDirty(final Object entity, Serializable id, final Object[] currentState,
					            final Object[] previousState, String[] propertyNames, Type[] types) {
		
		log.debug(this.getClass().getName() + ": Entering onFlushDirty()");
		//Study related property updates are spotted here for activating the corresponding rules.
		if(entity instanceof Study){
			for (int i = 0; i < propertyNames.length; i++) {
				if (propertyNames[i].equals("coordinatingCenterStudyStatus")){		
					if(previousState != null && currentState != null ){
						handleStudyStatusChange(previousState[i], currentState[i], entity);
					}
															
				}
			}
		}
		
		//Study Site related property updates are spotted here for activating the corresponding rules.
		if(entity instanceof StudySite){
			for (int i = 0; i < propertyNames.length; i++) {
				if (propertyNames[i].equals("siteStudyStatus")){
					if(previousState != null && currentState != null ){
						handleStudySiteStatusChange(previousState[i], currentState[i], entity);
					}
				}
			}
		}		
		log.debug(this.getClass().getName() + ": Exiting onFlushDirty()");
		return false;
	}
	
{'fraction_non_alphanumeric': 0.10410958904109589, 'fraction_numerical': 0.0032876712328767125, 'mean_word_length': 3.877605558524853, 'pattern_counts': {'":': 8, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3409054', 'n_tokens_mistral': 2844, 'n_tokens_neox': 2559, 'n_words': 602}