Skip to content

Commit

Permalink
SAK-47604 Gradebook adding or removing a TA's permissions cause dupli…
Browse files Browse the repository at this point in the history
…cate values (sakaiproject#11712)

Co-authored-by: Earle Nietzel <[email protected]>
  • Loading branch information
zhuoY121 and ern authored Jul 7, 2023
1 parent accc5f5 commit 8fc515a
Showing 1 changed file with 16 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
Expand All @@ -28,14 +29,12 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import org.sakaiproject.springframework.data.PersistableEntity;
import org.sakaiproject.springframework.data.Repository;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Slf4j
@Transactional(readOnly = true)
public abstract class SpringCrudRepositoryImpl<T extends PersistableEntity<ID>, ID extends Serializable> implements SpringCrudRepository<T, ID> {

Expand Down Expand Up @@ -89,14 +88,14 @@ public <S extends T> Iterable<S> saveAll(Iterable<S> entities) {
public Optional<T> findById(ID id) {

Assert.notNull(id, "The id cannot be null");
return Optional.ofNullable((T) sessionFactory.getCurrentSession().get(domainClass, id));
return Optional.ofNullable(sessionFactory.getCurrentSession().get(domainClass, id));
}

@Override
public T getById(ID id) {

Assert.notNull(id, "The id cannot be null");
return (T) sessionFactory.getCurrentSession().load(domainClass, id);
return sessionFactory.getCurrentSession().load(domainClass, id);
}

@Override
Expand All @@ -116,7 +115,7 @@ public Page<T> findAll(Pageable pageable) {

Criteria criteria = sessionFactory.getCurrentSession().createCriteria(domainClass);
criteria.setFirstResult((int) pageable.getOffset());
criteria.setMaxResults((int) pageable.getPageSize());
criteria.setMaxResults(pageable.getPageSize());
return new PageImpl(criteria.list());
}

Expand All @@ -125,21 +124,18 @@ public Iterable<T> findAllById(Iterable<ID> ids) {

List<T> list = new ArrayList<>();
if (ids != null) {
ids.forEach(id -> findById(id).ifPresent(found -> list.add(found)));
ids.forEach(id -> findById(id).ifPresent(list::add));
}
return list;
}

@Override
@Transactional
public void delete(T entity) {

Session session = sessionFactory.getCurrentSession();

try {
session.delete(entity);
} catch (Exception he) {
session.delete(session.merge(entity));
if (entity != null) {
deleteById(entity.getId());
} else {
log.warn("Can not perform delete on a null entity");
}
}

Expand All @@ -161,7 +157,12 @@ public void deleteAll(Iterable<? extends T> entities) {
@Override
@Transactional
public void deleteById(ID id) {
findById(id).ifPresent(found -> delete(found));
if (id != null) {
Session session = sessionFactory.getCurrentSession();
findById(id).ifPresent(session::delete);
} else {
log.warn("Can not perform delete with a null id");
}
}

/**
Expand Down

0 comments on commit 8fc515a

Please sign in to comment.