Skip to content

Commit

Permalink
#4405 Invalidate move of to would cause loss of Download Data
Browse files Browse the repository at this point in the history
  • Loading branch information
sekmiller committed Feb 8, 2018
1 parent 56c771d commit f0c3516
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/Guestbook.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import java.util.List;
import java.util.Objects;
import javax.persistence.Column;
import javax.persistence.ManyToOne;
import javax.persistence.OrderBy;
Expand Down Expand Up @@ -297,5 +298,14 @@ public void setResponseCountDataverse(Long responseCountDataverse) {
this.responseCountDataverse = responseCountDataverse;
}

@Override
public boolean equals(Object object) {
if (!(object instanceof Guestbook)) {
return false;
}
Guestbook other = (Guestbook) object;
return Objects.equals(getId(), other.getId());
}


}
12 changes: 11 additions & 1 deletion src/main/java/edu/harvard/iq/dataverse/GuestbookServiceBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,18 @@ public Long findCountUsages(Long guestbookId, Long dataverseId) {
}
}

public Long findCountResponsesForGivenDataset(Long guestbookId, Long datasetId) {
String queryString = "";
if (guestbookId != null && datasetId != null) {
queryString = "select count(*) from guestbookresponse where guestbook_id = " + guestbookId + " and dataset_id = " + datasetId + ";";
Query query = em.createNativeQuery(queryString);
return (Long) query.getSingleResult();
} else {
return new Long(0);
}
}


public Guestbook find(Object pk) {
return em.find(Guestbook.class, pk);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ public void executeImpl(CommandContext ctxt) throws CommandException {
}
}
if (gbs == null || !gbs.contains(gb)) {
//if there are responses on this guestbook for this dataset invalidate the move.
long count = ctxt.guestbooks().findCountResponsesForGivenDataset(gb.getId(), moved.getId());
if (count > 0){
throw new IllegalCommandException("Dataset may not be moved because doing so would cause download data to be lost. ", this);
}
moved.setGuestbook(null);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import edu.harvard.iq.dataverse.Dataverse;
import edu.harvard.iq.dataverse.DataverseServiceBean;
import edu.harvard.iq.dataverse.Guestbook;
import edu.harvard.iq.dataverse.GuestbookResponse;
import edu.harvard.iq.dataverse.GuestbookServiceBean;
import edu.harvard.iq.dataverse.MetadataBlock;
import edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser;
import edu.harvard.iq.dataverse.engine.DataverseEngine;
Expand Down Expand Up @@ -53,12 +55,13 @@
* @author skraffmi
*/
public class MoveDatasetCommandTest {
Dataset moved;
Dataset moved, movedResponses;
Dataverse root, childA, childB, grandchildAA, childDraft, grandchildBB;
DataverseEngine testEngine;
MetadataBlock blockA, blockB, blockC, blockD;
AuthenticatedUser auth, nobody;
Guestbook gbA, gbB, gbC;
GuestbookResponse gbResp;
@Context
protected HttpServletRequest httpRequest;

Expand Down Expand Up @@ -105,6 +108,12 @@ public void setUp() {
moved = new Dataset();
moved.setOwner(root);
moved.setPublicationDate(new Timestamp(new Date().getTime()));
moved.setId(1l);

movedResponses = new Dataset();
movedResponses.setOwner(root);
movedResponses.setPublicationDate(new Timestamp(new Date().getTime()));
movedResponses.setId(2l);

childA.setOwner(root);
childB.setOwner(root);
Expand All @@ -120,6 +129,11 @@ public void setUp() {
gbC.setId(3l);

moved.setGuestbook(gbA);
movedResponses.setGuestbook(gbA);

GuestbookResponse gbResp = new GuestbookResponse();
gbResp.setGuestbook(gbA);
gbResp.setDataset(movedResponses);

List<Guestbook> includeA = new ArrayList();
includeA.add(gbA);
Expand Down Expand Up @@ -151,6 +165,21 @@ public Dataverse save(Dataverse dataverse) {
};
}

@Override
public GuestbookServiceBean guestbooks() {
return new GuestbookServiceBean() {
@Override
public Long findCountResponsesForGivenDataset(Long guestbookId, Long datasetId) {
//We're going to fake a response for a dataset with responses
if(datasetId == 1){
return new Long(0);
} else{
return new Long(1);
}
}
};
}

@Override
public IndexServiceBean index(){
return new IndexServiceBean(){
Expand Down Expand Up @@ -228,8 +257,19 @@ public void testRemoveGuestbook() throws Exception {

}



/*
Moving Dataset to target which does not contain GB
while there are G B responses is not allowed
*/

@Test(expected = IllegalCommandException.class)
public void testLoseGBResponses() throws Exception{

DataverseRequest aRequest = new DataverseRequest(auth, httpRequest);
testEngine.submit(new MoveDatasetCommand(aRequest, movedResponses, childB));
fail();

}

/**
* Moving DS to its owning DV
Expand Down

0 comments on commit f0c3516

Please sign in to comment.