/// <summary>
/// Helper class representing an attachment to a Forms record.
/// Don't construct this directly; use the GrvFormsRecord class methods.
/// </summary>
public class GrvFormsRecordAttachment
{
DataRow _attachmentRow;
DataRow _contentsRow;
// Construct from an existing row
internal GrvFormsRecordAttachment(DataRow attachmentRow)
{
_attachmentRow = attachmentRow;
DataRelation relationForContent = _attachmentRow.Table.ChildRelations[0];
DataRow[] contentRows = _attachmentRow.GetChildRows(relationForContent);
_contentsRow = contentRows[0];
}
// Construct a new attachment from name and content
internal GrvFormsRecordAttachment(DataRow recordRow, string name, byte[] content)
{
DataRelation relationForAttachProp = recordRow.Table.ChildRelations[0];
DataRow[] attachPropRows = recordRow.GetChildRows(relationForAttachProp);
DataRow attachPropRow;
if (attachPropRows.Length == 0)
{
DataTable propertiesTable = relationForAttachProp.ChildTable;
attachPropRow = propertiesTable.NewRow();
propertiesTable.Rows.Add(attachPropRow);
attachPropRow.SetParentRow(recordRow);
}
else
{
attachPropRow = attachPropRows[0];
}
// Create the row in the FileAttachment table
// Note: the attachment's name is the FullName field.
// in Groove V3.1 this was two properties, Name (filename with extension) and DisplayName (filename without extension)
DataRelation relationForFileAttach = attachPropRow.Table.ChildRelations[0];
DataTable fileAttachmentTable = relationForFileAttach.ChildTable;
_attachmentRow = fileAttachmentTable.NewRow();
_attachmentRow["FullName"] = name;
_attachmentRow["Type"] = "File";
_attachmentRow["Size"] = content.Length;
_attachmentRow.SetParentRow(attachPropRow);
fileAttachmentTable.Rows.Add(_attachmentRow);
// Create the row in the Contents table
// and set the binary contents of the attachment
DataRelation relationForContent = _attachmentRow.Table.ChildRelations[0];
DataTable contentsTable = relationForContent.ChildTable;
_contentsRow = contentsTable.NewRow();
_contentsRow["Base64"] = content;
_contentsRow.SetParentRow(_attachmentRow);
contentsTable.Rows.Add(_contentsRow);
}
// Delete this attachment.
internal void Delete()
{
_contentsRow.Delete();
_contentsRow = null;
_attachmentRow.Delete();
_attachmentRow = null;
}
/// <summary>
/// Filename of the attachment. Read-write.
/// </summary>
public string Name
{
get
{
return (string)_attachmentRow["FullName"];
}
set
{
_attachmentRow.BeginEdit();
_attachmentRow["FullName"] = value;
_attachmentRow.EndEdit();
_attachmentRow.AcceptChanges();
}
}
/// <summary>
/// Size of the attachment, in bytes. Read-only.
/// </summary>
public long Size
{
get
{
return (long)_attachmentRow["Size"];
}
}
/// <summary>
/// The attachment contents. Read-write.
/// </summary>
public byte[] Content
{
get
{
byte[] data = (byte[])_contentsRow["Base64"];
return data;
}
set
{
_attachmentRow.BeginEdit();
_attachmentRow["Size"] = value.Length;
_attachmentRow.EndEdit();
_attachmentRow.AcceptChanges();
_contentsRow.BeginEdit();
_contentsRow["Base64"] = value;
_contentsRow.EndEdit();
_contentsRow.AcceptChanges();
}
}
}