Skip to content

Commit

Permalink
Merge pull request #3155 from keeps/cafonso-dev-risk
Browse files Browse the repository at this point in the history
Completed risks api
  • Loading branch information
hmiguim committed May 7, 2024
2 parents f556218 + 9265f3d commit 12a8267
Show file tree
Hide file tree
Showing 20 changed files with 876 additions and 237 deletions.
Expand Up @@ -24,7 +24,7 @@
import org.roda.core.data.v2.ip.HasInstanceName;

@jakarta.xml.bind.annotation.XmlRootElement(name = RodaConstants.RODA_OBJECT_INCIDENCE)
@JsonInclude(JsonInclude.Include.ALWAYS)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class RiskIncidence implements IsModelObject, IsIndexed, HasId, HasInstanceID, HasInstanceName {
private static final long serialVersionUID = -1089167070045254627L;

Expand Down
@@ -0,0 +1,72 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE file at the root of the source
* tree and available online at
*
* https://github.com/keeps/roda
*/
package org.roda.core.data.v2.risks.api.incidences;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.roda.core.data.v2.IsRODAObject;
import org.roda.core.data.v2.index.select.SelectedItems;

import com.fasterxml.jackson.annotation.JsonTypeName;
import org.roda.core.data.v2.risks.RiskIncidence;

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonTypeName("SelectedIncidences")
public class SelectedIncidences<T extends IsRODAObject> {

private SelectedItems<T> selectedItems;
private String mitigatedDescription;
private String severity;
private String status;

// Constructor
public SelectedIncidences(){
// do nothing
}


public SelectedIncidences(SelectedItems<T> selectedItems, String mitigatedDescription, String severity, String status) {
this.selectedItems = selectedItems;
this.mitigatedDescription = mitigatedDescription;
this.severity = severity;
this.status = status;
}

public SelectedItems<T> getSelectedItems() {
return selectedItems;
}

public void setSelectedItems(SelectedItems<T> selectedItems) {
this.selectedItems = selectedItems;
}

public String getMitigatedDescription() {
return mitigatedDescription;
}

public void setMitigatedDescription(String mitigatedDescription) {
this.mitigatedDescription = mitigatedDescription;
}

public String getSeverity() {
return severity;
}

public void setSeverity(String severity) {
this.severity = severity;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}
}
Expand Up @@ -35,9 +35,9 @@
import org.roda.core.plugins.AbstractPlugin;
import org.roda.core.plugins.Plugin;
import org.roda.core.plugins.PluginException;
import org.roda.core.plugins.PluginHelper;
import org.roda.core.plugins.RODAObjectsProcessingLogic;
import org.roda.core.plugins.orchestrate.JobPluginInfo;
import org.roda.core.plugins.PluginHelper;
import org.roda.core.storage.StorageService;

public class UpdateIncidencesPlugin extends AbstractPlugin<RiskIncidence> {
Expand All @@ -50,25 +50,29 @@ public class UpdateIncidencesPlugin extends AbstractPlugin<RiskIncidence> {
private static Map<String, PluginParameter> pluginParameters = new HashMap<>();
static {
pluginParameters.put(RodaConstants.PLUGIN_PARAMS_RISK_INCIDENCE_STATUS,
new PluginParameter(RodaConstants.PLUGIN_PARAMS_RISK_INCIDENCE_STATUS, "Risk incidence status",
PluginParameterType.STRING, "UNMITIGATED", false, false, "Risk incidence status."));
new PluginParameter.PluginParameterBuilder(RodaConstants.PLUGIN_PARAMS_RISK_INCIDENCE_STATUS,
"Risk incidence status", PluginParameterType.STRING).withDefaultValue("UNMITIGATED").isMandatory(false)
.isReadOnly(false).withDescription("Risk incidence status.").build());

pluginParameters.put(RodaConstants.PLUGIN_PARAMS_RISK_INCIDENCE_SEVERITY,
new PluginParameter(RodaConstants.PLUGIN_PARAMS_RISK_INCIDENCE_SEVERITY, "Risk incidence severity",
PluginParameterType.STRING, "MODERATE", false, false, "Risk incidence severity."));
new PluginParameter.PluginParameterBuilder(RodaConstants.PLUGIN_PARAMS_RISK_INCIDENCE_SEVERITY,
"Risk incidence severity", PluginParameterType.STRING).withDefaultValue("MODERATE").isMandatory(false)
.isReadOnly(false).withDescription("Risk incidence severity.").build());

pluginParameters.put(RodaConstants.PLUGIN_PARAMS_RISK_INCIDENCE_MITIGATED_ON,
new PluginParameter(RodaConstants.PLUGIN_PARAMS_RISK_INCIDENCE_MITIGATED_ON, "Risk incidence mitigated on",
PluginParameterType.STRING, "", false, false, "Risk incidence mitigated on."));
new PluginParameter.PluginParameterBuilder(RodaConstants.PLUGIN_PARAMS_RISK_INCIDENCE_MITIGATED_ON,
"Risk incidence mitigated on", PluginParameterType.STRING).withDefaultValue("").isMandatory(false)
.isReadOnly(false).withDescription("Risk incidence mitigated on.").build());

pluginParameters.put(RodaConstants.PLUGIN_PARAMS_RISK_INCIDENCE_MITIGATED_BY,
new PluginParameter(RodaConstants.PLUGIN_PARAMS_RISK_INCIDENCE_MITIGATED_BY, "Risk incidence mitigated by",
PluginParameterType.STRING, "MODERATE", false, false, "Risk incidence mitigated by."));
new PluginParameter.PluginParameterBuilder(RodaConstants.PLUGIN_PARAMS_RISK_INCIDENCE_MITIGATED_BY,
"Risk incidence mitigated by", PluginParameterType.STRING).withDefaultValue("MODERATE").isMandatory(false)
.isReadOnly(false).withDescription("Risk incidence mitigated by.").build());

pluginParameters.put(RodaConstants.PLUGIN_PARAMS_RISK_INCIDENCE_MITIGATED_DESCRIPTION,
new PluginParameter(RodaConstants.PLUGIN_PARAMS_RISK_INCIDENCE_MITIGATED_DESCRIPTION,
"Risk incidence mitigated description", PluginParameterType.STRING, "MODERATE", false, false,
"Risk incidence mitigated description."));
new PluginParameter.PluginParameterBuilder(RodaConstants.PLUGIN_PARAMS_RISK_INCIDENCE_MITIGATED_DESCRIPTION,
"Risk incidence mitigated description", PluginParameterType.STRING).withDefaultValue("MODERATE")
.isMandatory(false).isReadOnly(false).withDescription("Risk incidence mitigated description.").build());
}

@Override
Expand Down
Expand Up @@ -74,6 +74,16 @@ core.roles.org.roda.wui.api.v2.controller.DisposalRuleController.retrieveDisposa
core.roles.org.roda.wui.api.v2.controller.DisposalRuleController.deleteDisposalRule = disposal_rule.manage
core.roles.org.roda.wui.api.v2.controller.DisposalRuleController.applyDisposalRules = disposal_schedule.associate

# Indexed risk roles
core.roles.org.roda.wui.api.v2.controller.IndexedRiskController.hasRiskVersions = risk.read
core.roles.org.roda.wui.api.v2.controller.IndexedRiskController.deleteRisk = risk.read
core.roles.org.roda.wui.api.v2.controller.IndexedRiskController.updateRisk = risk.read

# Risk incidence roles
core.roles.org.roda.wui.api.v2.controller.RiskIncidenceController.deleteRiskIncidences = incidence.read
core.roles.org.roda.wui.api.v2.controller.RiskIncidenceController.updateMultipleIncidences = incidence.read
core.roles.org.roda.wui.api.v2.controller.RiskIncidenceController.updateRiskIncidence = incidence.read

# Preservation events roles
core.roles.org.roda.wui.api.v2.controller.PreservationEventController.getPreservationAgents = preservation_metadata.read
core.roles.org.roda.wui.api.v2.controller.PreservationEventController.getLinkingIdentifierObjects = preservation_metadata.read
Expand All @@ -87,6 +97,13 @@ core.roles.org.roda.wui.api.v2.controller.PreservationAgentController.downloadBi
core.roles.org.roda.wui.api.v2.services.IndexService.find(Notification) = notification.read
core.roles.org.roda.wui.api.v2.services.IndexService.find(TransferredResource) = transfer.read
core.roles.org.roda.wui.api.v2.services.IndexService.find(IndexedAIP) = aip.read
core.roles.org.roda.wui.api.v2.services.IndexService.find(IndexedRisk) = risk.read
core.roles.org.roda.wui.api.v2.services.IndexService.find(RiskIncidence) = risk.read

# Generic findByUuid roles
core.roles.org.roda.wui.api.v2.services.IndexedService.findByUuid(IndexedRisk) = risk.read
core.roles.org.roda.wui.api.v2.controller.IndexedRiskController.findByUuid(User) = risk.read
core.roles.org.roda.wui.api.v2.controller.RiskIncidenceController.findByUuid(User) = incidence.read
core.roles.org.roda.wui.api.v2.services.IndexService.find(IndexedPreservationEvent) = preservation_metadata.read
core.roles.org.roda.wui.api.v2.services.IndexService.find(IndexedPreservationAgent) = preservation_metadata.read
core.roles.org.roda.wui.api.v2.services.IndexService.find(LogEntry) = log_entry.read
Expand All @@ -96,6 +113,8 @@ core.roles.org.roda.wui.api.v2.services.IndexService.find(DisposalConfirmation)
core.roles.org.roda.wui.api.v2.services.IndexService.count(TransferredResource) = transfer.read
core.roles.org.roda.wui.api.v2.services.IndexService.count(Notification) = notification.read
core.roles.org.roda.wui.api.v2.services.IndexService.count(IndexedAIP) = aip.read
core.roles.org.roda.wui.api.v2.services.IndexService.count(IndexedRisk) = risk.read
core.roles.org.roda.wui.api.v2.services.IndexService.count(RiskIncidence) = risk.read
core.roles.org.roda.wui.api.v2.services.IndexService.count(IndexedPreservationEvent) = preservation_metadata.read
core.roles.org.roda.wui.api.v2.services.IndexService.count(IndexedPreservationAgent) = preservation_metadata.read
core.roles.org.roda.wui.api.v2.services.IndexService.count(LogEntry) = log_entry.read
Expand All @@ -109,6 +128,8 @@ core.roles.org.roda.wui.api.v2.services.IndexService.retrieve(IndexedPreservatio
core.roles.org.roda.wui.api.v2.services.IndexService.retrieve(IndexedRepresentation) = representation.read
core.roles.org.roda.wui.api.v2.services.IndexService.retrieve(IndexedFile) = representation.read
core.roles.org.roda.wui.api.v2.services.IndexService.retrieve(LogEntry) = log_entry.read
core.roles.org.roda.wui.api.v2.services.IndexService.retrieve(IndexedRisk) = risk.read
core.roles.org.roda.wui.api.v2.services.IndexService.retrieve(RiskIncidence) = incidence.read

# Old roles - deprecated - and soon to be deleted
core.roles.org.roda.wui.api.controllers.Browser.appraisal = aip.appraisal
Expand Down
@@ -0,0 +1,185 @@
package org.roda.wui.api.v2.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.roda.core.data.common.RodaConstants;
import org.roda.core.data.exceptions.AuthorizationDeniedException;
import org.roda.core.data.exceptions.GenericException;
import org.roda.core.data.exceptions.RODAException;
import org.roda.core.data.exceptions.RequestNotValidException;
import org.roda.core.data.v2.index.CountRequest;
import org.roda.core.data.v2.index.FindRequest;
import org.roda.core.data.v2.index.IndexResult;
import org.roda.core.data.v2.index.select.SelectedItems;
import org.roda.core.data.v2.jobs.Job;
import org.roda.core.data.v2.log.LogEntryState;
import org.roda.core.data.v2.risks.IndexedRisk;
import org.roda.core.data.v2.risks.Risk;
import org.roda.core.data.v2.user.User;
import org.roda.wui.api.controllers.BrowserHelper;
import org.roda.wui.api.v2.exceptions.RESTException;
import org.roda.wui.api.v2.services.IndexService;
import org.roda.wui.api.v2.services.IndexedRiskService;
import org.roda.wui.client.services.IndexedRiskRestService;
import org.roda.wui.common.ControllerAssistant;
import org.roda.wui.common.model.RequestContext;
import org.roda.wui.common.utils.RequestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;

/**
* @author Carlos Afonso <cafonso@keep.pt>
*/
@RestController
@RequestMapping(path = "/api/v2/risks")
@Tag(name = IndexedRiskController.SWAGGER_ENDPOINT)
public class IndexedRiskController implements IndexedRiskRestService {

public static final String SWAGGER_ENDPOINT = "v2 risks";

@Autowired
private HttpServletRequest request;

@Autowired
private IndexedRiskService indexedRiskService;

@Autowired
private IndexService indexService;

@Override
public IndexedRisk findByUuid(String uuid) {
ControllerAssistant controllerAssistant = new ControllerAssistant() {};
RequestContext requestContext = RequestUtils.parseHTTPRequest(request);
LogEntryState state = LogEntryState.SUCCESS;

try {
// check user permissions
controllerAssistant.checkRoles(requestContext.getUser(), User.class);

// delegate
final IndexedRisk ret = indexService.retrieve(requestContext.getUser(), IndexedRisk.class, uuid,
new ArrayList<>());

// checking object permissions
controllerAssistant.checkObjectPermissions(requestContext.getUser(), ret, User.class);

return ret;
} catch (RODAException e) {
state = LogEntryState.FAILURE;
throw new RESTException(e);
} finally {
// register action
controllerAssistant.registerAction(requestContext.getUser(), uuid, state, RodaConstants.CONTROLLER_CLASS_PARAM,
User.class.getSimpleName(), RodaConstants.CONTROLLER_ID_PARAM, uuid);
}
}

@Override
public IndexResult<IndexedRisk> find(@RequestBody FindRequest findRequest, String localeString) {
RequestContext requestContext = RequestUtils.parseHTTPRequest(request);
if (findRequest.filter == null || findRequest.filter.getParameters().isEmpty()) {
return new IndexResult<>();
}
// delegate
return indexService.find(IndexedRisk.class, findRequest, localeString, requestContext.getUser());
}

@Override
public String count(CountRequest countRequest) {
RequestContext requestContext = RequestUtils.parseHTTPRequest(request);

return String.valueOf(
indexService.count(IndexedRisk.class, countRequest.filter, countRequest.onlyActive, requestContext.getUser()));
}

public Job deleteRisk(@RequestBody SelectedItems<IndexedRisk> selected) {
final ControllerAssistant controllerAssistant = new ControllerAssistant() {};
RequestContext requestContext = RequestUtils.parseHTTPRequest(request);

LogEntryState state = LogEntryState.SUCCESS;

try {
controllerAssistant.checkRoles(requestContext.getUser());
// delegate
return indexedRiskService.deleteRisk(requestContext.getUser(), selected);
} catch (RODAException e) {
state = LogEntryState.FAILURE;
throw new RESTException(e);
} finally {
// register action
controllerAssistant.registerAction(requestContext.getUser(), state, RodaConstants.CONTROLLER_SELECTED_PARAM,
selected);
}
}

@Override
public Void refreshRisk() {
final ControllerAssistant controllerAssistant = new ControllerAssistant() {};
RequestContext requestContext = RequestUtils.parseHTTPRequest(request);

LogEntryState state = LogEntryState.SUCCESS;

try {
// delegate
indexedRiskService.updateRiskCounters();
} catch (RODAException e) {
state = LogEntryState.FAILURE;
throw new RESTException(e);
} finally {
// register action
controllerAssistant.registerAction(requestContext.getUser(), state);
}
return null;
}

@Override
public Boolean hasRiskVersions(String riskId) {
final ControllerAssistant controllerAssistant = new ControllerAssistant() {};
RequestContext requestContext = RequestUtils.parseHTTPRequest(request);
LogEntryState state = LogEntryState.SUCCESS;

try {
controllerAssistant.checkRoles(requestContext.getUser());
// delegate
return indexedRiskService.hasRiskVersions(riskId);
} catch (RODAException e) {
state = LogEntryState.FAILURE;
throw new RESTException(e);
} finally {
// register action
controllerAssistant.registerAction(requestContext.getUser(), riskId, state,
RodaConstants.CONTROLLER_RISK_ID_PARAM, riskId);
}
}

@Override
public Risk updateRisk(@RequestBody Risk risk, String incidences) {
final ControllerAssistant controllerAssistant = new ControllerAssistant() {};
RequestContext requestContext = RequestUtils.parseHTTPRequest(request);
LogEntryState state = LogEntryState.SUCCESS;

try {
controllerAssistant.checkRoles(requestContext.getUser());
Map<String, String> properties = new HashMap<>();
properties.put(RodaConstants.VERSION_ACTION, RodaConstants.VersionAction.UPDATED.toString());

return indexedRiskService.updateRisk(risk, requestContext.getUser(), properties, true, Integer.parseInt(incidences));
} catch (RODAException e) {
state = LogEntryState.FAILURE;
throw new RESTException(e);
} finally {
// register action
controllerAssistant.registerAction(requestContext.getUser(), risk.getId(), state,
RodaConstants.CONTROLLER_RISK_PARAM, risk, RodaConstants.CONTROLLER_MESSAGE_PARAM,
RodaConstants.VersionAction.UPDATED.toString());
}
}

}

0 comments on commit 12a8267

Please sign in to comment.