/// <summary> /// ExecuteLongRunningCommand /// </summary> /// <param name="longRunningCommandObject"></param> public static void ExecuteLongRunningCommand(object longRunningCommandObject) { //Assume that the invocation of this was already outside of the gui thread // -- a good thing since we're going to "sleep" between status polls... LongRunningCommand longRunningCommand = (LongRunningCommand)longRunningCommandObject; if (ServiceFacade.UseRemoteServices) { //verify the taskType client-side... // no reason to make a round-trip to the server if we can fail faster Services.Native.ScheduledTaskTypes taskType = (ScheduledTaskTypes)Enum.Parse( typeof(ScheduledTaskTypes), longRunningCommand.Command); INativeSession nativeClient = ServiceFacade.CreateNativeSessionProxy(); Services.Native.NativeMethodTransportObject resultObject = ServiceFacade.InvokeNativeMethod(nativeClient, (int)ServiceCodes.MobiusTaskSchedulerService, (int)MobiusTaskSchedulerService.SubmitJob, new Services.Native.NativeMethodTransportObject(new object[] { (int)taskType, longRunningCommand.CommandArgs })); Services.Native.ScheduledTask task = (Services.Native.ScheduledTask)resultObject.Value; if (task != null) { //do a SHORT sleep before the first check in case the task failed or completed very quickly task.SuggestedTimeBetweenPolls = TimeSpan.FromSeconds(0.03); while (task.Status == ScheduledTaskStatus.Running) { if (longRunningCommand.UpdateProgress != null && task.Result != null && !String.IsNullOrEmpty(task.Result.ToString())) { longRunningCommand.UpdateProgress(task.Result.ToString(), ComOps.UmlautMobius.String, longRunningCommand.AllowCancel); } Thread.Sleep(task.SuggestedTimeBetweenPolls); resultObject = ServiceFacade.InvokeNativeMethod(nativeClient, (int)ServiceCodes.MobiusTaskSchedulerService, (int)MobiusTaskSchedulerService.CheckJobStatus, new Services.Native.NativeMethodTransportObject(new object[] { task.JobId })); task = (Services.Native.ScheduledTask)resultObject.Value; } longRunningCommand.Result = task.Result; } ((System.ServiceModel.IClientChannel)nativeClient).Close(); longRunningCommand.IsRunning = false; longRunningCommand.SignalCompletion(); if (longRunningCommand.OnCompletion != null) { longRunningCommand.OnCompletion(); } if (longRunningCommand.HideProgress != null) { longRunningCommand.HideProgress(); } if (longRunningCommand.ShowResponse != null && task.Result != null) { longRunningCommand.ShowResponse(task.Result.ToString()); } } }
/// <summary> /// Copy file from client to server /// </summary> /// <param name="clientFile2"></param> /// <param name="serverFile"></param> public static void CopyToServer( string clientFile, string serverFile) { string clientFile2 = SharePointUtil.CacheLocalCopyIfSharePointFile(clientFile); if (ServiceFacade.UseRemoteServices) { FileStream fs = new FileStream(clientFile2, FileMode.Open, FileAccess.Read); while (true) { byte[] buffer = new byte[UAL.ServerFile.TransferChunkSize]; int bufLen = fs.Read(buffer, 0, buffer.Length); if (bufLen < UAL.ServerFile.TransferChunkSize) // at end { fs.Close(); byte[] buffer2 = new byte[bufLen]; Array.Copy(buffer, buffer2, bufLen); buffer = buffer2; } NativeMethodTransportObject resultObject = ServiceFacade.CallServiceMethod( ServiceCodes.MobiusFileService, MobiusFileService.CopyToServer, new object[] { buffer, serverFile }); if (bufLen < UAL.ServerFile.TransferChunkSize) { break; } serverFile = null; // null for subsequent calls } } else // direct call { FileStream fs = new FileStream(clientFile2, FileMode.Open, FileAccess.Read); while (true) { byte[] buffer = new byte[UAL.ServerFile.TransferChunkSize]; int bufLen = fs.Read(buffer, 0, buffer.Length); if (bufLen < UAL.ServerFile.TransferChunkSize) // at end { fs.Close(); byte[] buffer2 = new byte[bufLen]; Array.Copy(buffer, buffer2, bufLen); buffer = buffer2; } UAL.ServerFile.CopyToServer(serverFile, buffer); if (bufLen < UAL.ServerFile.TransferChunkSize) { break; } serverFile = null; // null for subsequent calls } } if (clientFile2 != clientFile) { File.Delete(clientFile2); // delete any temp file } return; }
// Removed the 2 following methods and made the id and updateUi paramters optional on the actual method. //public static void Write(UserObject uo) //{ // Write(uo, -1); // return; //} //public static void Write( // write user object // UserObject uo, // int id) //{ // Write(uo, id, true); //} /// <summary> /// Write user object to database /// </summary> /// <param name="userObject"></param> /// <param name="id"></param> /// <param name="updateUi"></param> public static void Write( // write user object UserObject userObject, int id = -1, bool updateUi = true) { bool updated = false; // used for proper updating of contents tree UserObject existingUo = null; bool tempObject = Lex.Eq(userObject.ParentFolder, "TEMP_FOLDER"); if (!tempObject) // if not a temp object then see if UI needs updating { if (userObject.Id > 0) // if id defined see if object with same id exists { existingUo = ReadHeader(userObject.Id); if (existingUo != null) { updated = true; } } if (existingUo == null) // see if object with same name exists { existingUo = ReadHeader(userObject.Type, userObject.Owner, userObject.ParentFolder, userObject.Name); if (existingUo != null) { Delete(existingUo.Id, true); // delete any existing object with id & update UI } } } if (ServiceFacade.UseRemoteServices) { _sUo = userObject.SerializeBinary(); Mobius.Services.Native.INativeSession nativeClient = ServiceFacade.CreateNativeSessionProxy(); Services.Native.NativeMethodTransportObject resultObject = ServiceFacade.InvokeNativeMethod(nativeClient, (int)Services.Native.ServiceCodes.MobiusUserObjectService, (int)Services.Native.ServiceOpCodes.MobiusUserObjectService.Write, new Services.Native.NativeMethodTransportObject( new object[] { _sUo, id })); ((System.ServiceModel.IClientChannel)nativeClient).Close(); if (resultObject != null && resultObject.Value != null) { // Id/UpdateDT may have been changed during the write operation so update the UO in place UserObject uo2 = UserObject.DeserializeBinary((byte[])resultObject.Value); userObject.Id = uo2.Id; userObject.UpdateDateTime = uo2.UpdateDateTime; } else // failed { string msg = "Failed to write user object:\r\n"; try { msg += userObject.Serialize(); } catch (Exception ex) { msg += ex.Message; } throw new Exception(msg); } } else { UAL.UserObjectDao.Write(userObject, id); } if (updateUi && IUserObjectIUD != null) // notify UI of change { //if (uo.ParentFolderType != FolderTypeEnum.None) // don't update the content tree for items not in a folder //{ if (updated) { IUserObjectIUD.UserObjectUpdated(userObject); } else { IUserObjectIUD.UserObjectInserted(userObject); } //} } }
/// <summary> /// Copy file from server to client /// </summary> /// <param name="serverFile"></param> /// <param name="clientFile2"></param> public static void CopyToClient( string serverFile, string clientFile) { FileStream fw = null; try { string tempFile = TempFile.GetTempFileName(); if (ServiceFacade.UseRemoteServices) { fw = new FileStream(tempFile, FileMode.Create, FileAccess.Write); while (true) { NativeMethodTransportObject resultObject = ServiceFacade.CallServiceMethod( ServiceCodes.MobiusFileService, MobiusFileService.CopyToClient, new object[] { serverFile }); byte[] buffer = (byte[])resultObject.Value; if (buffer == null) { break; } fw.Write(buffer, 0, buffer.Length); if (buffer.Length < UAL.ServerFile.TransferChunkSize) { break; } serverFile = null; // null to get subsequent chunks System.Windows.Forms.Application.DoEvents(); } fw.Close(); } else // direct call { fw = new FileStream(tempFile, FileMode.Create, FileAccess.Write); while (true) { byte[] buffer = UAL.ServerFile.CopyToClient(serverFile); if (buffer == null) { break; } fw.Write(buffer, 0, buffer.Length); if (buffer.Length < UAL.ServerFile.TransferChunkSize) { break; } serverFile = null; // null to get subsequent chunks System.Windows.Forms.Application.DoEvents(); } fw.Close(); } bool normalFile = !SharePointUtil.IsSharePointName(clientFile); if (normalFile) { // normal file, move temp file to dest file FileUtil.ReplaceFile(clientFile, tempFile); } else // sharepoint { SharePointUtil.CopyToSharePoint(tempFile, clientFile); File.Delete(tempFile); // delete any temp file } return; } catch (Exception ex) // close file on any exception { if (fw != null) { try { fw.Close(); } catch { } } throw new Exception(ex.Message, ex); } }