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

374 lines
14 KiB
Java

package com.nttdata.calender.api;
import java.util.ArrayList;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.checkerframework.common.util.report.qual.ReportCall;
import org.json.JSONObject;
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.planTimes.CalendarWeek;
import com.nttdata.calender.planTimes.PlanTimes;
import com.nttdata.calender.planTimes.PlanTimesRequest;
import com.nttdata.calender.presets.Preset;
import com.nttdata.calender.presets.Presets;
import com.nttdata.calender.presets.PresetsGetResponse;
import com.nttdata.calender.presets.RenamePresetRequest;
import com.nttdata.calender.presets.SelectPresetRequest;
import com.nttdata.calender.presets.UpdatePresetRequest;
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;
import com.nttdata.calender.urlConstructor.UrlConstructor;
import com.nttdata.calender.urlConstructor.UrlResponse;
/**
* 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 UrlConstructor urlConstructor;
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, UrlConstructor urlConstructor, StateChange stateChange) {
this.javaAPI = javaAPI;
this.change = change;
this.implementer = implementer;
this.packageType = packageType;
this.approval = approval;
this.urlConstructor = urlConstructor;
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();
}
/**
* 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.");
}
/**
* Retrieves the URL response by calling the URL constructor with the Java API.
* Returns the constructed URL response.
*
* @return the URL response containing the constructed URL
* @throws ARException if an AR exception occurs
*/
@CrossOrigin("*")
@GetMapping("/api/getUrl")
public UrlResponse getLink() throws ARException {
return urlConstructor.construct(javaAPI);
}
/**
* Handles POST request to fetch plan times based on the provided request.
*
* @param request the PlanTimesRequest containing the required information
* @return the ArrayList of CalendarWeek representing the fetched plan times
* @throws ARException if an AR exception occurs
* @throws ValidationError if a validation error occurs
*/
@CrossOrigin("*")
@PostMapping("/api/fetchPlanTimes")
public ArrayList<CalendarWeek> getPlanTimes(@RequestBody PlanTimesRequest request)
throws ARException, ValidationError {
PlanTimes p = new PlanTimes(javaAPI);
p.setPlanTimes(request);
return p.getPlanTimes();
}
/**
*
* 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 a 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
* @throws NotFoundError if no changes are found in the specified context
* @throws ValidationError if a validation error occurs
*/
@CrossOrigin("*")
@PostMapping("/api/getChanges")
@ResponseBody
public ChangeResponse getChanges(@RequestBody ChangeRequest request)
throws ARException, NotFoundError, ValidationError {
return change.get(request);
}
/**
* Handles a 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 if a validation error occurs
*/
@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);
}
/**
* Updates an approval 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/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);
}
@CrossOrigin("*")
@PostMapping("/api/savePreset")
@ResponseBody
public List<Preset> savePreset(@RequestBody Preset request)
throws ARException, ValidationError, NotFoundError {
Presets presets = new Presets(javaAPI);
presets.savePreset(request);
return presets.getAll();
}
@CrossOrigin("*")
@PostMapping("/api/selectPreset")
@ResponseBody
public boolean selectPreset(@RequestBody SelectPresetRequest guid) throws ARException,
NotFoundError {
Presets presets = new Presets(javaAPI);
return presets.selectPreset(guid);
}
@CrossOrigin("*")
@GetMapping("api/initPresets")
@ResponseBody
public Object initPresets() throws ARException, NotFoundError {
Presets presets = new Presets(javaAPI);
return presets.initPresets();
}
@CrossOrigin("*")
@PostMapping("api/updatePreset")
@ResponseBody
public List<Preset> updatePreset(@RequestBody UpdatePresetRequest request) throws NotFoundError, ARException {
Presets presets = new Presets(javaAPI);
presets.updatePreset(request);
return presets.getAll();
}
@CrossOrigin
@PostMapping("api/renamePreset")
@ResponseBody
public List<Preset> renamePreset(@RequestBody RenamePresetRequest request) throws NotFoundError, ARException {
Presets presets = new Presets(javaAPI);
presets.renamePreset(request);
for (var v : presets.getAll()) {
System.out.println(v.getName());
}
return presets.getAll();
}
@CrossOrigin("*")
@GetMapping("/api/getAllPresets")
@ResponseBody
public List<Preset> getAllPresets() throws NotFoundError, ARException {
Presets presets = new Presets(javaAPI);
return presets.getAll();
}
/**
* Logs the received request with the endpoint and request body.
*
* @param endpoint the endpoint where the request was received
* @param request the request body as a string
*/
private void logRequest(String endpoint, String request) {
applicationLogger.info("Received request on endpoint %s with body:", endpoint);
applicationLogger.info(request);
}
}