Skip to content

Commit

Permalink
SAK-40848 allow removing attachments from items
Browse files Browse the repository at this point in the history
  • Loading branch information
bgarciaentornos authored and Miguel Pellicer committed Oct 29, 2018
1 parent f1143ac commit 8a501f1
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1202,7 +1202,7 @@ private ItemFacade loadItem(boolean isEditPendingAssessmentFlow) {
ItemService service = loadItemService(isEditPendingAssessmentFlow);
ItemFacade itemData = null;
// itemId == null => new questiion
if (this.itemId!=null){
if (StringUtils.isNotEmpty(this.itemId)){
try{
itemData = service.getItem(this.itemId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,6 @@ else if(item.getTypeId().equals(TypeFacade.IMAGEMAP_QUESTION)) {
else { //Other Types
item.setItemTextSet(prepareText(item, bean, itemauthor));
}


// prepare MetaData
item.setItemMetaDataSet(prepareMetaData(item, bean));
Expand All @@ -894,7 +893,7 @@ else if(item.getTypeId().equals(TypeFacade.IMAGEMAP_QUESTION)) {
}
}

updateAttachments(itemauthor.getAttachmentList(), item);
updateAttachments(itemauthor.getAttachmentList(), item, delegate);

//Manage the tags.
String[] tagsFromForm= FacesContext.getCurrentInstance().getExternalContext().getRequestParameterValuesMap().get("tag_selector[]");
Expand Down Expand Up @@ -938,7 +937,6 @@ else if(item.getTypeId().equals(TypeFacade.IMAGEMAP_QUESTION)) {
}
itemauthor.setTagsList(item.getItemTagSet());//To avoid add extra labels when refreshing the page manually.


if (isFromQuestionPool) {
// Came from Pool manager
if (item.getTypeId().equals(TypeFacade.EXTENDED_MATCHING_ITEMS)) {
Expand All @@ -953,11 +951,8 @@ else if(item.getTypeId().equals(TypeFacade.IMAGEMAP_QUESTION)) {

delegate.saveItem(item);



item = delegate.getItem(item.getItemId().toString());


QuestionPoolService qpdelegate = new QuestionPoolService();

if (!qpdelegate.hasItem(item.getItemIdString(),
Expand Down Expand Up @@ -1031,7 +1026,6 @@ else if (itemauthor.getTarget() != null && (itemauthor.getTarget().equals("samba
SectionFacade origsect= assessdelegate.getSection(bean.getOrigSection());
shiftItemsInOrigSection(delegate, origsect, oldSeq);


}
else {
// no action needed
Expand Down Expand Up @@ -3061,7 +3055,7 @@ private void updateItemTextAttachment(List newList, ItemTextIfc targetItemText,
}
}

private void updateAttachments(List newList, ItemFacade targetItem) {
private void updateAttachments(List newList, ItemFacade targetItem, ItemService delegate) {
final Map<Long, ItemAttachmentIfc> oldIds = targetItem.getItemAttachmentMap();
if ( newList != null && !(newList.isEmpty()) ) {
for (Object o : newList) {
Expand All @@ -3078,6 +3072,7 @@ private void updateAttachments(List newList, ItemFacade targetItem) {
// any "oldIds" left over must be orphans. delete them.
for ( Map.Entry<Long, ItemAttachmentIfc> e : oldIds.entrySet() ) {
targetItem.removeItemAttachment(e.getValue());
delegate.removeItemAttachment(e.getKey());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,16 @@
<key column="ITEMID"/>
<one-to-many class="org.sakaiproject.tool.assessment.data.dao.assessment.ItemFeedback"/>
</set>
<set name="itemAttachmentSet" table="SAM_ATTACHMENT_T" cascade="all-delete-orphan" order-by="createdDate asc" inverse="true" lazy="false" batch-size="50">
<set name="itemAttachmentSet" table="SAM_ATTACHMENT_T" cascade="all" order-by="createdDate asc" inverse="true" lazy="false" batch-size="50">
<key column="ITEMID"/>
<one-to-many class="org.sakaiproject.tool.assessment.data.dao.assessment.ItemAttachment"/>
</set>
<set name="itemTagSet" table="SAM_ITEMTAG_T" cascade="all-delete-orphan" inverse="true" lazy="false" batch-size="50" >
<set name="itemTagSet" table="SAM_ITEMTAG_T" cascade="all" inverse="true" lazy="false" batch-size="50" >
<key column="ITEMID"/>
<one-to-many class="org.sakaiproject.tool.assessment.data.dao.assessment.ItemTag" />
</set>
</class>


<class name="org.sakaiproject.tool.assessment.data.dao.assessment.ItemText" table="SAM_ITEMTEXT_T">
<id name="id" column="ITEMTEXTID" type="java.lang.Long" unsaved-value="0">
<generator class="native">
Expand All @@ -83,7 +82,7 @@
<key column="ITEMTEXTID"/>
<one-to-many class="org.sakaiproject.tool.assessment.data.dao.assessment.Answer"/>
</set>
<set name="itemTextAttachmentSet" table="SAM_ATTACHMENT_T" cascade="all-delete-orphan" order-by="createdDate asc" inverse="true" lazy="false" batch-size="50">
<set name="itemTextAttachmentSet" table="SAM_ATTACHMENT_T" cascade="all" order-by="createdDate asc" inverse="true" lazy="false" batch-size="50">
<key column="ITEMTEXTID"/>
<one-to-many class="org.sakaiproject.tool.assessment.data.dao.assessment.ItemTextAttachment"/>
</set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@
<key column="ITEMID"/>
<one-to-many class="org.sakaiproject.tool.assessment.data.dao.assessment.PublishedItemFeedback"/>
</set>
<set name="itemAttachmentSet" table="SAM_PUBLISHEDATTACHMENT_T" cascade="all-delete-orphan" order-by="createdDate asc" inverse="true" lazy="false" batch-size="50">
<set name="itemAttachmentSet" table="SAM_PUBLISHEDATTACHMENT_T" cascade="all" order-by="createdDate asc" inverse="true" lazy="false" batch-size="50">
<key column="ITEMID"/>
<one-to-many class="org.sakaiproject.tool.assessment.data.dao.assessment.PublishedItemAttachment"/>
</set>
<set name="itemTagSet" table="SAM_ITEMTAG_T" cascade="all-delete-orphan" inverse="true" lazy="false" batch-size="50" >
<set name="itemTagSet" table="SAM_ITEMTAG_T" cascade="all" inverse="true" lazy="false" batch-size="50" >
<key column="ITEMID"/>
<one-to-many class="org.sakaiproject.tool.assessment.data.dao.assessment.PublishedItemTag" />
</set>
Expand All @@ -74,7 +74,7 @@
<key column="ITEMTEXTID"/>
<one-to-many class="org.sakaiproject.tool.assessment.data.dao.assessment.PublishedAnswer"/>
</set>
<set name="itemTextAttachmentSet" table="SAM_PUBLISHEDATTACHMENT_T" cascade="all-delete-orphan" order-by="createdDate asc" inverse="true" lazy="false" batch-size="50">
<set name="itemTextAttachmentSet" table="SAM_PUBLISHEDATTACHMENT_T" cascade="all" order-by="createdDate asc" inverse="true" lazy="false" batch-size="50">
<key column="ITEMTEXTID"/>
<one-to-many class="org.sakaiproject.tool.assessment.data.dao.assessment.PublishedItemTextAttachment"/>
</set>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import lombok.extern.slf4j.Slf4j;
import org.hibernate.Query;
import org.sakaiproject.tool.assessment.data.dao.assessment.ItemAttachment;
import org.sakaiproject.tool.assessment.data.dao.assessment.ItemData;
import org.sakaiproject.tool.assessment.data.dao.assessment.ItemMetaData;
import org.sakaiproject.tool.assessment.data.dao.shared.TypeD;
Expand Down Expand Up @@ -310,6 +311,25 @@ public ItemFacade saveItem(ItemFacade item) throws DataFacadeException {
return list.isEmpty() ? null : list.get(0);
}

public void removeItemAttachment(Long itemAttachmentId) {
ItemAttachment itemAttachment = getHibernateTemplate().load(ItemAttachment.class, itemAttachmentId);
ItemDataIfc item = itemAttachment.getItem();
int retryCount = PersistenceService.getInstance().getPersistenceHelper().getRetryCount();
while (retryCount > 0) {
try {
if (item != null) {
Set set = item.getItemAttachmentSet();
set.remove(itemAttachment);
getHibernateTemplate().delete(getHibernateTemplate().merge(itemAttachment));
retryCount = 0;
}
} catch (Exception e) {
log.warn("Error while trying to delete itemAttachment: " + e.getMessage());
retryCount = PersistenceService.getInstance().getPersistenceHelper().retryDeadlock(e, retryCount);
}
}
}

/**
* Similar to saveItem(ItemFacade item), only we can process many items within a single transaction, thereby improving performance
* @param items
Expand All @@ -335,8 +355,14 @@ public List<ItemFacade> saveItems(List<ItemFacade> items) throws DataFacadeExcep
item.setItemId(itemdata.getItemId());
retryCount = 0;
} catch (Exception e) {
log.warn("saveitems - problem save or update itemdata: {}", e.getMessage());
retryCount = PersistenceService.getInstance().getPersistenceHelper().retryDeadlock(e, retryCount);
try{
itemdata = getHibernateTemplate().merge(itemdata);
item.setItemId(itemdata.getItemId());
retryCount = 0;
} catch (Exception e2) {
log.warn("saveitems - problem save or update itemdata: {}", e2.getMessage());
retryCount = PersistenceService.getInstance().getPersistenceHelper().retryDeadlock(e2, retryCount);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,5 @@ public interface ItemFacadeQueriesAPI

public Boolean itemExists(Long itemId);

public void removeItemAttachment(Long itemAttachmentId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2518,7 +2518,6 @@ public SectionAttachmentIfc createSectionAttachment(SectionDataIfc section,
public void removeSectionAttachment(Long sectionAttachmentId) {
PublishedSectionAttachment sectionAttachment = getHibernateTemplate().load(PublishedSectionAttachment.class, sectionAttachmentId);
SectionDataIfc section = sectionAttachment.getSection();
// String resourceId = sectionAttachment.getResourceId();
int retryCount = PersistenceService.getInstance().getPersistenceHelper().getRetryCount();
while (retryCount > 0) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.sakaiproject.tool.assessment.data.dao.assessment.PublishedItemAttachment;
import org.sakaiproject.tool.assessment.data.dao.assessment.PublishedItemData;
import org.sakaiproject.tool.assessment.data.ifc.assessment.ItemDataIfc;
import org.sakaiproject.tool.assessment.integration.helper.ifc.TagServiceHelper;
import org.sakaiproject.tool.assessment.osid.shared.impl.IdImpl;
import org.sakaiproject.tool.assessment.services.PersistenceService;
Expand Down Expand Up @@ -234,7 +236,6 @@ public BackfillItemHashResult backfillItemHashes(int batchSize, boolean backfill
getHibernateTemplate());
}


@Override
public List<Long> getPublishedItemsIdsByHash(String hash) {
final HibernateCallback<List<Long>> hcb = session -> {
Expand All @@ -246,7 +247,6 @@ public List<Long> getPublishedItemsIdsByHash(String hash) {
return list1;
}


@Override
public Long getPublishedAssessmentId(Long itemId) {
final HibernateCallback<List<Long>> hcb = session -> {
Expand All @@ -262,5 +262,23 @@ public Long getPublishedAssessmentId(Long itemId) {
}
}


@Override
public void removeItemAttachment(Long itemAttachmentId) {
PublishedItemAttachment itemAttachment = getHibernateTemplate().load(PublishedItemAttachment.class, itemAttachmentId);
ItemDataIfc item = itemAttachment.getItem();
int retryCount = PersistenceService.getInstance().getPersistenceHelper().getRetryCount();
while (retryCount > 0) {
try {
if (item != null) {
Set set = item.getItemAttachmentSet();
set.remove(itemAttachment);
getHibernateTemplate().delete(getHibernateTemplate().merge(itemAttachment));
retryCount = 0;
}
} catch (Exception e) {
log.warn("Error while trying to delete PublishedItemAttachment: " + e.getMessage());
retryCount = PersistenceService.getInstance().getPersistenceHelper().retryDeadlock(e, retryCount);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,6 @@ public interface PublishedItemFacadeQueriesAPI {
public Long getPublishedAssessmentId(Long itemId);

public Boolean itemExists(String itemId);

public void removeItemAttachment(Long itemAttachmentId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,15 @@ public void deleteSet(Set s)
}
}

public void removeItemAttachment(Long attachmentId) {
try {
PersistenceService.getInstance().getItemFacadeQueries().removeItemAttachment(attachmentId);
} catch(Exception e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}

/**
* Save favorite column choices for matrix survey question.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,14 @@ public void deleteItemTagBindingsHavingTagCollectionId(String tagCollectionId) {
public BackfillItemHashResult backfillItemHashes(int batchSize, boolean backfillBaselineHashes) {
return PersistenceService.getInstance().getPublishedItemFacadeQueries().backfillItemHashes(batchSize, backfillBaselineHashes);
}

@Override
public void removeItemAttachment(Long attachmentId) {
try {
PersistenceService.getInstance().getPublishedItemFacadeQueries().removeItemAttachment(attachmentId);
} catch(Exception e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}
}

0 comments on commit 8a501f1

Please sign in to comment.