|
99 | 99 | import java.util.stream.Collectors;
|
100 | 100 |
|
101 | 101 | import static edu.harvard.iq.dataverse.api.ApiConstants.*;
|
| 102 | +import edu.harvard.iq.dataverse.dataset.DatasetType; |
| 103 | +import edu.harvard.iq.dataverse.dataset.DatasetTypeServiceBean; |
102 | 104 | import static edu.harvard.iq.dataverse.util.json.JsonPrinter.*;
|
103 | 105 | import static edu.harvard.iq.dataverse.util.json.NullSafeJsonBuilder.jsonObjectBuilder;
|
104 | 106 | import static jakarta.ws.rs.core.Response.Status.BAD_REQUEST;
|
| 107 | +import static jakarta.ws.rs.core.Response.Status.NOT_FOUND; |
105 | 108 |
|
106 | 109 | @Path("datasets")
|
107 | 110 | public class Datasets extends AbstractApiBean {
|
@@ -187,6 +190,9 @@ public class Datasets extends AbstractApiBean {
|
187 | 190 | @Inject
|
188 | 191 | DatasetVersionFilesServiceBean datasetVersionFilesServiceBean;
|
189 | 192 |
|
| 193 | + @Inject |
| 194 | + DatasetTypeServiceBean datasetTypeSvc; |
| 195 | + |
190 | 196 | /**
|
191 | 197 | * Used to consolidate the way we parse and handle dataset versions.
|
192 | 198 | * @param <T>
|
@@ -5071,4 +5077,103 @@ public Response resetPidGenerator(@Context ContainerRequestContext crc, @PathPar
|
5071 | 5077 | return ok("Pid Generator reset to default: " + dataset.getEffectivePidGenerator().getId());
|
5072 | 5078 | }
|
5073 | 5079 |
|
| 5080 | + @GET |
| 5081 | + @Path("datasetTypes") |
| 5082 | + public Response getDatasetTypes() { |
| 5083 | + JsonArrayBuilder jab = Json.createArrayBuilder(); |
| 5084 | + List<DatasetType> datasetTypes = datasetTypeSvc.listAll(); |
| 5085 | + for (DatasetType datasetType : datasetTypes) { |
| 5086 | + JsonObjectBuilder job = Json.createObjectBuilder(); |
| 5087 | + job.add("id", datasetType.getId()); |
| 5088 | + job.add("name", datasetType.getName()); |
| 5089 | + jab.add(job); |
| 5090 | + } |
| 5091 | + return ok(jab.build()); |
| 5092 | + } |
| 5093 | + |
| 5094 | + @GET |
| 5095 | + @Path("datasetTypes/byName/{name}") |
| 5096 | + public Response getDatasetTypes(@PathParam("name") String name) { |
| 5097 | + DatasetType datasetType = datasetTypeSvc.getByName(name); |
| 5098 | + if (datasetType != null) { |
| 5099 | + return ok(datasetType.toJson()); |
| 5100 | + } else { |
| 5101 | + return error(NOT_FOUND, "Could not find a dataset type with name " + name); |
| 5102 | + } |
| 5103 | + } |
| 5104 | + |
| 5105 | + @POST |
| 5106 | + @AuthRequired |
| 5107 | + @Path("datasetTypes") |
| 5108 | + public Response addDatasetType(@Context ContainerRequestContext crc, String jsonIn) { |
| 5109 | + System.out.println("json in: " + jsonIn); |
| 5110 | + AuthenticatedUser user; |
| 5111 | + try { |
| 5112 | + user = getRequestAuthenticatedUserOrDie(crc); |
| 5113 | + } catch (WrappedResponse ex) { |
| 5114 | + return error(Response.Status.BAD_REQUEST, "Authentication is required."); |
| 5115 | + } |
| 5116 | + if (!user.isSuperuser()) { |
| 5117 | + return error(Response.Status.FORBIDDEN, "Superusers only."); |
| 5118 | + } |
| 5119 | + |
| 5120 | + if (jsonIn == null || jsonIn.isEmpty()) { |
| 5121 | + throw new IllegalArgumentException("JSON input was null or empty!"); |
| 5122 | + } |
| 5123 | + JsonObject jsonObject = JsonUtil.getJsonObject(jsonIn); |
| 5124 | + String nameIn = jsonObject.getString("name", null); |
| 5125 | + if (nameIn == null) { |
| 5126 | + throw new IllegalArgumentException("A name for the dataset type is required"); |
| 5127 | + } |
| 5128 | + |
| 5129 | + try { |
| 5130 | + DatasetType datasetType = new DatasetType(); |
| 5131 | + datasetType.setName(nameIn); |
| 5132 | + DatasetType saved = datasetTypeSvc.save(datasetType); |
| 5133 | + Long typeId = saved.getId(); |
| 5134 | + String name = saved.getName(); |
| 5135 | + actionLogSvc.log(new ActionLogRecord(ActionLogRecord.ActionType.Admin, "addDatasetType").setInfo("Dataset type added with id " + typeId + " and name " + name + ".")); |
| 5136 | + return ok(saved.toJson()); |
| 5137 | + } catch (WrappedResponse ex) { |
| 5138 | + return error(BAD_REQUEST, ex.getMessage()); |
| 5139 | + } |
| 5140 | + } |
| 5141 | + |
| 5142 | + @DELETE |
| 5143 | + @AuthRequired |
| 5144 | + @Path("datasetTypes/{id}") |
| 5145 | + public Response deleteDatasetType(@Context ContainerRequestContext crc, @PathParam("id") String doomed) { |
| 5146 | + AuthenticatedUser user; |
| 5147 | + try { |
| 5148 | + user = getRequestAuthenticatedUserOrDie(crc); |
| 5149 | + } catch (WrappedResponse ex) { |
| 5150 | + return error(Response.Status.BAD_REQUEST, "Authentication is required."); |
| 5151 | + } |
| 5152 | + if (!user.isSuperuser()) { |
| 5153 | + return error(Response.Status.FORBIDDEN, "Superusers only."); |
| 5154 | + } |
| 5155 | + |
| 5156 | + if (doomed == null || doomed.isEmpty()) { |
| 5157 | + throw new IllegalArgumentException("ID is required!"); |
| 5158 | + } |
| 5159 | + |
| 5160 | + long idToDelete; |
| 5161 | + try { |
| 5162 | + idToDelete = Long.parseLong(doomed); |
| 5163 | + } catch (NumberFormatException e) { |
| 5164 | + throw new IllegalArgumentException("ID must be a number"); |
| 5165 | + } |
| 5166 | + |
| 5167 | + try { |
| 5168 | + int numDeleted = datasetTypeSvc.deleteById(idToDelete); |
| 5169 | + if (numDeleted == 1) { |
| 5170 | + return ok("deleted"); |
| 5171 | + } else { |
| 5172 | + return error(BAD_REQUEST, "Something went wrong. Number of dataset types deleted: " + numDeleted); |
| 5173 | + } |
| 5174 | + } catch (WrappedResponse ex) { |
| 5175 | + return error(BAD_REQUEST, ex.getMessage()); |
| 5176 | + } |
| 5177 | + } |
| 5178 | + |
5074 | 5179 | }
|
0 commit comments