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 void ResetContent(Content content) { // Create "dummy" content var newContent = Content.CreateNew(content.ContentType.Name, content.ContentHandler.Parent, null); Aspect[] aspects = null; if (content.ContentHandler.HasProperty(GenericContent.ASPECTS)) { // Get aspects aspects = content.ContentHandler.GetReferences(GenericContent.ASPECTS).Cast <Aspect>().ToArray(); // Reset aspect fields var gc = content.ContentHandler as GenericContent; if (gc != null) { content.RemoveAllAspects(); gc.AspectData = null; gc.ClearReference(GenericContent.ASPECTS); } } // Reset regular fields foreach (var field in content.Fields.Values) { var fieldName = field.Name; if (newContent.Fields.Any(f => f.Value.Name == fieldName) && !field.ReadOnly && !SafeFieldsInReset.Contains(fieldName)) { content[fieldName] = newContent[fieldName]; } } if (content.ContentHandler.HasProperty(GenericContent.ASPECTS)) { // Re-add all the aspects content.AddAspects(aspects); } }
public override object Execute(Content content, params object[] parameters) { var aspectPaths = (string[])parameters[0]; var gc = content.ContentHandler as GenericContent; if (gc == null) { throw new InvalidOperationException("Cannot add an aspect to a content that is not a GenericContent."); } var aspects = new Aspect[aspectPaths.Length]; for (int i = 0; i < aspectPaths.Length; i++) { var pathOrName = aspectPaths[i]; aspects[i] = Aspect.LoadAspectByPathOrName(pathOrName); if (aspects[i] == null) { throw new InvalidOperationException("Unknown aspect: " + aspectPaths[i]); } } content.AddAspects(aspects); content.Save(); return(null); }
public void Aspect_AddingAspectDoesNotResetDefaultValuesOfTheOriginalFields() { Test(() => { var testRoot = CreateTestRoot(); var contentTypeName = "Aspect_AddingAspectDoesNotResetDefaultValuesOfTheOriginalFields"; var shortTextFieldName = "ShortText"; var displayName2Name = "DisplayName2"; var int32FieldName = "Int32"; var referenceFieldName = "GeneralReference"; var shortText2FieldName = "ShortText2"; var shortTextDefault = "Forty four"; var displayName2Default = "Friendly name"; var int32Default = 42; var referenceDefault = "/Root,/Root/IMS"; ContentTypeInstaller.InstallContentType(@"<?xml version='1.0' encoding='utf-8'?> <ContentType name='" + contentTypeName + @"' parentType='GenericContent' handler='SenseNet.ContentRepository.GenericContent' xmlns='http://schemas.sensenet.com/SenseNet/ContentRepository/ContentTypeDefinition'> <DisplayName>AddAspectAndFieldDefaultValueTest</DisplayName> <Description>AddAspectAndFieldDefaultValueTest</Description> <Fields> <Field name='" + shortTextFieldName + @"' type='ShortText'> <Configuration> <Compulsory>true</Compulsory> <DefaultValue>" + shortTextDefault + @"</DefaultValue> </Configuration> </Field> <Field name='" + int32FieldName + @"' type='Integer'> <Configuration> <Compulsory>true</Compulsory> <DefaultValue>" + int32Default + @"</DefaultValue> </Configuration> </Field> <Field name='" + referenceFieldName + @"' type='Reference'> <Configuration> <Compulsory>true</Compulsory> <AllowMultiple>true</AllowMultiple> <Compulsory>false</Compulsory> <DefaultValue>" + referenceDefault + @"</DefaultValue> </Configuration> </Field> <Field name='" + displayName2Name + @"' type='ShortText'> <Bind property='HiddenField'/> <Configuration> <Compulsory>true</Compulsory> <DefaultValue>" + displayName2Default + @"</DefaultValue> </Configuration> </Field> <Field name='" + shortText2FieldName + @"' type='ShortText' /> </Fields> </ContentType>"); var aspect1Name = contentTypeName + "_Aspect1"; var aspect2Name = contentTypeName + "_Aspect2"; var aspect3Name = contentTypeName + "_Aspect3"; var aspectShortTextFieldName = aspect1Name + ".ShortText"; var aspectDisplayName2FieldName = aspect1Name + ".DisplayName2"; var aspectInt32FieldName = aspect2Name + ".Int32"; var aspectReferenceFieldName = aspect3Name + ".GeneralReference"; var aspect1 = EnsureAspect(aspect1Name); aspect1.AspectDefinition = @"<AspectDefinition xmlns='http://schemas.sensenet.com/SenseNet/ContentRepository/AspectDefinition'> <Fields> <AspectField name='" + shortTextFieldName + @"' type='ShortText'> <Configuration> <Compulsory>true</Compulsory> <DefaultValue>" + shortTextDefault + @"</DefaultValue> </Configuration> </AspectField> <AspectField name='" + displayName2Name + @"' type='ShortText'> <Bind property='HiddenField'/> <Configuration> <Compulsory>true</Compulsory> <DefaultValue>" + displayName2Default + @"</DefaultValue> </Configuration> </AspectField> </Fields> </AspectDefinition>"; aspect1.Save(); var aspect2 = EnsureAspect(aspect2Name); aspect2.AspectDefinition = @"<AspectDefinition xmlns='http://schemas.sensenet.com/SenseNet/ContentRepository/AspectDefinition'> <Fields> <AspectField name='" + int32FieldName + @"' type='Integer'> <Configuration> <Compulsory>true</Compulsory> <DefaultValue>" + int32Default + @"</DefaultValue> </Configuration> </AspectField> </Fields> </AspectDefinition>"; aspect2.Save(); var aspect3 = EnsureAspect(aspect3Name); aspect3.AspectDefinition = @"<AspectDefinition xmlns='http://schemas.sensenet.com/SenseNet/ContentRepository/AspectDefinition'> <Fields> <AspectField name='" + referenceFieldName + @"' type='Reference'> <Configuration> <Compulsory>true</Compulsory> <AllowMultiple>true</AllowMultiple> <Compulsory>false</Compulsory> <DefaultValue>" + referenceDefault + @"</DefaultValue> </Configuration> </AspectField> </Fields> </AspectDefinition>"; aspect3.Save(); Content content = null; try { // #1 check not saved content = Content.CreateNew(contentTypeName, testRoot, Guid.NewGuid().ToString()); Assert.AreEqual(shortTextDefault, content[shortTextFieldName]); Assert.AreEqual(null, content[shortText2FieldName]); Assert.AreEqual(null, content[displayName2Name]); Assert.AreEqual(int32Default, content[int32FieldName]); Assert.AreEqual(referenceDefault, String.Join(",", ((IEnumerable <Node>)content[referenceFieldName]).Select(n => n.Path))); content.Save(); var contentId = content.Id; // #2 check reloaded content = Content.Load(contentId); Assert.AreEqual(shortTextDefault, content[shortTextFieldName]); Assert.AreEqual(null, content[shortText2FieldName]); Assert.AreEqual(null, content[displayName2Name]); Assert.AreEqual(int32Default, content[int32FieldName]); Assert.AreEqual(referenceDefault, String.Join(",", ((IEnumerable <Node>)content[referenceFieldName]).Select(n => n.Path))); // #3 add aspect1 and aspect2 with separated instruction without save content.AddAspects(aspect1); content.AddAspects(aspect2); // #4 check with 2 aspects / not saved Assert.AreEqual(shortTextDefault, content[shortTextFieldName]); Assert.AreEqual(null, content[shortText2FieldName]); Assert.AreEqual(null, content[displayName2Name]); Assert.AreEqual(int32Default, content[int32FieldName]); Assert.AreEqual(referenceDefault, String.Join(",", ((IEnumerable <Node>)content[referenceFieldName]).Select(n => n.Path))); Assert.AreEqual(shortTextDefault, content[aspectShortTextFieldName]); Assert.AreEqual(null, content[aspectDisplayName2FieldName]); Assert.AreEqual(int32Default, content[aspectInt32FieldName]); content.Save(); // #5 check with 2 aspects / reloaded content = Content.Load(contentId); Assert.AreEqual(shortTextDefault, content[shortTextFieldName]); Assert.AreEqual(null, content[displayName2Name]); Assert.AreEqual(null, content[shortText2FieldName]); Assert.AreEqual(int32Default, content[int32FieldName]); Assert.AreEqual(referenceDefault, String.Join(",", ((IEnumerable <Node>)content[referenceFieldName]).Select(n => n.Path))); Assert.AreEqual(shortTextDefault, content[aspectShortTextFieldName]); Assert.AreEqual(null, content[aspectDisplayName2FieldName]); Assert.AreEqual(int32Default, content[aspectInt32FieldName]); // #6 add aspect3 without save content.AddAspects(aspect3); // #7 check with all aspects / not saved Assert.AreEqual(shortTextDefault, content[shortTextFieldName]); Assert.AreEqual(null, content[displayName2Name]); Assert.AreEqual(null, content[shortText2FieldName]); Assert.AreEqual(int32Default, content[int32FieldName]); Assert.AreEqual(referenceDefault, String.Join(",", ((IEnumerable <Node>)content[referenceFieldName]).Select(n => n.Path))); Assert.AreEqual(shortTextDefault, content[aspectShortTextFieldName]); Assert.AreEqual(null, content[aspectDisplayName2FieldName]); Assert.AreEqual(int32Default, content[aspectInt32FieldName]); Assert.AreEqual(referenceDefault, String.Join(",", ((IEnumerable <Node>)content[aspectReferenceFieldName]).Select(n => n.Path))); content.Save(); // #8 check with all aspects / reloaded content = Content.Load(contentId); Assert.AreEqual(shortTextDefault, content[shortTextFieldName]); Assert.AreEqual(null, content[displayName2Name]); Assert.AreEqual(null, content[shortText2FieldName]); Assert.AreEqual(int32Default, content[int32FieldName]); Assert.AreEqual(referenceDefault, String.Join(",", ((IEnumerable <Node>)content[referenceFieldName]).Select(n => n.Path))); Assert.AreEqual(shortTextDefault, content[aspectShortTextFieldName]); Assert.AreEqual(null, content[aspectDisplayName2FieldName]); Assert.AreEqual(int32Default, content[aspectInt32FieldName]); Assert.AreEqual(referenceDefault, String.Join(",", ((IEnumerable <Node>)content[aspectReferenceFieldName]).Select(n => n.Path))); } finally { content.ForceDelete(); if (ContentType.GetByName(contentTypeName) != null) { ContentTypeInstaller.RemoveContentType(contentTypeName); } } }); }
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); }