//========================================================================================= Helper methods private static void AddContentListField(ContentRepository.Content content) { if (content == null) { return; } var contentList = ContentList.GetContentListByParentWalk(content.ContentHandler); if (contentList == null) { return; } //build longtext field for custom status messages var fs = new LongTextFieldSetting { ShortName = "LongText", Name = "#" + ContentNamingHelper.GetNameFromDisplayName(content.Name, content.DisplayName) + "_status", DisplayName = content.DisplayName + " status", Icon = content.Icon }; contentList.AddOrUpdateField(fs); }
// ======================================================================== Virtual methods protected virtual Content GetContent(Content parent, string fileName, string contentTypeName, bool overwrite) { var contentname = ContentNamingHelper.GetNameFromDisplayName(fileName); var path = RepositoryPath.Combine(Content.Path, contentname); Content content = null; if (overwrite) { // check if content exists content = Content.Load(path); if (content != null) { SetPreviewGenerationPriority(content); return(content); } } // create new content content = Content.CreateNew(contentTypeName, parent.ContentHandler, contentname); content.ContentHandler.AllowIncrementalNaming = true; SetPreviewGenerationPriority(content); return(content); }
/// <summary> /// Stores the given tag into the content repository as Tag node type. /// </summary> /// <param name="tag">Tag to store.</param> /// <param name="path">The folder path in repository, where you want to save the stored Tag node.</param> public static void AddToRepository(string tag, string path) { tag = tag.ToLower(); var parentNode = Node.LoadNode(path); var subFolderName = ContentNamingHelper.GetNameFromDisplayName("", tag); subFolderName = subFolderName.Replace('.', '_').Replace('"', '_').Replace('\'', '_'); subFolderName = subFolderName.Length > 1 ? subFolderName.Substring(0, 2) : subFolderName; var fullPath = RepositoryPath.Combine(path, subFolderName); if (!Node.Exists(fullPath)) { var cnt = ContentRepository.Content.CreateNew("Folder", parentNode, subFolderName); cnt.Save(); parentNode = cnt.ContentHandler; } else { parentNode = Node.LoadNode(fullPath); } var tagfileName = ContentNamingHelper.GetNameFromDisplayName("", tag); tagfileName = tagfileName.Replace('.', '_').Replace('"', '_').Replace('\'', '_'); var newTag = ContentRepository.Content.CreateNew("Tag", parentNode, UrlNameValidator.ValidUrlName(tagfileName)); newTag["DisplayName"] = tag.ToLower(); newTag["TrashDisabled"] = true; newTag["CreationDate"] = DateTime.Now; newTag.Save(); }
protected override void Execute(NativeActivityContext context) { var displayName = ContentDisplayName.Get(context); var originalName = ContentOriginalName.Get(context); var newName = ContentNamingHelper.GetNameFromDisplayName(originalName, displayName); Result.Set(context, newName); }
public void ContentNaming_FromDisplayName() { Assert.AreEqual("arvizturotukorfurogep", ContentNamingHelper.GetNameFromDisplayName("árvíztűrőtükörfúrógép")); Assert.AreEqual("ARVIZTUROTUKORFUROGEP", ContentNamingHelper.GetNameFromDisplayName("ÁRVÍZTŰRŐTÜKÖRFÚRÓGÉP")); Assert.AreEqual("ArvizturoTukorfurogep", ContentNamingHelper.GetNameFromDisplayName("ÁrvíztűrőTükörfúrógép")); Assert.AreEqual("a-()b-c", ContentNamingHelper.GetNameFromDisplayName("!@#$%a^&*()b{}|:<>?c/.,")); Assert.AreEqual("arvizturotukorfurogep.txt", ContentNamingHelper.GetNameFromDisplayName("árvíztűrőtükörfúrógép.txt")); Assert.AreEqual("arvizturotukorfurogep.doc.txt", ContentNamingHelper.GetNameFromDisplayName("árvíztűrőtükörfúrógép.txt", "árvíztűrőtükörfúrógép.doc")); Assert.AreEqual("arvizturotukorfurogep.doc.txt", ContentNamingHelper.GetNameFromDisplayName(".txt", "árvíztűrőtükörfúrógép.doc")); }
public void ContentNaming_FromDisplayName() { Assert.AreEqual("árvíztűrőtükörfúrógép", ContentNamingHelper.GetNameFromDisplayName("árvíztűrőtükörfúrógép")); Assert.AreEqual("ÁRVÍZTŰRŐTÜKÖRFÚRÓGÉP", ContentNamingHelper.GetNameFromDisplayName("ÁRVÍZTŰRŐTÜKÖRFÚRÓGÉP")); Assert.AreEqual("ÁrvíztűrőTükörfúrógép", ContentNamingHelper.GetNameFromDisplayName("ÁrvíztűrőTükörfúrógép")); Assert.AreEqual("!_2a_5f_7e_40_23_24_25a_5e_26()b_7b_7d_2b_22_27_7c_3a_3c_3e_3fc_2f._2c", ContentNamingHelper.GetNameFromDisplayName("!*_~@#$%a^&()b{}+\"'|:<>?c/.,")); Assert.AreEqual("árvíztűrőtükörfúrógép.txt", ContentNamingHelper.GetNameFromDisplayName("árvíztűrőtükörfúrógép.txt")); Assert.AreEqual("árvíztűrőtükörfúrógép.doc.txt", ContentNamingHelper.GetNameFromDisplayName("árvíztűrőtükörfúrógép.txt", "árvíztűrőtükörfúrógép.doc")); Assert.AreEqual("árvíztűrőtükörfúrógép.doc.txt", ContentNamingHelper.GetNameFromDisplayName(".txt", "árvíztűrőtükörfúrógép.doc")); }
private List <SyncResultObject> GetFoldersAndSync(Content cntToSync, string filePath) { var result = new List <SyncResultObject>(); // update files result.AddRange(GetFilesAndSync(cntToSync, filePath)); // now the folders DirectoryInfo[] folderInfos = null; try { DirectoryInfo dirInfo = new DirectoryInfo(filePath); folderInfos = dirInfo.GetDirectories(); } catch { // no problem, there are no folder's here. so what? } // nothing use this //IEnumerable<Content> children = cntToSync.Children.Where(c => c.TypeIs("Folder")); if (folderInfos != null && folderInfos.Length > 0) { foreach (var folder in folderInfos) { string folderName = string.Empty; try { folderName = ContentNamingHelper.GetNameFromDisplayName(folder.Name); Content folderContent = cntToSync.Children.Where(c => c.Name == folderName).FirstOrDefault(); if (folderContent == null) { var fileSyncAspect = Aspect.LoadAspectByPathOrName(ASPECTNAME); folderContent = Content.CreateNew("Folder", cntToSync.ContentHandler, folderName); folderContent.DisplayName = folder.Name; folderContent.AddAspects(fileSyncAspect); folderContent.Save(); } result.AddRange(GetFoldersAndSync(folderContent, folder.FullName)); } catch { result.Add(new SyncResultObject(string.Concat(cntToSync.Path, "/", folderName), SyncResult.SyncFailed)); } } } //// save sync date on parent SaveAsTechnicalUser(cntToSync, null, true); return(result); }
private Content CreateContent(JObject model, ODataRequest odataRequest) { var contentTypeName = GetPropertyValue <string>("__ContentType", model); var name = GetPropertyValue <string>("Name", model); if (string.IsNullOrEmpty(name)) { var displayName = GetPropertyValue <string>("DisplayName", model); name = ContentNamingHelper.GetNameFromDisplayName(displayName); } else { // do not allow saving a content with unencoded name name = ContentNamingHelper.GetNameFromDisplayName(name); } var parent = Node.Load <GenericContent>(odataRequest.RepositoryPath); if (string.IsNullOrEmpty(contentTypeName)) { var allowedChildTypeNames = parent.GetAllowedChildTypeNames(); if (allowedChildTypeNames is AllContentTypeNames) { contentTypeName = typeof(ContentRepository.File).Name; } else { var allowedContentTypeNames = parent.GetAllowedChildTypeNames().ToArray(); contentTypeName = allowedContentTypeNames.FirstOrDefault(); if (string.IsNullOrEmpty(contentTypeName)) { contentTypeName = typeof(ContentRepository.File).Name; } } } var content = Content.CreateNew(contentTypeName, parent, name); UpdateFields(content, model); if (odataRequest.MultistepSave) { content.Save(SavingMode.StartMultistepSave); } else { content.Save(); } return(content); }
protected override void Execute(NativeActivityContext context) { var ext = context.GetExtension <ContentWorkflowExtension>(); var parent = Node.LoadNode(ParentPath.Get(context)); if (parent == null) { throw new ApplicationException("Cannot create content because parent does not exist. Path: " + ParentPath.Get(context)); } var name = Name.Get(context); var displayName = ContentDisplayName.Get(context); if (string.IsNullOrEmpty(name)) { name = ContentNamingHelper.GetNameFromDisplayName(displayName); } var content = ContentManager.CreateContentFromRequest(GetContentTypeName(context), name, ParentPath.Get(context), true); if (!string.IsNullOrEmpty(displayName)) { content.DisplayName = displayName; } var fieldValues = FieldValues.Get(context); if (fieldValues != null) { foreach (var key in fieldValues.Keys) { content[key] = fieldValues[key]; } } SetContentFields(content, context); content.ContentHandler.DisableObserver(typeof(WorkflowNotificationObserver)); try { content.Save(); } catch (Exception e) { throw new ApplicationException(String.Concat("Cannot create content. See inner exception. Expected path: " , ParentPath.Get <string>(context), "/", Name.Get(context)), e); } Result.Set(context, new WfContent(content.ContentHandler)); }
public override object GetData() { var nameAvailableControl = _nameAvailableControl; if (IsTemplated) { nameAvailableControl = GetNameAvailableControl(); } // name control is available var nameControlAvailable = false; if (nameAvailableControl != null) { if (nameAvailableControl.Text != "0") { nameControlAvailable = true; } } var displayName = string.Empty; if (!IsTemplated) { displayName = _shortTextBox.Text; } else { var innerControl = GetInnerControl() as TextBox; displayName = innerControl != null ? innerControl.Text : _shortTextBox.Text; } if (!nameControlAvailable && (this.Content.Id == 0 || AlwaysUpdateName)) { // content name should be set automatically generated from displayname var newName = ContentNamingHelper.GetNameFromDisplayName(this.Content.Name, displayName); if (newName.Length > 0) { this.Content["Name"] = newName; } } return(displayName); }
public override object GetData() { if (this.ControlMode == FieldControlControlMode.Browse || this.ReadOnly) { return(_innerData); } var nameAvailableControl = GetNameAvailableControl(); // name control is available var nameControlAvailable = false; if (nameAvailableControl != null) { if (nameAvailableControl.Text != "0") { nameControlAvailable = true; } } var displayName = string.Empty; var innerControl = GetInnerControl() as TextBox; displayName = innerControl.Text; string className; string name; if (ResourceManager.ParseResourceKey(displayName, out className, out name)) { // get resources var allresStr = GetResourcesBoxControl().Text; // if resources JSON is empty, we just entered a resource key into displayname control, but it does not yet come from the resource editor if (!string.IsNullOrEmpty(allresStr)) { var ser = new JavaScriptSerializer(); var allres = ser.Deserialize <Portal.ResourceEditorController.ResourceKeyData>(allresStr); // value comes from the resource editor ui displayName = allres.Name; // if the entered value is a resource key, then update corresponding resources if (ResourceManager.ParseResourceKey(displayName, out className, out name)) { ResourceEditorController.SaveResource(className, name, allres.Datas); } } } if (!nameControlAvailable && (this.Content.Id == 0 || AlwaysUpdateName)) { // content name should be set automatically generated from displayname var newName = ContentNamingHelper.GetNameFromDisplayName(this.Content.Name, displayName); if (newName.Length > 0) { this.Content["Name"] = newName; } } return(displayName); }
private void UpdateFields(Content content, JObject model) { Field field; var isNew = content.Id == 0; foreach (var prop in model.Properties()) { if (string.IsNullOrEmpty(prop.Name) || prop.Name == "__ContentType" || prop.Name == "Type" || prop.Name == "ContentType") { continue; } var hasField = content.Fields.TryGetValue(prop.Name, out field); if (!hasField && content.SupportsAddingFieldsOnTheFly && prop.Value is JValue && ((JValue)prop.Value).Value != null) { var value = ((JValue)prop.Value).Value; var fieldSetting = FieldSetting.InferFieldSettingFromType(value.GetType(), prop.Name); var meta = new FieldMetadata(true, true, prop.Name, prop.Name, fieldSetting); hasField = content.AddFieldsOnTheFly(new[] { meta }) && content.Fields.TryGetValue(prop.Name, out field); } if (hasField) { if (!field.ReadOnly) { var jvalue = prop.Value as JValue; if (jvalue != null) { if (field is IntegerField) { field.SetData(Convert.ToInt32(jvalue.Value)); continue; } if (field is DateTimeField && jvalue.Value == null) { continue; } if (isNew && field is ReferenceField && jvalue.Value == null) { if (field.Name == "CreatedBy" || field.Name == "ModifiedBy") { continue; } } if (field is ReferenceField && jvalue.Value != null) { var refNode = jvalue.Type == JTokenType.Integer ? Node.LoadNode(Convert.ToInt32(jvalue.Value)) : Node.LoadNode(jvalue.Value.ToString()); field.SetData(refNode); continue; } if (isNew && field.Name == "Name" && jvalue.Value != null) { field.SetData(ContentNamingHelper.GetNameFromDisplayName(jvalue.Value.ToString())); continue; } field.SetData(jvalue.Value); continue; } var ovalue = prop.Value as JObject; if (ovalue != null) { //TODO: ODATA: setting field when posted value is JObject. //field.SetData(jvalue.Value); continue; } var avalue = prop.Value as JArray; if (avalue != null) { if (field is ReferenceField) { var refValues = avalue.Values().ToList(); if (refValues.Count == 0) { field.SetData(null); continue; } var fsetting = field.FieldSetting as ReferenceFieldSetting; var nodes = refValues.Select(rv => rv.Type == JTokenType.Integer ? Node.LoadNode(Convert.ToInt32(rv.ToString())) : Node.LoadNode(rv.ToString())); if (fsetting.AllowMultiple.HasValue && fsetting.AllowMultiple.Value) { field.SetData(nodes); } else { field.SetData(nodes.First()); } continue; } else if (field is ChoiceField) { // ChoiceField expects the value to be of type List<string> var list = new List <string>(); foreach (var token in avalue) { if (token is JValue) { list.Add(((JValue)token).Value.ToString()); } else { throw new Exception(string.Format("Token type {0} for field {1} (type {2}) is not supported.", token.GetType().Name, field.Name, field.GetType().Name)); } } field.SetData(list); } continue; } throw new NotImplementedException(); } } } }
protected override void Execute(NativeActivityContext context) { var message = Message.Get(context); var parentPath = ParentPath.Get(context); var overwrite = OverwriteExistingContent.Get(context); var displayName = ContentDisplayName.Get(context); var name = ContentName.Get(context); if (string.IsNullOrEmpty(name)) { name = ContentNamingHelper.GetNameFromDisplayName(displayName) + ".eml"; } var parent = Node.LoadNode(parentPath); if (parent == null) { throw new ApplicationException("Cannot create content because parent does not exist. Path: " + parentPath); } //var displayName = message.Subject; //var name = ContentNamingHelper.GetNameFromDisplayName(message.Subject) + ".eml"; // check existing file var node = Node.LoadNode(RepositoryPath.Combine(parentPath, name)); File file; if (node == null) { // file does not exist, create new one file = new File(parent); if (!string.IsNullOrEmpty(displayName)) { file.DisplayName = displayName; } file.Name = name; } else { // file exists if (overwrite) { // overwrite it, so we open it file = node as File; // node exists and it is not a file -> delete it and create a new one if (file == null) { node.ForceDelete(); file = new File(parent); } file.DisplayName = displayName; file.Name = name; } else { // do not overwrite it file = new File(parent); if (!string.IsNullOrEmpty(displayName)) { file.DisplayName = displayName; } file.Name = name; file.AllowIncrementalNaming = true; } } try { using (var memoryStream = new System.IO.MemoryStream(message.MimeContent.Content)) { var binaryData = new BinaryData(); binaryData.SetStream(memoryStream); file.Binary = binaryData; file.Save(); } } catch (Exception ex) { Logger.WriteException(ex); } }
private static string GetTagName(string tag) { return(ContentNamingHelper.GetNameFromDisplayName("", tag).Trim().Replace('.', '_').Replace('"', '_').Replace('\'', '_')); }
protected override void Execute(NativeActivityContext context) { var message = Message.Get(context); var parentPath = ParentPath.Get(context); var overwrite = OverwriteExistingContent.Get(context); var displayName = ContentDisplayName.Get(context); var name = ContentName.Get(context); if (string.IsNullOrEmpty(name)) { name = ContentNamingHelper.GetNameFromDisplayName(displayName) + ".eml"; } var parent = Node.LoadNode(parentPath); if (parent == null) { throw new ApplicationException("Cannot create content because parent does not exist. Path: " + parentPath); } // check existing file var node = Node.LoadNode(RepositoryPath.Combine(parentPath, name)); File file; if (node == null) { // file does not exist, create new one file = new File(parent); if (!string.IsNullOrEmpty(displayName)) { file.DisplayName = displayName; } file.Name = name; } else { // file exists if (overwrite) { // overwrite it, so we open it file = node as File; // node exists and it is not a file -> delete it and create a new one if (file == null) { try { node.ForceDelete(); } catch { Logger.WriteError(Logger.EventId.NotDefined, "Mail processor workflow: content could not be deleted during saving the email. Path: " + node.Path); return; } file = new File(parent); } file.DisplayName = displayName; file.Name = name; } else { // do not overwrite it file = new File(parent); if (!string.IsNullOrEmpty(displayName)) { file.DisplayName = displayName; } file.Name = name; file.AllowIncrementalNaming = true; } } try { var binaryData = new BinaryData() { FileName = name }; binaryData.SetStream(Tools.GetStreamFromString(message.Body)); file.Binary = binaryData; file.Save(); } catch (Exception ex) { Logger.WriteException(ex); } }
private List <SyncResultObject> GetFilesAndSync(Content cntToSync, string filePath) { var result = new List <SyncResultObject>(); DirectoryInfo dirInfo = new DirectoryInfo(filePath); IEnumerable <Content> children = cntToSync.Children.Where(c => c.TypeIs("File")); //Content lastContent = children.OrderByDescending(c => c.CreationDate).FirstOrDefault(); //DateTime lastContentDate = (lastContent != null) ? lastContent.CreationDate : DateTime.MinValue; //technical debt: I think creationdate won't be good here, we should probably use last syncdate var fileInfos = dirInfo.GetFiles(); //if (fileInfos.Length == 0) // result.Add(new SyncResultObject(filePath, SyncResult.NoSyncToDo)); //else if (fileInfos.Length > 0) { foreach (var file in fileInfos) { var fileSynced = false; string fileName = file.Name; try { using (Stream fileStream = file.Open(FileMode.Open, FileAccess.Read)) //Open the file ReadOnly mode { fileName = ContentNamingHelper.GetNameFromDisplayName(file.Name); using (new SystemAccount()) {//Technical Debt: as for now we do not check if file needs to be updated or not Content fileContent = cntToSync.Children.Where(c => c.Name == fileName).FirstOrDefault(); if (fileContent == null) { // create new SenseNet.ContentRepository.File newFile = new SenseNet.ContentRepository.File(cntToSync.ContentHandler); newFile.Name = ContentNamingHelper.GetNameFromDisplayName(file.Name); newFile.DisplayName = file.Name; newFile.Save(); fileContent = Content.Load(newFile.Id); var fileSyncAspect = Aspect.LoadAspectByPathOrName(ASPECTNAME); fileContent.Save(); // ez miert? elo is kell menteni? fileContent.AddAspects(fileSyncAspect); } SaveAsTechnicalUser(fileContent, fileStream); result.Add(new SyncResultObject(fileContent.Path, SyncResult.SyncSuccess)); } fileSynced = true; } } catch (Exception ex) { SnLog.WriteException(ex); result.Add(new SyncResultObject(string.Concat(cntToSync.Path, "/", fileName), SyncResult.SyncFailed)); } // result add would be better at here // delete file try { if (fileSynced) { //Logger.WriteInformation(40002, "FILESYNC delete 545 " + file.Name); file.Delete(); } // should we log deletion? } catch (Exception ex) { SnLog.WriteException(ex); } } } //// save refresh date on parent //SaveAsTechnicalUser(cntToSync, null, true); return(result); }