Skip to content

Commit

Permalink
SAK-48874 Assignments implementation of Interface HardDeleteAware (#1…
Browse files Browse the repository at this point in the history
…1569)

Co-authored-by: Earle Nietzel <earle@longsight.com>
  • Loading branch information
jkjanetschek and ern authored Jun 3, 2024
1 parent 821b8d9 commit 7ca0102
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ public class AssignmentAllPurposeItem extends AssignmentSupplementItemWithAttach
@Column(name = "HIDE", nullable = false)
private Boolean hide;

@OneToMany(mappedBy = "assignmentAllPurposeItem")
@OneToMany(mappedBy = "assignmentAllPurposeItem", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<AssignmentAllPurposeItemAccess> accessSet;
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
import org.sakaiproject.calendar.api.CalendarEvent;
import org.sakaiproject.calendar.api.CalendarService;
import org.sakaiproject.component.api.ServerConfigurationService;
import org.sakaiproject.content.api.ContentCollection;
import org.sakaiproject.content.api.ContentHostingService;
import org.sakaiproject.content.api.ContentResource;
import org.sakaiproject.content.api.ContentResourceEdit;
Expand Down Expand Up @@ -5178,6 +5179,73 @@ public String getAssignmentModifier(String modifier) {
}
}

/**
* Implementation of HardDeleteAware to allow content to be fully purged
*/
public void hardDelete(String siteId) {
log.info("Hard Delete of Tool Assignments for context: {}", siteId);

Collection<Assignment> assignments = getDeletedAssignmentsForContext(siteId);
assignments.addAll(getAssignmentsForContext(siteId));

//remove associated tags and delete assignment
Iterator<Assignment> it = assignments.iterator();
while (it.hasNext()) {
Assignment a = (org.sakaiproject.assignment.api.model.Assignment) it.next();
removeAssociatedTaggingItem(a);

try {
deleteAssignment(a);
} catch (PermissionException e) {
log.error("insufficient permissions to delete assignment ", e);
}
}

//remove attachements
List<ContentResource> resources = contentHostingService.getAllResources("/attachment/" + siteId + "/Assignments/");
for (ContentResource resource : resources) {
log.debug("Removing resource: {}", resource.getId());
try {
contentHostingService.removeResource(resource.getId());
} catch (Exception e) {
log.warn("Failed to remove content.", e);
}
}

// Cleanup the collections
ContentCollection contentCollection = null;
try {
contentCollection = contentHostingService.getCollection("/attachment/" + siteId + "/Assignments/");
} catch (IdUnusedException e) {
log.warn("id for collection does not exist " + e);
} catch (TypeException e1) {
log.warn("not a collection " + e1);
} catch (PermissionException e2) {
log.warn("insufficient permissions " + e2);
}

try{
if(contentCollection != null){
List<String> members = contentCollection.getMembers();
for(String member : members){
log.debug("remove contenCollection: " + member);
contentHostingService.removeCollection(member);
}
contentHostingService.removeCollection(contentCollection.getId());
}
}catch (IdUnusedException e) {
log.warn("id for collection does not exist " + e);
} catch (TypeException e1) {
log.warn("not a collection " + e1);
} catch (PermissionException e2) {
log.warn("insufficient permissions " + e2);
}catch (InUseException e3){
log.warn("InUseException " + e3);
}catch (ServerOverloadException e4){
log.warn("ServerOverloadException " + e4);
}
}

@Override
public boolean allowAddTags(String context) {
String resourceString = AssignmentReferenceReckoner.reckoner().context(context).reckon().getReference();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,15 @@

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.LockOptions;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.persister.collection.CollectionPropertyNames;
import org.sakaiproject.assignment.api.AssignmentConstants;
import org.sakaiproject.assignment.api.model.Assignment;
import org.sakaiproject.assignment.api.model.AssignmentSubmission;
import org.sakaiproject.assignment.api.model.AssignmentSubmissionSubmitter;
import org.sakaiproject.assignment.api.model.*;
import org.sakaiproject.assignment.api.persistence.AssignmentRepository;
import org.sakaiproject.hibernate.HibernateCriterionUtils;
import org.sakaiproject.serialization.BasicSerializableRepository;
Expand Down Expand Up @@ -114,6 +113,7 @@ public void deleteAssignment(String assignmentId) {
Assignment assignment = findOne(assignmentId);
if (assignment != null) {
delete(assignment);
hardDeleteHelper(assignmentId);
}
}

Expand Down Expand Up @@ -330,4 +330,53 @@ public Collection<String> findGroupsForAssignmentById(String assignmentId) {
.getResultList();
return result.stream().map(tuple -> (String) tuple.get(0)).collect(Collectors.toList());
}

private void hardDeleteHelper(String assignmentId){


try{
// only one per assignment
AssignmentAllPurposeItem apItem = (AssignmentAllPurposeItem) sessionFactory.getCurrentSession().createCriteria(AssignmentAllPurposeItem.class).add(Restrictions.eq("assignmentId", assignmentId)).uniqueResult();
if (apItem != null){
log.info("delete AssignmentAllPurposeItem for assignment: {}", assignmentId);
sessionFactory.getCurrentSession().delete(apItem);
}


// only one per assignment
AssignmentModelAnswerItem maItem = (AssignmentModelAnswerItem) sessionFactory.getCurrentSession().createCriteria(AssignmentModelAnswerItem.class).add(Restrictions.eq("assignmentId", assignmentId)).uniqueResult();
if(maItem != null){
log.info("delete AssignmentModelAnswerItem for assignment: {}", assignmentId);
sessionFactory.getCurrentSession().delete(maItem);
}


// only one per assignment
AssignmentNoteItem noteItem = (AssignmentNoteItem) sessionFactory.getCurrentSession().createCriteria(AssignmentNoteItem.class).add(Restrictions.eq("assignmentId", assignmentId)).uniqueResult();
if (noteItem != null) {
log.info("delete AssignmentNoteItem for assignment: {}", assignmentId);
sessionFactory.getCurrentSession().delete(noteItem);
}

// multiple possible per assignment
List<PeerAssessmentItem> peerAssessmentItems = (List<PeerAssessmentItem>) sessionFactory.getCurrentSession().createCriteria(PeerAssessmentItem.class).add(Restrictions.eq("assignmentId", assignmentId)).list();
if (!peerAssessmentItems.isEmpty()){
for(PeerAssessmentItem item : peerAssessmentItems){
//get submissionId and assessor_user_id for deletion of PeerAssessmentAttachment
String submissionId = item.getId().getSubmissionId();
String assessorUserId = item.getId().getAssessorUserId();
sessionFactory.getCurrentSession().delete(item);
List<PeerAssessmentAttachment> peerAssessmentItemAttach = (List) sessionFactory.getCurrentSession().createCriteria(PeerAssessmentAttachment.class).add(Restrictions.eq("submissionId", submissionId)).add(Restrictions.eq("assessorUserId", assessorUserId)).list();
if(peerAssessmentItemAttach.size() != 0){
for(PeerAssessmentAttachment attach: peerAssessmentItemAttach)
sessionFactory.getCurrentSession().delete(attach);
}
}
}

}catch (HibernateException e){
log.error("error hardDelete of assignment: {}", assignmentId, e);
}

}
}

0 comments on commit 7ca0102

Please sign in to comment.