Example #1
0
        public void SetAnonymous(bool isAnonymous)
        {
            var user = KawalDesaController.GetCurrentUser();

            ModelController <User, string>
            .Update(dbContext, user.Id)
            .Set(e => e.IsAnonymous, isAnonymous)
            .Save();
        }
            public void Upload(Multipart <Spreadsheet> multipart, AdapterContext context)
            {
                try
                {
                    List <Region> regions = null;
                    if (context.Region.Type == RegionType.NASIONAL)
                    {
                        regions = DbContext.Set <Region>().Where(r => r.Type == RegionType.KABUPATEN).ToList();
                    }
                    else
                    {
                        regions = DbContext.Set <Region>().Where(r => r.Type == RegionType.DESA && r.Parent.fkParentId == context.Region.Id).ToList();
                    }

                    var allocations = new AllocationSpreadsheetReader <TAllocation>().Read(regions, new FileInfo(multipart.Files[0].FilePath));
                    var spreadsheet = multipart.Entity;
                    var user        = KawalDesaController.GetCurrentUser();

                    var fileResult = multipart.Files[0];
                    var blob       = new Blob(fileResult);
                    DbContext.Set <Blob>().Add(blob);
                    DbContext.SaveChanges();
                    fileResult.Move(blob.FilePath);

                    spreadsheet.FileName         = blob.RelativeFileName;
                    spreadsheet.fkCreatedById    = user.Id;
                    spreadsheet.fkOrganizationId = user.fkOrganizationId.Value;
                    spreadsheet.DateCreated      = DateTime.Now;
                    spreadsheet.DateActivated    = DateTime.Now;
                    spreadsheet.Type             = context.Type;
                    spreadsheet.ApbnKey          = context.Apbn.Key;
                    spreadsheet.fkRegionId       = context.Region.Id;
                    spreadsheet.fkFileId         = blob.Id;
                    DbContext.Set <Spreadsheet>().Add(spreadsheet);
                    DbContext.SaveChanges();

                    foreach (var allocation in allocations)
                    {
                        allocation.fkSpreadsheetId = spreadsheet.Id;
                        Init(context, allocation);
                        DbContext.Set <TAllocation>().Add(allocation);
                    }
                    DbContext.SaveChanges();

                    new SpreadsheetActivator <TAllocation>().Activate(DbContext, spreadsheet);
                }
                finally
                {
                    multipart.DeleteUnmoved();
                }
            }
        public UserViewModel AddOrgVolunteer(long id, String email)
        {
            using (var tx = dbContext.Database.BeginTransaction())
            {
                var org   = dbSet.Find(id);
                var roles = new List <string> {
                    Role.VOLUNTEER
                };
                User inviter = dbContext.Set <User>().Find(KawalDesaController.GetCurrentUser().Id);
                var  token   = InvitationToken.Create(dbContext, email, inviter, org, roles, new List <Region>());
                new UserMailer().Invitation(token).Deliver();
                tx.Commit();

                return(userController.Convert(token.User));
            }
        }
        public UserViewModel AddOrgAdmin(long id, String email)
        {
            using (var tx = dbContext.Database.BeginTransaction())
            {
                var org   = dbSet.Find(id);
                var roles = new List <string> {
                    Role.VOLUNTEER_ALLOCATION, Role.VOLUNTEER_TRANSFER, Role.VOLUNTEER_DESA,
                    Role.VOLUNTEER_ACCOUNT, Role.VOLUNTEER_REALIZATION, Role.ORGANIZATION_ADMIN, Role.VOLUNTEER
                };
                var  national = dbContext.Set <Region>().Find("0");
                User inviter  = dbContext.Set <User>().Find(KawalDesaController.GetCurrentUser().Id);
                var  token    = InvitationToken.Create(dbContext, email, inviter, org, roles, new List <Region> {
                    national
                });
                new UserMailer().Invitation(token).Deliver();
                tx.Commit();

                return(userController.Convert(token.User));
            }
        }
Example #5
0
        public UserViewModel GetCurrentUser()
        {
            var user = KawalDesaController.GetCurrentUser();

            return(Convert(user));
        }
            public void Publish(Stream fileStream, AdapterContext context, string notes)
            {
                try
                {
                    List <Region> regions = null;
                    if (context.Region.Type == RegionType.NASIONAL)
                    {
                        regions = DbContext.Set <Region>().Where(r => r.Type == RegionType.KABUPATEN).ToList();
                    }
                    else
                    {
                        regions = DbContext.Set <Region>().Where(r => r.Type == RegionType.DESA && r.Parent.fkParentId == context.Region.Id).ToList();
                    }

                    Blob blob = new Blob();
                    blob.Name = "Alocation_xxx.xlsx";

                    DbContext.Set <Blob>().Add(blob);
                    DbContext.SaveChanges();

                    byte[] fileBytes = null;
                    using (MemoryStream ms = new MemoryStream())
                    {
                        fileStream.CopyTo(ms);
                        fileBytes = ms.ToArray();
                    }

                    string fileName = blob.Id + ".xlsx";
                    string root     = HttpContext.Current.Server.MapPath("~/Content/Files");
                    var    user     = KawalDesaController.GetCurrentUser();

                    Directory.CreateDirectory(root);
                    String filePath = Path.Combine(root, fileName);
                    File.WriteAllBytes(filePath, fileBytes);

                    Spreadsheet spreadsheet = new Spreadsheet();
                    spreadsheet.File             = blob;
                    spreadsheet.Notes            = notes;
                    spreadsheet.fkCreatedById    = user.Id;
                    spreadsheet.fkOrganizationId = user.fkOrganizationId.Value;
                    spreadsheet.DateCreated      = DateTime.Now;
                    spreadsheet.DateActivated    = DateTime.Now;
                    spreadsheet.Type             = context.Type;
                    spreadsheet.ApbnKey          = context.Apbn.Key;
                    spreadsheet.fkRegionId       = context.Region.Id;
                    spreadsheet.fkFileId         = blob.Id;
                    DbContext.Set <Spreadsheet>().Add(spreadsheet);
                    DbContext.SaveChanges();

                    var allocations = new AllocationSpreadsheetReader <TAllocation>().Read(regions, fileBytes);

                    foreach (var allocation in allocations)
                    {
                        allocation.fkSpreadsheetId = spreadsheet.Id;
                        Init(context, allocation);
                        DbContext.Set <TAllocation>().Add(allocation);
                    }
                    DbContext.SaveChanges();

                    new SpreadsheetActivator <TAllocation>().Activate(DbContext, spreadsheet);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        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);
        }
Example #8
0
        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();
            }
        }