Пример #1
0
        public virtual ActionResult ImportSite(ImportSiteModel model, string @return)
        {
            var data = new JsonResultData(ModelState);

            if (ModelState.IsValid)
            {
                data.RunWithTry((resultData) =>
                {
                    Site parent = null;
                    if (!string.IsNullOrEmpty(model.Parent))
                    {
                        parent = new Site(model.Parent);
                    }
                    Site createdSite = null;
                    if (Request.Files.Count > 0)
                    {
                        createdSite = ServiceFactory.SiteManager.Create(parent, model.Name, model.Repository, Request.Files[0].InputStream, User.Identity.Name);
                    }
                    else
                    {
                        createdSite = ServiceFactory.SiteManager.Import(parent, model.Name, model.Repository, model.File, User.Identity.Name);
                    }

                    resultData.RedirectUrl = Url.Action("SiteMap", new { controller = "Home", siteName = createdSite.FullName });
                });
            }
            return(Json(data));
        }
Пример #2
0
        public virtual ActionResult ImportSite(ImportSiteModel model, string @return)
        {
            var data = new JsonResultData(ModelState);

            if (ModelState.IsValid)
            {
                data.RunWithTry((resultData) =>
                {
                    Site parent = null;
                    if (!string.IsNullOrEmpty(model.Parent))
                    {
                        parent = new Site(model.Parent);
                    }
                    var options = new CreateSiteOptions()
                    {
                        MembershipName = model.Membership,
                        RepositoryName = model.Repository,
                        UserName       = User.Identity.Name
                    };
                    Site createdSite = null;
                    if (Request.Files.Count > 0)
                    {
                        createdSite = ServiceFactory.SiteManager.Create(parent, model.Name, Request.Files[0].InputStream, options);
                    }
                    else
                    {
                        var packageFile = _importedSiteManager.GetItemTemplate("", model.File);
                        createdSite     = ServiceFactory.SiteManager.Create(parent, model.Name, packageFile.TemplateFile, options);
                    }

                    resultData.RedirectUrl = Url.Action("SiteMap", new { controller = "Home", siteName = createdSite.FullName });
                });
            }
            return(Json(data));
        }
Пример #3
0
        public virtual ActionResult ImportSite(ImportSiteModel model, bool uploadNew)
        {
            var entry = new JsonResultEntry();

            Site parent = null;

            if (!string.IsNullOrEmpty(model.Parent))
            {
                parent = new Site(model.Parent);
            }

            try
            {
                if (ModelState.IsValid)
                {
                    if (uploadNew)
                    {
                        if (!Request.Files[0].FileName.EndsWith(".zip"))
                        {
                            throw new KoobooException("Please upload an zip file".Localize());
                        }

                        ServiceFactory.SiteManager.Create(parent, model.Name, model.Repository, Request.Files[0].InputStream, User.Identity.Name);
                    }
                    else
                    {
                        ServiceFactory.SiteManager.Import(parent, model.Name, model.Repository, model.File, User.Identity.Name);
                    }
                }
                else
                {
                    entry.AddModelState(ModelState);
                }
            }
            catch (Exception e)
            {
                entry.AddException(e);
            }
            return(Json(entry));
        }
Пример #4
0
        public IActionResult OnPostImport()
        {
            IFormFile file           = Request.Form.Files[0];
            string    folderName     = "Upload";
            string    folderroot     = "wwwroot";
            string    webRootPath    = "";
            string    errInputFormat = "Mohon check file input anda salah!";
            int       rowNum         = 1;

            if (HttpContext.Session.GetString(SessionKeyDomain) != null && HttpContext.Session.GetString(SessionKeyDomain) != "")
            {
                webRootPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            }
            else
            {
                webRootPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location.Substring(0, Assembly.GetEntryAssembly().Location.IndexOf("bin\\")));
            }
            string                 newPath     = Path.Combine(webRootPath, folderroot, folderName);
            StringBuilder          sb          = new StringBuilder();
            ImportSiteModel        model       = null;
            List <ImportSiteModel> listmodel   = new List <ImportSiteModel>();
            ImportSiteInputModel   ImportModel = new ImportSiteInputModel();

            if (!Directory.Exists(newPath))
            {
                Directory.CreateDirectory(newPath);
            }
            if (file.Length > 0)
            {
                string sFileExtension = Path.GetExtension(file.FileName).ToLower();
                ISheet sheet;
                string fullPath = Path.Combine(newPath, file.FileName);
                using (var stream = new FileStream(fullPath, FileMode.Create))
                {
                    file.CopyTo(stream);
                    stream.Position = 0;
                    if (sFileExtension == ".xls")
                    {
                        HSSFWorkbook hssfwb = new HSSFWorkbook(stream); //This will read the Excel 97-2000 formats
                        sheet = hssfwb.GetSheetAt(0);                   //get first sheet from workbook
                    }
                    else
                    {
                        XSSFWorkbook hssfwb = new XSSFWorkbook(stream); //This will read 2007 Excel format
                        sheet = hssfwb.GetSheetAt(0);                   //get first sheet from workbook
                    }
                    IRow headerRow = sheet.GetRow(0);                   //Get Header Row
                    int  cellCount = headerRow.LastCellNum;
                    sb.Append("<table class='table'><tr>");
                    for (int j = 0; j < cellCount; j++)
                    {
                        NPOI.SS.UserModel.ICell cell = headerRow.GetCell(j);
                        if (cell == null || string.IsNullOrWhiteSpace(cell.ToString()))
                        {
                            continue;
                        }
                        sb.Append("<th>" + cell.ToString() + "</th>");
                    }
                    sb.Append("</tr>");
                    sb.AppendLine("<tr>");
                    for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++) //Read Excel File
                    {
                        IRow row = sheet.GetRow(i);
                        if (row == null)
                        {
                            continue;
                        }
                        if (row.Cells.All(d => d.CellType == CellType.Blank))
                        {
                            continue;
                        }
                        model = new ImportSiteModel();
                        for (int j = row.FirstCellNum; j < cellCount; j++)
                        {
                            #region AddtoModel
                            if (j == 0)
                            {
                                model.No = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 1)
                            {
                                model.CP = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 2)
                            {
                                model.KodeFile = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 3)
                            {
                                model.KodeLokasi = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 4)
                            {
                                model.ArahLokasi = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 5)
                            {
                                model.Pulau = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 6)
                            {
                                model.KodePulau = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 7)
                            {
                                model.Provinsi = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 8)
                            {
                                model.KodeProvinsi = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 9)
                            {
                                model.Kota = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 10)
                            {
                                model.NamaCabang = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 11)
                            {
                                model.Alamat = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 12)
                            {
                                //type Location
                                model.KelasJalan = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 13)
                            {
                                double retNum;
                                bool   isNum = Double.TryParse(Convert.ToString(row.GetCell(j)), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);

                                if (row.GetCell(j).ToString() != "" && isNum)
                                {
                                    model.Panjang = double.Parse(row.GetCell(j).ToString());
                                }
                                else
                                {
                                    return(this.Content("Panjang Number Format => row nomor " + rowNum + " " + errInputFormat));
                                }
                            }
                            else if (j == 14)
                            {
                                double retNum;
                                bool   isNum = Double.TryParse(Convert.ToString(row.GetCell(j)), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);

                                if (row.GetCell(j).ToString() != "" && isNum)
                                {
                                    model.Lebar = double.Parse(row.GetCell(j).ToString());
                                }
                                else
                                {
                                    return(this.Content("Lebar Number Format => row nomor " + rowNum + " " + errInputFormat));
                                }
                            }
                            else if (j == 15)
                            {
                                double retNum;
                                bool   isNum = Double.TryParse(Convert.ToString(row.GetCell(j)), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);

                                if (row.GetCell(j).ToString() != "" && isNum)
                                {
                                    model.Luas = double.Parse(row.GetCell(j).ToString());
                                }
                                else
                                {
                                    return(this.Content("Luas Number Format => row nomor " + rowNum + " " + errInputFormat));
                                }
                            }
                            else if (j == 16)
                            {
                                model.HorV = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 17)
                            {
                                model.Type = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 18)
                            {
                                model.Lampu = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 19)
                            {
                                double retNum;
                                bool   isNum = Double.TryParse(Convert.ToString(row.GetCell(j)), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);

                                if (row.GetCell(j).ToString() != "" && isNum)
                                {
                                    model.Qty = double.Parse(row.GetCell(j).ToString());
                                }
                                else
                                {
                                    return(this.Content("Qty Number Format => row nomor " + rowNum + " " + errInputFormat));
                                }
                            }
                            else if (j == 20)
                            {
                                model.Satuan = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 21)
                            {
                                double retNum;
                                bool   isNum = Double.TryParse(Convert.ToString(row.GetCell(j)), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);

                                if (row.GetCell(j).ToString() != "" && isNum)
                                {
                                    model.HargaAwal = double.Parse(row.GetCell(j).ToString());
                                }
                                else
                                {
                                    return(this.Content("Harga Awal Number Format => row nomor " + rowNum + " " + errInputFormat));
                                }
                            }
                            else if (j == 22)
                            {
                                double retNum;
                                bool   isNum = Double.TryParse(Convert.ToString(row.GetCell(j)), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);

                                if (row.GetCell(j).ToString() != "" && isNum)
                                {
                                    model.HargaAkhir = double.Parse(row.GetCell(j).ToString());
                                }
                                else
                                {
                                    return(this.Content("Harga Akhir Number Format => row nomor " + rowNum + " " + errInputFormat));
                                }
                            }
                            else if (j == 23)
                            {
                                var minorder = int.Parse(row.GetCell(j).ToString().Split(" ")[0]);
                                var mindasar = row.GetCell(j).ToString().Split(" ")[1];
                                if (minorder > 0 && mindasar.Length > 0)
                                {
                                    if (mindasar.ToLower() == "hari")
                                    {
                                        model.MinimOrder = minorder;
                                        model.MinimDasar = mindasar;
                                    }
                                    else
                                    {
                                        return(this.Content("Satuan hari => row nomor " + rowNum + " " + errInputFormat));
                                    }
                                }
                                else
                                {
                                    return(this.Content("Minimum order harus diisi => row nomor " + rowNum + " " + errInputFormat));
                                }
                            }
                            else if (j == 24)
                            {
                                model.Cabang = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 25)
                            {
                                model.WW = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 26)
                            {
                                model.BID = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 27)
                            {
                                model.NonKop = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 28)
                            {
                                model.Note = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 29)
                            {
                                model.PIC = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 30)
                            {
                                model.Bendera = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 31)
                            {
                                model.ContactAgen = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 32)
                            {
                                model.Telepon = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 33)
                            {
                                double retNum;
                                bool   isNum = Double.TryParse(Convert.ToString(row.GetCell(j)), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);

                                if (row.GetCell(j).ToString() != "" && isNum)
                                {
                                    model.Scrore = double.Parse(row.GetCell(j).ToString());
                                }
                                else
                                {
                                    return(this.Content("Score Number Format => row nomor " + rowNum + " " + errInputFormat));
                                }
                            }
                            else if (j == 34)
                            {
                                model.Level = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 35)
                            {
                                model.A = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 36)
                            {
                                model.B = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 37)
                            {
                                model.C = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 38)
                            {
                                model.Keterangan = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 39)
                            {
                                model.LinkFolder = row.GetCell(j) == null ? "" : row.GetCell(j).ToString();
                            }
                            else if (j == 40)
                            {
                                double retNum;
                                bool   isNum = Double.TryParse(Convert.ToString(row.GetCell(j)), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);

                                if (row.GetCell(j).ToString() != "" && isNum)
                                {
                                    if (Double.Parse("-90") <= Double.Parse(row.GetCell(j).ToString()) && Double.Parse("90") >= Double.Parse(row.GetCell(j).ToString()))
                                    {
                                        model.Latitude = row.GetCell(j).ToString().Replace(",", ".");
                                    }
                                    else
                                    {
                                        return(this.Content("Latitude Range -90 s/d 90 => row nomor " + rowNum + " " + errInputFormat));
                                    }
                                }
                                else
                                {
                                    return(this.Content("Latitude Number Format => row nomor " + rowNum + " " + errInputFormat));
                                }
                            }
                            else if (j == 41)
                            {
                                double retNum;
                                bool   isNum = Double.TryParse(Convert.ToString(row.GetCell(j)), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);

                                if (row.GetCell(j).ToString() != "" && isNum)
                                {
                                    if (Double.Parse("-180") <= Double.Parse(row.GetCell(j).ToString()) && Double.Parse("180") >= Double.Parse(row.GetCell(j).ToString()))
                                    {
                                        model.Longitude = row.GetCell(j).ToString().Replace(",", ".");
                                    }
                                    else
                                    {
                                        return(this.Content("Longitude Range -180 s/d 180 => row nomor " + rowNum + " " + errInputFormat));
                                    }
                                }
                                else
                                {
                                    return(this.Content("Longitude Number Format => row nomor " + rowNum + " " + errInputFormat));
                                }
                            }
                            #endregion
                            if (row.GetCell(j) != null)
                            {
                                sb.Append("<td>" + row.GetCell(j).ToString() + "</td>");
                            }
                        }
                        sb.AppendLine("</tr>");
                        listmodel.Add(model);
                        rowNum++;
                    }
                    ImportModel.UserID = Guid.Parse(HttpContext.Session.GetString(SessionKeyID));
                    ImportModel.Input  = listmodel;
                    sb.Append("</table>");
                }
                //return this.Content(sb.ToString());
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri(BaseAPI + "Site/");
                    client.Timeout     = Timeout.InfiniteTimeSpan;
                    //HTTP POST
                    var postTask = client.PostAsJsonAsync <ImportSiteInputModel>("ImportSite", ImportModel);
                    postTask.Wait();

                    var result = postTask.Result;
                    if (result.IsSuccessStatusCode)
                    {
                        return(this.Content(sb.ToString()));
                    }
                    else
                    {
                        return(RedirectToAction("Index", "ExportImport"));
                    }
                }
            }
            return(this.Content(sb.ToString()));
        }