简体   繁体   中英

c# - How to call/link a workflow on a web application from a c# program?

I have a C# (WinForms) application that can scan documents via a printer. After scanning, I will be able to enter document details on it and have a button to finalize the documents. The documents details and info will be stored in my database ABC in certain tables.

Now, I have another web application written in Java(IntelliJ) that has some button functionality to upload documents and then start a workflow and route it to another user to approve the document. I won't go into detail on the specifics. This application also connects to the same database ABC.

So now comes the tougher part, I need to link these two applications in a way that when I finalize my document on the C# application, it has to auto trigger the workflow on the web application side. Rather than manually starting the workflow on the web application, it would just call or trigger the workflow, so I do not need to access the web application at all for the process to start.

 private void FinButton_Click(object sender, EventArgs e)
        {


            int count = 0;
            var txtBoxFields = new List<TextBox>
            {
                textBox1,
                textBox2,
                textBox3,
                textBox4,
                textBox5,
                textBox6,
                textBox7,
                textBox8,
                textBox9,
                textBox10,
                textBox11,
                textBox12,
                textBox13,
                textBox14,
                textBox15
            };

            var templateFields = new List<String>
            {
                "T1",
                "T2",
                "T3",
                "T4",
                "T5",
                "T6",
                "T7",
                "T8",
                "T9",
                "T10",
                "T11",
                "T12",
                "T13",
                "T14",
                "T15"
            };

            //long tid = 0;

            //Start insert query into templatebatch table in db
            var dbConnection2 = DBConnection.Instance();
            dbConnection2.DatabaseName = ConfigurationManager.AppSettings["dbName"];
            if (dbConnection2.IsConnect())
            {
                bool test = true;

                for (int i = 1; i <= 15; i++)
                {
                    var input = txtBoxFields[i - 1].Text;
                    var insertQuery = "INSERT INTO templateinfo(TID, THEADER, " + templateFields[i - 1] + ") VALUES(@tid, @theader,@t" + i + ")";
                    var insertCmd = new MySqlCommand(insertQuery, dbConnection2.Connection);
                    insertCmd.Parameters.AddWithValue("@tid", tid);
                    insertCmd.Parameters.AddWithValue("@theader", "N");


                    if (String.IsNullOrEmpty(input))
                    {
                        count = 1;
                        insertCmd.Parameters.AddWithValue("@t" + i, String.Empty);
                        break;
                    }
                    else
                    {
                        if (test)
                        {
                            insertCmd.Parameters.AddWithValue("@t" + i, txtBoxFields[i - 1].Text);
                            insertCmd.ExecuteNonQuery();
                            test = false;

                            var selectQuery = "select TINFOID from templateinfo where TID=" + tid + " and THEADER = 'N'";
                            var selectCmd = new MySqlCommand(selectQuery, dbConnection2.Connection);
                            var selectReader = selectCmd.ExecuteReader();

                            using (MySqlDataReader dr = selectReader)
                            {
                                while (dr.Read())
                                {
                                    tinfoid = Convert.ToInt32(dr["TINFOID"]);
                                }
                            }
                        }
                        else
                        {
                            var updateQuery = "update templateinfo set " + templateFields[i - 1] + "='" + txtBoxFields[i - 1].Text + "' where TINFOID = '" + tinfoid + "' and TID=" + tid + " and THEADER='N'";
                            var updateCmd = new MySqlCommand(updateQuery, dbConnection2.Connection);
                            var updateReader = updateCmd.ExecuteReader();
                            using (var reader = updateReader)
                            {

                            }
                        }
                    }
                }


            }


            if (count == 1)
            {
                //MessageBox.Show("Input field(s) cannot be left empty.");
            }

            //Finalize here
            var client = new LTATImagingServiceClient();
            client.Finalize(userID, tid, tinfoid, batchID);

            Debug.WriteLine(userID + ", " + tid + ", " + tinfoid + ", " + batchID);

            var batchName = templateView.SelectedNode.Text;

            var folderPath = @"C:\temp\batches\" + mastertemplatename + @"\" + subtemplatename + @"\" + batchName + @"\";


            ThumbnailLists.Items.Clear();
          //  var img = Image.FromFile(@"C:\temp\batch-done.png");

            if (ImageBox.Image != null)
            {
                ImageBox.Image.Dispose();
            }

            ImageBox.Image = null;

            try
            {
                using (new Impersonation(_remoteDomain, _remoteUser, _remotePassword))
                {
                  //  MessageBox.Show(_remoteUser);
                  //  MessageBox.Show(_remotePassword);
                    var tPath = @"\\126.32.3.178\PantonSys\SAM\Storage\3\" + mastertemplatename + @"\" + subtemplatename + @"\" + batchName + @"\";
                    bool exists = System.IO.Directory.Exists(tPath);

                    if (!exists)
                    {
                        System.IO.Directory.CreateDirectory(tPath);
                    }

                    string[] fileList = Directory.GetFiles(folderPath, "*");
                    foreach (var file in fileList)
                    {
                        File.Copy(file, tPath + Path.GetFileName(file));
                    }

                    CurrentPageBox.Text = "";
                    NumberPageBox.Text = "";
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                MessageBox.Show(ex.Message);
            }

            var dbConnection = DBConnection.Instance();
            dbConnection.DatabaseName = ConfigurationManager.AppSettings["dbName"];

            if (dbConnection.IsConnect())
            {
                var deleteBatchQuery = "DELETE FROM templatebatch WHERE batchname ='" + templateView.SelectedNode.Text + "'";
                var deleteBatchCmd = new MySqlCommand(deleteBatchQuery, dbConnection.Connection);
                var deleteBatchReader = deleteBatchCmd.ExecuteReader();
                using (var reader = deleteBatchReader)
                {
                    while (reader.Read())
                    {

                    }
                }

                templateView.Nodes.Remove(templateView.SelectedNode);
                Directory.Delete(folderPath, true);
                MessageBox.Show("Successfully Transferred.");

                foreach (var txtFields in txtBoxFields)
                {
                    txtFields.Text = "";
                    txtFields.Enabled = false;
                }

                finButton.Visible = false;
                finButton.Enabled = false;

            }

            bindButton.Visible = false;

        }

Would this be possible to achieve or just being far-fetched? I would appreciate any suggestions or pointers on this. Do let me know if there is anything unclear in my explanation.

EDIT:

Request URL: http://126.32.3.178:8111/process/taskmanager/start/start.jsp
Request Method: POST
Status Code: 200 OK
Remote Address: 126.32.3.178:8111
Referrer Policy: no-referrer-when-downgrade

Is there a way I could call this from the C# application?

You can send your file directly from your C# app with use of Http client . Here is code sample:

private async Task<bool> Upload(string filePath)
{
    const string actionUrl = @"http://126.32.3.178:8111/process/taskmanager/start/start.jsp";
    var fileName = Path.GetFileName(filePath);

    var fileBytes = File.ReadAllBytes(filePath);
    var fileContent = new ByteArrayContent(fileBytes);

    using (var client = new HttpClient())
    using (var formData = new MultipartFormDataContent())
    {
        formData.Add(fileContent, fileName);
        var response = await client.PostAsync(actionUrl, formData);
        return response.IsSuccessStatusCode;
    }
}

Also, note that there maybe some sort of authentication should be performed before you can post a request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM