states update

main
Said Gedik 2024-03-19 15:41:18 +01:00
parent c4d929751e
commit 625247e270
3 changed files with 128 additions and 70 deletions

View File

@ -47,7 +47,6 @@ public class RemedyJavaAPI {
server.setPort(46262);
this.connect();
// TODO: move to APIApplication.java or State.java - see where it fits
State.getInstance().queryState(this);
SupportGroup.getInstance().querySupportGroups(this);
@ -140,21 +139,20 @@ public class RemedyJavaAPI {
public String deleteEntry(String formName, String entryId) throws ARException {
// Specify 0 for the deleteOption as mentioned in the documentation
int deleteOption = 0;
// Attempt to delete the entry
server.deleteEntry(formName, entryId, deleteOption);
// Check for any status or warnings after the deletion
var lastStatus = server.getLastStatus();
if (!lastStatus.isEmpty()) {
applicationLogger.warn("Warning after deleting entry: " + lastStatus);
return "Warning: " + lastStatus.toString();
}
applicationLogger.info("Entry with ID " + entryId + " deleted successfully from form " + formName);
return "Entry deleted successfully. ID: " + entryId;
}
/**
* Takes an entry ID and a {@link Query} object with values and updates the
@ -351,6 +349,7 @@ public class RemedyJavaAPI {
}
}
public boolean isAdministrator() throws ARException {
return server.isAdministrator();
}

View File

@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import com.bmc.arsys.api.ARException;
import com.bmc.arsys.api.Entry;
@ -68,7 +69,7 @@ public class State {
* @throws ARException
*/
public void queryState(RemedyJavaAPI api) throws ARException, NotFoundError {
queryStateNames(api);
// queryStateNames(api);
queryPossibleStates(api);
if (state.isEmpty()) {
@ -94,45 +95,85 @@ public class State {
.addFieldId("actualState", 700003005)
.addFieldId("restartFlag", 700003010)
.addFieldId("implementerFlag", 700003012)
.addFieldId("possibleState", 700003006) // Include possibleState here
.build();
var stateFields = api.queryFieldsById("\'Menu\' = \"CHANGE_CALENDER_STATUS\"",
configurationQuery.getFieldIds(),
configurationQuery.getFormName(), null, 0, 0);
var configurationStatusQuery = new Query.QueryBuilder("ITSM:Configuration")
.addFieldId("actualState", 700003005)
.addFieldId("possibleState", 700003006)
.build();
var stateFields = api.queryFieldsById("\'Menu\' = \"CHANGE_CALENDER_STATUS\"",
configurationQuery.getFieldIds(),
configurationQuery.getFormName(), null, 0, 0);
var stateStatusFields = api.queryFieldsById("\'Menu\' = \"CHANGE_CALENDER_STATUS_TRANSITIONS\"",
configurationStatusQuery.getFieldIds(),
configurationStatusQuery.getFormName(), null, 0, 0);
for (int i = 0; i < stateFields.size(); i++) {
populateStateInfo(stateFields, stateStatusFields, configurationQuery, configurationStatusQuery);
for (int j = 0; j < stateStatusFields.size(); j++) {
// Print state information
printStateInfo();
}
int actualState = stateFields.get(i).get(configurationQuery.getFieldId("actualState")).getIntValue();
int statusActualState = stateStatusFields.get(j).get(configurationStatusQuery.getFieldId("actualState"))
private void populateStateInfo(List<Entry> stateFields, List<Entry> stateStatusFields,
Query configurationQuery, Query configurationStatusQuery) throws ARException {
for (var stateField : stateFields) {
int actualState = stateField.get(configurationQuery.getFieldId("actualState")).getIntValue();
boolean cancelFlag = false;
boolean restartFlag = false;
boolean implementerFlag = false;
try {
// Extract additional fields
cancelFlag = stateField.get(configurationQuery.getFieldId("cancelFlag")).toString()
.equals("CANCEL_YES");
restartFlag = stateField.get(configurationQuery.getFieldId("restartFlag")).toString()
.equals("RESTARTALLOWED_YES");
implementerFlag = stateField.get(configurationQuery.getFieldId("implementerFlag")).toString()
.equals("SETIMPLEMENTER_YES");
} catch (Exception e) {
e.printStackTrace();
}
String engName = stateField.get(configurationQuery.getFieldId("engName")).toString();
String gerName = stateField.get(configurationQuery.getFieldId("gerName")).toString();
// Initialize StateInfo with additional fields
StateInfo stateInfo = new StateInfo(engName, gerName, cancelFlag, restartFlag, implementerFlag);
for (var statusField : stateStatusFields) {
int statusActualState = statusField.get(configurationStatusQuery.getFieldId("actualState"))
.getIntValue();
if (actualState == statusActualState) {
int possibleState = stateStatusFields.get(j)
.get(configurationStatusQuery.getFieldId("possibleState")).getIntValue();
this.getState().get(actualState).addPossibleState(possibleState);
int possibleState = statusField.get(configurationStatusQuery.getFieldId("possibleState"))
.getIntValue();
stateInfo.addPossibleState(possibleState);
}
}
// Add StateInfo to the state HashMap
getState().put(actualState, stateInfo);
}
}
for (var v : get()) {
System.out.println(v.actualState);
private void printStateInfo() {
for (var state : get()) {
// Join possible states into a single string
String possibleStatesString = String.join(", ",
state.possibleStates.stream().map(Object::toString).collect(Collectors.toList()));
// Print the state information
System.out.printf("\n\n[%s]\n[%s]\n[%s]\n[%s]\n[%b]\n[%b]\n[%b]\n\n",
state.actualState, state.stateNameDE, state.stateNameEN,
possibleStatesString, state.cancelFlag, state.restartFlag, state.implementerFlag);
// Print the number of possible states
System.out.println("Possible States: " + state.possibleStates.size());
}
System.out.println(stateFields.size());
System.out.println(stateStatusFields.size());
// updatePossibleStates(stateFields, configurationQuery);
}
/**
@ -142,14 +183,16 @@ public class State {
* @param stateFields List of Entry to be processed
* @param configurationQuery Query object of stateFields
*/
public void updatePossibleStates(List<Entry> stateFields, Query configurationQuery) {
stateFields.stream().forEach(entry -> {
var actualState = entry.get(configurationQuery.getFieldId("actualState")).getIntValue();
var possibleState = entry.get(configurationQuery.getFieldId("possibleState")).getIntValue();
this.getState().get(actualState).addPossibleState(possibleState);
});
}
// public void updatePossibleStates(List<Entry> stateFields, Query
// configurationQuery) {
// stateFields.stream().forEach(entry -> {
// var actualState =
// entry.get(configurationQuery.getFieldId("actualState")).getIntValue();
// var possibleState =
// entry.get(configurationQuery.getFieldId("possibleState")).getIntValue();
// this.getState().get(actualState).addPossibleState(possibleState);
// });
// }
/**
* Query the German state name from the specified form. The German name in the
* form is associated with the English one. For merging the names into the
@ -159,22 +202,19 @@ public class State {
* @param api Remedy API object
* @throws ARException if an error occurs during the query
*/
private void queryStateNames(RemedyJavaAPI api) throws ARException {
var nameQuery = new Query.QueryBuilder("SYS:Menu Items Locale LkUp")
.addFieldId("englishName", 1000004339)
.addFieldId("germanName", 1000004338)
.addFieldId("Locale", 1000004342)
.addFieldId("SelectionCode", 1000004336)
.build();
var stateNames = api.queryFieldsById("\'Menu Type\' = \"Change Status Values\"",
nameQuery.getFieldIds(),
nameQuery.getFormName(), null, 0, 0);
updateStateNames(stateNames, nameQuery);
}
// private void queryStateNames(RemedyJavaAPI api) throws ARException {
// var nameQuery = new Query.QueryBuilder("SYS:Menu Items Locale LkUp")
// .addFieldId("englishName", 1000004339)
// .addFieldId("germanName", 1000004338)
// .addFieldId("Locale", 1000004342)
// .addFieldId("SelectionCode", 1000004336)
// .build();
// var stateNames = api.queryFieldsById("\'Menu Type\' = \"Change Status
// Values\"",
// nameQuery.getFieldIds(),
// nameQuery.getFormName(), null, 0, 0);
// updateStateNames(stateNames, nameQuery);
// }
/**
* Takes the relevant data about state names out of the {@code List<Entry>} and
* processes them to be saved into the State.
@ -182,24 +222,24 @@ public class State {
* @param stateFields List of Entry to be processed
* @param nameQuery Query object of stateFields
*/
public void updateStateNames(List<Entry> stateFields, Query nameQuery) {
stateFields.stream()
.filter(entry -> Optional.ofNullable(entry.get(nameQuery.getFieldId("Locale")))
.map(Object::toString)
.orElse("")
.equals("de"))
.forEach(
entry -> {
var selectionCode = nameQuery.getFieldId("SelectionCode");
var englishName = nameQuery.getFieldId("englishName");
var germanName = nameQuery.getFieldId("germanName");
this.getState().put(entry.get(selectionCode).getIntValue(),
new StateInfo(entry.get(englishName).toString(),
entry.get(germanName).toString()));
});
}
// public void updateStateNames(List<Entry> stateFields, Query nameQuery) {
// stateFields.stream()
// .filter(entry ->
// Optional.ofNullable(entry.get(nameQuery.getFieldId("Locale")))
// .map(Object::toString)
// .orElse("")
// .equals("de"))
// .forEach(
// entry -> {
// var selectionCode = nameQuery.getFieldId("SelectionCode");
// var englishName = nameQuery.getFieldId("englishName");
// var germanName = nameQuery.getFieldId("germanName");
// this.getState().put(entry.get(selectionCode).getIntValue(),
// new StateInfo(entry.get(englishName).toString(),
// entry.get(germanName).toString()));
// });
// }
/**
* Generate an Array of JSON objects that consists of all the state information.
* These are the integer representation of the actual state, an Array of integer
@ -209,10 +249,12 @@ public class State {
* @return Array of JSON objects with integer representation of state and
* {@link StateInfo}
*/
public ArrayList<StateResponse> get() {
var response = new ArrayList<StateResponse>();
this.state.forEach((key, value) -> {
response.add(new StateResponse(key, value.possibleState, value.stateNameEN, value.stateNameDE));
response.add(new StateResponse(key, value.possibleState, value.stateNameEN, value.stateNameDE,
value.cancelFlag, value.restartFlag, value.implementerFlag));
});
return response;
}
@ -224,6 +266,9 @@ public class State {
String stateNameEN;
String stateNameDE;
ArrayList<Integer> possibleState;
boolean cancelFlag;
boolean restartFlag;
boolean implementerFlag;
/**
* Initializes an instance of the {@link StateInfo} class.
@ -245,10 +290,14 @@ public class State {
* @param stateNameEN state name in English
* @param stateNameDE state name in German
*/
public StateInfo(String stateNameEN, String stateNameDE) {
this.possibleState = new ArrayList<>();
public StateInfo(String stateNameEN, String stateNameDE, boolean cancelFlag, boolean restartFlag,
boolean implementerFlag) {
this.stateNameEN = stateNameEN;
this.stateNameDE = stateNameDE;
this.cancelFlag = cancelFlag;
this.restartFlag = restartFlag;
this.implementerFlag = implementerFlag;
this.possibleState = new ArrayList<>();
}
/**
@ -298,6 +347,5 @@ public class State {
public void setStateNameEN(String stateNameEN) {
this.stateNameEN = stateNameEN;
}
}
}

View File

@ -10,6 +10,9 @@ public class StateResponse {
public ArrayList<Integer> possibleStates;
public String stateNameEN;
public String stateNameDE;
public boolean cancelFlag;
public boolean restartFlag;
public boolean implementerFlag;
/**
* Initialize the instance of the JSON response.
@ -20,11 +23,19 @@ public class StateResponse {
* @param stateNameDE state name in german
*/
public StateResponse(int actualState, ArrayList<Integer> possibleState, String stateNameEN,
String stateNameDE) {
String stateNameDE, boolean cancelFlag, boolean restartFlag, boolean implemeneterFlag) {
this.actualState = actualState;
this.possibleStates = possibleState;
this.stateNameEN = stateNameEN;
this.stateNameDE = stateNameDE;
this.cancelFlag = cancelFlag;
this.restartFlag = restartFlag;
this.implementerFlag = implemeneterFlag;
}
public Object get(int englishName) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'get'");
}
}