public bool Add(FileItDocument doc)
        {
            ExceptionHelper.LogDebug("In Add Cabinet Document...");
            var           result = false;
            StringBuilder sql    = new StringBuilder();

            int fileSize = 501;

            //add the document to the database
            sql.Append("INSERT INTO " + CABINETID + "_IMAGES ");
            sql.Append("(");
            sql.Append("FILENAME, DELETED, PAGENUMBER, INDEXEDON, EXTENSION, ARCHIVED, FILESIZE, PUBLIC, VERSION_NO, VERSIONID, USERID, STATIONID, BATCHNO");
            doc.Indexes.ToList().ForEach(i =>
            {
                sql.Append(", INDEX" + i.Key.ToString());
            });
            sql.Append(") VALUES (");
            sql.Append("'" + doc.FILENAME.Replace("'", "''") + "',");
            sql.Append("'" + (doc.DELETED ? "Y" : "N") + "',");
            sql.Append("'" + "1" + "',");
            sql.Append("'" + doc.INDEXEDON.ToString("MM/dd/yyyy") + "',");
            sql.Append("'" + doc.EXTENSION.ToString().Replace("'", "''") + "',");
            sql.Append("'" + doc.ARCHIVED.ToString().Replace("'", "''") + "',");
            sql.Append("'" + fileSize.ToString() + "',");
            sql.Append("'" + (doc.PUBLIC ? "Y" : "N") + "',");
            sql.Append("'" + doc.VERSION_NO.ToString().Replace("'", "''") + "',");
            sql.Append("'" + doc.VERSIONID.ToString().Replace("'", "''") + "',");
            sql.Append("'" + doc.USERID.ToString().Replace("'", "''") + "',");
            sql.Append("'" + doc.STATIONID.ToString().Replace("'", "''") + "',");
            sql.Append("'" + doc.BATCHNO.ToString().Replace("'", "''") + "'");
            ExceptionHelper.LogDebug(sql.ToString());
            doc.Indexes.ToList().ForEach(i =>
            {
                //i.Value.ToString().Replace("'", "''")
                sql.Append(", " + FireBirdHelper.FormatValue(i.Value));
            });
            sql.Append(")");

            //var t = sql.ToString();
            ExceptionHelper.LogDebug("SQL Insert:" + sql.ToString());

            if (FireBirdHelper.GenericCommand(sql.ToString(), CabinetConnectionString))
            {
                //save the file
                var folderPath = doc.AddImageFileFolderPath(OSTOREPATH);
                //make sure there are no extra . in the name
                var filePath = System.IO.Path.Combine(folderPath, doc.FILENAME + (doc.EXTENSION.IndexOf('.') > -1 ? "": ".") + doc.EXTENSION);
                if (!Directory.Exists(folderPath))
                {
                    Directory.CreateDirectory(folderPath);
                }
                FileHelper.Base64ToFile(filePath, doc.ImageBase64);
                result = true;
            }
            return(result);
        }
        public FileItResponse UploadDocuments(string user, string pass, string cabinetId, DTOs.FileItDocument[] documents)
        {
            var response = new FileItResponse();

            response.FileNameFileItID = new Dictionary <string, string>();

            using (var db = new FileItDataLayer.Models.SystemFileitEntities())
            {
                Debug("Uploading to cabinet: " + cabinetId + " " + documents.Length.ToString() + " docs");
                var cab = db.CABINETS.Single(c => c.CABINETID.Equals(cabinetId, StringComparison.CurrentCultureIgnoreCase));
                response.Success = true;
                documents.ToList().ForEach(d =>
                {
                    var filename = FileItDataLayer.Models.FileItDocument.GetNextFileName();
                    Debug("Got filename: " + filename);
                    var doc = new FileItDataLayer.Models.FileItDocument()
                    {
                        FILENAME    = filename,
                        ARCHIVED    = "N",
                        DELETED     = false,
                        ImageBase64 = d.ImageBase64,
                        EXTENSION   = System.IO.Path.GetExtension(d.FileName).Replace(".", ""),
                        INDEXEDON   = DateTime.Now,
                        PUBLIC      = true,
                        VERSION_NO  = 1,
                        VERSIONID   = "",
                        USERID      = user,
                        STATIONID   = AppGlobal.StationID,
                        BATCHNO     = "BATCH",
                        Indexes     = d.IndexInformation
                    };
                    if (!cab.Add(doc))
                    {
                        Debug("Failed to add doc.");
                        response.Success = false;
                    }
                    else
                    {
                        //TODO: retain the doc id and return
                        response.FileNameFileItID.Add(d.FileName, filename);
                    }
                });
            }
            return(response);
        }