using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Neo.Afx.Services.Documents.Entities;
namespace Neo.Afx.Services.Documents
{
///
/// Document logic for generating templates, database actions ect
///
public partial class DocumentUtilities
{
readonly DocumentsDatabase _database = new DocumentsDatabase();
#region Upload
///
/// Uploads the specified document.
///
/// The user ID.
/// The NT account name of the user.
/// The document ID.
/// The entity ID.
/// The item ID.
/// The document type ID.
/// The document store location ID.
/// Name of the file.
/// Type of the content.
/// The file contents.
///
public Document Upload(long userID, string userName, long documentID, int entityID, long itemID, short documentTypeID, long documentStoreLocationID, string fileName, string contentType, byte[] fileContents)
{
var name = fileName.Contains("\\") ? fileName.Substring(fileName.LastIndexOf("\\") + 1) : fileName;
return _database.UploadFile(userID, userName, documentID, entityID, itemID, documentTypeID, documentStoreLocationID, name, contentType, fileContents);
}
#endregion
#region Copy
///
/// Copies the specified document.
///
/// The user ID.
/// The source document I ds.
/// The target entity ID.
/// The target item ID.
///
public KeyValuePair Copy(long userID, string sourceDocumentIDs, int targetEntityID, long targetItemID)
{
return _database.CopyDocuments(userID, sourceDocumentIDs, targetEntityID, targetItemID);
}
#endregion
#region Get
///
/// Gets the specified document.
///
/// The document ID.
///
public Document Get(long documentID)
{
return _database.GetDocument(documentID);
}
#endregion
#region Get
///
/// Gets the specified documents.
///
/// The entity ID.
/// The item ID.
///
public IEnumerable Get(int entityID, long itemID)
{
return _database.GetDocuments(entityID, itemID);
}
#endregion
#region Create
///
/// Creates the specified user ID.
///
/// The user ID.
/// The NT account of the user.
/// The template document ID.
/// The entity ID.
/// The item ID.
/// The document type ID.
/// The document store location ID.
///
public Document Create(long userID, string userName, long templateDocumentID, int entityID, long itemID, short documentTypeID, long documentStoreLocationID)
{
var docStoreData = _database.GetDocumentStores(templateDocumentID);
if(docStoreData == null)
{
throw new Exception("System could not find the document template.");
}
var docStore = docStoreData.FirstOrDefault();
var template = _database.GetDocumentTemplateData(docStore.Document.EntityID, itemID);
if(template == null)
{
throw new Exception("System could not retrieve the data needed to populate the document template.");
}
var templateValuesDic = template.Descendants().Select(d => new KeyValuePair(d.Name.LocalName, d.Value)).ToList();
if(templateValuesDic.Count == 0)
{
throw new Exception("System failed to convert the data necessary to fill the document template.");
}
var templateBytes = FillTemplatedDocument(docStore.ContentBLOB, templateValuesDic);
if(templateBytes == null)
{
throw new Exception("Document template failed to generate.");
}
return _database.UploadFile(userID,
userName,
-1,
entityID,
itemID,
documentTypeID,
documentStoreLocationID,
docStore.Name,
docStore.MimeType,
templateBytes);
}
#endregion
#region UpdateItemID
///
/// Updates the item ID.
///
/// The user ID.
/// The entity ID.
/// The old item ID.
/// The new item ID.
public void UpdateItemID(long userID, long entityID, long oldItemID, long newItemID)
{
_database.UpdateDocumentItemID(userID, entityID, oldItemID, newItemID);
}
#endregion
#region Delete
///
/// Deletes the specified document.
///
/// The user ID.
/// The document ID.
///
public string Delete(long userID, long documentID)
{
return _database.Delete(userID, documentID);
}
#endregion
#region HexStringToByteArray
///
/// Converts a file hex encoded string to a file byte array
///
/// The s.
///
public static byte[] HexStringToByteArray(string s)
{
var buffer = new byte[s.Length / 2];
for(var i = 0; i < buffer.Length; i++)
{
buffer[i] = (byte)Int32.Parse(s.Substring(2 * i, 2), System.Globalization.NumberStyles.HexNumber);
}
return buffer;
}
#endregion
#region ByteArrayToHexString
///
/// Converts a file byte array to a Hex encoded string.
///
/// The buffer.
///
public static string ByteArrayToHexString(byte[] buffer)
{
if(buffer == null || buffer.Length <= 0)
{
return null;
}
const string digits = "0123456789ABCDEF";
byte ch = 0x00;
var i = 0;
var sb = new StringBuilder(buffer.Length * 2);
while(i < buffer.Length)
{
ch = (byte)(buffer[i] & 0xF0); // strip off high nibble
ch = (byte)(ch >> 4); // shift the bits down
ch = (byte)(ch & 0x0F); // must do this if high order bit is on!
sb.Append(digits[(int)ch]); // convert the nibble to a String Character
ch = (byte)(buffer[i] & 0x0F); // strip off low nibble
sb.Append(digits[(int)ch]); // convert the nibble to a String Character
i++;
}
return sb.ToString();
}
#endregion
}
}