Having issues with my content part's driver not receiving information

Topics: Troubleshooting, Writing modules
Aug 25, 2011 at 4:56 PM
Edited Aug 25, 2011 at 5:12 PM

Hello community!
I'm working on a content part right now that is intended to store XML and XSLT code to display and use to query a server. I am still at the point of writing an editor template to enter the code from in the Orchard Dashboard, but I am stuck because nothing I enter into my fields makes it's way to the driver methods I have here. I'll explain how it works. I had initially tried using a model that simply contained 2 strings, one to hold the XML code, and another fro XSLT. But I believe/assume SQL can't store such large strings, as every time I tried to save, it would abort the transaction. So then I opted for a model with 4 fields, the original two, and two more strings for and XML path and an XSLT path. My plan was to take the code form the user, save it to a file, store the file paths in the pathXML and pathXSLT variables, and then clear the code from codeXML and codeXSLT, THEN updating the database. It was the only thing I could come up with.

Now the issue is, when I hit "Save" or "Publish now", the methods in the driver don't actually receive the code I put into the fields in the editor screen in Orchard! I don't think I changed anything that would cause it to do that either... So now my module saves "" to text files, and it does properly save the file path, then it loads the "" back into the model and sends it off in a shape to be displayed. This effectively loses what I typed in, and when I debug line by line, the code I enter never actually ever sees the driver.

Here are the edit methods from my driver, hopefully they can illustrate better than my explanation does:

 

 

                //GET
		protected override DriverResult Editor(SynergyServicePart part, dynamic shapeHelper)
		{
			if (!((part.pathXML == null) || (part.pathXSLT == null)))
			{
				part.codeXML = System.IO.File.ReadAllText(part.pathXML);
				part.codeXSLT = System.IO.File.ReadAllText(part.pathXSLT);
			}

			return ContentShape("Parts_SynergyService_Edit",
								() => shapeHelper.EditorTemplate(TemplateName: "Parts/SynergyService", Model: part, Prefix: Prefix));
		}
		//POST
		protected override DriverResult Editor(SynergyServicePart part, IUpdateModel updater, dynamic shapeHelper)
		{
			if ((part.pathXML == null) || (part.pathXSLT == null))
			{
				// Logic to determine if log directory exits and create if it doesn't
				string synXMLPath = ConfigurationManager.AppSettings["SynergyXMLPath"];
				if (!Directory.Exists(synXMLPath))
					Directory.CreateDirectory(synXMLPath);
				string synXSLTPath = ConfigurationManager.AppSettings["SynergyXSLTPath"];
				if (!Directory.Exists(synXSLTPath))
					Directory.CreateDirectory(synXSLTPath);

				//Make unique file name
				string dateNow = String.Format("{0:yyyyMMddHHmmssff}", DateTime.Now);
				synXMLPath += @"\SynergyServiceXML_" + dateNow + ".txt";
				synXSLTPath += @"\SynergyServiceXSLT_" + dateNow + ".txt";

				//Set path with new file name
				part.pathXML = synXMLPath;
				part.pathXSLT = synXSLTPath;
			}

			//Write code to file
			System.IO.File.WriteAllText(part.pathXML, part.codeXML);
			System.IO.File.WriteAllText(part.pathXSLT, part.codeXSLT);

			//Clear code fields, as with XML files it is often too big for SQL, and will error trying to save it
			part.codeXML = "See file";
			part.codeXSLT = "See file";


			//Update database
			updater.TryUpdateModel(part, Prefix, null, null);


			//Reload code from file for displaying
			part.codeXML = System.IO.File.ReadAllText(part.pathXML);
			part.codeXSLT = System.IO.File.ReadAllText(part.pathXSLT);

			return Editor(part, shapeHelper);
		}

 

 

Can anyone see what I may be doing wrong? (Just to clarify, when I hit publish on a new entry and it breaks on the first  line of the post method, the part that the method receives is all null.

Coordinator
Aug 25, 2011 at 7:17 PM

Your driver saves to the file before it updates the model. So yes, of course it's writing empty files :)

Aug 25, 2011 at 7:27 PM

Ohhh, I thought updating the model was saving it to the database, which was where I had problems with truncating and stuff. However, I relized I coould just open Orchard's sbf and change my varchar limit from 255 to 4000 (which is the highest it will let me put for some reason). This way I can avoid saving to file altogether. Thank you though, content parts confuse me quite a bit coming from MVC 3.

Coordinator
Aug 25, 2011 at 7:50 PM

No, updating the model will take the form data and attempt to apply it on the provided part. The part may or may not persist itself to the database later when the transaction is committed (in your case it won't).

4000 is a nHibernate convention that means unlimited.

Aug 25, 2011 at 7:51 PM

That makes this content part 10 times easier. Thank you!

Aug 25, 2011 at 7:53 PM
Edited Aug 25, 2011 at 7:57 PM

Quick question, is there a way to initially set the nvarchar limit to 4000 in the migrations file, in order to avoid having to manually edit the table afterwards?

 

Edit: Here's what it looks like now (taken almost directly from an example somewhere in the Orchard docs)

		SchemaBuilder.CreateTable("SynergyServiceRecord", table => table
			.ContentPartRecord()
			.Column("codeXML", DbType.String)
			.Column("codeXSLT", DbType.String)
		);

Coordinator
Aug 25, 2011 at 8:10 PM

Of course: here is how the Body part does it:

.Column<string>("Text", column => column.Unlimited())