Java Code Examples for org.hibernate.Hibernate#getClass()

The following examples show how to use org.hibernate.Hibernate#getClass() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClosureEventTriggeringInterceptor.java    From gorm-hibernate5 with Apache License 2.0 6 votes vote down vote up
public boolean onPreInsert(PreInsertEvent hibernateEvent) {
    Object entity = hibernateEvent.getEntity();
    Class type = Hibernate.getClass(entity);
    PersistentEntity persistentEntity = mappingContext.getPersistentEntity(type.getName());
    AbstractPersistenceEvent grailsEvent;
    ModificationTrackingEntityAccess entityAccess = null;
    if(persistentEntity != null) {
        entityAccess = new ModificationTrackingEntityAccess(mappingContext.createEntityAccess(persistentEntity, entity));
        grailsEvent = new org.grails.datastore.mapping.engine.event.PreInsertEvent(this.datastore, persistentEntity, entityAccess);
    }
    else {
        grailsEvent = new org.grails.datastore.mapping.engine.event.PreInsertEvent(this.datastore, entity);
    }

    publishEvent(hibernateEvent, grailsEvent);

    boolean cancelled = grailsEvent.isCancelled();
    if(!cancelled && entityAccess != null) {
        synchronizeHibernateState(hibernateEvent, entityAccess);
    }
    return cancelled;
}
 
Example 2
Source File: ClosureEventTriggeringInterceptor.java    From gorm-hibernate5 with Apache License 2.0 6 votes vote down vote up
public boolean onPreUpdate(PreUpdateEvent hibernateEvent) {
    Object entity = hibernateEvent.getEntity();
    Class type = Hibernate.getClass(entity);
    MappingContext mappingContext = datastore.getMappingContext();
    PersistentEntity persistentEntity = mappingContext.getPersistentEntity(type.getName());
    AbstractPersistenceEvent grailsEvent;
    ModificationTrackingEntityAccess entityAccess = null;
    if(persistentEntity != null) {
        entityAccess = new ModificationTrackingEntityAccess(mappingContext.createEntityAccess(persistentEntity, entity));
        grailsEvent = new org.grails.datastore.mapping.engine.event.PreUpdateEvent(this.datastore, persistentEntity, entityAccess);
    }
    else {
        grailsEvent = new org.grails.datastore.mapping.engine.event.PreUpdateEvent(this.datastore, entity);
    }

    publishEvent(hibernateEvent, grailsEvent);
    boolean cancelled = grailsEvent.isCancelled();
    if(!cancelled && entityAccess != null) {
        boolean autoTimestamp = persistentEntity.getMapping().getMappedForm().isAutoTimestamp();
        synchronizeHibernateState(hibernateEvent, entityAccess, autoTimestamp);
    }
    return cancelled;

}
 
Example 3
Source File: PersistenceUnitUtilImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private Object getIdentifierFromPersister(Object entity) {
	Class<?> entityClass = Hibernate.getClass( entity );
	final EntityPersister persister;
	try {
		persister = sessionFactory.getMetamodel().entityPersister( entityClass );
		if ( persister == null ) {
			throw new IllegalArgumentException( entityClass.getName() + " is not an entity" );
		}
	}
	catch (MappingException ex) {
		throw new IllegalArgumentException( entityClass.getName() + " is not an entity", ex );
	}
	return persister.getIdentifier( entity, null );
}
 
Example 4
Source File: HibernateEventListener.java    From gorm-hibernate5 with Apache License 2.0 5 votes vote down vote up
protected ClosureEventListener findEventListener(Object entity, SessionFactoryImplementor factory) {
    if (entity == null) return null;
    Class<?> clazz = Hibernate.getClass(entity);

    SoftKey<Class<?>> key = new SoftKey<Class<?>>(clazz);
    ClosureEventListener eventListener = eventListeners.get(key);
    if (eventListener != null) {
        return eventListener;
    }

    Boolean shouldTrigger = cachedShouldTrigger.get(key);
    if (shouldTrigger == null || shouldTrigger) {
        synchronized(cachedShouldTrigger) {
            eventListener = eventListeners.get(key);
            if (eventListener == null) {
                AbstractHibernateDatastore datastore = getDatastore();
                boolean isValidSessionFactory = MultiTenant.class.isAssignableFrom(clazz) || factory == null || datastore.getSessionFactory().equals(factory);
                PersistentEntity persistentEntity = datastore.getMappingContext().getPersistentEntity(clazz.getName());
                shouldTrigger = (persistentEntity != null && isValidSessionFactory);
                if (shouldTrigger) {
                    eventListener = new ClosureEventListener(persistentEntity, failOnError, failOnErrorPackages);
                    ClosureEventListener previous = eventListeners.putIfAbsent(key, eventListener);
                    if (previous != null) {
                        eventListener = previous;
                    }
                }
                cachedShouldTrigger.put(key, shouldTrigger);
            }
        }
    }
    return eventListener;
}
 
Example 5
Source File: LamsToolService.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
    * Finds the nearest Leader Select activity. Works recursively. Tries to find Leader Select activity in the previous
    * activities set first, and then inside the parent set.
    */
   @SuppressWarnings("rawtypes")
   private Activity getNearestLeaderSelectionActivity(Activity activity, Integer userId, Long lessonId) {
// check if current activity is Leader Select one. if so - stop searching and return it.
Class activityClass = Hibernate.getClass(activity);
if (activityClass.equals(ToolActivity.class)) {
    ToolActivity toolActivity;

    // activity is loaded as proxy due to lazy loading and in order to prevent quering DB we just re-initialize
    // it here again
    Hibernate.initialize(activity);
    if (activity instanceof HibernateProxy) {
	toolActivity = (ToolActivity) ((HibernateProxy) activity).getHibernateLazyInitializer()
		.getImplementation();
    } else {
	toolActivity = (ToolActivity) activity;
    }

    if (ILamsToolService.LEADER_SELECTION_TOOL_SIGNATURE.equals(toolActivity.getTool().getToolSignature())) {
	return activity;
    }

    //in case of a floating activity
} else if (activityClass.equals(FloatingActivity.class)) {
    LearnerProgress learnerProgress = lessonService.getUserProgressForLesson(userId, lessonId);
    Map<Activity, CompletedActivityProgress> completedActivities = learnerProgress.getCompletedActivities();

    //find the earliest finished Leader Select Activity
    Date leaderSelectActivityFinishDate = null;
    Activity leaderSelectionActivity = null;
    for (Activity completedActivity : completedActivities.keySet()) {

	if (completedActivity instanceof ToolActivity) {
	    ToolActivity completedToolActivity = (ToolActivity) completedActivity;
	    if (ILamsToolService.LEADER_SELECTION_TOOL_SIGNATURE
		    .equals(completedToolActivity.getTool().getToolSignature())) {
		Date finishDate = completedActivities.get(completedActivity).getFinishDate();

		if ((leaderSelectActivityFinishDate == null)
			|| (finishDate.compareTo(leaderSelectActivityFinishDate) < 0)) {
		    leaderSelectionActivity = completedToolActivity;
		    leaderSelectActivityFinishDate = completedActivities.get(completedActivity).getFinishDate();
		}

	    }
	}

    }

    return leaderSelectionActivity;
}

// check previous activity
Transition transitionTo = activity.getTransitionTo();
if (transitionTo != null) {
    Activity fromActivity = transitionTo.getFromActivity();
    return getNearestLeaderSelectionActivity(fromActivity, userId, lessonId);
}

// check parent activity
Activity parent = activity.getParentActivity();
if (parent != null) {
    return getNearestLeaderSelectionActivity(parent, userId, lessonId);
}

return null;
   }
 
Example 6
Source File: LamsToolService.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
    * Finds all preceding activities that can provide confidence levels (currently only Assessment and MCQ provide
    * them). Please note, it does not check whether enableConfidenceLevels advanced option is ON in those activities.
    */
   @SuppressWarnings("rawtypes")
   private void findPrecedingConfidenceProvidingActivities(Activity activity,
    Set<Long> confidenceProvidingActivityIds) {
// check if current activity is Leader Select one. if so - stop searching and return it.
Class activityClass = Hibernate.getClass(activity);
if (activityClass.equals(ToolActivity.class)) {
    ToolActivity toolActivity;

    // activity is loaded as proxy due to lazy loading and in order to prevent quering DB we just re-initialize
    // it here again
    Hibernate.initialize(activity);
    if (activity instanceof HibernateProxy) {
	toolActivity = (ToolActivity) ((HibernateProxy) activity).getHibernateLazyInitializer()
		.getImplementation();
    } else {
	toolActivity = (ToolActivity) activity;
    }

    String toolSignature = toolActivity.getTool().getToolSignature();
    if (TOOL_SIGNATURE_ASSESSMENT.equals(toolSignature) || TOOL_SIGNATURE_MCQ.equals(toolSignature)) {
	confidenceProvidingActivityIds.add(toolActivity.getActivityId());
    }

    //in case of a floating activity - return all available confidence providing activities
} else if (activityClass.equals(FloatingActivity.class)) {
    Set<Activity> activities = activity.getLearningDesign().getActivities();
    for (Activity activityIter : activities) {
	if (activityIter instanceof ToolActivity) {
	    String toolSignatureIter = ((ToolActivity) activityIter).getTool().getToolSignature();
	    if (TOOL_SIGNATURE_ASSESSMENT.equals(toolSignatureIter)
		    || TOOL_SIGNATURE_MCQ.equals(toolSignatureIter)) {
		confidenceProvidingActivityIds.add(activityIter.getActivityId());
	    }
	}
    }
    return;
}

// check previous activity
Transition transitionTo = activity.getTransitionTo();
if (transitionTo != null) {
    Activity fromActivity = transitionTo.getFromActivity();
    findPrecedingConfidenceProvidingActivities(fromActivity, confidenceProvidingActivityIds);
    return;
}

// check parent activity
Activity parent = activity.getParentActivity();
if (parent != null) {
    findPrecedingConfidenceProvidingActivities(parent, confidenceProvidingActivityIds);
    return;
}
   }