This is a simple task that should be set up on a schedule to monitor a network location for XML files. Once found, it will look for an expected format and read them, before creating the job.
It will try to use a Job Type attribute to match a job template but if not found, will use a blank job.
The code and some sample XML are included below.
<Job JobName="Sample Job" JobNumber="D1234" Description="This is a sample job" JobType="Design" />
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Contracts; using Contracts.Data; using Contracts.Databases; using Contracts.Responses; namespace TaskExample { /// <summary> /// Simple task to monitor a network location and look for xml files that define job details /// </summary> public class CreateJobTask : SynergyServerScripting.Task { private const string INPUT_NETWORK_LOCATION = "Network Location for Config Files"; /// <summary> /// Constructor - always pass in a GUID /// </summary> public CreateJobTask() : base(Guid.Parse("{2AE7B699-17C8-48CC-B3D3-0E9957DD9AB1}")) { } /// <summary> /// Get a description /// </summary> /// <returns></returns> public override string GetDescription() { return "Creates a Job based on information written to XML files at a network location"; } /// <summary> /// Lock any entities - not relevant /// </summary> /// <param name="locks"></param> /// <param name="db"></param> public override void GetEntitiesToLock(IEntityLocks locks, IDatabase db) { return; } /// <summary> /// Get the name /// </summary> /// <returns></returns> public override string GetName() { return "Job Builder"; } /// <summary> /// Run the task /// </summary> /// <param name="user_id">the id of the user that is running the task, set at schedule time</param> /// <param name="inputs">the input values</param> /// <param name="db">a database connection</param> /// <returns></returns> protected override OperationResponse PerformTask(EntityID user_id, Dictionary<string, ScriptInput> inputs, IDatabase db) { // check for inputs if (!inputs.ContainsKey(INPUT_NETWORK_LOCATION)) return OperationResponse.Error(-1, "Network location not set!"); var input = inputs[INPUT_NETWORK_LOCATION] as PrimitiveScriptInput; if (input == null) return OperationResponse.Error(-1, "Network location not set!"); string network_location = input.type_value; if (string.IsNullOrWhiteSpace(network_location)) return OperationResponse.Error(-1, "Network location not set!"); if (!System.IO.Directory.Exists(network_location)) return OperationResponse.Error(-1, "Network location set but is not valid!"); // get our project number attribute and job type attribute var job_number_attrib = SynergyServerScripting.Entities.Attribute.FindAttributeByName("JobNumber", db).FirstOrDefault(); if (job_number_attrib == null) return OperationResponse.Error(-1, "Unable to find expected ProjectNumber attribute"); var job_type_attrib = SynergyServerScripting.Entities.Attribute.FindAttributeByName("JobType", db).FirstOrDefault(); if (job_type_attrib == null) return OperationResponse.Error(-1, "Unable to find expected ProjectNumber attribute"); var files = System.IO.Directory.GetFiles(network_location, "*.xml"); foreach (var f in files) { // read from the XML file var xml = System.Xml.Linq.XDocument.Load(f); var job_info = xml.Descendants("Job").Select(x => new { Name = x.Attribute("JobName").Value.ToString(), JobNumber = x.Attribute("JobNumber").Value.ToString(), Description = x.Attribute("Description").Value.ToString(), JobType = x.Attribute("JobType").Value.ToString(), }).FirstOrDefault(); if (job_info == null) continue; // does it already_exist? var found_id = SynergyServerScripting.Entities.Project.GetProjectID(job_info.Name, null, db); if (found_id != null) continue; // find our template, based on the job type AttributeValueCollection template_attribs = new AttributeValueCollection(); template_attribs[job_type_attrib.id] = new AttributeValue(job_info.JobType); var template = SynergyServerScripting.Entities.Project.MatchTemplate(template_attribs, user_id, db); EntityID created_id = null; // found a template, so use that if (template != null) { created_id = SynergyServerScripting.Entities.Project.ReplicateProject(job_info.Name, job_info.Description, 1, template.project_id, null, user_id, db, null); } else { // didn't find a template - use an empty job created_id = SynergyServerScripting.Entities.Project.CreateProject(SynergyServerScripting.Entities.Server.GetServerID(), job_info.Name, job_info.Description, null, null, user_id, db); } // attach our job number attribute SynergyServerScripting.Entities.Project.AttachAttributeToProject(created_id, job_number_attrib.id, false, false, db); var copied = job_number_attrib.CopyWithNewEmptyValue(); copied.value = new AttributeValue(job_info.JobNumber); SynergyServerScripting.Entities.Project.SetProjectAttributeValue(created_id, copied, db); // attach our job type attribute SynergyServerScripting.Entities.Project.AttachAttributeToProject(created_id, job_type_attrib.id, false, false, db); copied = job_number_attrib.CopyWithNewEmptyValue(); copied.value = new AttributeValue(job_info.JobType); SynergyServerScripting.Entities.Project.SetProjectAttributeValue(created_id, copied, db); // apply permissions for our current user RoleAccessType access = RoleAccess.GetFullAccess(); SynergyServerScripting.Entities.Project.AssignUserAccess(created_id, user_id, access, user_id, db); // index the job so it is discoverable - index has a number of different parameters // in this case, we want it to index the job - but not sub folders or sub jobs SynergyServerScripting.Entities.Project.Index(created_id, false, true, false, db); } return new OperationResponse(); } /// <summary> /// Get all the inputs we need /// </summary> /// <returns></returns> public override IEnumerable<ScriptInput> GetConfigurableInputs() { return new List<ScriptInput>() { new PrimitiveScriptInput(INPUT_NETWORK_LOCATION, "The location where XML files will be stored", false, PrimitiveScriptInputType.String) }; } } }
This language is not supported or no code example is available.