Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using inRiver.Remoting;
- using inRiver.Remoting.Exceptions;
- using inRiver.Remoting.Objects;
- using inRiver.Remoting.Query;
- using inRiver.Remoting.Security;
- using log4net;
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Security.Authentication;
- using System.ServiceModel;
- namespace Haglofs.GetDuplicateEntities {
- class Program
- {
- private static readonly ILog Log4 = LogManager.GetLogger(typeof(Program));
- static void Main(string[] args) {
- string server = ConfigurationManager.AppSettings["server"];
- string user = ConfigurationManager.AppSettings["username"];
- string password = ConfigurationManager.AppSettings["password"];
- Log4.Info("Starting code.");
- AuthenticationTicket ticket;
- try
- {
- ticket = RemoteManager.Authenticate(server, user, password);
- }
- catch (EndpointNotFoundException epEx)
- {
- Log4.Error("Authentication failed for communication reason!");
- Log4.ErrorFormat("Error message: {0}", epEx.Message);
- Console.ReadLine();
- return;
- }
- catch (SecurityException secEx)
- {
- Log4.Error("Authentication failed for Security reason!");
- Log4.ErrorFormat("Error message: {0}", secEx.Message);
- Console.ReadLine();
- return;
- }
- catch (AuthenticationException autEx)
- {
- Log4.Error("Authentication failed!");
- Log4.ErrorFormat("Error message: {0}", autEx.Message);
- Console.ReadLine();
- return;
- }
- catch (Exception ex)
- {
- Log4.Error("Authentication failed for unknown reason!");
- Log4.ErrorFormat("Error message: {0}", ex.Message);
- Console.ReadLine();
- return;
- }
- try
- {
- RemoteManager.CreateInstance(server, ticket);
- Log4.Info(
- "Authentication Succeeded.");
- Console.Clear();
- int firstUserInput = 0;
- do
- {
- firstUserInput = DisplayFirstMenu();
- var secondUserInput = DisplaySecondMenu();
- var thirdUserInput = DisplayThirdMenu();
- if (firstUserInput == 1) {
- Console.WriteLine("Processing");
- FindDuplicates("Product", secondUserInput, thirdUserInput);
- Console.WriteLine("Done!");
- } else if (firstUserInput == 2) {
- Console.WriteLine("Processing");
- FindDuplicates("Item", secondUserInput, thirdUserInput);
- Console.WriteLine("Done!");
- }
- } while (firstUserInput != 3);
- }
- catch (Exception e)
- {
- Log4.Error("Couldn't create instance: " + e.Message);
- }
- }
- internal static void FindDuplicates(string entityName, int printChoice, int? startId){
- Log4.Info("Searching for all entityIds");
- var entityIds = RemoteManager.DataService.GetAllEntityIdsForEntityType(entityName);
- var addedEntities = new List<string>();
- Log4.Info("Found " + entityIds.Count + " Entities");
- var startIndex = 0;
- if (startId != null){
- Log4.Info("Looking for startIndex");
- startIndex = entityIds.IndexOf((int)startId);
- }
- for (var i=startIndex; i<entityIds.Count; i++) {
- try
- {
- Log4.Info("Retrieving data for SystemId: " + entityIds[i]);
- var entity = RemoteManager.DataService.GetEntity(entityIds[i], LoadLevel.DataOnly);
- var articleNumber = entity.GetField(entityName + "Number").Data.ToString();
- Log4.Info("Got data for SystemId: " + entityIds[i] + " with articleNumber: " + articleNumber);
- if (!addedEntities.Contains(articleNumber)) {
- Log4.Info("Searching for articlenumber: " + articleNumber + " duplicates");
- var duplicatesFound = SearchForDuplicateEntities(entityName, articleNumber);
- if (duplicatesFound.Count > 1){
- Log4.Info("Found " + duplicatesFound.Count + " duplicates");
- addedEntities.Add(articleNumber);
- Log4.Info("Checking for field differences between the duplicates");
- var differentingFields = FindFieldsWithDifferences(duplicatesFound);
- if (!(printChoice == 2 && differentingFields != "") && !(printChoice == 1 && differentingFields == "")) {
- Log4.Info("Creating string to log");
- var stringToWrite = CreateStringEntry(duplicatesFound, entityName, articleNumber, differentingFields);
- Log4.Info("Write string to file");
- WriteToFile(stringToWrite, entityName, printChoice, differentingFields);
- }
- }
- }
- }
- catch (Exception e)
- {
- Log4.Error("Something went wrong: " + e.Message);
- }
- }
- Log4.Info("Complete! Went through all entities");
- }
- static string FindFieldsWithDifferences(List<Entity> entityList){
- var differencesString = "";
- try
- {
- var listOfFieldValues = new List<string>();
- var savedIndices = new List<int>();
- foreach (var entity in entityList) {
- Log4.Info("Checking fields for entity: " + entity.Id);
- if (listOfFieldValues.Count == 0) {
- foreach (var field in entity.Fields){
- if (field.Data != null){
- listOfFieldValues.Add(field.Data.ToString());
- }
- else{
- listOfFieldValues.Add(null);
- }
- }
- } else {
- for (var i = 0; i < entity.Fields.Count; i++) {
- if (entity.Fields[i].Data == null && listOfFieldValues[i] != null){
- Log4.Info("Found difference in field " + entity.Fields[i].FieldType.Id);
- savedIndices.Add(i);
- }
- if (entity.Fields[i].Data != null){
- if (entity.Fields[i].Data.ToString() != listOfFieldValues[i]) {
- Log4.Info("Found difference in field " + entity.Fields[i].FieldType.Id);
- savedIndices.Add(i);
- }
- }
- }
- }
- }
- foreach (var i in savedIndices){
- differencesString = differencesString + entityList.FirstOrDefault().Fields[i].FieldType.Name[new CultureInfo("sv")] + "\t";
- }
- return differencesString;
- } catch (Exception e)
- {
- Log4.Error("Couldn't find differenting fields in for duplicated entity because: " + e.Message);
- return differencesString;
- }
- }
- internal static List<Entity> SearchForDuplicateEntities(string entity, string articleNumber) {
- try
- {
- Query query = new Query();
- Criteria criteria1 = new Criteria();
- criteria1.FieldTypeId = entity + "Number";
- criteria1.Operator = Operator.Equal;
- criteria1.Value = articleNumber;
- query.Criteria = new List<Criteria>();
- query.Criteria.Add(criteria1);
- return RemoteManager.DataService.Search(query, LoadLevel.DataOnly);
- } catch (Exception e)
- {
- Log4.Error("Couldn't search for " + articleNumber + " because " + e);
- return null;
- }
- }
- internal static string CreateStringEntry(List<Entity> duplicatesFound, string entityName, string articleNumber, string differentingFields){
- try
- {
- var stringToWrite = "";
- foreach (var entity in duplicatesFound){
- string line = string.Format(
- "{0}\t{1}\t{2}\t{3}\t{4}\r\n",
- entity.Id,
- articleNumber ?? "Saknas",
- entity.GetField(entityName + "Season").Data ?? "Saknas",
- entity.LastModified.Date.ToShortDateString(),
- differentingFields
- );
- stringToWrite = stringToWrite + line;
- }
- return stringToWrite;
- } catch (Exception e)
- {
- Log4.Error("Couldn't create string for " + articleNumber + " because " + e);
- return null;
- }
- }
- internal static void WriteToFile(string message,string entity, int printChoice, string differentingFields) {
- var destination = string.Format("{0}\\{1}", Environment.CurrentDirectory, "files");
- var fileNameDiff = string.Format("{0}\\{1}-differentiating.txt", destination, entity);
- var fileNameNonDiff = string.Format("{0}\\{1}-nondifferentiating.txt", destination, entity);
- try {
- if (!Directory.Exists(destination)){
- Directory.CreateDirectory(destination);
- }
- if (printChoice == 1) {
- PrintToFile(fileNameDiff, message);
- } else if (printChoice == 2) {
- PrintToFile(fileNameNonDiff, message);
- } else {
- if (differentingFields == "") {
- PrintToFile(fileNameNonDiff, message);
- } else {
- PrintToFile(fileNameDiff, message);
- }
- }
- } catch(Exception e)
- {
- Log4.Error("Couldn't write to file because " + e);
- }
- }
- internal static void PrintToFile(string fileName, string message) {
- if (!File.Exists(fileName)) {
- using (StreamWriter file = new StreamWriter(fileName)) {
- file.Write("SysID\tArtiklenummer\tSeason\tLastModified\tChangedField\r\n");
- file.Flush();
- file.Close();
- }
- }
- File.AppendAllText(fileName, message);
- }
- internal static int DisplayFirstMenu()
- {
- Console.WriteLine("GetDuplicates");
- Console.WriteLine();
- Console.WriteLine("1. Create file with Product duplicates");
- Console.WriteLine("2. Create file with Item duplicates");
- Console.WriteLine();
- var result = Console.ReadLine();
- return Convert.ToInt32(result);
- }
- internal static int DisplaySecondMenu() {
- Console.Clear();
- Console.WriteLine("1. Only print duplicates with fields differentiating");
- Console.WriteLine("2. Only print duplicates with fields NOT differentiating");
- Console.WriteLine("3. Both");
- Console.WriteLine();
- var result = Console.ReadLine();
- return Convert.ToInt32(result);
- }
- internal static int? DisplayThirdMenu()
- {
- Console.Clear();
- Console.WriteLine("Do you want to start at a specific SystemId? If not, just press enter");
- Console.WriteLine();
- var result = Console.ReadLine();
- try
- {
- return Convert.ToInt32(result);
- } catch (FormatException e)
- {
- return null;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement