Advertisement
Guest User

Untitled

a guest
Feb 16th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.65 KB | None | 0 0
  1. using inRiver.Remoting;
  2. using inRiver.Remoting.Exceptions;
  3. using inRiver.Remoting.Objects;
  4. using inRiver.Remoting.Query;
  5. using inRiver.Remoting.Security;
  6. using log4net;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Configuration;
  10. using System.Globalization;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Security.Authentication;
  14. using System.ServiceModel;
  15.  
  16. namespace Haglofs.GetDuplicateEntities {
  17. class Program
  18. {
  19. private static readonly ILog Log4 = LogManager.GetLogger(typeof(Program));
  20.  
  21. static void Main(string[] args) {
  22. string server = ConfigurationManager.AppSettings["server"];
  23. string user = ConfigurationManager.AppSettings["username"];
  24. string password = ConfigurationManager.AppSettings["password"];
  25. Log4.Info("Starting code.");
  26.  
  27.  
  28. AuthenticationTicket ticket;
  29. try
  30. {
  31. ticket = RemoteManager.Authenticate(server, user, password);
  32. }
  33. catch (EndpointNotFoundException epEx)
  34. {
  35. Log4.Error("Authentication failed for communication reason!");
  36. Log4.ErrorFormat("Error message: {0}", epEx.Message);
  37. Console.ReadLine();
  38. return;
  39. }
  40. catch (SecurityException secEx)
  41. {
  42. Log4.Error("Authentication failed for Security reason!");
  43. Log4.ErrorFormat("Error message: {0}", secEx.Message);
  44. Console.ReadLine();
  45. return;
  46. }
  47. catch (AuthenticationException autEx)
  48. {
  49. Log4.Error("Authentication failed!");
  50. Log4.ErrorFormat("Error message: {0}", autEx.Message);
  51. Console.ReadLine();
  52. return;
  53. }
  54. catch (Exception ex)
  55. {
  56. Log4.Error("Authentication failed for unknown reason!");
  57. Log4.ErrorFormat("Error message: {0}", ex.Message);
  58. Console.ReadLine();
  59. return;
  60. }
  61.  
  62. try
  63. {
  64. RemoteManager.CreateInstance(server, ticket);
  65. Log4.Info(
  66. "Authentication Succeeded.");
  67.  
  68. Console.Clear();
  69. int firstUserInput = 0;
  70. do
  71. {
  72. firstUserInput = DisplayFirstMenu();
  73. var secondUserInput = DisplaySecondMenu();
  74. var thirdUserInput = DisplayThirdMenu();
  75. if (firstUserInput == 1) {
  76. Console.WriteLine("Processing");
  77. FindDuplicates("Product", secondUserInput, thirdUserInput);
  78. Console.WriteLine("Done!");
  79. } else if (firstUserInput == 2) {
  80. Console.WriteLine("Processing");
  81. FindDuplicates("Item", secondUserInput, thirdUserInput);
  82. Console.WriteLine("Done!");
  83. }
  84. } while (firstUserInput != 3);
  85.  
  86. }
  87. catch (Exception e)
  88. {
  89. Log4.Error("Couldn't create instance: " + e.Message);
  90. }
  91. }
  92.  
  93. internal static void FindDuplicates(string entityName, int printChoice, int? startId){
  94. Log4.Info("Searching for all entityIds");
  95. var entityIds = RemoteManager.DataService.GetAllEntityIdsForEntityType(entityName);
  96. var addedEntities = new List<string>();
  97. Log4.Info("Found " + entityIds.Count + " Entities");
  98.  
  99. var startIndex = 0;
  100. if (startId != null){
  101. Log4.Info("Looking for startIndex");
  102. startIndex = entityIds.IndexOf((int)startId);
  103. }
  104.  
  105. for (var i=startIndex; i<entityIds.Count; i++) {
  106. try
  107. {
  108. Log4.Info("Retrieving data for SystemId: " + entityIds[i]);
  109. var entity = RemoteManager.DataService.GetEntity(entityIds[i], LoadLevel.DataOnly);
  110. var articleNumber = entity.GetField(entityName + "Number").Data.ToString();
  111. Log4.Info("Got data for SystemId: " + entityIds[i] + " with articleNumber: " + articleNumber);
  112.  
  113. if (!addedEntities.Contains(articleNumber)) {
  114. Log4.Info("Searching for articlenumber: " + articleNumber + " duplicates");
  115. var duplicatesFound = SearchForDuplicateEntities(entityName, articleNumber);
  116. if (duplicatesFound.Count > 1){
  117. Log4.Info("Found " + duplicatesFound.Count + " duplicates");
  118. addedEntities.Add(articleNumber);
  119.  
  120. Log4.Info("Checking for field differences between the duplicates");
  121. var differentingFields = FindFieldsWithDifferences(duplicatesFound);
  122.  
  123. if (!(printChoice == 2 && differentingFields != "") && !(printChoice == 1 && differentingFields == "")) {
  124.  
  125. Log4.Info("Creating string to log");
  126. var stringToWrite = CreateStringEntry(duplicatesFound, entityName, articleNumber, differentingFields);
  127. Log4.Info("Write string to file");
  128. WriteToFile(stringToWrite, entityName, printChoice, differentingFields);
  129. }
  130. }
  131. }
  132. }
  133. catch (Exception e)
  134. {
  135. Log4.Error("Something went wrong: " + e.Message);
  136. }
  137. }
  138. Log4.Info("Complete! Went through all entities");
  139. }
  140.  
  141. static string FindFieldsWithDifferences(List<Entity> entityList){
  142. var differencesString = "";
  143. try
  144. {
  145. var listOfFieldValues = new List<string>();
  146. var savedIndices = new List<int>();
  147. foreach (var entity in entityList) {
  148. Log4.Info("Checking fields for entity: " + entity.Id);
  149. if (listOfFieldValues.Count == 0) {
  150. foreach (var field in entity.Fields){
  151. if (field.Data != null){
  152. listOfFieldValues.Add(field.Data.ToString());
  153. }
  154. else{
  155. listOfFieldValues.Add(null);
  156. }
  157. }
  158. } else {
  159. for (var i = 0; i < entity.Fields.Count; i++) {
  160. if (entity.Fields[i].Data == null && listOfFieldValues[i] != null){
  161. Log4.Info("Found difference in field " + entity.Fields[i].FieldType.Id);
  162. savedIndices.Add(i);
  163. }
  164. if (entity.Fields[i].Data != null){
  165. if (entity.Fields[i].Data.ToString() != listOfFieldValues[i]) {
  166. Log4.Info("Found difference in field " + entity.Fields[i].FieldType.Id);
  167. savedIndices.Add(i);
  168. }
  169. }
  170. }
  171. }
  172. }
  173. foreach (var i in savedIndices){
  174. differencesString = differencesString + entityList.FirstOrDefault().Fields[i].FieldType.Name[new CultureInfo("sv")] + "\t";
  175. }
  176. return differencesString;
  177. } catch (Exception e)
  178. {
  179. Log4.Error("Couldn't find differenting fields in for duplicated entity because: " + e.Message);
  180. return differencesString;
  181. }
  182. }
  183.  
  184. internal static List<Entity> SearchForDuplicateEntities(string entity, string articleNumber) {
  185. try
  186. {
  187. Query query = new Query();
  188. Criteria criteria1 = new Criteria();
  189. criteria1.FieldTypeId = entity + "Number";
  190. criteria1.Operator = Operator.Equal;
  191. criteria1.Value = articleNumber;
  192.  
  193. query.Criteria = new List<Criteria>();
  194. query.Criteria.Add(criteria1);
  195. return RemoteManager.DataService.Search(query, LoadLevel.DataOnly);
  196. } catch (Exception e)
  197. {
  198. Log4.Error("Couldn't search for " + articleNumber + " because " + e);
  199. return null;
  200. }
  201. }
  202.  
  203. internal static string CreateStringEntry(List<Entity> duplicatesFound, string entityName, string articleNumber, string differentingFields){
  204. try
  205. {
  206. var stringToWrite = "";
  207. foreach (var entity in duplicatesFound){
  208. string line = string.Format(
  209. "{0}\t{1}\t{2}\t{3}\t{4}\r\n",
  210. entity.Id,
  211. articleNumber ?? "Saknas",
  212. entity.GetField(entityName + "Season").Data ?? "Saknas",
  213. entity.LastModified.Date.ToShortDateString(),
  214. differentingFields
  215. );
  216. stringToWrite = stringToWrite + line;
  217. }
  218. return stringToWrite;
  219. } catch (Exception e)
  220. {
  221. Log4.Error("Couldn't create string for " + articleNumber + " because " + e);
  222. return null;
  223. }
  224. }
  225.  
  226. internal static void WriteToFile(string message,string entity, int printChoice, string differentingFields) {
  227. var destination = string.Format("{0}\\{1}", Environment.CurrentDirectory, "files");
  228. var fileNameDiff = string.Format("{0}\\{1}-differentiating.txt", destination, entity);
  229. var fileNameNonDiff = string.Format("{0}\\{1}-nondifferentiating.txt", destination, entity);
  230. try {
  231. if (!Directory.Exists(destination)){
  232. Directory.CreateDirectory(destination);
  233. }
  234. if (printChoice == 1) {
  235. PrintToFile(fileNameDiff, message);
  236. } else if (printChoice == 2) {
  237. PrintToFile(fileNameNonDiff, message);
  238. } else {
  239. if (differentingFields == "") {
  240. PrintToFile(fileNameNonDiff, message);
  241. } else {
  242. PrintToFile(fileNameDiff, message);
  243. }
  244. }
  245. } catch(Exception e)
  246. {
  247. Log4.Error("Couldn't write to file because " + e);
  248. }
  249. }
  250.  
  251. internal static void PrintToFile(string fileName, string message) {
  252. if (!File.Exists(fileName)) {
  253. using (StreamWriter file = new StreamWriter(fileName)) {
  254. file.Write("SysID\tArtiklenummer\tSeason\tLastModified\tChangedField\r\n");
  255. file.Flush();
  256. file.Close();
  257. }
  258. }
  259. File.AppendAllText(fileName, message);
  260. }
  261.  
  262. internal static int DisplayFirstMenu()
  263. {
  264. Console.WriteLine("GetDuplicates");
  265. Console.WriteLine();
  266. Console.WriteLine("1. Create file with Product duplicates");
  267. Console.WriteLine("2. Create file with Item duplicates");
  268. Console.WriteLine();
  269. var result = Console.ReadLine();
  270. return Convert.ToInt32(result);
  271. }
  272.  
  273. internal static int DisplaySecondMenu() {
  274. Console.Clear();
  275. Console.WriteLine("1. Only print duplicates with fields differentiating");
  276. Console.WriteLine("2. Only print duplicates with fields NOT differentiating");
  277. Console.WriteLine("3. Both");
  278. Console.WriteLine();
  279. var result = Console.ReadLine();
  280. return Convert.ToInt32(result);
  281. }
  282.  
  283. internal static int? DisplayThirdMenu()
  284. {
  285. Console.Clear();
  286. Console.WriteLine("Do you want to start at a specific SystemId? If not, just press enter");
  287. Console.WriteLine();
  288. var result = Console.ReadLine();
  289. try
  290. {
  291. return Convert.ToInt32(result);
  292. } catch (FormatException e)
  293. {
  294. return null;
  295. }
  296.  
  297. }
  298.  
  299.  
  300. }
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement