I have to go through files with metadata and I am unhappy with the iteration and how I have build it due to the fact, that I have a modeltree with lists. So basically I iterate through filesOfFolder
and pick each file and then I am adding the metadata. In the else
I am checking, if the fileName has the same beginning then the fileNameBefore, because then it is a child of the last file, and it needs a different documentOrderNumber
.
The modeltree is like and it cannot get changed:
Mainmodel
Documents (public MainDocumentModel[] and AttDocumentModel[])
MainDocumentModel
FileContent
AttDocumentModel
FileContent
So as of this tree I did it so and I am totally unhappy with that:
string fileNameBefore = "";
model.Documentlist = new Documents
{
Attdocument = new AttDocumentModel[filesOfFolder.Count() + 1]
};
int i = 0;
int j = 0;
int k = 0;
string mainPath = System.Web.HttpContext.Current.ApplicationInstance.Server.MapPath("~/App_Data/" + model.DocId + "/");
string directoryAttPath = mainPath + "Att/";
Directory.CreateDirectory(directoryAttPath);
foreach (var file in filesOfFolder)
{
if (file.f_name.Contains("Maindocument") || file.f_name.Contains("Wrapdocument"))
{
FileContent fContent = new FileContent();
fContent.FileType = "PDF";
fContent.FileName = "Maindocument.pdf";
MainDocumentModel mainDoc = new MainDocumentModel();
mainDoc.MainDoc = new List<FileContent>();
mainDoc.Message = "";
mainDoc.Status = true;
mainDoc.MainDoc.Add(fContent);
model.Documentlist.Maindocument = new MainDocumentModel[1];
model.Documentlist.Maindocument[0] = mainDoc;
string mainDocumentPath = mainPath + fContent.FileName;
System.IO.File.WriteAllBytes(mainDocumentPath, file.f_file);
}
else
{
if (!(fileNameBefore.Split(' ')[0] == file.f_name.Split(' ')[0]))
{
j = 0;
i++;
}
else
{
j++;
}
FileContent attachment = new FileContent();
string documentOrderNumber = GenerateOrderNumberForRis(i, j, fileNameBefore);
attachment.FileName = documentOrderNumber + ".pdf";
attachment.FileType = "PDF";
AttDocumentModel attDoc = new AttDocumentModel();
attDoc.AttDoc = new List<FileContent>();
attDoc.Message = "";
attDoc.Status = true;
attDoc.ShowName = file.f_subject;
attDoc.AttDoc.Add(attachment);
model.Documentlist.Attdocument[k] = attDoc;
System.IO.File.WriteAllBytes(directoryAttPath + documentOrderNumber + ".pdf", file.f_file);
k++;
fileNameBefore = file.f_name;
}
}
Here are the models:
public class MainModel
{
....
[XmlIgnore]
public MainDocumentModel Maindocument { get; set; }
[XmlIgnore]
public AttDocumentModel Attdocument { get; set; }
[XmlElement(ElementName = "documentlist")]
public Documents Documentlist { get; set; }
}
[XmlRoot(elementName: "maindocument")]
public class MainDocumentModel
{
[XmlIgnore]
public String Message { get; set; }
[XmlIgnore]
public Boolean Status { get; set; }
[XmlElement("content")]
public List<FileContent> MainDoc { get; set; }
}
[XmlRoot(elementName: "attacheddocument")]
public class AttDocumentModel
{
[XmlIgnore]
public String Message { get; set; }
[XmlIgnore]
public Boolean Status { get; set; }
[XmlElement("showname")]
public string ShowName { get; set; }
[XmlElement("content")]
public List<FileContent> AttDoc { get; set; }
}
public class FileContent
{
[XmlElement("dataformat")]
public string FileType { get; set; }
[XmlElement("uri")]
public string FileName { get; set; }
}
fileNameBefore = file.f_name;
\$\endgroup\$