예제 #1
0
        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));
        }
예제 #2
0
        private string ParseSuffixTest(string name, int expected)
        {
            name = Path.GetFileNameWithoutExtension(name);
            string nameBase;
            bool   inValidNumber;
            var    actual = ContentNamingHelper.ParseSuffix(name, out nameBase, out inValidNumber);

            if (actual == expected)
            {
                return(null);
            }
            return(String.Format("Result is {0}, expected: {1}", actual, expected));
        }
예제 #3
0
        private Content CreateContent(JObject model, ODataRequest odataRequest, PortalContext portalContext)
        {
            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);
            }

            var parent = Node.Load <GenericContent>(odataRequest.RepositoryPath);

            if (string.IsNullOrEmpty(contentTypeName))
            {
                var allowedChildTypeNames = parent.GetAllowedChildTypeNames();

                if (allowedChildTypeNames is AllContentTypeNames)
                {
                    contentTypeName = typeof(SenseNet.ContentRepository.File).Name;
                }
                else
                {
                    var allowedContentTypeNames = parent.GetAllowedChildTypeNames().ToArray();
                    contentTypeName = allowedContentTypeNames.FirstOrDefault();
                    if (string.IsNullOrEmpty(contentTypeName))
                    {
                        contentTypeName = typeof(SenseNet.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);
        }
예제 #4
0
        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);
        }
예제 #5
0
        public void ContentNaming_IncrementNameSuffix()
        {
            string msg;

            msg = IncrementNameSuffixTest("Car", "Car(1)"); Assert.IsNull(msg, msg);
            msg = IncrementNameSuffixTest("Car(12)", "Car(13)"); Assert.IsNull(msg, msg);
            msg = IncrementNameSuffixTest("Car.xml", "Car(1).xml"); Assert.IsNull(msg, msg);
            msg = IncrementNameSuffixTest("Car(8).xml", "Car(9).xml"); Assert.IsNull(msg, msg);
            msg = IncrementNameSuffixTest("Car((8).xml", "Car((9).xml"); Assert.IsNull(msg, msg);
            msg = IncrementNameSuffixTest("(1)", "(2)"); Assert.IsNull(msg, msg); Assert.IsNull(msg, msg);
            msg = IncrementNameSuffixTest(")", ")(1)"); Assert.IsNull(msg, msg); Assert.IsNull(msg, msg);

            msg = IncrementNameSuffixTest("Car(string)(12)", "Car(string)(13)"); Assert.IsNull(msg, msg);

            // suffices are guids for the following.
            // if the last suffixed name from db is in the form 'name(x)' where x is not a number, we are not able to decide the next suffix, so it is guid
            var actual = ContentNamingHelper.IncrementNameSuffix("Car(string)");

            Assert.AreEqual("Car(string)(1)", actual);
            actual = ContentNamingHelper.IncrementNameSuffix("Car(8)).xml");
            Assert.AreEqual("Car(8))(1).xml", actual);
            actual = ContentNamingHelper.IncrementNameSuffix("Car()");
            Assert.AreEqual("Car()(1)", actual);
        }
예제 #6
0
        protected static DocumentFormat GetFormatByName(string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                return(DocumentFormat.Pdf);
            }

            var extension = ContentNamingHelper.GetFileExtension(fileName).ToLower();

            if (PDF_EXTENSIONS.Contains(extension))
            {
                return(DocumentFormat.Pdf);
            }

            if (PRESENTATION_EXTENSIONS.Contains(extension))
            {
                return(DocumentFormat.Ppt);
            }

            if (PRESENTATIONEX_EXTENSIONS.Contains(extension))
            {
                return(DocumentFormat.Pptx);
            }

            if (WORKBOOK_EXTENSIONS.Contains(extension))
            {
                return(extension.EndsWith("x") ? DocumentFormat.Xlsx : DocumentFormat.Xls);
            }

            if (WORD_EXTENSIONS.Contains(extension))
            {
                return(extension.EndsWith("x") ? DocumentFormat.Docx : DocumentFormat.Doc);
            }

            return(DocumentFormat.Pdf);
        }
예제 #7
0
        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);
        }
예제 #8
0
        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();
                    }
                }
            }
        }
예제 #9
0
 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"));
 }
예제 #10
0
        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);
            }
        }
예제 #11
0
 private static string GetTagName(string tag)
 {
     return(ContentNamingHelper.GetNameFromDisplayName("", tag).Trim().Replace('.', '_').Replace('"', '_').Replace('\'', '_'));
 }
예제 #12
0
        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);
        }
예제 #14
0
 public override bool IsContentSupported(STORAGE.Node content)
 {
     return(SUPPORTED_EXTENSIONS.Contains(ContentNamingHelper.GetFileExtension(content.Name).ToLower()));
 }