示例#1
0
        private bool GenerateKMZ()
        {
            string NotUsed = "";

            //StringBuilder sbKMZ = new StringBuilder();
            //DateTime CD = DateTime.Now;

            //string Language = "_" + _TaskRunnerBaseService._BWObj.appTaskModel.Language;

            //string DateText = "_" + CD.Year.ToString() +
            //    "_" + (CD.Month > 9 ? CD.Month.ToString() : "0" + CD.Month.ToString()) +
            //    "_" + (CD.Day > 9 ? CD.Day.ToString() : "0" + CD.Day.ToString()) +
            //    "_" + (CD.Hour > 9 ? CD.Hour.ToString() : "0" + CD.Hour.ToString()) +
            //    "_" + (CD.Minute > 9 ? CD.Minute.ToString() : "0" + CD.Minute.ToString());

            switch (reportTypeModel.TVType)
            {
            case TVTypeEnum.MikeScenario:
                break;

            case TVTypeEnum.Subsector:
                break;

            default:
                break;
            }

            //if (!RenameStartOfFileNameKMZ(fi))
            //{
            //    return false;
            //}

            fi = new FileInfo(fi.FullName.Replace(".html", ".kml"));

            if (fi.Exists)
            {
                try
                {
                    fi.Delete();
                    fi = new FileInfo(fi.FullName);
                }
                catch (Exception ex)
                {
                    NotUsed = string.Format(TaskRunnerServiceRes.CouldNotDeleteFile_Error_, fi.FullName, ex.Message + (ex.InnerException != null ? " Inner: " + ex.InnerException.Message : ""));
                    _TaskRunnerBaseService._BWObj.TextLanguageList = _TaskRunnerBaseService.GetTextLanguageFormat2List("CouldNotDeleteFile_Error_", fi.FullName, ex.Message + (ex.InnerException != null ? " Inner: " + ex.InnerException.Message : ""));
                    return(false);
                }
            }

            switch (reportTypeModel.TVType)
            {
            case TVTypeEnum.Root:
            {
                if (!GenerateKMZRoot())
                {
                    return(false);
                }
            }
            break;

            case TVTypeEnum.Area:
            case TVTypeEnum.Country:
            case TVTypeEnum.Infrastructure:
            case TVTypeEnum.MikeScenario:
            {
                if (!GenerateKMZMikeScenario())
                {
                    return(false);
                }
            }
            break;

            case TVTypeEnum.MikeSource:
            case TVTypeEnum.Municipality:
            case TVTypeEnum.MWQMSite:
            case TVTypeEnum.PolSourceSite:
            case TVTypeEnum.Province:
            case TVTypeEnum.Sector:
            case TVTypeEnum.Subsector:
            {
                if (!GenerateKMZSubsector())
                {
                    return(false);
                }
            }
            break;

            case TVTypeEnum.BoxModel:
            case TVTypeEnum.VisualPlumesScenario:
                if (!GenerateKMZNotImplemented())
                {
                    return(false);
                }
                break;

            default:
                break;
            }


            DirectoryInfo di = new DirectoryInfo(fi.DirectoryName);

            if (!di.Exists)
            {
                try
                {
                    di.Create();
                }
                catch (Exception ex)
                {
                    NotUsed = string.Format(TaskRunnerServiceRes.CouldNotCreateDirectory__, di.FullName, ex.Message + (ex.InnerException != null ? " Inner: " + ex.InnerException.Message : ""));
                    _TaskRunnerBaseService._BWObj.TextLanguageList = _TaskRunnerBaseService.GetTextLanguageFormat2List("CouldNotCreateDirectory__", di.FullName, ex.Message + (ex.InnerException != null ? " Inner: " + ex.InnerException.Message : ""));
                    return(false);
                }
            }

            StreamWriter sw = fi.CreateText();

            sw.Write(sb.ToString());
            sw.Flush();
            sw.Close();

            fi = new FileInfo(fi.FullName);

            if (!fi.Exists)
            {
                NotUsed = string.Format(TaskRunnerServiceRes.CouldNotFindFile_, fi.FullName);
                _TaskRunnerBaseService._BWObj.TextLanguageList = _TaskRunnerBaseService.GetTextLanguageFormat1List("CouldNotFindFile_", fi.FullName);
                return(false);
            }


            //ProcessStartInfo pZip = new ProcessStartInfo();
            //pZip.Arguments = "a -tzip \"" + fi.FullName.Replace(".kml", ".kmz") + "\" \"" + fi.FullName + "\"";
            //pZip.RedirectStandardInput = true;
            //pZip.UseShellExecute = false;
            //pZip.CreateNoWindow = true;
            //pZip.WindowStyle = ProcessWindowStyle.Hidden;

            //Process processZip = new Process();
            //processZip.StartInfo = pZip;
            //try
            //{
            //    pZip.FileName = @"C:\Program Files\7-Zip\7z.exe";
            //    processZip.Start();
            //}
            //catch (Exception ex)
            //{
            //    NotUsed = string.Format(TaskRunnerServiceRes.CompressKMLDidNotWorkWith7zError_, ex.Message);
            //    _TaskRunnerBaseService._BWObj.TextLanguageList = _TaskRunnerBaseService.GetTextLanguageFormat1List("CompressKMLDidNotWorkWith7zError_", ex.Message);
            //    return false;
            //}

            //while (!processZip.HasExited)
            //{
            //    // waiting for the processZip to finish then continue
            //}

            //fi = new FileInfo(fi.FullName);

            //if (fi.Exists)
            //{
            //    try
            //    {
            //        //fi.Delete();
            //    }
            //    catch (Exception ex)
            //    {
            //        NotUsed = string.Format(TaskRunnerServiceRes.CouldNotDeleteFile_Error_, fi.FullName, ex.Message + (ex.InnerException != null ? " Inner: " + ex.InnerException.Message : ""));
            //        _TaskRunnerBaseService._BWObj.TextLanguageList = _TaskRunnerBaseService.GetTextLanguageFormat2List("CouldNotDeleteFile_Error_", fi.FullName, ex.Message + (ex.InnerException != null ? " Inner: " + ex.InnerException.Message : ""));
            //        return false;
            //    }
            //}

            //fi = new FileInfo(fi.FullName.Replace(".kml", ".kmz"));

            //if (!fi.Exists)
            //{
            //    NotUsed = string.Format(TaskRunnerServiceRes.CouldNotFindFile_, fi.FullName);
            //    _TaskRunnerBaseService._BWObj.TextLanguageList = _TaskRunnerBaseService.GetTextLanguageFormat1List("CouldNotFindFile_", fi.FullName);
            //    return false;
            //}

            TVFileService tvFileService = new TVFileService(_TaskRunnerBaseService._BWObj.appTaskModel.Language, _TaskRunnerBaseService._User);
            TVItemService tvItemService = new TVItemService(_TaskRunnerBaseService._BWObj.appTaskModel.Language, _TaskRunnerBaseService._User);
            TVItemModel   tvItemModel   = tvItemService.PostAddChildTVItemDB(_TaskRunnerBaseService._BWObj.appTaskModel.TVItemID, fi.Name.Replace(fi.Extension, ""), TVTypeEnum.File);

            if (!string.IsNullOrWhiteSpace(tvItemModel.Error))
            {
                NotUsed = string.Format(TaskRunnerServiceRes.CouldNotAdd_Error_, TaskRunnerServiceRes.TVItem, _TaskRunnerBaseService._BWObj.appTaskModel.TVItemID.ToString());
                _TaskRunnerBaseService._BWObj.TextLanguageList = _TaskRunnerBaseService.GetTextLanguageFormat2List("CouldNotAdd_Error_", TaskRunnerServiceRes.TVItem, _TaskRunnerBaseService._BWObj.appTaskModel.TVItemID.ToString());
                return(false);
            }

            TVFileModel tvFileModelNew = new TVFileModel();

            tvFileModelNew.TVFileTVItemID            = tvItemModel.TVItemID;
            tvFileModelNew.TemplateTVType            = 0;
            tvFileModelNew.ReportTypeID              = reportTypeModel.ReportTypeID;
            tvFileModelNew.Parameters                = Parameters;
            tvFileModelNew.ServerFileName            = fi.Name;
            tvFileModelNew.FilePurpose               = FilePurposeEnum.ReportGenerated;
            tvFileModelNew.Language                  = _TaskRunnerBaseService._BWObj.appTaskModel.Language;
            tvFileModelNew.Year                      = DateTime.Now.Year;
            tvFileModelNew.FileDescription           = reportTypeModel.Description;
            tvFileModelNew.FileType                  = tvFileService.GetFileType(fi.Extension);
            tvFileModelNew.FileSize_kb               = (((int)fi.Length / 1024) == 0 ? 1 : (int)fi.Length / 1024);
            tvFileModelNew.FileInfo                  = TaskRunnerServiceRes.FileName + "[" + fi.Name + "]\r\n" + TaskRunnerServiceRes.FileType + "[" + fi.Extension + "]\r\n";
            tvFileModelNew.FileCreatedDate_UTC       = fi.LastWriteTimeUtc;
            tvFileModelNew.ServerFilePath            = (fi.DirectoryName + @"\").Replace(@"C:\", @"E:\");
            tvFileModelNew.LastUpdateDate_UTC        = DateTime.UtcNow;
            tvFileModelNew.LastUpdateContactTVItemID = _TaskRunnerBaseService._BWObj.appTaskModel.LastUpdateContactTVItemID;

            TVFileModel tvFileModelRet = tvFileService.PostAddTVFileDB(tvFileModelNew);

            if (!string.IsNullOrWhiteSpace(tvFileModelRet.Error))
            {
                NotUsed = string.Format(TaskRunnerServiceRes.CouldNotAdd_Error_, TaskRunnerServiceRes.TVFile, tvFileModelRet.Error);
                _TaskRunnerBaseService._BWObj.TextLanguageList = _TaskRunnerBaseService.GetTextLanguageFormat2List("CouldNotAdd_Error_", TaskRunnerServiceRes.TVFile, tvFileModelRet.Error);
                return(false);
            }

            return(true);
        }
        public TVItemModel PictureFileUploadDB(HttpRequestBase Request)
        {
            string AdminEmail = Request.QueryString["e"];
            int    TVItemID   = int.Parse(Request.QueryString["t"]);

            IPrincipal user = new GenericPrincipal(new GenericIdentity(AdminEmail, "Forms"), null);

            ContactService contactService = new ContactService(LanguageRequest, user);
            ContactModel   contactModel   = contactService.GetContactModelWithLoginEmailDB(AdminEmail);

            if (!string.IsNullOrWhiteSpace(contactModel.Error))
            {
                return(new TVItemModel()
                {
                    Error = "ERROR: " + string.Format(ServiceRes.NoUserWithEmail_, AdminEmail)
                });
            }

            TVItemService tvItemService = new TVItemService(LanguageRequest, user);
            TVFileService tvFileService = new TVFileService(LanguageRequest, user);

            List <string> AllowableExt = tvFileService.GetAllowableExt();

            TVFileModel tvFileModelRet = new TVFileModel();

            using (TransactionScope ts = new TransactionScope())
            {
                string FileName = "";

                if (Request.Files.Count != 1)
                {
                    return new TVItemModel()
                           {
                               Error = "ERROR: " + ServiceRes.CanOnlyLoadOneFileAtATime
                           }
                }
                ;

                HttpPostedFileBase hpf = null;
                foreach (string file in Request.Files)
                {
                    hpf = Request.Files[file];
                }

                if (hpf == null)
                {
                    return new TVItemModel()
                           {
                               Error = "ERROR: " + ServiceRes.PleaseSelectAFileToUpload
                           }
                }
                ;

                FileName = hpf.FileName;

                FileInfo fi = new FileInfo(FileName);

                if (!AllowableExt.Contains(fi.Extension.ToLower()))
                {
                    string AllowableExtText = "";

                    foreach (string s in AllowableExt)
                    {
                        AllowableExtText += s + " ";
                    }
                    return(new TVItemModel()
                    {
                        Error = "ERROR: " + string.Format(ServiceRes.PleaseSelectAFileOfType_, AllowableExtText)
                    });
                }

                string ServerFileName = "";
                if (FileName.Contains(@"\"))
                {
                    ServerFileName = FileName.Substring(FileName.LastIndexOf(@"\") + 1);
                }
                else
                {
                    ServerFileName = FileName;
                }

                TVItemModel tvItemModelPSS = tvItemService.GetTVItemModelWithTVItemIDDB(TVItemID);
                if (!string.IsNullOrWhiteSpace(tvItemModelPSS.Error))
                {
                    return(new TVItemModel()
                    {
                        Error = "ERROR: " + tvItemModelPSS.Error
                    });
                }

                TVItemModel tvItemModelExist = tvItemService.GetChildTVItemModelWithParentIDAndTVTextAndTVTypeDB(tvItemModelPSS.TVItemID, ServerFileName, TVTypeEnum.File);
                if (string.IsNullOrEmpty(tvItemModelExist.Error))
                {
                    return(new TVItemModel()
                    {
                        Error = "ERROR: " + string.Format(ServiceRes._AlreadyExists, ServerFileName)
                    });
                }

                TVItemModel tvItemModelTVFileRet = tvItemService.PostAddChildTVItemDB(tvItemModelPSS.TVItemID, ServerFileName, TVTypeEnum.File);
                if (!string.IsNullOrEmpty(tvItemModelTVFileRet.Error))
                {
                    return(new TVItemModel()
                    {
                        Error = "ERROR: " + tvItemModelTVFileRet.Error
                    });
                }

                string ServerFilePath = tvFileService.GetServerFilePath(tvItemModelPSS.TVItemID);

                int FileLength = hpf.ContentLength;

                DirectoryInfo di = new DirectoryInfo(ServerFilePath);
                if (!di.Exists)
                {
                    di.Create();
                }

                fi = new FileInfo(ServerFilePath + ServerFileName);

                if (fi.Exists)
                {
                    return(new TVItemModel()
                    {
                        Error = "ERROR: " + string.Format(ServiceRes.File_AlreadyExist, ServerFileName)
                    });
                }

                hpf.SaveAs(fi.FullName);

                FileTypeEnum fileType = tvFileService.GetFileType(fi.Extension.ToUpper());

                TVFileModel tvFileModelNew = new TVFileModel()
                {
                    TVFileTVItemID      = tvItemModelTVFileRet.TVItemID,
                    FilePurpose         = FilePurposeEnum.Picture,
                    FileDescription     = "Temp description",
                    FileType            = fileType,
                    FileSize_kb         = Math.Max(hpf.ContentLength / 1024, 1),
                    FileInfo            = "Uploaded file",
                    FileCreatedDate_UTC = DateTime.Now,
                    FromWater           = false,
                    ClientFilePath      = FileName,
                    ServerFileName      = ServerFileName,
                    ServerFilePath      = ServerFilePath,
                    Language            = LanguageEnum.en,
                    Year = DateTime.Now.Year,
                };

                tvFileModelRet = tvFileService.PostAddTVFileDB(tvFileModelNew);
                if (!string.IsNullOrWhiteSpace(tvFileModelRet.Error))
                {
                    DeleteFileFromServer(fi);
                    {
                        return(new TVItemModel()
                        {
                            Error = "ERROR: " + tvFileModelRet.Error
                        });
                    }
                }

                ts.Complete();
            }

            return(new TVItemModel()
            {
                Error = tvFileModelRet.TVFileTVItemID.ToString()
            });
        }