import("system.process");
import("KeywordRegistry_basic");
import("system.db");
import("ActivityTask_lib");
import("Employee_lib");
import("system.util");
import("system.notification");
import("system.notificationtypes");
import("system.entities");
import("system.project");
import("system.indexsearch");
import("system.question");
import("system.logging");
import("DuplicateScanner_lib");
import("JditoFilter_lib");

//let testFields = [];
//let filters = DuplicateScannerUtils.loadFilters("PersonDuplicates", "Person_entity")
//logging.log("filters -> " + filters);
//
//for (let i = 0; i < filters.length; i++) 
//{
//    logging.log("filters[i] -> " + filters[i]);
//    let filter = JSON.parse(filters[i][0]).filter;
//    let fields = JditoFilterUtils.getFilterFields(filter.childs);
//    testFields = testFields.concat(fields);
//}
//logging.log("testFields -> " + testFields);


//##############################Test Duplicate Scan######################################################

//var filterName = "PersonDuplicates";
//var targetEntity = "Person_entity";
//var resultFieldsIdFieldName = "CONTACTID";
//
//var tmpFieldsInFilterRay = ["CONTACTID", "FIRSTNAME", "LASTNAME", "GENDER"];
//var queryPersonContactIds = "select CONTACTID, FIRSTNAME, LASTNAME, GENDER from CONTACT"
//                            + " join PERSON on PERSONID = PERSON_ID";
//
//
//var filterFieldValueRays = [["CONTACTID", "29271db0-4253-46c9-b7c2-5e25376b9d19"], ["FIRSTNAME", "Narkus"], ["LASTNAME", "Bltinger"], ["GENDER", "m"]];
//
////DuplicateScannerUtils.ScanForDuplicatesIndex = function(pFilterName, pTargetEntity, pFilterFieldValueRays,
////pTargetEntityResultFields, pRecordIdFieldToIgnore, pRecordIdValueToIgnore)
//
///* 
// * 
// */
//
//
//
//let duplicateFieldsConfig = DuplicateScannerUtils.LoadDuplicateIndexFieldsConfiguration(filterName, targetEntity);
//
//let querySelectFields = "";
//for (let i = 0; i < duplicateFieldsConfig.length; i++) 
//{
//    querySelectFields += duplicateFieldsConfig[i][0];
//    
//    if(i < duplicateFieldsConfig.length)
//        querySelectFields += ", ";
//}
//
//let queryPersonFieldData = "select " + querySelectFields + " from CONTACT"
//                            + " join PERSON on PERSONID = PERSON_ID";
//
//DuplicateScannerUtils.GetEntityFieldNameValueMap(duplicateFieldsConfig);
//
//DuplicateScannerUtils.ScanForDuplicatesIndex(filterName, targetEntity, 
//filterFieldValueRays, [], resultFieldsIdFieldName, "29271db0-4253-46c9-b7c2-5e25376b9d19");


//##############################ANs Beispiel######################################################


//logging.log("TEST INDEX API with Entities");
//logging.log(indexsearch.lookupIndexField("Person_entity", "FIRSTNAME"));
//logging.log(indexsearch.lookupIndexField("Person_entity", "FIRSTNAME.value"));
//logging.log(indexsearch.lookupIndexField("Person_entity", "PersAddresses.CITY"));
//logging.log(indexsearch.lookupIndexField("Person_entity", "PersAddresses.CITY.value"));
//logging.log(indexsearch.lookupIndexField(null, "Person_entity.FIRSTNAME.value"));
//logging.log(indexsearch.lookupIndexField(null, "Person_entity.PersAddresses.CITY.value"));
//var json = '{"entity":"Person_entity","filter":{"type":"group","operator":"AND","childs":[{"type":"row","name":"FIRSTNAME","operator":"STARTSWITH","value":"asd","key":"","contenttype":"TEXT"},{"type":"group","operator":"OR","childs":[{"type":"row","name":"LASTNAME","operator":"STARTSWITH","value":"L","key":"","contenttype":"TEXT"}]}]}}';
//logging.log(indexsearch.buildQueryFromSearchCondition(json));
//
//var t1 = indexsearch.createTerm("lisa").setEntityField("Person_entity.FIRSTNAME");
//var t2 = indexsearch.createTerm("sommer").setEntityField("Person_entity.LASTNAME");
//var t3 = indexsearch.createWildcardTerm("L").setEntityField("Person_entity.PersAddresses.CITY");
//
//var patternConf = indexsearch.createPatternConfig().or(t1).or(t2).or(t3);
//var pattern = indexsearch.buildPatternString(patternConf);
//logging.log(pattern); 
//
//var query = indexsearch.createIndexQuery()
//.setPattern(pattern)
//.setEntities("Person_entity")
////.addResultIndexFields(indexsearch.FIELD_ID)
//.addResultFields("Person_entity.FIRSTNAME")
//.addSearchFields("Person_entity.FIRSTNAME", "Person_entity.LASTNAME");
//
//var res = indexsearch.searchIndex(query);
//logging.log("" + res);



//######################################Demosuche nach Datensatz##############################################




//
//let indexQuery = indexsearch.createIndexQuery()
//                            .setPattern("(+(-contactid_value:(29271db0-4253-46c9-b7c2-5e25376b9d19)) +gender_value:m*)")
//                            .setEntities(["Person_entity"])
//                            .addResultFields("Person_entity.FIRSTNAME")
//                            .setRows(50);
//
//
//let filterTerm1 = indexsearch.createTerm("Barkus")
//                            .setIndexField("firstname_value")
//                            .setFuzzySearchFactor(0);
////let filterTerm2 = indexsearch.createTerm("Altinger")
////                            .setIndexField("lastname_value")
////                            .setFuzzySearchFactor(0);
//
//let filterPatternConfig = indexsearch.createPatternConfig().and(filterTerm1);
//
//let filterPatternString = indexsearch.buildPatternString(filterPatternConfig);
//logging.log("Hauptsuche filterPatternString -> " + filterPatternString);
//indexQuery = indexQuery.addFilter(filterPatternString);
//
//let searchResult = indexsearch.searchIndex(indexQuery);
//logging.log("searchResult -> " + searchResult);
//
//logging.log("searchResults hits length -> " + searchResult[indexsearch.HITS].length);
//
//for (let i = 0; i < searchResult[indexsearch.HITS].length; i++) 
//{
//    logging.log("Treffer Nr -> " + i);
//    //searchResults hits 0 -> {#ADITO_SEARCH_ID=1868bd3a-05af-4b7f-a633-e3aec50ac45c, _index_group_=Person, #ADITO_SEARCH_TYPE=Person, firstname_value=Peter, _local_id_=1868bd3a-05af-4b7f-a633-e3aec50ac45c}
//    let localId = searchResult[indexsearch.HITS][i]["_local_id_"];
//    let firstname = searchResult[indexsearch.HITS][i]["firstname_value"];
//    let indexGroup = searchResult[indexsearch.HITS][i]["_index_group_"];
//    logging.log("localId -> " + localId);
//    logging.log("firstname -> " + firstname);
//    logging.log("indexGroup -> " + indexGroup);
//}





//####################################################################################


//
//var filterName = "PersonDuplicates";
//var targetEntity = "Person_entity";
//var resultFieldsIdFieldName = "CONTACTID";
//var queryPersonContactIds = "select CONTACTID, FIRSTNAME, LASTNAME, GENDER from CONTACT"
//                            + " join PERSON on PERSONID = PERSON_ID";
//var tmpFieldsInFilterRay = ["CONTACTID", "FIRSTNAME", "LASTNAME", "GENDER"];
//
//logging.log("Löschen von PERSON Dubletten -> ");
//DuplicateScannerUtils.deleteClustersByTargetEntity("Person_entity");
//
//logging.log("Neu berechnen von PERSON Dubletten -> ");
//DuplicateScannerUtils.rebuildDuplicatesCache(filterName, targetEntity, queryPersonContactIds,
//tmpFieldsInFilterRay, resultFieldsIdFieldName);
//
//DuplicateScannerUtils.refreshUnrelatedDuplicateRelations(targetEntity);

//##############################################################################

//filterName = "OrganisationDuplicates";
//targetEntity = "Organisation_entity";
//resultFieldsIdFieldName = "CONTACTID";
//queryPersonContactIds = "select CONTACTID, ORGANISATION.NAME from ORGANISATION"
//                            + " join CONTACT on CONTACT.CONTACTID = ORGANISATION.ORGANISATIONID"
//                            + " where CONTACTID != '0'";
//tmpFieldsInFilterRay = ["CONTACTID", "NAME"];
//
//
//logging.log("Löschen von ORGANISATION Dubletten -> ");
//DuplicateScannerUtils.deleteClustersByTargetEntity(targetEntity)
//
//logging.log("Neu berechnen von ORGANISATION Dubletten -> ");
//DuplicateScannerUtils.rebuildDuplicatesCache(filterName, targetEntity, queryPersonContactIds,
//tmpFieldsInFilterRay, resultFieldsIdFieldName);
//
//DuplicateScannerUtils.refreshUnrelatedDuplicateRelations(targetEntity);


//####################################Rebuild person duplicates##########################################

//var filterName = "PersonDuplicates";
//var targetEntity = "Person_entity";
//
//let duplicateFieldsConfig = DuplicateScannerUtils.LoadIndexFieldsConfiguration(filterName, targetEntity);
//let resultFields = DuplicateScannerUtils.getResultFields(filterName, targetEntity);
//
//logging.log("duplicateFieldsConfig -> " + duplicateFieldsConfig);
//logging.log("resultFields -> " + resultFields);
//
//let querySelectFields = DuplicateScannerUtils.BuildSqlSelectFieldsFromFieldConfig(duplicateFieldsConfig);
//logging.log("querySelectFields -> " + querySelectFields);
//
//let queryPersonFieldData = "select " + querySelectFields + " from CONTACT"
//                            + " join PERSON on PERSONID = PERSON_ID"
//                            + " left join ADDRESS on ADDRESS.CONTACT_ID = CONTACT.CONTACTID";
//
//logging.log("Löschen von PERSON Dubletten -> ");
//DuplicateScannerUtils.deleteClustersByTargetEntity(targetEntity);
//
//let formatToJsonAndCallWsCallback = function(pPossibleDuplicatesRay)
//{
//    let indexResultFields = DuplicateScannerUtils.translateEntityToIndexFields(targetEntity, resultFields)
//
//    //Run thru every duplicate result an read out the resultfields
//    for (let i = 0; i < pPossibleDuplicatesRay.length; i++)
//    {
//        for (let b = 0; b < resultFields.length; b++)
//        {
//            let entityFieldName = resultFields[b];
//            let indexFieldName = indexResultFields[entityFieldName];
//            //logging.log("Entity Field -> "+ pPossibleDuplicatesRay[i][indexFieldName]);
//            //format values
//        }
//    }
//    //call webservice
//    //reformat results to same structure as before
//    return pPossibleDuplicatesRay;
//};
//
//logging.log("Neu berechnen von PERSON Dubletten -> ");
//DuplicateScannerUtils.rebuildDuplicatesCache(filterName, targetEntity, queryPersonFieldData,
//duplicateFieldsConfig, resultFields, formatToJsonAndCallWsCallback);
//
//DuplicateScannerUtils.refreshUnrelatedDuplicateRelations(targetEntity);





//##################################single scanForDuplicates#######################################################################


//var filterName = "PersonDuplicates";
//var targetEntity = "Person_entity";
//
////Values to check, the same fields as configured
//let valuesToCheck = {};
//valuesToCheck["CONTACTID"] = "c7ddf982-0e58-4152-b82b-8f5673b0b729";
//valuesToCheck["FIRSTNAME"] = "Tim";
//valuesToCheck["GENDER"] = "m                                   ";
//
////The result values can be accessed as seen above in "formatToJsonAndCallWsCallback"
//let pPossibleDuplicatesRay = DuplicateScannerUtils.scanForDuplicates(filterName, targetEntity, valuesToCheck, null);
//
//logging.log(" pPossibleDuplicatesRay-> " + pPossibleDuplicatesRay);
//
//for (let i = 0; i < pPossibleDuplicatesRay.length; i++) 
//{
//    logging.log("pPossibleDuplicatesRay[i] -> " + pPossibleDuplicatesRay[i]);
//}
//


//################################ entity structure auslesen ##############################################


//var model = project.getEntityStructure("Person_entity"); 
//logging.log("Name: " + model.name); 
//logging.log("Title: " + model.title); 
//logging.log("Description: " + model.description); 
//logging.log("UsePermissions: " + model.usePermissions); 
//
//for (fieldname in model.fields) 
//{ 
//    field = model.fields[fieldname]; 
//    if(field.fieldType == project.ENTITYFIELDTYPE_FIELD)
//    {
//        logging.log(" Name: " + field.name); 
//        logging.log(" Type: " + field.fieldType); 
//        logging.log(" Title: " + field.title); 
//        logging.log(" Description: " + field.description); 
//        logging.log(" UsePermissions: " + field.usePermissions); 
//        logging.log("###################### -> ");
//    }
//    if(field.fieldType == project.ENTITYFIELDTYPE_CONSUMER)
//    {
//        let consumerEntity = field.entityName;
//        
//        if(consumerEntity == null || consumerEntity == "")
//            continue;
//        
//        let consumerEntityFields = project.getEntityStructure(consumerEntity);
//        for (consumerEntityFieldname in consumerEntityFields.fields) 
//        { 
//            consumerField = consumerEntityFields.fields[consumerEntityFieldname]; 
//            if(consumerField.fieldType == project.ENTITYFIELDTYPE_FIELD)
//            {
//                logging.log(" Name: " + consumerField.name); 
//                logging.log(" Type: " + consumerField.fieldType); 
//                logging.log(" Title: " + consumerField.title); 
//                logging.log(" Description: " + consumerField.description); 
//                logging.log(" UsePermissions: " + consumerField.usePermissions); 
//                logging.log("###################### -> ");
//            }
//        }
//    }
//}

//##############################################################################

//var model = project.getEntityStructure("Person_entity"); 
//let duplicateFieldsConfig = DuplicateScannerUtils.LoadIndexFieldsConfiguration(filterName, targetEntity);
//
//let combinedData = []
//let entityFieldsToLoad = [];
//for (field in duplicateFieldsConfig) 
//{
//    let entityFieldName = duplicateFieldsConfig[field][0];
//    let isIdField = duplicateFieldsConfig[field][1];
//    let useForIndexSearch = duplicateFieldsConfig[field][2];
//    let entityFieldData = model[entiyFieldName];
//    combinedData.push(entityFieldName, isIdField, useForIndexSearch, entityFieldData);
//}
//
//var filterName = "PersonDuplicates";
//var targetEntity = "Person_entity";
//DuplicateScannerUtils.getEntityRecords(targetEntity, entityFieldsToLoad, 0, 50);

//Beispiel 1: 
//Feld mit verknüpftem Consumer

//[entity, feldname, consumerName, ProviderName]
//let test = ["Communication_entity", "STANDARD_EMAIL_COMMUNICATION", "EmailCommunications", "EmailCommunications"];
//
//let testrow = entities.createConfigForLoadingRows()
//                        .fields([test[1]])
//                        .entity(test[0])
//                        .provider(test[3])
//                        .addParameter("ContactId_param", "d4c1bec3-656f-45ec-ae03-1c4d729d99fe")
//                        //.uid()
//let resultTest = entities.getRows(testrow);
//logging.log("resultTest -> " + JSON.stringify(resultTest));



//Beispiel 2: 
//Feld direkt von anderem Entity
//let testrow = entities.createConfigForLoadingRows()
//                        .fields(["ZIP"])
//                        .entity("Address_entity")
//                        .uid("1a67eaa7-21da-4a18-97ab-755ac5cb74f7")
//
//let resultTest = entities.getRows(testrow);
//logging.log("resultTest Beispiel 2 -> " + JSON.stringify(resultTest));


//indexsearch.runIndexer(["Person"]);




//let resultClusterId = DuplicateScannerUtils.GetClusterWithDuplicates(["7a34d9d0-04c7-478c-a8e2-f584fe625c45", "c7ddf982-0e58-4152-b82b-8f5673b0b729"]);
//logging.log("resultClusterId -> " + resultClusterId);


//
//var filterName = "PersonDuplicates";
//var targetEntity = "Person_entity";
//var recordBlockSize = DuplicateScannerUtils.GetBlockSizeForScanner(filterName, targetEntity);
//
//logging.log("recordBlockSize -> " + recordBlockSize);

//try 
//{
//    let sourceContactId = "sourceContactId";
//    let targetContactId = "targetContactId";
//    
//    var activityDataForInsert = {
//        subject: "Es wurde ein Personendatensatz in diesen integriert",
//        content: "Person mit ID " + sourceContactId + " wurde in Person mit ID " + targetContactId + " integriert",
//        //categoryKeywordId: $KeywordRegistry.ac
//        directionKeywordId: "x",
//        responsibleContactId: EmployeeUtils.getCurrentContactId()
//    };
//    
//    var activityLinks = [["Person", "6e667085-bb97-4039-8dfe-2230002985e0"]]
//
//    //activityLinks = ArrayUtils.distinct2d(activityLinks);//TODO: better check before adding the elements into the array if it already exists there
//    
//    var activityRes = ActivityUtils.insertNewActivity(activityDataForInsert, activityLinks, null, db.getCurrentAlias());
//} 
//catch (exception) 
//{ 
//    logging.log("exception -> " + exception);
//}

////notification.addNotification(util.getNewUUID(), null, null, null, notification., notification.PRIO_NORMAL, 2, notification.STATE_UNSEEN, [user], message, description);


//let currentContactId = EmployeeUtils.getCurrentContactId();
//DuplicateScannerUtils.CreateMergeSuccessActivity("a2e084e2-d68a-4f1e-a1bb-f8d46ad6293d", "6e667085-bb97-4039-8dfe-2230002985e0", currentContactId, "Person");


//logging.log("$KeywordRegistry.activityDirection$internal() -> " + $KeywordRegistry.activityDirection$internal());



//let processParameters = {
//    filterName: "PersonDuplicates",
//    targetEntity: "Person_entity" //process.execute is only able to handle strings
//}
//let userId = EmployeeUtils.getCurrentUserId();
//if(userId == null)
//    userId == "";
//try 
//{
//    let processId = "manualrun_rebuild_duplicatecache_" + util.getNewUUID();
//    process.executeTimer(processId, "RebuildAllDuplicatesCache_serverProcess", 0, true, false, process.TIMERTYPE_SERVER_RUN, userId, false, process.THREADPRIORITY_LOW)
//    process.stopTimer(processId);
//    logging.log("test -> ");
//    process.executeAsync("RebuildAllDuplicateCaches_serverProcess", processParameters, false, userId, process.THREADPRIORITY_LOW)
//    logging.log("test2 -> ");
//} 
//catch (exception) 
//{ 
//    logging.log(" exception-> " + exception);
//}