Skip to content

SAK-48970 gradebook supports removal of data when a site is hard deleted #13694

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,5 @@ List<AssignmentGradeRecord> getAssignmentGradeRecordsForAssignmentIdsAndStudentI

Optional<GradebookProperty> getGradebookProperty(String name);
GradebookProperty saveGradebookProperty(GradebookProperty property);
boolean isGradebookDefined(final String gradebookUid);
}
Original file line number Diff line number Diff line change
Expand Up @@ -934,5 +934,6 @@ public void updateExternalAssessmentComments(String gradebookUid, String siteId,
public Long getMatchingUserGradebookItemId(String siteId, String userId, String gradebookItemIdString);
public List<String> getGradebookInstancesForUser(String siteId, String userId);
public void initializeGradebooksForSite(String siteId);
public void hardDeleteGradebook(String siteId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ public interface GradebookRepository extends SpringCrudRepository<Gradebook, Lon

Optional<Gradebook> findByUid(String uid);
int deleteByUid(String uid);

public boolean isGradebookDefined(final String gradebookUid);
public int deleteSpreadsheetsForGradebook(final Long id);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what are spreadsheets?

}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ public void deleteGradebook(String gradebookUid) {

categoryRepository.deleteAll(categoryRepository.findByGradebook_Uid(gradebookUid));

gradebookRepository.deleteSpreadsheetsForGradebook(gradebook.getId());

permissionRepository.deleteAll(permissionRepository.findByGradebookId(gradebook.getId()));

gradeMappingRepository.deleteAll(gradeMappingRepository.findByGradebook_Uid(gradebookUid));

gradebookRepository.delete(gradebook);
Expand Down Expand Up @@ -389,4 +393,8 @@ public Optional<GradebookProperty> getGradebookProperty(String name) {
public GradebookProperty saveGradebookProperty(GradebookProperty property) {
return gradebookPropertyRepository.save(property);
}

public boolean isGradebookDefined(final String gradebookUid) {
return gradebookRepository.isGradebookDefined(gradebookUid);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5422,4 +5422,29 @@ public Long getMatchingUserGradebookItemId(String siteId, String userId, String

return null;
}

/*
@Override
public void hardDelete(String siteId) {
if (isGradebookDefined(context)) {
log.debug("Site " + context + " has been deleted. Removing associated gradebook data.");
try {
this.gradebookFrameworkService.deleteGradebook(context);
} catch (final GradebookNotFoundException e) {
log.debug("Couldnt find gradebook. Nothing to delete.", e);
}


*/

Comment on lines +5425 to +5439
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

Suggested change
/*
@Override
public void hardDelete(String siteId) {
if (isGradebookDefined(context)) {
log.debug("Site " + context + " has been deleted. Removing associated gradebook data.");
try {
this.gradebookFrameworkService.deleteGradebook(context);
} catch (final GradebookNotFoundException e) {
log.debug("Couldnt find gradebook. Nothing to delete.", e);
}
*/


public void hardDeleteGradebook(String siteId) {
try {
if (gradingPersistenceManager.isGradebookDefined(siteId)) {
deleteGradebook(siteId);
}
} catch (Exception e) {
log.warn("Could not hardDelete gradebook for context {}", siteId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
log.warn("Could not hardDelete gradebook for context {}", siteId);
log.warn("Could not hard delete gradebook for context {}", siteId, e);

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.transaction.annotation.Transactional;

import org.sakaiproject.grading.api.model.Gradebook;
import org.sakaiproject.grading.api.model.Spreadsheet;
import org.sakaiproject.grading.api.repository.GradebookRepository;
import org.sakaiproject.springframework.data.SpringCrudRepositoryImpl;

Expand All @@ -53,4 +54,33 @@ public int deleteByUid(String uid) {
delete.where(cb.equal(gradebook.get("uid"), uid));
return session.createQuery(delete).executeUpdate();
}

@Transactional(readOnly = true)
public boolean isGradebookDefined(final String gradebookUid) {
/*
return ((Long) sessionFactory.getCurrentSession().createCriteria(Gradebook.class)
.add(Restrictions.eq("uid", gradebookUid))
.setProjection(Projections.rowCount())
.uniqueResult()) == 1L;
*/
Comment on lines +60 to +65
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

Suggested change
/*
return ((Long) sessionFactory.getCurrentSession().createCriteria(Gradebook.class)
.add(Restrictions.eq("uid", gradebookUid))
.setProjection(Projections.rowCount())
.uniqueResult()) == 1L;
*/

Session session = sessionFactory.getCurrentSession();
CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery<Gradebook> query = cb.createQuery(Gradebook.class);
Root<Gradebook> gradebook = query.from(Gradebook.class);
query.where(cb.equal(gradebook.get("uid"), gradebookUid));
return session.createQuery(query).uniqueResultOptional().isPresent();
}

@Transactional
public int deleteSpreadsheetsForGradebook(final Long id) {
Session session = sessionFactory.getCurrentSession();
CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaDelete<Spreadsheet> delete = cb.createCriteriaDelete(Spreadsheet.class);
Root<Spreadsheet> spreadsheet = delete.from(Spreadsheet.class);
delete.where(cb.equal(spreadsheet.get("gradebook").get("id"), id));
return session.createQuery(delete).executeUpdate();
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.sakaiproject.entity.api.EntityManager;
import org.sakaiproject.entity.api.EntityProducer;
import org.sakaiproject.entity.api.EntityTransferrer;
import org.sakaiproject.entity.api.HardDeleteAware;
import org.sakaiproject.entity.api.Reference;
import org.sakaiproject.exception.IdUnusedException;
import org.sakaiproject.grading.api.GradingConstants;
Expand Down Expand Up @@ -61,7 +62,7 @@
* sites
*/
@Slf4j
public class GradebookNgEntityProducer implements EntityProducer, EntityTransferrer {
public class GradebookNgEntityProducer implements EntityProducer, EntityTransferrer, HardDeleteAware {

protected static final String[] TOOL_IDS = { "sakai.gradebookng" };

Expand Down Expand Up @@ -416,4 +417,10 @@ public Map<String, String> transferCopyEntities(String fromContext, String toCon
// now migrate
return this.transferCopyEntities(fromContext, toContext, ids, null);
}

@Override
public void hardDelete(String siteId) {
gradingService.hardDeleteGradebook(siteId);
}

}
Loading