Example #1
0
 public object StartTransaction()
 {
     if (_FirstCall)
     {
         _FirstCall             = false;
         _FileSystemTransaction = new FileSystemTransaction();
     }
     return(_FileSystemTransaction);
 }
Example #2
0
        protected override object Write(IDataLoaderWriteContext execContext)
        {
            //Node node = (Node)execContext.CurrentNode;
            //string directoryPath = Path.Combine(
            //execContext.ProcessingContext.InputModel.EnvironmentSettings.ContentRootPath,
            //"Modules",
            //execContext.ProcessingContext.InputModel.Module,
            //"Public");

            //var filename = execContext.Evaluate("rawfilename");
            //var filecontents = execContext.Evaluate("rawfilecontents");
            //string fileName = filename.Value.ToString();
            //string filePath = Path.Combine(directoryPath, fileName);
            //string fileContents = filecontents.Value.ToString();

            //FileSystemTransaction fst = ((ITransactionScope)execContext.OwnContextScoped).StartTransaction() as FileSystemTransaction;

            ////Check if you need something else from the InputModel.Data
            //if(!File.Exists(filePath)) fst.Create(filePath);
            //fst.Write(filePath, fileContents);

            //return new Dictionary<string, object>(); /*{
            //    { "filename", fileName},
            //    { "content", fileContents },
            //    { "length", fileContents.Length}
            //};*/

            KraftGlobalConfigurationSettings kraftSettings =
                execContext.PluginServiceManager.GetService <KraftGlobalConfigurationSettings>(typeof(KraftGlobalConfigurationSettings));

            string modulePath = string.Empty;

            execContext.DataLoaderContextScoped.CustomSettings.TryGetValue("Path", out modulePath);

            if (kraftSettings != default(KraftGlobalConfigurationSettings) || modulePath.Length == 0)
            {
                string path = Path.Combine(
                    kraftSettings.GeneralSettings.ModulesRootFolder(execContext.ProcessingContext.InputModel.Module),
                    execContext.ProcessingContext.InputModel.Module,
                    modulePath);

                if (Directory.Exists(path))
                {
                    string[] commands;

                    switch (execContext.Operation)
                    {
                    case "insert":
                        commands = execContext.CurrentNode.Write.Insert.Query.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).ToArray();
                        break;

                    case "update":
                        commands = execContext.CurrentNode.Write.Update.Query.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).ToArray();
                        break;

                    case "delete":
                        commands = execContext.CurrentNode.Write.Delete.Query.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).ToArray();
                        break;

                    default:
                        throw new Exception(string.Empty);
                    }

                    if (commands == default(string[]) || commands.Length < 0)
                    {
                        throw new Exception(string.Empty);
                    }

                    string currentCommand      = string.Empty;
                    FileSystemTransaction fst  = ((ITransactionScope)execContext.OwnContextScoped).StartTransaction() as FileSystemTransaction;
                    Type fileSystemCommandType = typeof(IFileSystemCommand);

                    foreach (string commandName in commands)
                    {
                        currentCommand = commandName + "Command";

                        Type type = AppDomain.CurrentDomain.GetAssemblies()
                                    ?.FirstOrDefault(a => a.GetTypes().Any(t => fileSystemCommandType.IsAssignableFrom(t)))
                                    ?.GetTypes()
                                    ?.FirstOrDefault(t => String.Equals(currentCommand, t.Name, StringComparison.OrdinalIgnoreCase) && fileSystemCommandType.IsAssignableFrom(t)) ?? throw new Exception(string.Empty);

                        ConstructorInfo constructorInfo = type.GetConstructors().OrderByDescending(c => c.GetParameters().Length).FirstOrDefault();

                        ParameterInfo[] parameters      = constructorInfo.GetParameters();
                        string[]        parameterValues = new string[parameters.Length];
                        string          value           = string.Empty;

                        for (int i = 0; i < parameters.Length; i++)
                        {
                            value = execContext.Evaluate(parameters[i].Name.ToLower()).Value?.ToString() ?? throw new Exception(string.Empty);
                            parameterValues[i] = Path.Combine(path, value);
                        }

                        IFileSystemCommand command = (IFileSystemCommand)Activator.CreateInstance(type, parameterValues);

                        fst.Add(command);
                    }

                    if (fst.HasCommands())
                    {
                        fst.Commit();
                    }
                }
            }

            return(null);

            //throw new Exception(string.Empty);
        }