private void UpdateRepoItemGuids(RepositoryItemBase item, List <GuidMapper> guidMappingList) { foreach (FieldInfo PI in item.GetType().GetFields()) { var token = PI.GetCustomAttribute(typeof(IsSerializedForLocalRepositoryAttribute)); if (token == null) { continue; } // we drill down to ObservableList if (typeof(IObservableList).IsAssignableFrom(PI.FieldType)) { IObservableList obj = (IObservableList)PI.GetValue(item); if (obj == null) { return; } List <object> items = ((IObservableList)obj).ListItems; if ((items != null) && (items.Count > 0) && (items[0].GetType().IsSubclassOf(typeof(RepositoryItemBase)))) { foreach (RepositoryItemBase ri in items.Cast <RepositoryItemBase>()) { GuidMapper mapping = new GuidMapper(); mapping.Original = ri.Guid; ri.Guid = Guid.NewGuid(); mapping.newGuid = ri.Guid; guidMappingList.Add(mapping); UpdateRepoItemGuids(ri, guidMappingList); } } } } }
private void HandleFileChange(FileSystemEventArgs e) { if (IsRepositoryFile(e.FullPath)) { RepositoryItemBase item = null; switch (e.ChangeType) { case WatcherChangeTypes.Changed: WaitforFileIsReadable(e.FullPath); // reLoad the object to mem updating fields item = GetItemFromCacheByFileName(e.FullPath); if (item != null) { NewRepositorySerializer.ReloadObjectFromFile(item); item.RefreshSourceControlStatus(); } break; case WatcherChangeTypes.Deleted: //remove from cache and list item = GetItemFromCacheByFileName(e.FullPath); RemoveItemFromLists(item); break; case WatcherChangeTypes.Created: WaitforFileIsReadable(e.FullPath); // add item to cache and list T newItem = LoadItemfromFile <T>(e.FullPath, Path.GetDirectoryName(e.FullPath)); AddItemtoCache(e.FullPath, newItem); mSolutionRepositoryItemInfo.AddItemToCache((T)(object)newItem); mFolderItemsList.Add(newItem); break; } } SolutionRepository.RefreshParentFoldersSoucerControlStatus(Path.GetDirectoryName(e.FullPath)); }
private void UpdateFolderItemsCacheFilePath() { ObservableList <T> cacheItems = new ObservableList <T>(); foreach (T item in mFolderItemsCache.Items <T>()) { cacheItems.Add(item); } foreach (T cacheitem in cacheItems) { RepositoryItemBase item = (RepositoryItemBase)(object)cacheitem; mFolderItemsCache.DeleteItem(item.FilePath); item.FilePath = Path.Combine(FolderFullPath, Path.GetFileName(PathHelper.GetLongPath(((RepositoryItemBase)item).FilePath))); item.FileName = Path.Combine(FolderFullPath, Path.GetFileName(PathHelper.GetLongPath((item).FilePath))); item.ContainingFolder = FolderRelativePath; item.ContainingFolderFullPath = FolderFullPath; mFolderItemsCache[item.FilePath] = item; item.OnPropertyChanged(nameof(RepositoryItemBase.FilePath)); item.OnPropertyChanged(nameof(RepositoryItemBase.ContainingFolder)); item.OnPropertyChanged(nameof(RepositoryItemBase.ContainingFolderFullPath)); item.OnPropertyChanged(nameof(RepositoryItemBase.RelativeFilePath)); item.OnPropertyChanged(nameof(RepositoryItemBase.FileName)); } }
// We have Repository item but we want to reload from disk // Can happen if we modified the file on the file system internal static void ReloadObjectFromFile(RepositoryItemBase repositoryItem) { string txt = File.ReadAllText(repositoryItem.FilePath); DeserializeFromText(txt, repositoryItem, filePath: repositoryItem.FilePath); }
//TODO: fix this method name or cretae or !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! public string CreateRepositoryItemFileName(RepositoryItemBase RI, string containingFolder = "") { var v = GetSolutionRepositoryItemInfo(RI.GetType()); string name = RI.ItemName; // (string)RI.GetType().GetProperty(v.PropertyForFileName).GetValue(RI); if (RI.FilePath != null && File.Exists(RI.FilePath) && string.IsNullOrEmpty(containingFolder)) { return(RI.FilePath); } else { //probably new item so create new path for it //FOLDER string fileFolderPath = string.Empty; if (string.IsNullOrEmpty(containingFolder)) { fileFolderPath = RI.ContainingFolder; } else { fileFolderPath = containingFolder; } if (!fileFolderPath.StartsWith(cSolutionRootFolderSign) || !fileFolderPath.StartsWith(mSolutionFolderPath)) { string A = mSolutionFolderPath; string B = fileFolderPath.Replace(cSolutionRootFolderSign, Path.DirectorySeparatorChar.ToString()).TrimStart(Path.DirectorySeparatorChar).TrimEnd(Path.DirectorySeparatorChar); if (!A.EndsWith(Path.DirectorySeparatorChar.ToString())) { A += Path.DirectorySeparatorChar; } fileFolderPath = Path.Combine(A, B); } string fileName = Amdocs.Ginger.Common.GeneralLib.General.RemoveInvalidFileNameChars(name); string fullName = v.Pattern.Replace("*", fileName); string filefullPath = Path.Combine(fileFolderPath, fullName); //TODO: remove max 255 once we switch all to work with .Net core 2.0 no limit //Validate Path length - MAX_PATH is 260 if (fileName.Length > 255) { //FIXME !!!!!!!!!!!!!! int gap = filefullPath.Length + 2 - 255; string NewName = fileName.Substring(0, fileName.Length - gap); //TODO: validate that works as expected using unit test !!!!!!!!!! file extension must remain or give err //tODO: throw exception if file name too short... NewName = NewName + "~1"; NewName = v.Pattern.Replace("*", NewName); filefullPath = Path.Combine(fileFolderPath, NewName); } //validate no other file with same name //find first file which doesn't exist, add ~counter until found int counter = 0; string Nameex = ""; string ext = v.Pattern.Replace("*", ""); while (File.Exists(filefullPath)) { if (Nameex != "") { filefullPath = filefullPath.Replace(Nameex, ""); } counter++; Nameex = "~" + counter; filefullPath = filefullPath.Replace(ext, Nameex + ext); if (counter > 100) { throw new Exception("cannot find unique file after 100 tries"); } } return(filefullPath); } }
/// <summary> /// Get the Repository Folder of the Repository Item /// </summary> /// <param name="repositoryItem"></param> public RepositoryFolderBase GetItemRepositoryFolder(RepositoryItemBase repositoryItem) { SolutionRepositoryItemInfoBase SRII = GetSolutionRepositoryItemInfo(repositoryItem.GetType()); return(SRII.GetItemRepositoryFolder(repositoryItem)); }
public void SaveToFile(RepositoryItemBase ri, string FileName) { string txt = SerializeToString(ri); File.WriteAllText(FileName, txt); }
public static object OnNewRepositorySerializerEvent(NewRepositorySerilizerEventArgs.eEventType EvType, string FilePath, string XML, RepositoryItemBase TargetObj) { NewRepositorySerializerEventHandler handler = NewRepositorySerializerEvent; if (handler != null) { return(handler(new NewRepositorySerilizerEventArgs(EvType, FilePath, XML, TargetObj))); } return(null); }
/// <summary> /// Get the parent folder of the Repository Item /// </summary> /// <param name="repositoryItem"></param> /// <returns></returns> public abstract RepositoryFolderBase GetItemRepositoryFolder(RepositoryItemBase repositoryItem);
public virtual void UpdateInstance(RepositoryItemBase instanceItem, string itemPartToUpdate, RepositoryItemBase hostItem = null) { throw new NotImplementedException("UpdateInstance() was not implemented for this Item type"); }
public virtual RepositoryItemBase GetUpdatedRepoItem(RepositoryItemBase selectedItem, RepositoryItemBase existingItem, string itemPartToUpdate) { throw new NotImplementedException("GetUpdatedRepoItem() was not implemented for this Item type"); }
public async Task SaveRepositoryItemAsync(RepositoryItemBase repositoryItem) { await Task.Run(() => SaveRepositoryItem(repositoryItem)); }
//TODO: fix this method name or cretae or !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! public string CreateRepositoryItemFileName(RepositoryItemBase RI, string containingFolder = "") { var v = GetSolutionRepositoryItemInfo(RI.GetType()); string name = RI.ItemName; // (string)RI.GetType().GetProperty(v.PropertyForFileName).GetValue(RI); if (RI.FilePath != null && File.Exists(RI.FilePath) && string.IsNullOrEmpty(containingFolder)) { return(RI.FilePath); } else { //probably new item so create new path for it //FOLDER string fileFolderPath = string.Empty; if (string.IsNullOrEmpty(containingFolder)) { fileFolderPath = RI.ContainingFolder; } else { fileFolderPath = containingFolder; } if (!fileFolderPath.StartsWith(cSolutionRootFolderSign) || !fileFolderPath.StartsWith(mSolutionFolderPath)) { // Fix me for Linux !!! string A = mSolutionFolderPath; //.TrimEnd(Path.DirectorySeparatorChar).TrimStart(Path.DirectorySeparatorChar); string B = fileFolderPath.Replace(cSolutionRootFolderSign, Path.DirectorySeparatorChar.ToString()).TrimStart(Path.DirectorySeparatorChar).TrimEnd(Path.DirectorySeparatorChar); A += Path.DirectorySeparatorChar; fileFolderPath = Path.Combine(A, B); Console.WriteLine("fileFolderPath3 =" + fileFolderPath); } //FILE string fileName = name; //Removing all possible invalid path chars and checking the file name length is legal (<= 255) foreach (char invalidChar in Path.GetInvalidFileNameChars()) { fileName = fileName.Replace(invalidChar.ToString(), ""); } fileName = fileName.Replace(@".", ""); string fileExtention = v.Pattern; //string.Format(".{0}.xml", RI.ObjFileExt); string fullName = v.Pattern.Replace("*", fileName); string filefullPath = Path.Combine(fileFolderPath, fullName); //TODO: remove max 255 once we swithc all to work with .Net core 2.0 no limit //Validate Path length - MAX_PATH is 260 //if (filefullPath.Length > 255) if (fileName.Length > 255) { //FIXME !!!!!!!!!!!!!! int gap = filefullPath.Length + 2 - 255; string NewName = fileName.Substring(0, fileName.Length - gap); //TODO: validate that works as expected using unit test !!!!!!!!!! file extension must remain or give err //tODO: throw exception if file name too short... NewName = NewName + "~1"; NewName = v.Pattern.Replace("*", NewName); filefullPath = Path.Combine(fileFolderPath, NewName); } //validate no other file with same name //find first file which doesn't exist, add ~counter until found int counter = 0; string Nameex = ""; string ext = v.Pattern.Replace("*", ""); while (File.Exists(filefullPath)) { if (Nameex != "") { filefullPath = filefullPath.Replace(Nameex, ""); } counter++; Nameex = "~" + counter; filefullPath = filefullPath.Replace(ext, Nameex + ext); if (counter > 100) { throw new Exception("cannot find uniqe file after 100 tries"); } } return(filefullPath); } }
public NewRepositorySerilizerEventArgs(eEventType EventType, string FilePath, string XML, RepositoryItemBase TargetObj) { this.EventType = EventType; this.FilePath = FilePath; this.XML = XML; this.TargetObj = TargetObj; }
/// <summary> /// Get the parent folder of the Repository Item /// </summary> /// <param name="repositoryItem"></param> /// <returns></returns> public override RepositoryFolderBase GetItemRepositoryFolder(RepositoryItemBase repositoryItem) { return(GetRepositoryFolder(repositoryItem, null)); }
private void WriteRepoItemFields(XmlTextWriter xml, RepositoryItemBase ri) { var Fields = ri.GetType().GetMembers().Where(x => x.MemberType == MemberTypes.Field).OrderBy(x => x.Name); foreach (MemberInfo fi in Fields) { object v = null; IsSerializedForLocalRepositoryAttribute token = Attribute.GetCustomAttribute(fi, typeof(IsSerializedForLocalRepositoryAttribute), false) as IsSerializedForLocalRepositoryAttribute; if (token == null) { continue; } if (LazyLoadAttr.Contains(fi.Name)) { bool b = ((IObservableList)(ri.GetType().GetField(fi.Name).GetValue(ri))).LazyLoad; if (b) { // Hurray! string s = ((IObservableList)(ri.GetType().GetField(fi.Name).GetValue(ri))).StringData; xml.WriteStartElement("Activities"); //!!! xml.WriteString(s); xml.WriteEndElement(); } } v = ri.GetType().GetField(fi.Name).GetValue(ri); // We write the property value only if it is not null and different than default when serialized if (v == null) { continue; } if (IsValueDefault(v, token)) { continue; } if (v != null) { if (v is IObservableList) { IObservableList vv = (IObservableList)v; if (vv.Count != 0) // Write only if we have items - save xml space { xmlwriteObservableList(xml, fi.Name, (IObservableList)v); } } else { if (v is List <string> ) { xmlwriteStringList(xml, fi.Name, (List <string>)v); } else if (v is RepositoryItemBase) { xmlwriteSingleObjectField(xml, fi.Name, v); } else { //xml.WriteComment(">>>>>>>>>>>>>>>>> Unknown Field type to serialize - " + fi.Name + " - " + v.ToString()); throw new Exception("Unknown Field type to serialize - " + fi.Name + " - " + v.ToString()); } } } } }
public bool ContainsRepositoryItems; // { get { return true; } } /// <summary> /// Save the Repository Item to folder and add it to cache /// </summary> /// <param name="repositoryItem"></param> public abstract void AddRepositoryItem(RepositoryItemBase repositoryItem);
private static object xmlReadObject(Object Parent, XmlReader xdr, RepositoryItemBase targetObj = null) { string className = xdr.Name; //bool conversion = false; // if (className == "GingerCore.Platforms.ApplicationPlatform") className = "GingerCoreNET.SolutionRepositoryLib.RepositoryObjectsLib.PlatformsLib"; try { int level = xdr.Depth; object obj; if (targetObj == null) { obj = CreateObject(className); } else { obj = targetObj; } SetObjectSerialziedAttrDefaultValue(obj); SetObjectAttributes(xdr, obj); xdr.Read(); // Set lists attrs // read all object sub elements like lists - obj members while (xdr.Depth == level + 1) { // Check if it one obj attr or list string attrName = xdr.Name; FieldInfo FI = obj.GetType().GetField(attrName); // PropertyInfo FI = obj.GetType().GetProperty(attrName); // string bt = FI.FieldType.Name; // We check if it is list by arg count - List<string> will have string etc... // another option is check the nake to start with List, Observ... //or find a better way // meanwhile it is working if (FI.FieldType.GenericTypeArguments.Count() > 0) { SetObjectListAttrs(xdr, obj); } else { // Read the attr name/move next xdr.ReadStartElement(); // read the actual object we need to put on the attr object item = xmlReadObject(obj, xdr); // Set the attr val with the object FI.SetValue(obj, item); // Create UT for below and then remove the next if xdr.ReadEndElement(); } //Keep it here if (xdr.NodeType == XmlNodeType.EndElement) { xdr.ReadEndElement(); } } //if (conversion) //{ // //for converting old actions keep the ID // if (obj is DriverAction) // { // DriverAction DA = ((DriverAction)obj); // DA.OldClassName = OldClassName; // //temp moved from here to conversion class or function // if (DA.OldClassName == "GingerCore.Actions.ActGotoURL") // { // DA.ID = "GotoURL"; // DA.InputValues[0].Param = "URL"; //convert param name 'Value' to 'URL' // } // if (DA.OldClassName == "GingerCore.Actions.ActTextBox") // { // DA.ID = "UIElementAction"; // string LocateBy = "ByID"; // string LocateValue = "UserName"; // temp !!!!!!!!!!!!!!!!!!!!!!!!!!!!! // string Action = "SetValue"; // string Value = (from x in DA.InputValues where x.Param == "Value" select x.Value).FirstOrDefault(); // DA.InputValues.Clear(); // DA.InputValues.Add(new ActInputValue() { Param = "LocateBy", Value = LocateBy }); // DA.InputValues.Add(new ActInputValue() { Param = "LocateValue", Value = LocateValue }); // DA.InputValues.Add(new ActInputValue() { Param = "Action", Value = Action }); // DA.InputValues.Add(new ActInputValue() { Param = "Value", Value = Value }); // } // } //} return(obj); } catch (Exception ex) { NewReporter.ToConsole("Error:Cannot create instance of: " + className + ", for attribute: " + xdr.Name + " - " + ex.Message); throw new Exception("Error:Cannot create instance of: " + className + ", for attribute: " + xdr.Name + " - " + ex.Message); } }
/// <summary> /// Delete the Repository Item from folder and cache /// </summary> /// <param name="repositoryItem"></param> public abstract void DeleteRepositoryItem(RepositoryItemBase repositoryItem);
/// <summary> /// Save the Repository Item to the Root folder and add it to cache /// </summary> /// <param name="repositoryItem"></param> public void AddRepositoryItem(RepositoryItemBase repositoryItem) { SolutionRepositoryItemInfoBase SRII = GetSolutionRepositoryItemInfo(repositoryItem.GetType()); SRII.ItemRootRepositoryFolder.AddRepositoryItem(repositoryItem); }
/// <summary> /// Move Repository item from current folder to another folder /// </summary> /// <param name="repositoryFolder"></param> public abstract void MoveItem(RepositoryItemBase repositoryItem, RepositoryFolderBase repositoryFolder);
//TODO: fix this method name or cretae or !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! public string CreateRepositoryItemFileName(RepositoryItemBase repositoryItemBase, string containingFolder = "") { var repositoryItemInfoBaseType = GetSolutionRepositoryItemInfo(repositoryItemBase.GetType()); string name = repositoryItemBase.ItemName; // (string)RI.GetType().GetProperty(v.PropertyForFileName).GetValue(RI); if (repositoryItemBase.FilePath != null && File.Exists(repositoryItemBase.FilePath) && string.IsNullOrEmpty(containingFolder)) { return(repositoryItemBase.FilePath); } else { //probably new item so create new path for it //FOLDER string fileFolderPath = string.Empty; if (string.IsNullOrEmpty(containingFolder)) { fileFolderPath = repositoryItemBase.ContainingFolder; } else { fileFolderPath = containingFolder; } if (!fileFolderPath.StartsWith(cSolutionRootFolderSign) || !fileFolderPath.StartsWith(mSolutionFolderPath)) { string solutionPath = mSolutionFolderPath; string filefolder = fileFolderPath.Replace(cSolutionRootFolderSign, Path.DirectorySeparatorChar.ToString()).TrimStart(Path.DirectorySeparatorChar).TrimEnd(Path.DirectorySeparatorChar); if (!solutionPath.EndsWith(Path.DirectorySeparatorChar.ToString())) { solutionPath += Path.DirectorySeparatorChar; } fileFolderPath = Path.Combine(solutionPath, filefolder); if (fileFolderPath.Length > 260) { ThrowErrorIfPathLimitExeceeded(); } } string fileName = Amdocs.Ginger.Common.GeneralLib.General.RemoveInvalidFileNameChars(name); string fullName = repositoryItemInfoBaseType.Pattern.Replace("*", fileName); string filefullPath = Path.Combine(fileFolderPath, fullName); //TODO: remove max 255 once we switch all to work with .Net core 2.0 no limit //Validate Path length - MAX_PATH is 260 if (filefullPath.Length > 260) { var newFileName = string.Empty; var noOfCharToEscape = 0; if (fileName.Length > 255) { noOfCharToEscape = filefullPath.Length + 2 - 255; newFileName = fileName.Substring(0, fileName.Length - noOfCharToEscape); //TODO: validate that works as expected using unit test !!!!!!!!!! file extension must remain or give err newFileName = newFileName + "~1"; newFileName = repositoryItemInfoBaseType.Pattern.Replace("*", newFileName); filefullPath = Path.Combine(fileFolderPath, newFileName); } if (filefullPath.Length > 260 && fileName.Length > 3) { noOfCharToEscape = filefullPath.Length - 257; newFileName = fileName.Substring(0, fileName.Length - noOfCharToEscape); if (newFileName.Length < 3) { ThrowErrorIfPathLimitExeceeded(); } newFileName = newFileName + "~1"; newFileName = repositoryItemInfoBaseType.Pattern.Replace("*", newFileName); filefullPath = Path.Combine(fileFolderPath, newFileName); } else if (filefullPath.Length > 260 && fileName.Length < 3) { ThrowErrorIfPathLimitExeceeded(); } } //validate no other file with same name //find first file which doesn't exist, add ~counter until found int counter = 0; string Nameex = ""; string ext = repositoryItemInfoBaseType.Pattern.Replace("*", ""); while (File.Exists(filefullPath)) { if (Nameex != "") { filefullPath = filefullPath.Replace(Nameex, ""); } counter++; Nameex = "~" + counter; filefullPath = filefullPath.Replace(ext, Nameex + ext); if (counter > 100) { throw new Exception("cannot find unique file after 100 tries"); } } return(filefullPath); } }
private RepositoryItemBase CopyRIObject(RepositoryItemBase repoItemToCopy) { Type objType = repoItemToCopy.GetType(); var targetObj = Activator.CreateInstance(objType) as RepositoryItemBase; var objMembers = repoItemToCopy.GetType().GetMembers().Where(x => (x.MemberType == MemberTypes.Property || x.MemberType == MemberTypes.Field)); repoItemToCopy.PrepareItemToBeCopied(); targetObj.PreDeserialization(); Parallel.ForEach(objMembers, mi => { try { if (IsDoNotBackupAttr(mi)) { return; } object memberValue = null; if (mi.MemberType == MemberTypes.Property) { var propInfo = repoItemToCopy.GetType().GetProperty(mi.Name); if (propInfo.CanWrite) { memberValue = propInfo.GetValue(repoItemToCopy); if (memberValue is IObservableList && typeof(IObservableList).IsAssignableFrom(propInfo.PropertyType)) { var copiedList = (IObservableList)propInfo.GetValue(targetObj); if (copiedList == null) { Type listItemType = memberValue.GetType().GetGenericArguments().SingleOrDefault(); var listOfType = typeof(ObservableList <>).MakeGenericType(listItemType); copiedList = (IObservableList)Activator.CreateInstance(listOfType); } CopyRIList((IObservableList)memberValue, copiedList); propInfo.SetValue(targetObj, copiedList); } else { propInfo.SetValue(targetObj, memberValue); } } } else { FieldInfo fieldInfo = repoItemToCopy.GetType().GetField(mi.Name); memberValue = fieldInfo.GetValue(repoItemToCopy); fieldInfo.SetValue(targetObj, memberValue); } } catch (Exception ex) { Reporter.ToLog(eLogLevel.ERROR, string.Format("Error occured during object copy of the item: '{0}', type: '{1}', property/field: '{2}'", this.ItemName, this.GetType(), mi.Name), ex); } }); targetObj.PostDeserialization(); targetObj.UpdateCopiedItem(); return(targetObj); }