-
Notifications
You must be signed in to change notification settings - Fork 493
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added: API endpoint for updating a dataverse featured item
- Loading branch information
Showing
9 changed files
with
305 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/main/java/edu/harvard/iq/dataverse/api/dto/UpdatedDataverseFeaturedItemDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package edu.harvard.iq.dataverse.api.dto; | ||
|
||
import org.glassfish.jersey.media.multipart.FormDataContentDisposition; | ||
|
||
import java.io.InputStream; | ||
|
||
public class UpdatedDataverseFeaturedItemDTO { | ||
private String content; | ||
private int displayOrder; | ||
private boolean keepFile; | ||
private InputStream imageFileInputStream; | ||
private String imageFileName; | ||
|
||
public static UpdatedDataverseFeaturedItemDTO fromFormData(String content, | ||
int displayOrder, | ||
boolean keepFile, | ||
InputStream imageFileInputStream, | ||
FormDataContentDisposition contentDispositionHeader) { | ||
UpdatedDataverseFeaturedItemDTO updatedDataverseFeaturedItemDTO = new UpdatedDataverseFeaturedItemDTO(); | ||
|
||
updatedDataverseFeaturedItemDTO.content = content; | ||
updatedDataverseFeaturedItemDTO.displayOrder = displayOrder; | ||
updatedDataverseFeaturedItemDTO.keepFile = keepFile; | ||
|
||
if (imageFileInputStream != null) { | ||
updatedDataverseFeaturedItemDTO.imageFileInputStream = imageFileInputStream; | ||
updatedDataverseFeaturedItemDTO.imageFileName = contentDispositionHeader.getFileName(); | ||
} | ||
|
||
return updatedDataverseFeaturedItemDTO; | ||
} | ||
|
||
public void setContent(String content) { | ||
this.content = content; | ||
} | ||
|
||
public String getContent() { | ||
return content; | ||
} | ||
|
||
public void setDisplayOrder(int displayOrder) { | ||
this.displayOrder = displayOrder; | ||
} | ||
|
||
public int getDisplayOrder() { | ||
return displayOrder; | ||
} | ||
|
||
public void setKeepFile(boolean keepFile) { | ||
this.keepFile = keepFile; | ||
} | ||
|
||
public boolean isKeepFile() { | ||
return keepFile; | ||
} | ||
|
||
public void setImageFileInputStream(InputStream imageFileInputStream) { | ||
this.imageFileInputStream = imageFileInputStream; | ||
} | ||
|
||
public InputStream getImageFileInputStream() { | ||
return imageFileInputStream; | ||
} | ||
|
||
public void setImageFileName(String imageFileName) { | ||
this.imageFileName = imageFileName; | ||
} | ||
|
||
public String getImageFileName() { | ||
return imageFileName; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...u/harvard/iq/dataverse/engine/command/impl/AbstractWriteDataverseFeaturedItemCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package edu.harvard.iq.dataverse.engine.command.impl; | ||
|
||
import edu.harvard.iq.dataverse.*; | ||
import edu.harvard.iq.dataverse.authorization.Permission; | ||
import edu.harvard.iq.dataverse.engine.command.AbstractCommand; | ||
import edu.harvard.iq.dataverse.engine.command.CommandContext; | ||
import edu.harvard.iq.dataverse.engine.command.DataverseRequest; | ||
import edu.harvard.iq.dataverse.engine.command.RequiredPermissions; | ||
import edu.harvard.iq.dataverse.engine.command.exception.CommandException; | ||
import edu.harvard.iq.dataverse.engine.command.exception.InvalidCommandArgumentsException; | ||
import edu.harvard.iq.dataverse.util.BundleUtil; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.List; | ||
|
||
/** | ||
* An abstract base class for commands that perform write operations on {@link DataverseFeaturedItem}s. | ||
*/ | ||
@RequiredPermissions({Permission.EditDataverse}) | ||
abstract class AbstractWriteDataverseFeaturedItemCommand extends AbstractCommand<DataverseFeaturedItem> { | ||
|
||
protected final Dataverse dataverse; | ||
|
||
public AbstractWriteDataverseFeaturedItemCommand(DataverseRequest request, Dataverse affectedDataverse) { | ||
super(request, affectedDataverse); | ||
this.dataverse = affectedDataverse; | ||
} | ||
|
||
protected void setFileImageIfAvailableOrNull(DataverseFeaturedItem featuredItem, String imageFileName, InputStream imageFileInputStream, CommandContext ctxt) throws CommandException { | ||
if (imageFileName != null && imageFileInputStream != null) { | ||
try { | ||
ctxt.dataverseFeaturedItems().saveDataverseFeaturedItemImageFile(imageFileInputStream, imageFileName, dataverse.getId()); | ||
} catch (DataverseFeaturedItemServiceBean.InvalidImageFileException e) { | ||
throw new InvalidCommandArgumentsException( | ||
e.getMessage(), | ||
this | ||
); | ||
} catch (IOException e) { | ||
throw new CommandException( | ||
BundleUtil.getStringFromBundle( | ||
"dataverse.create.featuredItem.error.imageFileProcessing", | ||
List.of(e.getMessage()) | ||
), | ||
this | ||
); | ||
} | ||
featuredItem.setImageFileName(imageFileName); | ||
} else { | ||
featuredItem.setImageFileName(null); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...java/edu/harvard/iq/dataverse/engine/command/impl/UpdateDataverseFeaturedItemCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package edu.harvard.iq.dataverse.engine.command.impl; | ||
|
||
import edu.harvard.iq.dataverse.Dataverse; | ||
import edu.harvard.iq.dataverse.DataverseFeaturedItem; | ||
import edu.harvard.iq.dataverse.api.dto.UpdatedDataverseFeaturedItemDTO; | ||
import edu.harvard.iq.dataverse.engine.command.*; | ||
import edu.harvard.iq.dataverse.engine.command.exception.CommandException; | ||
|
||
/** | ||
* Updates a particular featured item {@link DataverseFeaturedItem} of a {@link Dataverse}. | ||
*/ | ||
public class UpdateDataverseFeaturedItemCommand extends AbstractWriteDataverseFeaturedItemCommand { | ||
|
||
private final DataverseFeaturedItem dataverseFeaturedItem; | ||
private final UpdatedDataverseFeaturedItemDTO updatedDataverseFeaturedItemDTO; | ||
|
||
public UpdateDataverseFeaturedItemCommand(DataverseRequest request, | ||
DataverseFeaturedItem dataverseFeaturedItem, | ||
UpdatedDataverseFeaturedItemDTO updatedDataverseFeaturedItemDTO) { | ||
super(request, dataverseFeaturedItem.getDataverse()); | ||
this.dataverseFeaturedItem = dataverseFeaturedItem; | ||
this.updatedDataverseFeaturedItemDTO = updatedDataverseFeaturedItemDTO; | ||
} | ||
|
||
@Override | ||
public DataverseFeaturedItem execute(CommandContext ctxt) throws CommandException { | ||
dataverseFeaturedItem.setContent(updatedDataverseFeaturedItemDTO.getContent()); | ||
dataverseFeaturedItem.setDisplayOrder(updatedDataverseFeaturedItemDTO.getDisplayOrder()); | ||
|
||
if (!updatedDataverseFeaturedItemDTO.isKeepFile()) { | ||
setFileImageIfAvailableOrNull( | ||
dataverseFeaturedItem, | ||
updatedDataverseFeaturedItemDTO.getImageFileName(), | ||
updatedDataverseFeaturedItemDTO.getImageFileInputStream(), | ||
ctxt | ||
); | ||
} | ||
|
||
return ctxt.dataverseFeaturedItems().save(dataverseFeaturedItem); | ||
} | ||
} |
Oops, something went wrong.