Summary Table

Categories Total Count
PII 0
URL 0
DNS 0
EKL 0
IP 0
PORT 0
VsID 0
CF 0
AI 0
VPD 0
PL 0
Other 0

File Content

package gov.va.vamf.scheduling.varutility.resource;

import gov.va.vamf.scheduling.varutility.domain.CustomMessage;
import gov.va.vamf.scheduling.varutility.domain.CustomMessages;
import gov.va.vamf.scheduling.varutility.service.CustomMessageService;
import gov.va.vamf.scheduling.varutility.utils.VarUtilityConstants;
import gov.va.vamf.security.v1.filters.JwtRbacRestricted;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Authorization;
import io.swagger.annotations.AuthorizationScope;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.ws.rs.Consumes;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import java.util.Iterator;

@Component
@Path("/custom-messages")
@Api(value = "/custom-messages", description = "Custom messages API")
@Scope("request")
public class CustomMessageResource implements InitializingBean {

@Value("${default.messages.id}")
String defaultMessagesId;

@Value("${default.messages.text}")
String defaultMessagesText;

private String[] defaultMessageIds;
private String[] defaultMessageTexts;

@Autowired
private CustomMessageService customMessageService;

@Override
public void afterPropertiesSet() throws Exception {
defaultMessageIds = defaultMessagesId.split("\\|");
defaultMessageTexts = defaultMessagesText.split("\\|");
}

@GET
@JwtRbacRestricted("Staff")
@Path("/default-message/message-id/{message-id}")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@ApiOperation(value = "Retrieves the default custom message for a provided message type", authorizations = {
@Authorization(
value = VarUtilityConstants.RBAC_JWT_SECURITY_NAME,
scopes = {@AuthorizationScope(scope = "Staff", description = "Admin role")}
)
}, response = CustomMessage.class)
public CustomMessage fetchDefaultMessage(@ApiParam(value = "message id", required = true) @PathParam("message-id") String messageId) {

CustomMessage defaultMessage = new CustomMessage();

if (messageId != null) {
for (int i = 0; i < defaultMessageIds.length; i++) {
if (messageId.equals(defaultMessageIds[i])) {
defaultMessage.setMessageId(defaultMessageIds[i]);
defaultMessage.setMessageText(defaultMessageTexts[i]);
break;
}
}
}

return defaultMessage;
}

@GET
@JwtRbacRestricted("Staff")
@Path("/site/{site-code}")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@ApiOperation(value = "Retrieves the custom messages for a provided site code", authorizations = {
@Authorization(
value = VarUtilityConstants.RBAC_JWT_SECURITY_NAME,
scopes = {@AuthorizationScope(scope = "Staff", description = "Staff role")}
)
}, response = CustomMessage.class, responseContainer = "List")
public CustomMessages fetchCustomMessages(@ApiParam(value = "site code", required = true) @PathParam("site-code") String siteCode) {

CustomMessages customMessages = customMessageService.fetchCustomMessagesBySiteCode(siteCode);

if (customMessages == null) {
customMessages = new CustomMessages();
}

for (int i = 0; i < defaultMessageIds.length; i++) {
if (!containsMessageId(defaultMessageIds[i], customMessages)) {

CustomMessage defaultMessage = new CustomMessage();
defaultMessage.setSiteCode(siteCode);
defaultMessage.setMessageId(defaultMessageIds[i]);
defaultMessage.setMessageText(defaultMessageTexts[i]);

CustomMessage savedDefaultMessage = customMessageService.saveCustomMessage(defaultMessage);

if (savedDefaultMessage != null) {
customMessages.add(savedDefaultMessage);
}
}
}

return customMessages;
}

@PUT
@JwtRbacRestricted("Staff")
@Path("/site/{site-code}")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@ApiOperation(value = "Saves the custom messages for a provided site code", authorizations = {
@Authorization(
value = VarUtilityConstants.RBAC_JWT_SECURITY_NAME,
scopes = {@AuthorizationScope(scope = "Staff", description = "Staff role")}
)
}, response = CustomMessage.class, responseContainer = "List")
public CustomMessages saveCustomMessages(@ApiParam(value = "custom messages to custom", required = true) CustomMessages customMessages,
@ApiParam(value = "site code", required = true) @PathParam("site-code") String siteCode) {

CustomMessages savedCustomMessages = new CustomMessages();

CustomMessage foundCustomMessage = null;
CustomMessage newCustomMessage = null;
CustomMessage savedCustomMessage = null;

Iterator<CustomMessage> customMessagesIterator = customMessages.iterator();

while (customMessagesIterator.hasNext()) {
newCustomMessage = customMessagesIterator.next();

foundCustomMessage = customMessageService.fetchCustomMessageBySiteCodeAndMessageId(siteCode, newCustomMessage.getMessageId());

if (foundCustomMessage != null && foundCustomMessage.getId() != null) {
newCustomMessage.setId(foundCustomMessage.getId());
}

newCustomMessage.setSiteCode(siteCode);
savedCustomMessage = customMessageService.saveCustomMessage(newCustomMessage);

if (savedCustomMessage != null) {
savedCustomMessages.add(savedCustomMessage);
}
}

return savedCustomMessages;
}

@DELETE
@JwtRbacRestricted("Staff")
@Path("/site/{site-code}")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@ApiOperation(value = "Deletes the custom messages for a provided site code", authorizations = {
@Authorization(
value = VarUtilityConstants.RBAC_JWT_SECURITY_NAME,
scopes = {@AuthorizationScope(scope = "Staff", description = "Staff role")}
)
}, response = CustomMessage.class, responseContainer = "List")
public CustomMessages deleteCustomMessages(@ApiParam(value = "site code", required = true) @PathParam("site-code") String siteCode) {

CustomMessages customMessages = customMessageService.deleteCustomMessagesBySiteCode(siteCode);

if (customMessages == null) {
customMessages = new CustomMessages();
}

return customMessages;
}

private boolean containsMessageId(String messageId, CustomMessages customMessages) {
Iterator<CustomMessage> customMessagesIterator = customMessages.iterator();

while (customMessagesIterator.hasNext()) {
if (messageId.equals(customMessagesIterator.next().getMessageId())) {
return true;
}
}

return false;
}
}