public void SaveNewTarget(Target target) { var srvDao = NinjectKernelFactory.Kernel.Get<ITargetDataAccess>(); using (var tr = TransactionsFactory.CreateTransactionScope()) { try { srvDao.Save(target); } catch (ADOException) { throw new FaultException<TargetNameAlreadyExistException>( new TargetNameAlreadyExistException { MessageError = Dictionary.TARGET_NAME_CONSTRAINT_EXCEPTION_MSG }, new FaultReason(Dictionary.UNIQUE_CONSTRAINT_EXCEPTION_REASON)); } catch (StaleObjectStateException) { throw new FaultException<ConcurrentUpdateException>( new ConcurrentUpdateException {MessageError = Dictionary.TARGET_CONCURRENT_UPDATE_EXCEPTION_MSG}, new FaultReason(Dictionary.TARGET_NAME_CONSTRAINT_EXCEPTION_MSG)); } catch (Exception ex) { throw new FaultException<Exception>( new Exception(ex.Message), new FaultReason(Dictionary.UNKNOWN_REASON)); } tr.Complete(); } }
protected void btnCreateTarget_OnClick(object sender, EventArgs e) { SetErrorMessages(); EnableValidator(true); Page.Validate(); if (!Page.IsValid) { return; } try { /* Get latitude and longtitude from textbox * Issues: * _ I haven't not validate the case which string is not float format because of time * => will be fixed using tryparse */ if (txtLat.Text != string.Empty && txtLong.Text != string.Empty) { m_latitude = double.Parse(txtLat.Text); m_longtitude = double.Parse(txtLong.Text); } string targetId; if (Request.QueryString["Type"] == Dictionary.CAMPAIGN_TYPE_CHECK_IN_URL) { targetId = Dictionary.NA; } else { // Save image target to server - just help debugging, it does not affect system. var fileName = Dictionary.TARGET + m_mission.MissionId + FileUpload_Target.FileName; var filePath = Server.MapPath(Dictionary.PATH_UPLOADED_TARGET + fileName); FileUpload_Target.SaveAs(filePath); // Call VWS var callPostNewTarget = new WebClient(); var result = callPostNewTarget.DownloadString(new Uri( "http://*****:*****@"""TargetCreated""", StringComparison.Ordinal); if (resultCodeIndex > 0) { var resultTargetId = result.IndexOf(@"""target_id""", StringComparison.Ordinal); var startIndex = result.IndexOf("\"", resultTargetId + 12, StringComparison.Ordinal) + 1; var endIndex = result.IndexOf("\"", startIndex, StringComparison.Ordinal) - 1; targetId = result.Substring(startIndex, endIndex - startIndex + 1); } // Save target id to ListTarget.txt if (!File.Exists(Server.MapPath(Dictionary.PATH_LIST_TARGET))) { using (var sw = File.CreateText(Server.MapPath(Dictionary.PATH_LIST_TARGET))) { sw.WriteLine(targetId); } } else { using (var sw = new StreamWriter(Server.MapPath(Dictionary.PATH_LIST_TARGET), true)) { sw.WriteLine(targetId); } } } // Save target to database var target = new Target { Url = targetId, TargetName = txtTargetName.Text, Address = txtAddress.Text, Latitude = m_latitude, Longitude = m_longtitude, FacebookUrl = txtFacebookUrl.Text, YoutubeUrl = txtYoutubeUrl.Text, Mission = m_mission }; // Notes: Will be migrated into helper file // Create ArResources var arResources = new ArResources(); // Create ARSM-PicturesGallery var commonAttributes = new CommonAttributes(); var platforms = new Platforms(); var platform = new Platform(); var processors = new Processors(); var uploadedPicturesGallery = FileUpload_PicturesGallery.PostedFiles; if (FileUpload_PicturesGallery.HasFiles) { // Create Attribute in CommonAttributes foreach (var uploadedPicture in uploadedPicturesGallery) { var fileName = "Ar" + m_mission.MissionId + uploadedPicture.FileName; var filePath = Dictionary.PATH_UPLOADED_TARGET + fileName; FileUpload_Target.SaveAs(Server.MapPath(filePath)); // Get the object used to communicate with the server. var urip = "ftp://[email protected]/www.ara288.somee.com/Ar_Data/PicturesGallery/"; var requestp = (FtpWebRequest) WebRequest.Create(urip + fileName); requestp.Method = WebRequestMethods.Ftp.UploadFile; // FTP site logon requestp.Credentials = new NetworkCredential(Authentication.FTP_USER, Authentication.FTP_PASSWORD); // Copy the entire contents of the file to the request stream. var sourceStreamp = new StreamReader(Server.MapPath(filePath)); var fileContentsp = Encoding.UTF8.GetBytes(sourceStreamp.ReadToEnd()); sourceStreamp.Close(); requestp.ContentLength = fileContentsp.Length; // Upload the file stream to the server. var requestStreamp = requestp.GetRequestStream(); requestStreamp.Write(fileContentsp, 0, fileContentsp.Length); requestStreamp.Close(); // Get the response from the FTP server. var responsep = (FtpWebResponse) requestp.GetResponse(); // Close the connection = Happy a FTP server. responsep.Close(); var attribute = new Attribute { Key = Dictionary.AR_KEY_URL, Value = filePath }; commonAttributes.Attribute = new List<Attribute> {attribute}; } // Create Platform with Processor in Platforms platform.PlatformId = Dictionary.AR_PLATFORM_ID_ANDROID; processors.Processor = new Processor {ProcessorType = Dictionary.AR_PROCESSOR_TYPE_IMAGE_SWITCHER}; platforms.Platform = platform; // Add ArResource to ArResources arResources.ArResource = new List<ArResource> { new ArResource { CommonAttributes = commonAttributes, ArType = Dictionary.ARSM_PICTURES_GALLERY, Platforms = platforms } }; } // Create ARSM-Youtube commonAttributes = new CommonAttributes(); platforms = new Platforms(); platform = new Platform(); processors = new Processors(); // Create Attribute in CommonAttributes commonAttributes.Attribute = new List<Attribute> { new Attribute { Key = Dictionary.AR_KEY_URL, Value = txtYoutubeUrl.Text } }; // Create Platform with Processor in Platforms platform.PlatformId = Dictionary.AR_PLATFORM_ID_ANDROID; processors.Processor = new Processor {ProcessorType = Dictionary.AR_PROCESSOR_TYPE_YOUTUBE}; platforms.Platform = platform; // Add ArResource to ArResources arResources.ArResource.Add(new ArResource { CommonAttributes = commonAttributes, ArType = Dictionary.ARSM_YOUTUBE, Platforms = platforms }); // Create ARSM-Facebook commonAttributes = new CommonAttributes(); platforms = new Platforms(); platform = new Platform(); processors = new Processors(); // Create Attribute in CommonAttributes commonAttributes.Attribute = new List<Attribute> { new Attribute { Key = Dictionary.AR_KEY_URL, Value = txtFacebookUrl.Text } }; // Create Platform with Processor in Platforms platform.PlatformId = Dictionary.AR_PLATFORM_ID_ANDROID; processors.Processor = new Processor {ProcessorType = Dictionary.AR_PROCESSOR_TYPE_FACEBOOK}; platforms.Platform = platform; // Add ArResource to ArResources arResources.ArResource.Add(new ArResource { CommonAttributes = commonAttributes, ArType = Dictionary.ARSM_FACEBOOK, Platforms = platforms }); // Create ARSM-Text commonAttributes = new CommonAttributes(); platforms = new Platforms(); platform = new Platform(); processors = new Processors(); // Create Attribute in CommonAttributes commonAttributes.Attribute = new List<Attribute> { new Attribute { Key = Dictionary.AR_KEY_NAME, Value = txtArName.Text }, new Attribute { Key = Dictionary.AR_KEY_DIRECTOR, Value = txtDirector.Text }, new Attribute { Key = Dictionary.AR_KEY_ACTOR, Value = txtActor.Text }, new Attribute { Key = Dictionary.AR_KEY_DESCRIPTION, Value = txtDescription.Text } }; // Create Platform with Processor in Platforms platform.PlatformId = Dictionary.AR_PLATFORM_ID_ANDROID; processors.Processor = new Processor {ProcessorType = Dictionary.AR_PROCESSOR_TYPE_TEXTVIEW}; platforms.Platform = platform; // Add ArResource to ArResources arResources.ArResource.Add(new ArResource { CommonAttributes = commonAttributes, ArType = Dictionary.ARSM_TEXT, Platforms = platforms }); var rootObject = new RootObject {ArResources = arResources}; // Save JSON to server var arResourcesJson = JsonConvert.SerializeObject(rootObject); var jsonPath = Dictionary.PATH_UPLOADED_TARGET + target.Url + ".json"; File.Create(Server.MapPath(jsonPath)).Dispose(); // ReSharper disable once AssignNullToNotNullAttribute - Added by PhucLS File.WriteAllText(Server.MapPath(jsonPath), arResourcesJson); // Get the object used to communicate with the server. string uri = "ftp://[email protected]/www.ara288.somee.com/Ar_Data/Json/"; var request = (FtpWebRequest) WebRequest.Create(uri + target.Url + ".json"); request.Method = WebRequestMethods.Ftp.UploadFile; // FTP site logon request.Credentials = new NetworkCredential(Authentication.FTP_USER, Authentication.FTP_PASSWORD); // Copy the entire contents of the file to the request stream. var sourceStream = new StreamReader(Server.MapPath(jsonPath)); var fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd()); sourceStream.Close(); request.ContentLength = fileContents.Length; // Upload the file stream to the server. var requestStream = request.GetRequestStream(); requestStream.Write(fileContents, 0, fileContents.Length); requestStream.Close(); // Get the response from the FTP server. var response = (FtpWebResponse) request.GetResponse(); // Close the connection = Happy a FTP server. response.Close(); // Save new target ClientServiceFactory.TargetService.SaveNewTarget(target); Response.Redirect(Routes.NAVIGATION_TO_CAMPAIGN_PAGE_OF_COMPANY); } catch (FaultException<TargetNameAlreadyExistException> ex) { lblMessage.Text = ex.Detail.MessageError; } catch (FaultException<MissionAlreadyDeletedException> ex) { lblMessage.Text = ex.Detail.MessageError; } }