Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: f5efcd767a
Fetching contributors…

Cannot retrieve contributors at this time

363 lines (307 sloc) 12.049 kb
using System;
using System.Data;
using System.Linq;
using System.Collections.Generic;
using System.Reflection;
using System.IO;
using System.Text;
using Gtk;
using LearningRegistry;
using LearningRegistry.RDDD;
using LumenWorks.Framework.IO.Csv;
public partial class CsvToLrWindow : Gtk.Window
{
private List<Label> _missingFields;
private List<string> _rawRowDataList = new List<string>();
private Dictionary<string, List<string>> _rowData;
private string[] _columns;
List<Type> nestedTypes = new List<Type>()
{
typeof(lr_identity),
typeof(lr_TOS),
typeof(lr_digital_signature)
};
public CsvToLrWindow () :
base(Gtk.WindowType.Toplevel)
{
this.Build ();
}
public void PopulateFromCsv(string path)
{
loadFromCsv(path);
FieldInfo[] infos = typeof(lr_document).GetFields();
foreach(var info in infos)
{
var attrs = info.GetCustomAttributes(typeof(RequiredField), false);
if(attrs.Length > 0)
{
if(((RequiredField)attrs[0]).Immutable)
continue;
}
if(nestedTypes.Contains(info.FieldType))
{
//this is done internally
if(info.FieldType.Equals(typeof(lr_digital_signature)))
continue;
//Nesting only goes one level, which makes this work
foreach(var subInfo in info.FieldType.GetFields())
{
string name = String.Join(".", info.Name, subInfo.Name);
if(subInfo.GetCustomAttributes(typeof(LearningRegistry.RequiredField), true).Length > 0)
name = name.Insert(0, "*");
MapRowsContainer.Add(new CsvToLrMapRow(_columns, name));
}
}
else
{
string name = info.Name;
if(info.GetCustomAttributes(typeof(LearningRegistry.RequiredField), true).Length > 0)
name = name.Insert(0, "*");
CsvToLrMapRow row = new CsvToLrMapRow(_columns, name);
MapRowsContainer.Add(row);
}
}
}
private bool ValidateRequiredFields()
{
Gdk.Color black = new Gdk.Color();
Gdk.Color.Parse("black", ref black);
List<Label> missingFields = new List<Label>();
//Validate the rows
foreach(CsvToLrMapRow row in MapRowsContainer)
{
if (row.Key.Contains("*") && (
(row.DropDownValue == null && (!row.IsConstant || !row.HasConstantValues)) ||
(row.IsConstant && String.IsNullOrEmpty(row.ConstantValue))
))
{
HBox container = (HBox)row.Children[0];
missingFields.Add((Label)container.Children[0]);
}
}
missingFields.AddRange(this.SignatureInformationWidget.GetMissingFields());
missingFields.AddRange(this.ServerInfoWidget.GetMissingFields());
if(this._missingFields != null)
{
foreach(var lbl in _missingFields)
{
if(missingFields.Where( x => x.LabelProp == lbl.LabelProp ).Count() == 0)
lbl.ModifyFg(StateType.Normal, black);
}
}
_missingFields = missingFields;
return _missingFields.Count == 0;
}
protected void ShowMissingFields()
{
Gdk.Color red = new Gdk.Color();
Gdk.Color.Parse("red", ref red);
foreach(var field in _missingFields)
field.ModifyFg(StateType.Normal, red);
PublishGUI.Helper.CreateNotficationWindow(PublishGUI.Helper.MSG_MISSING_FIELDS);
}
protected void PublishDocuments(object sender, EventArgs e)
{
bool validated = ValidateRequiredFields();
if(validated)
{
lr_Envelope envelope = buildEnvelopeFromMapping();
LRClient client = new LRClient(this.ServerInfoWidget.NodeUrl);
if (!String.IsNullOrEmpty(this.ServerInfoWidget.HttpUsername))
{
client.Username = ServerInfoWidget.HttpUsername;
client.Password = ServerInfoWidget.HttpPassword;
}
PublishResponse res = client.Publish(envelope);
buildAndShowNotification(res);
}
else
ShowMissingFields();
}
private void buildAndShowNotification(PublishResponse response)
{
StringBuilder sb = new StringBuilder();
bool success = response.OK;
if(!response.OK)
sb.AppendLine("General response error: " + response.error);
foreach(DocPublishResult docResult in response.document_results)
{
if(!docResult.OK)
{
success = false;
sb.AppendLine("Doc error: " + docResult.error);
}
}
if(success)
{
PublishGUI.Helper.SavePublishHistory(this.ServerInfoWidget.NodeUrl, response);
sb.AppendLine("Documents were successfully published!");
sb.AppendLine("To view the docs online, or for further information from the responses,"
+"you may go to History -> Show All in the main menu.");
}
else
sb.AppendLine("One or more errors occurred while publishing your documents. See above for detailed error report.");
PublishGUI.Helper.CreateNotficationWindow(sb.ToString());
}
private lr_Envelope buildEnvelopeFromMapping()
{
var sigInfo = this.SignatureInformationWidget;
PgpSigner signer = null;
bool needToSign = false;
if(sigInfo.SignatureType == PublishGUI.SignatureType.LR_PGP)
{
signer = new PgpSigner(sigInfo.PgpPublicKeyLocations,
sigInfo.PgpKeyringLocation,
sigInfo.PgpSecretKeyUsername,
sigInfo.PgpSecretKeyPassphrase);
needToSign = true;
}
Dictionary<string, string> map = new Dictionary<string, string>();
FieldInfo[] infos = typeof(lr_document).GetFields();
int j = 0;
for (int i = 0; i < MapRowsContainer.Children.Length; i++)
{
var info = infos[j];
var mapRow = (CsvToLrMapRow)MapRowsContainer.Children[i];
var customAttributes = info.GetCustomAttributes(typeof(RequiredField), false);
if (customAttributes.Length > 0)
{
RequiredField attr = (RequiredField)customAttributes[0];
if (attr.Immutable)
{
i--; //It was never added to the RowContainer, so we need to examine mapRow again with the next property
j++; //We examined the property, increment this index
continue;
}
}
if (nestedTypes.Contains(info.FieldType))
{
//Never added to RowContainer, decrement i and increment j
if (info.FieldType == typeof(lr_digital_signature))
{
i--;
j++;
continue;
}
foreach (var subInfo in info.FieldType.GetFields())
{
mapRow = (CsvToLrMapRow)MapRowsContainer.Children[i++];
string key = String.Join(".", info.Name, subInfo.Name);
map[key] = mapRow.DropDownValue;
}
if(info.FieldType.GetFields().Length > 0)
i--; //avoid double incrementing from loop iterator statment
j++;
}
else
map[infos[j++].Name] = mapRow.DropDownValue;
}
//Create the docs from the map and the dataDict
lr_Envelope envelope = new lr_Envelope();
for (int i = 0; i < _rawRowDataList.Count; i++)
{
lr_document doc = new lr_document();
int rowIndex = 0;
foreach (var info in infos)
{
CsvToLrMapRow currentRow = (CsvToLrMapRow)MapRowsContainer.Children[rowIndex++];
var customAttributes = info.GetCustomAttributes(typeof(RequiredField), false);
if(customAttributes.Length > 0 )
{
RequiredField attr = (RequiredField)customAttributes[0];
if (attr.Immutable)
{
rowIndex--;
continue;
}
}
if (!nestedTypes.Contains(info.FieldType))
{
string val;
if (currentRow.IsConstant)
val = currentRow.ConstantValue;
else if (currentRow.IsSerializeToRow)
val = _rawRowDataList[i];
else if (currentRow.HasConstantValues)
val = currentRow.DropDownValue;
else if (currentRow.DropDownValue == null)
continue; //Nothing to map to or assign if they did not choose a value from the dropdown
else
val = _rowData[map[info.Name]][i];
if (info.FieldType == typeof(List<string>))
{
List<string> vals = val.Split(',').ToList<string>();
info.SetValue(doc, vals);
}
else
info.SetValue(doc, Convert.ChangeType(val, info.FieldType));
}
else //This is a nested object property
{
var subFields = info.FieldType.GetFields();
object currentObj = info.GetValue(doc);
if (currentObj == null)
currentObj = new object();
rowIndex--;
if (typeof(lr_digital_signature).Equals(info.FieldType))
continue;
foreach (var subField in subFields)
{
var rowToAdd = MapRowsContainer.Children.Cast<CsvToLrMapRow>()
.Where(x =>
x.Key.Replace("*", "").Split('.')[0] == info.Name &&
x.Key.Replace("*", "").Split('.')[1] == subField.Name)
.ToList()[0];
rowIndex++; //Used another row from subInfo, need to update the index
string val;
if (rowToAdd.IsConstant)
val = rowToAdd.ConstantValue;
else if (rowToAdd.IsSerializeToRow)
val = _rawRowDataList[i];
else if (rowToAdd.HasConstantValues)
val = rowToAdd.DropDownValue;
else if (rowToAdd.DropDownValue == null)
continue;
else
val = _rowData[map[String.Join(".", info.Name, subField.Name)]][i];
if (subField.FieldType == typeof(List<string>))
{
List<string> vals = val.Split(',').ToList<string>();
subField.SetValue(currentObj, vals);
}
else
subField.SetValue(currentObj, val);
}
}
}
if(needToSign)
doc = signer.Sign(doc);
envelope.documents.Add(doc);
}
return envelope;
}
private void loadFromCsv(string path)
{
using (CsvReader csv = new CsvReader(new StreamReader(path), true))
{
_columns = csv.GetFieldHeaders();
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
Dictionary<string, List<string>> data = new Dictionary<string, List<string>>();
List<string> rawRowDataList = new List<string>();
while (csv.ReadNextRecord())
{
var rowDict = new Dictionary<string, string>();
for (int i = 0; i < csv.FieldCount; i++)
{
if (!data.ContainsKey(_columns[i]))
data[_columns[i]] = new List<string>();
data[_columns[i]].Add(csv[i]);
rowDict[_columns[i]] = csv[i];
}
rawRowDataList.Add(serializer.Serialize(rowDict));
}
_rawRowDataList = rawRowDataList;
_rowData = data;
}
}
}
Jump to Line
Something went wrong with that request. Please try again.