public virtual void InitializeItemsCache(Item parent, string fieldNameForKeys, ref LevelLogger logger) { if (parent == null) { logger.AddError("Parent was null in InitializeItemsCache", "The parent was null."); return; } if (String.IsNullOrEmpty(fieldNameForKeys)) { logger.AddError("The fieldNameForKeys was null or empty.", String.Format("The fieldNameForKeys was null or empty. fieldNameForKeys: {0}", fieldNameForKeys)); return; } var startKey = GetBaseCacheKey(parent, ref logger); var fieldNameKeys = fieldNameForKeys.Split('|'); using (new LanguageSwitcher(BaseDataMap.ImportToLanguageVersion)) { var getItemsByKeyLogger = logger.CreateLevelLogger(); string pattern = "{0}//*"; var query = String.Format(pattern, parent.Paths.FullPath); try { var items = BaseDataMap.SitecoreDB.SelectItems(query); if (items != null) { if (items.Any()) { foreach (var item in items) { if (item != null) { foreach (var fieldNameKey in fieldNameKeys) { if (!String.IsNullOrEmpty(fieldNameKey)) { var keyValue = item[fieldNameKey]; if (!String.IsNullOrEmpty(keyValue)) { AddToCache(startKey, item, fieldNameKey, keyValue, ref getItemsByKeyLogger); } } } } } } } } catch (Exception ex) { getItemsByKeyLogger.AddError("Exception in InitializeItemsCache", ex.Message); } } }
private void AddToCache(string startKey, Item item, string fieldName, string key, ref LevelLogger logger) { if (Caching == null) { logger.AddError("The Caching object was null in AddToCache.", String.Format("StartKey: {0}. FieldName: {1}. Key: {2}.", startKey, fieldName, key)); return; } if (item == null) { logger.AddError("The item was null in AddToCache.", String.Format("StartKey: {0}. FieldName: {1}. Key: {2}.", startKey, fieldName, key)); return; } if (String.IsNullOrEmpty(fieldName)) { logger.AddError("The fieldName was null or empty in AddToCache.", String.Format("StartKey: {0}. Item: {1}. FieldName: {2}. Key: {3}.", startKey, BaseDataMap.GetItemDebugInfo(item), fieldName, key)); return; } if (String.IsNullOrEmpty(key)) { logger.AddError("The key was null or empty in AddToCache.", String.Format("StartKey: {0}. Item: {1}. FieldName: {2}. Key: {3}.", startKey, BaseDataMap.GetItemDebugInfo(item), fieldName, key)); return; } var completeKey = startKey + "|" + fieldName + "|" + key; completeKey = completeKey.ToLower(); var keyItems = Caching.Get(completeKey) as List <Item>; if (keyItems == null) { keyItems = new List <Item> { item }; } else { bool sameIdAlreadyAdded = false; foreach (var foundItem in keyItems) { if (foundItem == item) { sameIdAlreadyAdded = true; } } if (!sameIdAlreadyAdded) { keyItems.Add(item); } } Caching.Set(completeKey, keyItems); }
public static MediaItem ImageHelper(string filename, string originalFileNameWithExtension, string imageAltText, ID mediaItemParentId, bool isToReplaceExistingImage, MemoryStream memoryStream, ref LevelLogger logger) { var filepath = Helper.GetSitecoreMediaItemPath(mediaItemParentId); if (string.IsNullOrEmpty(filepath)) { logger.AddError(CategoryConstants.TheImageMediaLibraryPathIsNotSet, String.Format("The image media library path is not set. mediaItemParentId: {0}.", mediaItemParentId)); return(null); } var mediaItem = Helper.IsImageExisting(mediaItemParentId, filename); if (mediaItem != null) { if (isToReplaceExistingImage) { //AttachNewImageToMediaItem(mediaItem, filename, imageBytes, imageAltText, filepath, ref errorMessage); } else { return(mediaItem); } } var imageItem = CreateMediaItem(filename, originalFileNameWithExtension, imageAltText, filepath, memoryStream, ref logger); return(imageItem); }
/// <summary> /// Attach new image to media item /// </summary> /// <param name="imageItem"></param> /// <param name="filename"></param> /// <param name="imageBytes"></param> /// <param name="altText"></param> /// <param name="destPath"></param> /// <param name="errorMessage"></param> private static void AttachNewImageToMediaItem(MediaItem imageItem, string filename, byte[] imageBytes, string altText, string destPath, ref LevelLogger logger) { try { destPath = destPath + "/" + filename; if (imageBytes != null) { using (var ms = new MemoryStream(imageBytes)) { var options = new MediaCreatorOptions { FileBased = false, IncludeExtensionInItemName = false, KeepExisting = false, Versioned = false, AlternateText = altText, Destination = destPath, Database = Database }; MediaManager.Creator.AttachStreamToMediaItem(ms, destPath, filename, options); } } } catch (Exception ex) { logger.AddError(ExceptionTryingToAttachNewImageToExistingMediaitem, String.Format("Error trying to attach new image to existing mediaitem: InnerException {0}. Message {1}", ex.InnerException, ex.Message)); } }
protected virtual string GetBaseCacheKey(Item parent, ref LevelLogger logger) { if (parent == null) { logger.AddError("The parent item was null in the GetBaseCacheKey method.", "The parent item was null in the GetBaseCacheKey method."); return(null); } return(parent.ID + ""); }
public static void SendLogReport(ref LevelLogger logger, OutputHandlerBase exporter) { var dataSyncItem = logger.GetData(Utility.Constants.DataSyncItem) as Item; if (dataSyncItem == null) { logger.AddError("Error", "The DataSyncItem was null. Therefor we couldn't retrieve any mail recipients."); return; } if (exporter == null) { logger.AddError("Error", "The ExporterBase was null. This class is used to output the logger."); return; } var recipient = dataSyncItem[FieldNameMailRecipients]; if (String.IsNullOrEmpty(recipient)) { logger.AddInfo("Add Mail Recipients to receive email reports", "If you want to receive email, then fill out the field 'Mail Recipients'."); } else { var replyTo = dataSyncItem[FieldNameMailReplyTo]; if (String.IsNullOrEmpty(replyTo)) { replyTo = DefaultReplyTo; logger.AddError("Error", "The 'Mail Reply To' field must be defined. Please provide a replyTo address for the mail."); } var subject = dataSyncItem[FieldNameMailSubject]; if (String.IsNullOrEmpty(subject)) { subject = DefaultSubject; } var result = !logger.HasFatalsOrErrors() ? Success : Failure; try { subject = String.Format(subject, exporter.GetIdentifier(), result); } catch (Exception exception) { logger.AddError("Error", "SendLogReport had an exception trying to format the subject of the mail." + exception.Message); } var doNotSendMailOnSuccess = dataSyncItem[FieldNameDoNotSendMailOnSuccess] == SitecoreBooleanTrue; if ((doNotSendMailOnSuccess && result == Failure) || !doNotSendMailOnSuccess) { try{ if (SendMail.SendMailWithoutAttachment(recipient, replyTo, subject, exporter.Export()) == Failure) { logger.AddError("Error", "The SendMailWithoutAttachment failed."); } } catch (Exception ex) { logger.AddError("Error", "The SendMailWithoutAttachment failed with an exception: " + ex.Message); } } } }
public BaseDataMap InstantiateDataMap(Item dataSyncItem, ref LevelLogger logger) { var currentDB = Configuration.Factory.GetDatabase("master"); string handlerAssembly = dataSyncItem[FieldNameHandlerAssembly]; string handlerClass = dataSyncItem[FieldNameHandlerClass]; if (!String.IsNullOrEmpty(handlerAssembly)) { if (!String.IsNullOrEmpty(handlerClass)) { BaseDataMap map = null; try { map = (BaseDataMap)Reflection.ReflectionUtil.CreateObject(handlerAssembly, handlerClass, new object[] { currentDB, dataSyncItem, logger }); } catch (FileNotFoundException fnfe) { logger.AddError("Error", "The binary specified could not be found" + fnfe.Message); } if (map != null) { return(map); } logger.AddError("Error", String.Format("The data map provided could not be instantiated. Assembly:'{0}' Class:'{1}'", handlerAssembly, handlerClass)); } else { logger.AddError("Error", "Import handler class is not defined"); } } else { logger.AddError("Error", "import handler assembly is not defined"); } return(null); }
public LevelLogger RunDataSyncJob(Item dataSyncItem, ref LevelLogger logger) { string errorMessage = String.Empty; var map = InstantiateDataMap(dataSyncItem, ref logger); if (!String.IsNullOrEmpty(errorMessage)) { logger.AddError("Error", errorMessage); return(logger); } if (map != null) { logger = map.Process(); } return(logger); }