ChangeCalendar/backend/src/main/java/com/nttdata/calender/api/KalenderRestController.java

266 lines
10 KiB
Java

package com.nttdata.calender.api;
import java.util.ArrayList;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.bmc.arsys.api.ARException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.nttdata.calender.approval.Approval;
import com.nttdata.calender.approval.ApprovalUpdateRequest;
import com.nttdata.calender.changes.Change;
import com.nttdata.calender.changes.ChangeRequest;
import com.nttdata.calender.changes.ChangeResponse;
import com.nttdata.calender.changes.ChangeUpdateRequest;
import com.nttdata.calender.contracts.Contract;
import com.nttdata.calender.contracts.ContractGetResponse;
import com.nttdata.calender.errorhandling.ErrorTypes.NotFoundError;
import com.nttdata.calender.errorhandling.ErrorTypes.ValidationError;
import com.nttdata.calender.implementer.Implementer;
import com.nttdata.calender.implementer.ImplementerGetRequest;
import com.nttdata.calender.implementer.ImplementerGetResponse;
import com.nttdata.calender.implementer.ImplementerUpdateRequest;
import com.nttdata.calender.packageType.PackageItems;
import com.nttdata.calender.packageType.PackageType;
import com.nttdata.calender.states.State;
import com.nttdata.calender.states.StateChange;
import com.nttdata.calender.states.StateChangeRequest;
import com.nttdata.calender.states.StateResponse;
import com.nttdata.calender.supportgroup.SupportGroup;
/**
* REST Controller for Remedy Data
*/
@RestController
public class KalenderRestController {
private final RemedyJavaAPI javaAPI;
private final Change change;
private final Implementer implementer;
private final PackageType packageType;
private final Approval approval;
private final StateChange stateChange;
private static final Logger applicationLogger = LogManager.getLogger("application");
@Autowired
public KalenderRestController(RemedyJavaAPI javaAPI, Change change, Implementer implementer,
PackageType packageType, Approval approval, StateChange stateChange) {
this.javaAPI = javaAPI;
this.change = change;
this.implementer = implementer;
this.packageType = packageType;
this.approval = approval;
this.stateChange = stateChange;
}
/**
* Handles GET request to retrieve all the states and their actual and possible
* states and results a JSON array.
*
* @return JSON Array of states with actualState, possibleStates, stateNameEN,
* stateNameDE
* @throws JsonProcessingException if an exception occurs during JSON processing
*/
@CrossOrigin("*")
@GetMapping("/api/getStates")
@ResponseBody
public ArrayList<StateResponse> printState() {
var state = State.getInstance();
return state.get();
}
@CrossOrigin("*")
@GetMapping("/api/getMany")
@ResponseBody
public void getMany() {
var query = new Query.QueryBuilder("ASF:WI_CFG_CIMaintenanceRel").addFieldId("something", 666000001).build();
try {
var entries = javaAPI.queryFieldsById("", query.getFieldIds(), query.getFormName(), null, 0, 0);
System.out.println(entries.size());
} catch (ARException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Handles GET request to retrieve all support groups.
*
* @return List of support groups if not empty, otherwise error message in
* response body
* @throws ARException if an AR exception occurs
*/
@CrossOrigin("*")
@GetMapping("/api/getSupportGroups")
@ResponseBody
public ResponseEntity<?> getSupportGroups() throws ARException {
var supportGroup = SupportGroup.getInstance();
if (supportGroup.get() != null)
return ResponseEntity.ok(supportGroup.get());
return ResponseEntity.internalServerError().body("Support Groups couldnt be fetched.");
}
/**
*
* Handles a GET request to retrieve the user's support group.
*
* @return Support group of user if not empty, otherwise error message in
* response body
* @throws ARException if an error occurs during the retrieval of the user's
* support group.
*/
@CrossOrigin("*")
@GetMapping("/api/getUserSupportGroup")
@ResponseBody
public ResponseEntity<?> getUserSupportGroup() throws ARException {
var user = SupportGroup.getInstance().getUserSG(javaAPI);
return (user != null) ? ResponseEntity.ok(user)
: ResponseEntity.internalServerError().body("User doesn't have a support group.");
}
/**
* Handles GET request to retrieve a list of all contracts.
*
* @return {@link ArrayList} of {@link ContractGetResponse} objects
* @throws ARException if an AR exception occurs
*/
@CrossOrigin("*")
@GetMapping("/api/getContracts")
@ResponseBody
public ArrayList<ContractGetResponse> getContracts() throws ARException {
var contract = Contract.getInstance();
return contract.get();
}
/**
* Handles GET request to retrieve a list of all package types.
*
* @return {@link ArrayList} of {@link PackageItems} objects
* @throws ARException if an AR exception occurs
*/
@CrossOrigin("*")
@GetMapping("/api/getPackageTypes")
@ResponseBody
public ArrayList<PackageItems> getPackageTypes() throws ARException {
return packageType.get();
}
/**
* This method is used only in development to reset all dates coming from
* Remedy.
* It will be deleted when going to production.
*
* @return "OK" as the status message
* @throws ARException if an AR exception occurs
*/
@CrossOrigin("*")
@GetMapping("/api/resetDates")
public String first_Api() throws ARException {
// TODO: is this still needed?
return "OK";
}
/**
* Handles GET request to retrieve a List of all {@link Change} objects
*
* @param request the request object
* @return {@link ChangeResponse} containing the JSON array of all changes
* @throws ARException if an AR exception occurs
*/
@CrossOrigin("*")
@PostMapping("/api/getChanges")
@ResponseBody
public ChangeResponse getChanges(@RequestBody ChangeRequest request)
throws ARException, NotFoundError, ValidationError {
return change.get(request);
}
/**
* Handles POST request to update the date for a specific {@link Change} object.
*
* @param request the request object containing the ID of the change entry and
* the new timestamp
* @return the updated {@link ChangeUpdateRequest} object
* @throws ARException if an AR exception occurs
* @throws ValidationError
*/
@CrossOrigin("*")
@PostMapping("/api/updateChange")
@ResponseBody
public ChangeUpdateRequest updateChange(@RequestBody ChangeUpdateRequest request)
throws ARException, ValidationError {
change.modifyTimestamp(request);
return request;
}
/**
* Updates the state based on the provided {@link StateChangeRequest} object.
* Returns a response entity with the updated state value.
*
* @param request the {@link StateChangeRequest} object containing the
* necessary information
* @param stateChange the autowired {@link StateChange} object for state change
* operations
* @return a response entity with the updated state value as a string
* @throws ARException if an AR exception occurs
*/
@CrossOrigin("*")
@PostMapping("/api/updateState")
public ResponseEntity<String> updateState(@RequestBody StateChangeRequest request) throws ARException {
logRequest("/updateState", request.toString());
var response = stateChange.createStateChange(request);
return ResponseEntity.ok(response);
}
/**
* Updates an implementer based on the data sent in the request.
* Returns an error when the action is not possible or the request has an
* incorrect format.
*
* @param request the request object
* @return {@link ResponseEntity} with the updated state value "OK"
* @throws ARException if an AR exception occurs
*/
@CrossOrigin("*")
@PostMapping("api/updateImplementer")
public ResponseEntity<String> updateImplementer(@RequestBody ImplementerUpdateRequest request) throws ARException {
var response = implementer.update(request);
return ResponseEntity.ok(response);
}
@CrossOrigin("*")
@PostMapping("api/updateApproval")
@ResponseBody
public ResponseEntity<String> updateApproval(@RequestBody ApprovalUpdateRequest request) throws ARException {
var response = approval.update(request);
return ResponseEntity.ok(response);
}
/**
* Returns all the members of the support group assigned to the specific change
* provided by the entryId of {@link ImplementerGetRequest}.
*
* @param request the request object
* @return the {@link ImplementerGetResponse} containing the response
* @throws ARException if an AR exception occurs
*/
@CrossOrigin("*")
@PostMapping("api/getImplementer")
public ImplementerGetResponse getImplementer(@RequestBody ImplementerGetRequest request) throws ARException {
return implementer.get(request);
}
private void logRequest(String endpoint, String request) {
applicationLogger.info("Received request on endpoint %s with body:", endpoint);
applicationLogger.info(request);
}
}