Skip to content

Commit

Permalink
Added: API endpoint for updating a dataverse featured item
Browse files Browse the repository at this point in the history
  • Loading branch information
GPortas committed Jan 2, 2025
1 parent f7d60c7 commit 034cea4
Show file tree
Hide file tree
Showing 9 changed files with 305 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ public DataverseFeaturedItem findById(Long id) {
public DataverseFeaturedItem save(DataverseFeaturedItem dataverseFeaturedItem) {
if (dataverseFeaturedItem.getId() == null) {
em.persist(dataverseFeaturedItem);
em.flush();
return dataverseFeaturedItem;
} else {
return em.merge(dataverseFeaturedItem);
dataverseFeaturedItem = em.merge(dataverseFeaturedItem);
}
em.flush();
return dataverseFeaturedItem;
}

public InputStream getImageFileAsInputStream(DataverseFeaturedItem dataverseFeaturedItem) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@

import edu.harvard.iq.dataverse.*;
import edu.harvard.iq.dataverse.api.auth.AuthRequired;
import edu.harvard.iq.dataverse.api.dto.UpdatedDataverseFeaturedItemDTO;
import edu.harvard.iq.dataverse.engine.command.impl.*;
import edu.harvard.iq.dataverse.util.BundleUtil;
import jakarta.ejb.Stateless;
import jakarta.inject.Inject;
import jakarta.ws.rs.*;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;

import java.io.InputStream;
import java.text.MessageFormat;

import static edu.harvard.iq.dataverse.util.json.JsonPrinter.json;

@Stateless
@Path("dataverseFeaturedItems")
public class DataverseFeaturedItems extends AbstractApiBean {
Expand All @@ -35,4 +42,27 @@ public Response deleteFeaturedItem(@Context ContainerRequestContext crc, @PathPa
return e.getResponse();
}
}

@PUT
@AuthRequired
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("{id}")
public Response updateFeaturedItem(@Context ContainerRequestContext crc,
@PathParam("id") Long id,
@FormDataParam("content") String content,
@FormDataParam("displayOrder") int displayOrder,
@FormDataParam("keepFile") boolean keepFile,
@FormDataParam("file") InputStream imageFileInputStream,
@FormDataParam("file") FormDataContentDisposition contentDispositionHeader) {
try {
DataverseFeaturedItem dataverseFeaturedItem = dataverseFeaturedItemServiceBean.findById(id);
if (dataverseFeaturedItem == null) {
throw new WrappedResponse(error(Response.Status.NOT_FOUND, MessageFormat.format(BundleUtil.getStringFromBundle("dataverseFeaturedItems.errors.notFound"), id)));
}
UpdatedDataverseFeaturedItemDTO updatedDataverseFeaturedItemDTO = UpdatedDataverseFeaturedItemDTO.fromFormData(content, displayOrder, keepFile, imageFileInputStream, contentDispositionHeader);
return ok(json(execCommand(new UpdateDataverseFeaturedItemCommand(createDataverseRequest(getRequestUser(crc)), dataverseFeaturedItem, updatedDataverseFeaturedItemDTO))));
} catch (WrappedResponse e) {
return e.getResponse();
}
}
}
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;
}
}
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,37 @@

import edu.harvard.iq.dataverse.Dataverse;
import edu.harvard.iq.dataverse.DataverseFeaturedItem;
import edu.harvard.iq.dataverse.DataverseFeaturedItemServiceBean;
import edu.harvard.iq.dataverse.api.dto.NewDataverseFeaturedItemDTO;
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;
public class CreateDataverseFeaturedItemCommand extends AbstractWriteDataverseFeaturedItemCommand {

@RequiredPermissions({Permission.EditDataverse})
public class CreateDataverseFeaturedItemCommand extends AbstractCommand<DataverseFeaturedItem> {

private final Dataverse dataverse;
private final NewDataverseFeaturedItemDTO newDataverseFeaturedItemDTO;

public CreateDataverseFeaturedItemCommand(DataverseRequest request, Dataverse dataverse, NewDataverseFeaturedItemDTO newDataverseFeaturedItemDTO) {
public CreateDataverseFeaturedItemCommand(DataverseRequest request,
Dataverse dataverse,
NewDataverseFeaturedItemDTO newDataverseFeaturedItemDTO) {
super(request, dataverse);
this.dataverse = dataverse;
this.newDataverseFeaturedItemDTO = newDataverseFeaturedItemDTO;
}

@Override
public DataverseFeaturedItem execute(CommandContext ctxt) throws CommandException {
DataverseFeaturedItem featuredItem = new DataverseFeaturedItem();

setImageIfAvailable(featuredItem, ctxt);
DataverseFeaturedItem dataverseFeaturedItem = new DataverseFeaturedItem();

featuredItem.setContent(newDataverseFeaturedItemDTO.getContent());
featuredItem.setDisplayOrder(newDataverseFeaturedItemDTO.getDisplayOrder());
featuredItem.setDataverse(dataverse);
setFileImageIfAvailableOrNull(
dataverseFeaturedItem,
newDataverseFeaturedItemDTO.getImageFileName(),
newDataverseFeaturedItemDTO.getImageFileInputStream(),
ctxt
);

return ctxt.dataverseFeaturedItems().save(featuredItem);
}
dataverseFeaturedItem.setContent(newDataverseFeaturedItemDTO.getContent());
dataverseFeaturedItem.setDisplayOrder(newDataverseFeaturedItemDTO.getDisplayOrder());
dataverseFeaturedItem.setDataverse(dataverse);

private void setImageIfAvailable(DataverseFeaturedItem featuredItem, CommandContext ctxt) throws CommandException {
String imageFileName = newDataverseFeaturedItemDTO.getImageFileName();
InputStream imageFileInputStream = newDataverseFeaturedItemDTO.getImageFileInputStream();
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);
}
return ctxt.dataverseFeaturedItems().save(dataverseFeaturedItem);
}
}
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);
}
}
Loading

0 comments on commit 034cea4

Please sign in to comment.