/// <exclude /> public static void FunctionRenamed(IInlineFunction newFunction, IInlineFunction oldFunction) { newFunction.UpdateCodePath(); string directoryPath = PathUtil.Resolve(GlobalSettingsFacade.InlineCSharpFunctionDirectory); string oldFilepath = Path.Combine(directoryPath, oldFunction.CodePath); string newFilepath = Path.Combine(directoryPath, newFunction.CodePath); C1File.Move(oldFilepath, newFilepath); }
private static void UpdateFilenames() { List <string> filepaths = C1Directory.GetFiles(_metaDataPath, "*.xml").ToList(); var ids = new Dictionary <Guid, string>(); foreach (string filepath in filepaths) { Guid id = GetGuidFromFilename(filepath); if (!ids.ContainsKey(id)) { ids.Add(id, filepath); } else // This should never happen, but is here to be robust { if (!IsMetaDataFileName(Path.GetFileNameWithoutExtension(filepath))) // Old version of the file, delete it { FileUtils.Delete(filepath); } else // Old version is stored in ids, delete it and change the value to new version { FileUtils.Delete(ids[id]); ids[id] = filepath; } } } foreach (var kvp in ids) { string filepath = kvp.Value; if (!IsMetaDataFileName(Path.GetFileNameWithoutExtension(filepath))) { continue; } var dataTypeDescriptor = LoadFromFile(filepath); string newFilepath = CreateFilename(dataTypeDescriptor); FileUtils.RemoveReadOnly(filepath); Func <string, string> normalizeFileName = f => f.Replace('_', ' ').ToLowerInvariant(); if (normalizeFileName(filepath) != normalizeFileName(newFilepath)) { C1File.Move(filepath, newFilepath); } } }
private void finalizeCodeActivity_Finalize_ExecuteCode(object sender, EventArgs e) { string oldPath = this.FilePath; if (oldPath != null && C1File.Exists(oldPath)) { string newPath = Path.Combine(Path.GetDirectoryName(oldPath), Path.GetFileNameWithoutExtension(oldPath)); if (Path.GetExtension(oldPath) == ".html") { newPath += ".xml"; } else { newPath += ".html"; } C1File.Move(oldPath, newPath); this.RefreshRootEntityToken(); } }
public void Update(IEnumerable <IData> datas) { foreach (IData data in datas) { if (data == null) { throw new ArgumentException("Data in list to update must be non-null"); } } foreach (IData data in datas) { MediaDataId dataId = data.DataSourceId.DataId as MediaDataId; if (dataId == null) { throw new ArgumentException("Invalid IData"); } if (dataId.MediaType == _fileType) { IMediaFile updatedFile = (IMediaFile)data; if (updatedFile.StoreId != this.Store.Id) { continue; } if (updatedFile.IsReadOnly) { throw new ArgumentException("Cannot update read only media file " + dataId.FileName); } if (updatedFile.FileName != dataId.FileName || updatedFile.FolderPath != dataId.Path) { string oldPos = GetAbsolutePath(dataId); string newPos = GetAbsolutePath(updatedFile); C1File.Move(oldPos, newPos); } using (Stream readStream = updatedFile.GetReadStream()) { using (Stream writeStream = C1File.Open(GetAbsolutePath(updatedFile), FileMode.Create)) { readStream.CopyTo(writeStream); } } } else { IMediaFileFolder updatedFolder = (IMediaFileFolder)data; if (updatedFolder.StoreId != this.Store.Id) { continue; } if (updatedFolder.IsReadOnly) { throw new ArgumentException("Cannot update read only media folder " + dataId.Path); } C1Directory.Move(GetAbsolutePath(dataId), GetAbsolutePath(updatedFolder)); } } }
/// <summary> /// Will pull up most recent from disk - if no good file is found an empty store will be constructed anyway /// </summary> /// <param name="filePath"></param> /// <param name="elementName"></param> /// <param name="keyGetter"></param> /// <returns></returns> private static FileRecord LoadFileRecordFromDisk(string filePath, string elementName, Func <XElement, IDataId> keyGetter) { XDocument dataDocument = null; C1FileInfo usedFile = null; IList <C1FileInfo> candidateFiles = GetCandidateFiles(filePath); foreach (C1FileInfo candidateFile in candidateFiles) { bool tryLoad = true; while (tryLoad && dataDocument == null) { dataDocument = TryLoad(candidateFile); if (dataDocument == null) { if ((DateTime.Now - candidateFile.LastWriteTime).TotalSeconds > 30) { tryLoad = false; } else { Thread.Sleep(250); // other processes/servers may be writing to this file at the moment. Patience young padawan! } } } if (dataDocument != null) { usedFile = candidateFile; break; } } if (dataDocument == null) { dataDocument = new XDocument(new XElement("fallback")); Log.LogWarning(LogTitle, "Did not find a healthy XML document for '{0}' - creating an empty store.", filePath); } List <XElement> elements = ExtractElements(dataDocument); var index = new Hashtable <IDataId, XElement>(elements.Count); foreach (var element in elements) { IDataId id = keyGetter(element); if (!index.ContainsKey(id)) { index.Add(id, element); } else { Log.LogWarning(LogTitle, "Found multiple elements in '{0}' sharing same key - duplicates ignored.", filePath); } } if (usedFile != null) { // clean up old and unused files foreach (C1FileInfo file in candidateFiles.Where(f => f.LastWriteTime < usedFile.LastWriteTime)) { try { C1File.Move(file.FullName, file.FullName + ".ghost"); } catch (Exception) { Log.LogWarning(LogTitle, "Failed to clean up ghost file '{0}'.", filePath); } } } DateTime lastModifiedFileDate = usedFile != null ? usedFile.LastWriteTime : DateTime.Now; return(new FileRecord { FilePath = filePath, ElementName = elementName, RecordSet = new RecordSet { Index = index }, ReadOnlyElementsList = new List <XElement>(elements), LastModified = DateTime.Now, FileModificationDate = lastModifiedFileDate }); }
private void saveCodeActivity_ExecuteCode(object sender, EventArgs e) { try { IXsltFunction xslt = this.GetBinding <IXsltFunction>("CurrentXslt"); IXsltFunction previousXslt = DataFacade.GetData <IXsltFunction>(f => f.Id == xslt.Id).SingleOrDefault(); IFile persistemTemplateFile = IFileServices.TryGetFile <IXsltFile>(xslt.XslFilePath); if (persistemTemplateFile != null) { string persistemTemplate = (persistemTemplateFile != null ? persistemTemplateFile.ReadAllText() : ""); if (this.GetBinding <int>("XslTemplateLastSaveHash") != persistemTemplate.GetHashCode()) { this.Bindings["XslTemplate"] = persistemTemplate; this.RerenderView(); ConsoleMessageQueueFacade.Enqueue(new LogEntryMessageQueueItem { Level = LogLevel.Fine, Message = "XSLT file on file system has been changed by another process. In-browser editor updated to reflect file on file system.", Sender = this.GetType() }, this.GetCurrentConsoleId()); } } string xslTemplate = this.GetBinding <string>("XslTemplate"); var parameters = this.GetBinding <IEnumerable <ManagedParameterDefinition> >("Parameters"); IEnumerable <NamedFunctionCall> FunctionCalls = this.GetBinding <IEnumerable <NamedFunctionCall> >("FunctionCalls"); if (FunctionCalls.Select(f => f.Name).Distinct().Count() != FunctionCalls.Count()) { ShowMessage(DialogType.Error, GetString("EditXsltFunctionWorkflow.SameLocalFunctionNameClashTitle"), GetString("EditXsltFunctionWorkflow.SameLocalFunctionNameClashMessage")); return; } using (TransactionScope transactionScope = TransactionsFacade.CreateNewScope()) { // Renaming related file if necessary string oldRelativePath = previousXslt.XslFilePath.Replace('\\', '/'); // This replace takes care of old paths having \ in them string newRelativePath = xslt.CreateXslFilePath(); if (string.Compare(oldRelativePath, newRelativePath, true) != 0) { var xlsFile = IFileServices.GetFile <IXsltFile>(previousXslt.XslFilePath); string systemPath = (xlsFile as FileSystemFileBase).SystemPath; // Implement it in another way? string xsltFilesRoot = systemPath.Substring(0, systemPath.Length - previousXslt.XslFilePath.Length); string newSystemPath = (xsltFilesRoot + newRelativePath).Replace('\\', '/'); if ((string.Compare(systemPath, newSystemPath, true) != 0) && C1File.Exists(newSystemPath)) { FlowControllerServicesContainer serviceContainer = WorkflowFacade.GetFlowControllerServicesContainer(WorkflowEnvironment.WorkflowInstanceId); var consoleMessageService = serviceContainer.GetService <IManagementConsoleMessageService>(); consoleMessageService.ShowMessage( DialogType.Error, GetString("EditXsltFunctionWorkflow.InvalidName"), GetString("EditXsltFunctionWorkflow.CannotRenameFileExists").FormatWith(newSystemPath)); return; } string directoryPath = Path.GetDirectoryName(newSystemPath); if (!C1Directory.Exists(directoryPath)) { C1Directory.CreateDirectory(directoryPath); } C1File.Move(systemPath, newSystemPath); xslt.XslFilePath = newRelativePath; // TODO: Implement removing empty Xslt directories } IFile file = IFileServices.GetFile <IXsltFile>(xslt.XslFilePath); file.SetNewContent(xslTemplate); ManagedParameterManager.Save(xslt.Id, parameters); DataFacade.Update(xslt); DataFacade.Update(file); this.Bindings["XslTemplateLastSaveHash"] = xslTemplate.GetHashCode(); DataFacade.Delete <INamedFunctionCall>(f => f.XsltFunctionId == xslt.Id); DataFacade.AddNew <INamedFunctionCall>(ConvertFunctionCalls(FunctionCalls, xslt.Id)); transactionScope.Complete(); } if (previousXslt.Namespace != xslt.Namespace || previousXslt.Name != xslt.Name || previousXslt.Description != xslt.Description) { // This is a some what nasty hack. Due to the nature of the BaseFunctionProviderElementProvider, this hack is needed BaseFunctionFolderElementEntityToken entityToken = new BaseFunctionFolderElementEntityToken("ROOT:XsltBasedFunctionProviderElementProvider"); RefreshEntityToken(entityToken); } SetSaveStatus(true); } catch (Exception ex) { LoggingService.LogCritical("XSLT Save", ex); FlowControllerServicesContainer serviceContainer = WorkflowFacade.GetFlowControllerServicesContainer(WorkflowEnvironment.WorkflowInstanceId); var consoleMsgService = serviceContainer.GetService <IManagementConsoleMessageService>(); consoleMsgService.ShowMessage(DialogType.Error, "Error", ex.Message); SetSaveStatus(false); } }
private void codeActivity1_ExecuteCode(object sender, EventArgs e) { IXmlPageTemplate pageTemplate = this.GetBinding <IXmlPageTemplate>("PageTemplate"); string pageTemplateMarkup = this.GetBinding <string>("PageTemplateMarkup"); bool xhtmlParseable = true; string parseError = null; try { XDocument parsedElement = XDocument.Parse(pageTemplateMarkup); ValidatePageTemplate(parsedElement); } catch (Exception ex) { xhtmlParseable = false; parseError = ex.Message; } if (!xhtmlParseable) { FlowControllerServicesContainer serviceContainer = WorkflowFacade.GetFlowControllerServicesContainer(WorkflowEnvironment.WorkflowInstanceId); var consoleMessageService = serviceContainer.GetService <IManagementConsoleMessageService>(); consoleMessageService.ShowMessage( DialogType.Error, GetString("EditXmlPageTemplateWorkflow.InvalidXmlTitle"), GetString("EditXmlPageTemplateWorkflow.InvalidXmlMessage").FormatWith(parseError)); return; } // Renaming related file if necessary string fileName = GetTemplateFileName(pageTemplate); if (Path.GetFileName(pageTemplate.PageTemplateFilePath) != fileName) { IPageTemplateFile file = IFileServices.GetFile <IPageTemplateFile>(pageTemplate.PageTemplateFilePath); string systemPath = (file as FileSystemFileBase).SystemPath; string newSystemPath = Path.Combine(Path.GetDirectoryName(systemPath), fileName); if (string.Compare(systemPath, newSystemPath, true) != 0 && C1File.Exists(newSystemPath)) { FlowControllerServicesContainer serviceContainer = WorkflowFacade.GetFlowControllerServicesContainer(WorkflowEnvironment.WorkflowInstanceId); var consoleMessageService = serviceContainer.GetService <IManagementConsoleMessageService>(); consoleMessageService.ShowMessage( DialogType.Error, GetString("EditXmlPageTemplateWorkflow.InvalidXmlTitle"), GetString("EditXmlPageTemplateWorkflow.CannotRenameFileExists").FormatWith(newSystemPath)); return; } C1File.Move(systemPath, newSystemPath); string newRelativePath = Path.Combine(Path.GetDirectoryName(pageTemplate.PageTemplateFilePath), fileName); pageTemplate.PageTemplateFilePath = newRelativePath; } IPageTemplateFile templateFile = IFileServices.GetFile <IPageTemplateFile>(pageTemplate.PageTemplateFilePath); templateFile.SetNewContent(pageTemplateMarkup); DataFacade.Update(templateFile); DataFacade.Update(pageTemplate); UpdateTreeRefresher updateTreeRefresher = this.CreateUpdateTreeRefresher(this.EntityToken); updateTreeRefresher.PostRefreshMesseges(pageTemplate.GetDataEntityToken()); SetSaveStatus(true); }