Untitled

mail@pastecode.io avatar
unknown
java
15 days ago
8.7 kB
0
Indexable
Never
package sk.dwcslovakia.skeventaudit;

import CooLib.CooObject;
import CooLib.CooObjectArray;
import CooLib.CooStringArray;
import CooLib.CooTrace;
import CooLib.CooValue;

import static sk.dwcslovakia.skeventaudit.generated.DUCX.coort;
import static sk.dwcslovakia.skeventaudit.generated.DUCX.cootx;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import CooLib.CooException;
import CooLib.CooMethod;
import sk.dwcslovakia.skeventaudit.generated.DUCXImplementation;
import sk.dwcslovakia.skeventaudit.generated.EA.EA;
import sk.dwcslovakia.skeventaudit.generated.EA.actions.LogSearchResult;
import sk.dwcslovakia.skeventaudit.generated.EA.enums.TypeEnumEventLogLevel;
import sk.dwcslovakia.skeventaudit.generated.EA.structs.TypeAggrWidget;
//import sk.dwcslovakia.skeventaudit.generated.EA.EA;
import sk.dwcslovakia.skeventaudit.generated.SYS.SYS;
import sk.dwcslovakia.skeventaudit.generated.SYS.classes.Object;
import sk.dwcslovakia.skeventaudit.generated.EA.structs.TypeAggrWidget;
import sk.dwcslovakia.skeventaudit.generated.EA.structs.TypeAggrWidget;

public class SearchClass extends sk.dwcslovakia.skeventaudit.generated.SYS.classes.Object {
	
	//private static CooObject RestMethodClass = null;
	private static CooObject actcall = null;
	
	public SearchClass (CooObject obj) {
		  super(obj);
		  
	}
	// public String[]
	@DUCXImplementation("SKEVENTAUDIT@103.510:LogSearch")
	public LogSearchResult SearchData(final java.lang.String searchText) throws CooException {
		CooTrace.enter();
		CooTrace.message("SearchData ----> start");
		CooTrace.message(searchText);
		String searchQuery = "";
		actcall = coort.GetComponentObject(SYS.getClass_Action(), "SKWEBSVC@103.510:ActCallRestMethod");
		
		List<TypeAggrWidget> data = new ArrayList<TypeAggrWidget>();
		LogSearchResult result = new LogSearchResult();

		if (notNullOrEmpty(searchText)) {
			searchQuery = searchText.toLowerCase();
		}
		CooTrace.message("searchQuery " + searchQuery);

		CooObject ClassRestMeth = coort.GetComponentObject(sk.dwcslovakia.skeventaudit.generated.SYS.SYS.getClass_ObjectClass(), "SKWEBSVC@103.510:ClassRESTWSMethod");
		CooObject ElasticPutRest = coort.GetComponentObject(ClassRestMeth, "SKWEBSVC@103.510:ElasticLogPostRESTMeth");
		
		String urlExt = "/_search";
		//String searchStr = "{ \"size\": 10000,";
		//searchStr += " \"query\": { \"query_string\": { \"query\": \"" + searchQuery + "\", ";

		//searchStr += "\"fields\": [\"AuditLogID\", \"MessageID\", \"ReferenceID\", \"CorrelationID\", \"LogLevel\", \"ModulName\", \"MethodName\", \"UseCase\", \"SourceSystem\", \"ExternalUser\", \"CallTime\", \"SourceData\", \"Progress\", \"ResultCode\", \"ResultDescription\", \"ResultData\", \"Description\"] } } }";
		String searchStr = "{ \"size\": 10000, \"query\": { \"query_string\": { \"query\": \"" + searchQuery + "\", \"fields\": "
				+ "[\"AuditLogID\", "
				+ "\"MessageID\", "
				+ "\"ReferenceID\", "
				+ "\"CorrelationID\", "
				+ "\"LogLevel\", "
				+ "\"ModulName\", "
				+ "\"MethodName\", "
				+ "\"UseCase\", "
				+ "\"SourceSystem\", "
				+ "\"ExternalUser\", "
				+ "\"CallTime\", "
				+ "\"SourceData\", "
				+ "\"Progress\", "
				+ "\"ResultCode\", "
				+ "\"ResultDescription\", "
				+ "\"ResultData\", "
				+ "\"Description\"] } } }";

		
		JSONObject searchJSON = new JSONObject(searchStr);
		//CooTrace.message("----searchJSON: " + searchJSON.toString());
		
		CooTrace.message("---searchStr: " + searchStr);
		CooTrace.message("---urlExt: " + urlExt);
		//CooTrace.message("SEARCHJSON: " + searchJSON);
		CooTrace.message("---searchJSON:  " + searchJSON.toString());
		CooMethod CreateIndexLogMeth = ElasticPutRest.GetMethod(cootx, actcall);
		CreateIndexLogMeth.SetParameterValue(1, SYS.getTypeStringDef_STRING(), searchJSON.toString());
		CreateIndexLogMeth.SetParameterValue(2, SYS.getTypeStringDef_STRING(), urlExt);
		ElasticPutRest.CallMethod(cootx, CreateIndexLogMeth);
		
		
		CooStringArray dataOut = new CooStringArray(CreateIndexLogMeth.GetParameter(5, SYS.getTypeStringDef_STRINGLIST()));
		CooTrace.message("data out: " + dataOut);
		
		// parsovanie a spracovanie odpovede
		if (dataOut.size() > 0) {
		    String resp = dataOut.get(0).trim();
		    JSONObject response = new JSONObject(resp);

		    if (response.has("hits")) {
		        JSONObject hits = response.getJSONObject("hits");
		        if (hits.has("hits")) {
		            JSONArray hitsArr = hits.getJSONArray("hits");
		            for (int i = 0; i < hitsArr.length(); i++) {
		                JSONObject searchItem = hitsArr.getJSONObject(i).getJSONObject("_source");
		                TypeAggrWidget aggrLine = TypeAggrWidget.create();
		                
		                // Set the values for each field from searchItem
		                aggrLine.EA_AttrStrAuditLogID = searchItem.optString("AuditLogID", "N/A");
		                aggrLine.EA_AttrStrMessageID = searchItem.optString("MessageID", "N/A");
		                aggrLine.EA_AttrStrReferenceID = searchItem.optString("ReferenceID", "N/A");
		                aggrLine.EA_AttrStrCorrelationID = searchItem.optString("CorrelationID", "N/A");
		                aggrLine.EA_AttrEnumLogLevel = safeLogLevelFromString(searchItem.optString("LogLevel", "INFO"));
		                aggrLine.EA_AttrStrModulName = searchItem.optString("ModuleName", "N/A");
		                aggrLine.EA_AttrStrMethodName = searchItem.optString("MethodName", "N/A");
		                aggrLine.EA_AttrStrUseCase = searchItem.optString("UseCase", "N/A");
		                aggrLine.EA_AttrStrSourceSystem = searchItem.optString("SourceSystem", "N/A");
		                aggrLine.EA_AttrStrExternalUser = searchItem.optString("ExternalUser", "N/A");
		                aggrLine.EA_AttrStrCallTime = searchItem.optString("CallTime", "N/A");
		                aggrLine.EA_AttrStrProgress = searchItem.optString("Progress", "N/A");
		                aggrLine.EA_AttrStrResultCode = searchItem.optString("ResultCode", "N/A");
		                aggrLine.EA_AttrStrResultDescription = searchItem.optString("ResultDescription", "N/A");
		                aggrLine.EA_AttrStrDescription = searchItem.optString("Description", "N/A");

		             // Log the retrieved data
                        CooTrace.message("AuditLogID: " + aggrLine.EA_AttrStrAuditLogID + ", MessageID: " + aggrLine.EA_AttrStrMessageID +
                                         ", ReferenceID: " + aggrLine.EA_AttrStrReferenceID + ", CorrelationID: " + aggrLine.EA_AttrStrCorrelationID +
                                         ", LogLevel: " + aggrLine.EA_AttrEnumLogLevel + ", ModuleName: " + aggrLine.EA_AttrStrModulName +
                                         ", MethodName: " + aggrLine.EA_AttrStrMethodName + ", UseCase: " + aggrLine.EA_AttrStrUseCase +
                                         ", SourceSystem: " + aggrLine.EA_AttrStrSourceSystem + ", ExternalUser: " + aggrLine.EA_AttrStrExternalUser +
                                         ", CallTime: " + aggrLine.EA_AttrStrCallTime + ", Progress: " + aggrLine.EA_AttrStrProgress +
                                         ", ResultCode: " + aggrLine.EA_AttrStrResultCode + ", ResultDescription: " + aggrLine.EA_AttrStrResultDescription +
                                         ", Description: " + aggrLine.EA_AttrStrDescription);
		                // Adding to aggregate
		                //this.EA_AttrAggrResultLines.addAll(EA_AttrAggrResultLines);
		                //this.EA_AttrAggrResultLines.add(aggrLine);
                        data.add(aggrLine);
                        CooTrace.message("### DATA ### " + data);
		                //CooTrace.message("### THIS ### " + this);
		                //CooTrace.message("---NAPLNENY AGREGAT: ---" + this.EA_AttrAggrResultLines);
		            }
		            //this.EA_AttrAggrResultLines.addAll(temporaryAggregate);
		            result.data = data;
		            CooTrace.message("+++ ALL +++ " + result);
		        }
		    }
		}

	    
		CooTrace.leave();
		return result;
	}
	private TypeEnumEventLogLevel safeLogLevelFromString(String logLevelStr) {
	    for (TypeEnumEventLogLevel level : TypeEnumEventLogLevel.values()) {
	        if (level.name().equalsIgnoreCase(logLevelStr)) {
	            return level;
	        }
	    }
	    return null; // Return null if no match is found
	}



	
	public static boolean notNullOrEmpty(String strValue) {
		return (strValue != null) && (strValue.trim().length() > 0);
	}
}
Leave a Comment