Skip to content

Commit

Permalink
OEQ-2051 - chore: delete related key resources when item is deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
edalex-yinzi committed Aug 14, 2024
1 parent 5235a5a commit 59a3274
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,18 @@
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import org.hibernate.annotations.AttributeAccessor;
import org.hibernate.annotations.Index;

@Entity
@Table(name = "hierarchy_topic_key_resources")
@AttributeAccessor("field")
@NamedQuery(
name = "getByItemUuidAndInstitution",
query =
"FROM HierarchyTopicKeyResource t WHERE t.itemUuid = :itemUuid AND t.institution = :institution")
public class HierarchyTopicKeyResource {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Expand Down
6 changes: 0 additions & 6 deletions Source/Plugins/Core/com.equella.core/plugin-jpf.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1405,12 +1405,6 @@
<parameter id="frequency" value="daily" />
<parameter id="scope" value="server" />
</extension>
<extension plugin-id="com.tle.core.scheduler" point-id="scheduledTask" id="removeInvalidDynamicKeyResource">
<parameter id="id" value="Remove Invalid Dynamic Key Resource" />
<parameter id="bean" value="bean:com.tle.core.scheduler.standard.task.RemoveInvalidDynamicKeyResource" />
<parameter id="frequency" value="weekly" />
<parameter id="scope" value="institution" />
</extension>
<extension plugin-id="com.tle.web.workflow" point-id="bulkExtension" id="bulkApproveOp">
<parameter id="bean" value="bean:com.tle.web.bulk.workflowtask.dialog.BulkWorkflowApproveTaskOperation" />
</extension>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public interface HierarchyDao extends AbstractTreeDao<HierarchyTopic> {
HierarchyTopic findByUuid(String uuid, Institution institution);

/** Save the given key resource entity into DB */
void saveKeyResources(HierarchyTopicKeyResource entity);
void saveKeyResource(HierarchyTopicKeyResource entity);

/** Get all key resources for a given topic in a given institution. */
List<HierarchyTopicKeyResource> getKeyResources(
Expand All @@ -55,6 +55,10 @@ List<HierarchyTopicKeyResource> getKeyResources(
List<HierarchyTopicKeyResource> getKeyResources(
String itemUuid, int itemVersion, Institution institution);

/** Get all key resources for a given item UUID and institution. */
List<HierarchyTopicKeyResource> getKeyResourcesByItemUuid(
String itemUuid, Institution institution);

/** Get a key resource for a given item in a given topic. */
Optional<HierarchyTopicKeyResource> getKeyResource(
String legacyHierarchyCompoundUuid,
Expand All @@ -65,6 +69,9 @@ Optional<HierarchyTopicKeyResource> getKeyResource(
/** Get all key resources for a given institution. */
List<HierarchyTopicKeyResource> getAllKeyResources(Institution institution);

/** Delete key resource by a given entity. */
void deleteKeyResource(HierarchyTopicKeyResource entity);

/** Delete a key resource for a given item in a given topic. */
void deleteKeyResource(String topicId, String itemUuid, int itemVersion);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ FreeTextBooleanQuery getSearchClause(
/** Get all key resources for a given hierarchy compound UUID. */
List<HierarchyTopicKeyResource> getKeyResources(HierarchyCompoundUuid compoundUuid);

/** Get all key resources for a given item UUID. */
List<HierarchyTopicKeyResource> getKeyResources(String itemUuid);

/** Get all key resources and convert to Item for a given hierarchy compound UUID. */
List<Item> getKeyResourceItems(HierarchyCompoundUuid compoundUuid);

Expand Down Expand Up @@ -161,8 +164,15 @@ void updateKeyResources(

void delete(HierarchyTopic topic);

/** Delete key resource by the given key resource entity. */
void deleteKeyResource(HierarchyTopicKeyResource keyResource);

/** Delete key resource by the given itemId from current institution. */
void deleteKeyResources(ItemKey itemId);

void deleteKeyResources(Item item);

/** Delete key resource by the given itemId from the given hierarchy. */
void deleteKeyResources(HierarchyCompoundUuid hierarchyCompoundUuid, ItemKey itemId);

void removeDeletedItemReference(String uuid, int version);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ public void doImport(TemporaryFileHandle staging, Institution institution, Conve

entries.forEach(
entry -> {
HierarchyTopicKeyResource keyResources =
HierarchyTopicKeyResource keyResource =
xmlHelper.readXmlFile(keyResourceImportFolder, entry);
keyResources.setInstitution(institution);
keyResource.setInstitution(institution);

hierarchyDao.saveKeyResources(keyResources);
hierarchyDao.saveKeyResource(keyResource);
hierarchyDao.flush();
hierarchyDao.clear();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,15 @@ public void deleteAllKeyResources(Institution institution) {
}

@Override
public void saveKeyResources(HierarchyTopicKeyResource entity) {
public void saveKeyResource(HierarchyTopicKeyResource entity) {
getHibernateTemplate().save(entity);
}

@Override
public void deleteKeyResource(HierarchyTopicKeyResource entity) {
getHibernateTemplate().delete(entity);
}

@Override
public List<HierarchyTopicKeyResource> getKeyResources(
String dynamicHierarchyId, Institution institution) {
Expand Down Expand Up @@ -259,6 +264,21 @@ public List<HierarchyTopicKeyResource> getKeyResources(
return keyResources;
}

@Override
public List<HierarchyTopicKeyResource> getKeyResourcesByItemUuid(
String itemUuid, Institution institution) {
return (List<HierarchyTopicKeyResource>)
getHibernateTemplate()
.execute(
(Session session) -> {
return session
.getNamedQuery("getByItemUuidAndInstitution")
.setParameter("itemUuid", itemUuid)
.setParameter("institution", CurrentInstitution.get())
.list();
});
}

@Override
public List<HierarchyTopicKeyResource> getAllKeyResources(Institution institution) {
List<HierarchyTopicKeyResource> keyResources =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.tle.beans.hierarchy.HierarchyTreeNode;
import com.tle.beans.item.Item;
import com.tle.beans.item.ItemId;
import com.tle.beans.item.ItemIdKey;
import com.tle.beans.item.ItemKey;
import com.tle.common.Check;
import com.tle.common.beans.exception.ValidationError;
Expand Down Expand Up @@ -184,7 +185,7 @@ public void addKeyResource(HierarchyCompoundUuid compoundUuid, ItemKey itemId) {
newKeyResources.setItemVersion(itemId.getVersion());
newKeyResources.setInstitution(CurrentInstitution.get());
newKeyResources.setDateCreated(new Date());
dao.saveKeyResources(newKeyResources);
dao.saveKeyResource(newKeyResources);
}

/** Check whether the given item is a key resource of the given hierarchy topic. */
Expand All @@ -198,6 +199,18 @@ public boolean hasKeyResource(HierarchyCompoundUuid compoundUuid, ItemKey itemId
return keyResource.isPresent();
}

@Override
@Transactional
public void deleteKeyResource(HierarchyTopicKeyResource keyResource) {
dao.deleteKeyResource(keyResource);
}

@Override
@Transactional
public void deleteKeyResources(ItemKey itemId) {
dao.deleteKeyResources(itemId.getUuid(), itemId.getVersion(), CurrentInstitution.get());
}

@Override
@Transactional
public void deleteKeyResources(Item item) {
Expand Down Expand Up @@ -681,9 +694,31 @@ public XStream getXStream() {

@Override
public void itemDeletedEvent(ItemDeletedEvent event) {
Item item = new Item();
item.setId(event.getKey());
// TODO: OEQ-2051 Delete related key resources
// When an Item delete event is published, a key resource should be deleted when:
// * the referenced Item version is exactly the same as the Item to be deleted, or
// * pointing to the latest version (which is represented by 0) and there are not any more
// versions of that Item.
// In the second case, please note that the Item is NOT removed until all the listeners for Item
// delete event complete their tasks.
// This means when there is only one version of that Item remaining, the pointing to the latest
// version key resource should be removed as well.
final int lastItemFlag = 1;

ItemIdKey itemIdKey = event.getItemId();
String deletedItemUuid = itemIdKey.getUuid();
int deletedItemVersion = itemIdKey.getVersion();

List<Item> allItems = itemService.getVersionDetails(deletedItemUuid);

getKeyResources(deletedItemUuid)
.forEach(
keyResource -> {
int keyResourceItemVersion = keyResource.getItemVersion();
if (keyResourceItemVersion == deletedItemVersion
|| (keyResourceItemVersion == 0 && allItems.size() == lastItemFlag)) {
deleteKeyResource(keyResource);
}
});
}

@Override
Expand Down Expand Up @@ -722,6 +757,11 @@ public List<HierarchyTopicKeyResource> getKeyResources(HierarchyCompoundUuid com
return dao.getKeyResources(compoundUuid.buildString(true), CurrentInstitution.get());
}

@Override
public List<HierarchyTopicKeyResource> getKeyResources(String itemUuid) {
return dao.getKeyResourcesByItemUuid(itemUuid, CurrentInstitution.get());
}

@Override
public List<Item> getKeyResourceItems(HierarchyCompoundUuid compoundUuid) {
// HierarchyTopicKeyResource.
Expand Down

This file was deleted.

0 comments on commit 59a3274

Please sign in to comment.