protected virtual void DeleteItemRecursive(ISerializedReference reference) { foreach (var child in reference.GetChildReferences(false)) { DeleteItemRecursive(child); } // kill the serialized file var fileItem = reference.GetItem(); if (fileItem != null && File.Exists(fileItem.ProviderId)) { File.Delete(fileItem.ProviderId); } // remove any serialized children var directory = SerializationPathUtility.GetReferenceDirectoryPath(reference); if (Directory.Exists(directory)) { Directory.Delete(directory, true); } // clean up any hashpaths for this item var shortDirectory = SerializationPathUtility.GetShortSerializedReferencePath(_rootPath, reference); if (Directory.Exists(shortDirectory)) { Directory.Delete(shortDirectory, true); } // clean up empty parent folder(s) var parentDirectory = Directory.GetParent(directory); if (!parentDirectory.Exists) { return; } do { if (parentDirectory.GetFileSystemInfos().Length > 0) { break; } parentDirectory.Delete(true); parentDirectory = parentDirectory.Parent; } while (parentDirectory != null && parentDirectory.Exists); }
/// <summary> /// Loads a set of children from a serialized path /// </summary> protected virtual void LoadOneLevel(ISerializedReference root, IDeserializeFailureRetryer retryer, IConsistencyChecker consistencyChecker) { Assert.ArgumentNotNull(root, "root"); Assert.ArgumentNotNull(retryer, "retryer"); var orphanCandidates = new Dictionary<ID, ISourceItem>(); // grab the root item's full metadata var rootSerializedItem = root.GetItem(); if (rootSerializedItem == null) { Logger.SkippedItemMissingInSerializationProvider(root, SerializationProvider.GetType().Name); return; } // get the corresponding item from Sitecore ISourceItem rootItem = SourceDataProvider.GetItemById(rootSerializedItem.DatabaseName, rootSerializedItem.Id); // we add all of the root item's direct children to the "maybe orphan" list (we'll remove them as we find matching serialized children) if (rootItem != null) { var rootChildren = rootItem.Children; foreach (ISourceItem child in rootChildren) { // if the preset includes the child add it to the orphan-candidate list (if we don't deserialize it below, it will be marked orphan) var included = Predicate.Includes(child); if (included.IsIncluded) orphanCandidates[child.Id] = child; else { Logger.SkippedItem(child, Predicate.GetType().Name, included.Justification ?? string.Empty); } } } // check for direct children of the target path var children = rootSerializedItem.GetChildItems(); foreach (var child in children) { try { if (child.IsStandardValuesItem) { orphanCandidates.Remove(child.Id); // avoid marking standard values items orphans retryer.AddItemRetry(child, new StandardValuesException(child.ItemPath)); } else { // load a child item var loadedItem = DoLoadItem(child, consistencyChecker); if (loadedItem.Item != null) { orphanCandidates.Remove(loadedItem.Item.Id); // check if we have any child serialized items under this loaded child item (existing children) - // if we do not, we can orphan any children of the loaded item as well var loadedItemsChildren = child.GetChildReferences(false); if (loadedItemsChildren.Length == 0) // no children were serialized on disk { var loadedChildren = loadedItem.Item.Children; foreach (ISourceItem loadedChild in loadedChildren) { orphanCandidates.Add(loadedChild.Id, loadedChild); } } } else if (loadedItem.Status == ItemLoadStatus.Skipped) // if the item got skipped we'll prevent it from being deleted orphanCandidates.Remove(child.Id); } } catch (ConsistencyException) { throw; } catch (Exception ex) { // if a problem occurs we attempt to retry later retryer.AddItemRetry(child, ex); // don't treat errors as cause to delete an item orphanCandidates.Remove(child.Id); } } // if we're forcing an update (ie deleting stuff not on disk) we send the items that we found that weren't on disk off to get evaluated as orphans if (orphanCandidates.Count > 0) { bool disableNewSerialization = UnicornDataProvider.DisableSerialization; try { UnicornDataProvider.DisableSerialization = true; Evaluator.EvaluateOrphans(orphanCandidates.Values.ToArray()); } finally { UnicornDataProvider.DisableSerialization = disableNewSerialization; } } }
/// <summary> /// Loads a set of children from a serialized path /// </summary> protected virtual void LoadOneLevel(ISerializedReference root, IDeserializeFailureRetryer retryer, IConsistencyChecker consistencyChecker) { Assert.ArgumentNotNull(root, "root"); Assert.ArgumentNotNull(retryer, "retryer"); var orphanCandidates = new Dictionary <ID, ISourceItem>(); // grab the root item's full metadata var rootSerializedItem = root.GetItem(); if (rootSerializedItem == null) { Logger.SkippedItemMissingInSerializationProvider(root, SerializationProvider.GetType().Name); return; } // get the corresponding item from Sitecore ISourceItem rootItem = SourceDataProvider.GetItemById(rootSerializedItem.DatabaseName, rootSerializedItem.Id); // we add all of the root item's direct children to the "maybe orphan" list (we'll remove them as we find matching serialized children) if (rootItem != null) { var rootChildren = rootItem.Children; foreach (ISourceItem child in rootChildren) { // if the preset includes the child add it to the orphan-candidate list (if we don't deserialize it below, it will be marked orphan) var included = Predicate.Includes(child); if (included.IsIncluded) { orphanCandidates[child.Id] = child; } else { Logger.SkippedItem(child, Predicate.GetType().Name, included.Justification ?? string.Empty); } } } // check for direct children of the target path var children = rootSerializedItem.GetChildItems(); foreach (var child in children) { try { if (child.IsStandardValuesItem) { orphanCandidates.Remove(child.Id); // avoid marking standard values items orphans retryer.AddItemRetry(child, new StandardValuesException(child.ItemPath)); } else { // load a child item var loadedItem = DoLoadItem(child, consistencyChecker); if (loadedItem.Item != null) { orphanCandidates.Remove(loadedItem.Item.Id); // check if we have any child serialized items under this loaded child item (existing children) - // if we do not, we can orphan any included children of the loaded item as well var loadedItemsChildren = child.GetChildReferences(false); if (loadedItemsChildren.Length == 0) // no children were serialized on disk { var loadedChildren = loadedItem.Item.Children; foreach (ISourceItem loadedChild in loadedChildren) { if (Predicate.Includes(loadedChild).IsIncluded) { orphanCandidates.Add(loadedChild.Id, loadedChild); } } } } else if (loadedItem.Status == ItemLoadStatus.Skipped) // if the item got skipped we'll prevent it from being deleted { orphanCandidates.Remove(child.Id); } } } catch (ConsistencyException) { throw; } catch (Exception ex) { // if a problem occurs we attempt to retry later retryer.AddItemRetry(child, ex); // don't treat errors as cause to delete an item orphanCandidates.Remove(child.Id); } } // if we're forcing an update (ie deleting stuff not on disk) we send the items that we found that weren't on disk off to get evaluated as orphans if (orphanCandidates.Count > 0) { bool disableNewSerialization = UnicornDataProvider.DisableSerialization; try { UnicornDataProvider.DisableSerialization = true; Evaluator.EvaluateOrphans(orphanCandidates.Values.ToArray()); } finally { UnicornDataProvider.DisableSerialization = disableNewSerialization; } } }
private string GetErrorValue(SaveArgs args) { var results = new Dictionary <Item, IList <FieldDesynchronization> >(); try { foreach (var item in args.Items) { Item existingItem = Client.ContentDatabase.GetItem(item.ID, item.Language, item.Version); Assert.IsNotNull(existingItem, "Existing item {0} did not exist! This should never occur.", item.ID); var existingSitecoreItem = new SitecoreSourceItem(existingItem); foreach (var configuration in _configurations) { // ignore conflicts on items that Unicorn is not managing if (!configuration.Resolve <IPredicate>().Includes(existingSitecoreItem).IsIncluded) { continue; } ISerializedReference serializedReference = configuration.Resolve <ISerializationProvider>().GetReference(existingSitecoreItem); if (serializedReference == null) { continue; } // not having an existing serialized version means no possibility of conflict here ISerializedItem serializedItem = serializedReference.GetItem(); if (serializedItem == null) { continue; } var fieldPredicate = configuration.Resolve <IFieldPredicate>(); var fieldIssues = GetFieldSyncStatus(existingSitecoreItem, serializedItem, fieldPredicate); if (fieldIssues.Count == 0) { continue; } results.Add(existingItem, fieldIssues); } } // no problems if (results.Count == 0) { return(null); } var sb = new StringBuilder(); sb.Append("CRITICAL MESSAGE FROM UNICORN:\n"); sb.Append("You need to run a Unicorn sync. The following fields did not match the serialized version:\n"); foreach (var item in results) { if (results.Count > 1) { sb.AppendFormat("\n{0}: {1}", item.Key.DisplayName, string.Join(", ", item.Value.Select(x => x.FieldName))); } else { sb.AppendFormat("\n{0}", string.Join(", ", item.Value.Select(x => x.FieldName))); } } sb.Append("\n\nDo you want to overwrite anyway?\nTHIS MAY CAUSE LOST WORK."); return(sb.ToString()); } catch (Exception ex) { Log.Error("Exception occurred while performing serialization conflict check!", ex, this); return("Exception occurred: " + ex.Message); // this will cause a few retries } }
protected virtual void DeleteItemRecursive(ISerializedReference reference) { foreach (var child in reference.GetChildReferences(false)) { DeleteItemRecursive(child); } // kill the serialized file var fileItem = reference.GetItem(); if (fileItem != null && File.Exists(fileItem.ProviderId)) File.Delete(fileItem.ProviderId); // remove any serialized children var directory = SerializationPathUtility.GetReferenceDirectoryPath(reference); if (Directory.Exists(directory)) Directory.Delete(directory, true); // clean up any hashpaths for this item var shortDirectory = SerializationPathUtility.GetShortSerializedReferencePath(_rootPath, reference); if (Directory.Exists(shortDirectory)) Directory.Delete(shortDirectory, true); // clean up empty parent folder(s) var parentDirectory = Directory.GetParent(directory); if (!parentDirectory.Exists) return; do { if (parentDirectory.GetFileSystemInfos().Length > 0) break; parentDirectory.Delete(true); parentDirectory = parentDirectory.Parent; } while (parentDirectory != null && parentDirectory.Exists); }