コード例 #1
0
        /// <inheritdoc/>
        public void ArchiveServiceModel <T>(T dataToSerialize, Guid instanceId, Type type, string org, string appName, int instanceOwnerId)
        {
            string developer        = AuthenticationHelper.GetDeveloperUserName(_httpContextAccessor.HttpContext);
            string archiveDirectory = $"{_settings.GetTestdataForPartyPath(org, appName, developer)}{instanceOwnerId}/Archive/";

            if (!Directory.Exists(archiveDirectory))
            {
                Directory.CreateDirectory(archiveDirectory);
            }

            string formDataFilePath = $"{archiveDirectory}{instanceId}.xml";

            try
            {
                using (Stream stream = File.Open(formDataFilePath, FileMode.Create, FileAccess.ReadWrite))
                {
                    XmlSerializer serializer = new XmlSerializer(type);
                    serializer.Serialize(stream, dataToSerialize);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Unable to archive service model: ", ex);
            }
        }
コード例 #2
0
        /// <summary>
        /// Creates a list of form instances stored on disk for a given partyId and serviceId
        /// </summary>
        /// <param name="partyId">The partyId</param>
        /// <param name="org">The Organization code for the service owner</param>
        /// <param name="service">The service code for the current service</param>
        /// <param name="developer">The developer for the current service if any</param>
        /// <returns>The service instance list</returns>
        public List <ServiceInstance> GetFormInstances(int partyId, string org, string service, string developer = null)
        {
            List <ServiceInstance> formInstances = new List <ServiceInstance>();
            string formDataFilePath  = _settings.GetTestdataForPartyPath(org, service, developer) + partyId;
            string archiveFolderPath = $"{formDataFilePath}/Archive/";

            if (!Directory.Exists(archiveFolderPath))
            {
                Directory.CreateDirectory(archiveFolderPath);
            }

            string[] files = Directory.GetFiles(formDataFilePath);
            foreach (string file in files)
            {
                if (int.TryParse(Path.GetFileNameWithoutExtension(file), out int instanceId))
                {
                    ServiceInstance serviceInstance = new ServiceInstance()
                    {
                        ServiceInstanceID = instanceId,
                        LastChanged       = File.GetLastWriteTime(file),
                    };

                    string archiveFilePath = archiveFolderPath + "/" + serviceInstance.ServiceInstanceID + ".xml";
                    if (File.Exists(archiveFilePath))
                    {
                        serviceInstance.LastChanged = File.GetLastWriteTime(archiveFilePath);
                        serviceInstance.IsArchived  = true;
                    }

                    formInstances.Add(serviceInstance);
                }
            }

            return(formInstances);
        }
コード例 #3
0
        /// <inheritdoc/>
        public Task <Instance> InsertData <T>(T dataToSerialize, Guid instanceGuid, Type type, string org, string app, int instanceOwnerId)
        {
            string developer        = AuthenticationHelper.GetDeveloperUserName(_httpContextAccessor.HttpContext);
            string testDataForParty = _settings.GetTestdataForPartyPath(org, app, developer);
            string dataPath         = $"{testDataForParty}{instanceOwnerId}/{instanceGuid}/data";

            if (!Directory.Exists(dataPath))
            {
                Directory.CreateDirectory(dataPath);
            }

            string instanceFilePath = $"{testDataForParty}{instanceOwnerId}/{instanceGuid}/{instanceGuid}.json";

            string   dataId   = Guid.NewGuid().ToString();
            Instance instance = null;

            lock (Guard(instanceGuid))
            {
                string instanceData = File.ReadAllText(instanceFilePath);
                instance = JsonConvert.DeserializeObject <Instance>(instanceData);

                DataElement data = new DataElement
                {
                    Id                  = dataId,
                    ElementType         = FORM_ID,
                    ContentType         = "application/Xml",
                    FileName            = $"{dataId}.xml",
                    StorageUrl          = $"{app}/{instanceGuid}/data/{dataId}",
                    CreatedBy           = instanceOwnerId.ToString(),
                    CreatedDateTime     = DateTime.UtcNow,
                    LastChangedBy       = instanceOwnerId.ToString(),
                    LastChangedDateTime = DateTime.UtcNow,
                };

                instance.Data = new List <DataElement> {
                    data
                };
                string instanceDataAsString = JsonConvert.SerializeObject(instance);

                File.WriteAllText(instanceFilePath, instanceDataAsString);
            }

            string formDataFilePath = $"{dataPath}/{dataId}";

            try
            {
                using (Stream stream = File.Open(formDataFilePath, FileMode.Create, FileAccess.ReadWrite))
                {
                    XmlSerializer serializer = new XmlSerializer(type);
                    serializer.Serialize(stream, dataToSerialize);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Unable to insert data into xml file: ", ex);
            }

            return(Task.FromResult(instance));
        }
コード例 #4
0
        /// <inheritdoc/>
        public async Task <Instance> InstantiateInstance(StartServiceModel startServiceModel, object serviceModel, IServiceImplementation serviceImplementation)
        {
            Guid   instanceGuid    = Guid.NewGuid();
            string appName         = startServiceModel.Service;
            string org             = startServiceModel.Org;
            int    instanceOwnerId = startServiceModel.PartyId;
            int    userId          = startServiceModel.UserId;

            ServiceState currentTask = _workflow.GetInitialServiceState(org, appName);

            Instance instance = new Instance
            {
                Id = $"{instanceOwnerId}/{instanceGuid}",
                InstanceOwnerId = instanceOwnerId.ToString(),
                AppId           = $"{org}/{appName}",
                Org             = org,
                CreatedBy       = userId.ToString(),
                CreatedDateTime = DateTime.UtcNow,
                Process         = new Storage.Interface.Models.ProcessState()
                {
                    CurrentTask = currentTask.State.ToString(),
                    IsComplete  = false,
                },
                InstanceState = new Storage.Interface.Models.InstanceState()
                {
                    IsArchived            = false,
                    IsDeleted             = false,
                    IsMarkedForHardDelete = false,
                },
                LastChangedDateTime = DateTime.UtcNow,
                LastChangedBy       = userId.ToString(),
            };

            string developer         = AuthenticationHelper.GetDeveloperUserName(_httpContextAccessor.HttpContext);
            string testDataForParty  = $"{_settings.GetTestdataForPartyPath(org, appName, developer)}{instanceOwnerId}";
            string folderForInstance = Path.Combine(testDataForParty, instanceGuid.ToString());

            Directory.CreateDirectory(folderForInstance);
            string instanceFilePath = $"{testDataForParty}/{instanceGuid}/{instanceGuid}.json";

            File.WriteAllText(instanceFilePath, JsonConvert.SerializeObject(instance).ToString(), Encoding.UTF8);

            // Save instantiated form model
            instance = await _data.InsertData(
                serviceModel,
                instanceGuid,
                serviceImplementation.GetServiceModelType(),
                org,
                appName,
                instanceOwnerId);

            return(instance);
        }
コード例 #5
0
        /// <inheritdoc/>
        public Task <bool> DeleteAllInstanceEvents(string instanceId, string instanceOwnerId, string org, string app)
        {
            string developer        = AuthenticationHelper.GetDeveloperUserName(_httpContextAccessor.HttpContext);
            string testDataForParty = _settings.GetTestdataForPartyPath(org, app, developer);
            string folderForEvents  = $"{testDataForParty}{instanceOwnerId}/{instanceId}/events";

            if (Directory.Exists(folderForEvents))
            {
                Directory.Delete(folderForEvents, true);
            }

            return(Task.FromResult(true));
        }
コード例 #6
0
        /// <inheritdoc/>
        public Task <Instance> UpdateInstance(object dataToSerialize, string app, string org, int instanceOwnerId, Guid instanceId)
        {
            string developer         = AuthenticationHelper.GetDeveloperUserName(_httpContextAccessor.HttpContext);
            string testDataForParty  = $"{_settings.GetTestdataForPartyPath(org, app, developer)}{instanceOwnerId}";
            string folderForInstance = Path.Combine(testDataForParty, instanceId.ToString());

            if (!Directory.Exists(folderForInstance))
            {
                Directory.CreateDirectory(folderForInstance);
            }

            string   instanceFilePath = $"{testDataForParty}/{instanceId}/{instanceId}.json";
            Instance instance         = (Instance)dataToSerialize;

            File.WriteAllText(instanceFilePath, JsonConvert.SerializeObject(dataToSerialize).ToString(), Encoding.UTF8);

            return(System.Threading.Tasks.Task.FromResult(instance));
        }
コード例 #7
0
        /// <inheritdoc/>
        public ServiceState GetCurrentState(Guid instanceId, string org, string app, int instanceOwnerId)
        {
            string   developer            = AuthenticationHelper.GetDeveloperUserName(_httpContextAccessor.HttpContext);
            string   serviceStatePath     = $"{_settings.GetTestdataForPartyPath(org, app, developer)}{instanceOwnerId}/{instanceId}/{instanceId}.json";
            string   currentStateAsString = File.ReadAllText(serviceStatePath, Encoding.UTF8);
            Instance instance             = JsonConvert.DeserializeObject <Instance>(currentStateAsString);

            Enum.TryParse <WorkflowStep>(instance.Process.CurrentTask.ProcessElementId, out WorkflowStep current);
            return(new ServiceState
            {
                State = current,
            });
        }
コード例 #8
0
        /// <summary>
        /// Creates a list of form instances stored on disk for a given partyId and serviceId
        /// </summary>
        /// <param name="partyId">The partyId</param>
        /// <param name="org">The Organization code for the service owner</param>
        /// <param name="service">The service code for the current service</param>
        /// <param name="developer">The developer for the current service if any</param>
        /// <returns>The service instance list</returns>
        public List <ServiceInstance> GetFormInstances(int partyId, string org, string service, string developer = null)
        {
            List <ServiceInstance> formInstances = new List <ServiceInstance>();
            string instancesPath     = $"{_settings.GetTestdataForPartyPath(org, service, developer)}{partyId}";
            string archiveFolderPath = $"{instancesPath}/Archive/";

            if (!Directory.Exists(archiveFolderPath))
            {
                Directory.CreateDirectory(archiveFolderPath);
            }

            string[] files = Directory.GetDirectories(instancesPath);
            foreach (string file in files)
            {
                string instance = new DirectoryInfo(file).Name;

                if (Guid.TryParse(instance, out Guid instanceId))
                {
                    ServiceInstance serviceInstance = new ServiceInstance()
                    {
                        ServiceInstanceID = instanceId,
                        LastChanged       = File.GetLastWriteTime(file),
                    };

                    string archiveFilePath = archiveFolderPath + "/" + serviceInstance.ServiceInstanceID + ".xml";
                    if (File.Exists(archiveFilePath))
                    {
                        serviceInstance.LastChanged = File.GetLastWriteTime(archiveFilePath);
                        serviceInstance.IsArchived  = true;
                    }

                    formInstances.Add(serviceInstance);
                }
            }

            return(formInstances);
        }
コード例 #9
0
ファイル: FormStudioSI.cs プロジェクト: maritg/altinn-studio
        /// <inheritdoc />
        public object GetPrefill(string org, string appName, Type type, int instanceOwnerId, string prefillkey)
        {
            string developer       = AuthenticationHelper.GetDeveloperUserName(_httpContextAccessor.HttpContext);
            string prefillFilePath = $"{_settings.GetTestdataForPartyPath(org, appName, developer)}{instanceOwnerId}/prefill/{prefillkey}.xml";

            try
            {
                using (Stream stream = File.Open(prefillFilePath, FileMode.Open, FileAccess.Read))
                {
                    XmlSerializer serializer = new XmlSerializer(type);
                    return(serializer.Deserialize(stream));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Error occured when trying to fetch prefil: ", ex);
            }

            return(null);
        }
コード例 #10
0
        public FileResult GetFormModel(string org, string service, string developer, int partyId, Guid instanceId)
        {
            string testDataForParty = _settings.GetTestdataForPartyPath(org, service, developer);
            string formDataFilePath = $"{testDataForParty}{partyId}/{instanceId}/data/{instanceId}.xml";

            return(File(_execution.GetFileStream(formDataFilePath), "application/xml", $"{instanceId}.xml"));
        }
コード例 #11
0
        public FileResult GetFormModel(string org, string service, string developer, int partyId, int formID)
        {
            string formDataFilePath = $"{_settings.GetTestdataForPartyPath(org, service, developer)}{partyId}/{formID}.xml";

            return(File(_execution.GetFileStream(formDataFilePath), "application/xml", $"{formID}.xml"));
        }