public HttpResponseMessage GetTemplate(int type, string regionId, string apbnKey) { DocumentUploadType t = (DocumentUploadType)type; var context = new AdapterContext(dbContext, t, regionId, apbnKey); return(adapters[t].GetTemplate(context)); }
public AdapterContext(DbContext dbContext, DocumentUploadType type, string regionId, string apbnKey) { this.Type = type; this.Region = dbContext.Set <Region>() .Include(r => r.Parent) .Include(r => r.Parent.Parent) .Include(r => r.Parent.Parent.Parent) .Include(r => r.Parent.Parent.Parent.Parent) .First(r => r.Id == regionId); this.Apbn = dbContext.Set <Apbn>().First(a => a.Key == apbnKey); }
public void Upload(Multipart <Spreadsheet> multipart, int type, string regionId, string apbnKey) { KawalDesaController.CheckRegionAllowed(dbContext, regionId); DocumentUploadType t = (DocumentUploadType)type; var context = new AdapterContext(dbContext, t, regionId, apbnKey); if (context.Region.Type != RegionType.NASIONAL && context.Region.Type != RegionType.KABUPATEN) { throw new ApplicationException("only allowed to upload on nasional or kabupaten"); } using (var tx = dbContext.Database.BeginTransaction()) { adapters[t].Upload(multipart, context); tx.Commit(); } }
public String GetCurrentSheetUrl(DocumentUploadType type, string regionId, string apbnKey) { User user = KawalDesaController.GetCurrentUser(); var context = new AdapterContext(dbContext, type, regionId, apbnKey); var region = dbContext.Set <Region>().Find(regionId); String userId = user != null ? user.Id : null; String userEmail = user != null ? user.Email : null; String userName = user != null ? user.Name : "Anonymous"; var workItem = dbContext.Set <SpreadsheetWorkItem>() .FirstOrDefault(w => w.fkUserId == userId && w.fkRegionId == region.Id && w.Type == type && w.ApbnKey == context.Apbn.Key); if (workItem == null) { using (var tx = dbContext.Database.BeginTransaction()) { var typeStr = type.ToString(); typeStr = typeStr.Replace("National", ""); typeStr = typeStr.Replace("Regional", ""); typeStr = typeStr.ToUpper(); var root = HttpContext.Current.Server.MapPath("~/Content/sheets"); Directory.CreateDirectory(root); var safeFileName = typeStr + " " + apbnKey + " " + regionId + " " + region.Name + ".xlsx"; var fullPath = Path.Combine(root, safeFileName); var bytes = adapters[type].GetBytes(context); File.WriteAllBytes(fullPath, bytes); var authEmail = ConfigurationManager.AppSettings["Drive.AuthEmail"]; var authKey = ConfigurationManager.AppSettings["Drive.AuthKey"]; var parentDir = ConfigurationManager.AppSettings["Drive.ParentDir"]; var driveUtils = new DriveUtils(authEmail, authKey, parentDir); var workDir = dbContext.Set <SpreadsheetWorkDir>().FirstOrDefault( d => d.fkUserId == userId); if (workDir == null) { var dirName = string.Format("KawalDesa Sheets - ({0})", userName); workDir = new SpreadsheetWorkDir() { GoogleSheetId = driveUtils.CreateParentDirectory(userEmail, dirName), fkUserId = userId }; dbContext.Set <SpreadsheetWorkDir>().Add(workDir); dbContext.SaveChanges(); } workItem = new SpreadsheetWorkItem() { GoogleSheetId = driveUtils.UploadFile(workDir.GoogleSheetId, fullPath, safeFileName), fkUserId = userId, fkRegionId = region.Id, ApbnKey = apbnKey, Type = type }; dbContext.Set <SpreadsheetWorkItem>().Add(workItem); dbContext.SaveChanges(); tx.Commit(); } } //string fullyQualifiedUrl = Request.RequestUri.GetLeftPart(UriPartial.Authority); //return fullyQualifiedUrl + "/Content/sheets/" + safeFileName; return("https://docs.google.com/spreadsheets/d/" + workItem.GoogleSheetId); }
public void Upload(Multipart <Transfer> multipart, DocumentUploadType type, SourceDocumentFunction fn, string regionId, string apbnKey) { try { KawalDesaController.CheckRegionAllowed(dbContext, regionId); DocumentUploadType t = (DocumentUploadType)type; var region = dbContext.Set <Region>() .Include(r => r.Parent) .Include(r => r.Parent.Parent) .Include(r => r.Parent.Parent.Parent) .Include(r => r.Parent.Parent.Parent.Parent) .First(r => r.Id == regionId); var apbn = dbContext.Set <Apbn>().First(a => a.Key == apbnKey); var user = KawalDesaController.GetCurrentUser(); //if (region.Type != RegionType.NASIONAL && region.Type != RegionType.KABUPATEN) // throw new ApplicationException("only allowed to upload on nasional or kabupaten"); using (var tx = dbContext.Database.BeginTransaction()) { Transfer transfer = multipart.Entity; if (transfer != null) { ModelState.Clear(); Validate(transfer); if (!ModelState.IsValid) { throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } transfer.IsActivated = true; transfer.fkRegionId = regionId; transfer.Year = Convert.ToInt32(apbnKey.Substring(0, 4)); dbContext.Set <Transfer>().Add(transfer); dbContext.SaveChanges(); dumpMessager.Message("p " + apbnKey + " " + regionId); } if (fn == SourceDocumentFunction.Allocation) { string stype = null; switch (type) { case DocumentUploadType.NationalDd: case DocumentUploadType.RegionalDd: stype = "dd"; break; case DocumentUploadType.NationalAdd: case DocumentUploadType.RegionalAdd: stype = "add"; break; case DocumentUploadType.NationalBhpr: case DocumentUploadType.RegionalBhpr: stype = "bhpr"; break; } if (stype == null) { throw new ApplicationException("invalid doc type: " + type); } dumpMessager.Message(stype + " " + apbnKey + " " + regionId); } foreach (var fileResult in multipart.Files) { var blob = new Blob(fileResult); dbContext.Set <Blob>().Add(blob); dbContext.SaveChanges(); fileResult.Move(blob.FilePath); var doc = new SourceDocument(); doc.ThumbnailCreated = false; doc.FileName = blob.RelativeFileName; doc.fkCreatedById = user.Id; doc.fkOrganizationId = user.fkOrganizationId.Value; doc.DateCreated = DateTime.Now; doc.Type = type; doc.Function = fn; doc.ApbnKey = apbnKey; doc.fkRegionId = regionId; doc.fkFileId = blob.Id; if (transfer != null) { doc.fkTransferId = transfer.Id; } dbContext.Set <SourceDocument>().Add(doc); dbContext.SaveChanges(); } tx.Commit(); } } finally { multipart.DeleteUnmoved(); } }