Building the Jobs Web Service

Throughout this chapter, you will examine specific methods that you will place within the Jobs web service. To create the Jobs web service, perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. Within the New Project dialog box Project Types list, click Visual Basic Projects. Then, within the Templates field, click ASP.NET Web Service. Finally, within the Location field, specify the folder within which you want to store the program and the program name Jobs. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the service’s components.

  3. Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code add the program statements in Listing 16.1.

    Listing 16.1 Jobs.asmx.vb

    start example
    Private Function ValidatePassword(ByVal Username As _ Â String, ByVal Password As String) As Boolean     If (UCase(Username) = UCase("Username")) And _     Â (UCase(Password) = UCase("Password")) Then           ValidatePassword = True     Else           ValidatePassword = False     End If End Function
    end example

As you examine additional methods in this chapter, you can edit the Jobs web service and add the method code.

Posting an RTF-Based Job or Resume

The Jobs web service supports two methods that let employees and employers post an RTF-based resume or job listing:

boolean PostRTFResume(string Username, string ÂPassword, string Category, string Description, byte Data()) boolean PostRTFJobListing(string Username, string ÂPassword, string Company, string Description, byte Data()) 

When a program calls one of the web-service methods, code within the method will first validate the username and password by calling the ValidatePassword method. Next, based on the category or company name, the methods will select a folder within which it will store the posting. Listing 16.2 implements the PostRTFResume and the PostRTFJobListing methods.

Listing 16.2 Jobs.asmx.vb

start example
Imports System.IO <WebMethod()> Public Function PostRTFResume(ByVal Username As String, _ Â  ByVal Password As String, ByVal Category As String, ByVal _ Â  Description As String, ByVal Data As Byte()) As Boolean     If Not Context.Request.IsSecureConnection() Then       Throw New Exception("Method requires secure connection")     End If     If (ValidatePassword(Username, Password)) Then       Try         Dim ValidCategory As Boolean = False         If (UCase(Category) = "MANAGEMENT") Then           ValidCategory = True         ElseIf (UCase(Category) = "PROGRAMMER") Then           ValidCategory = True         ElseIf (UCase(Category) = "INFORMATION_TECHNOLOGY") Then           ValidCategory = True         ElseIf (UCase(Category) = "ADMIN") Then           ValidCategory = True         End If         If (Not ValidCategory) Then           Throw New Exception("Invalid resume category specified")         End If         If (File.Exists("C:\Jobs\Resumes\" & Category & "\" & _         Â  Description & ".RTF")) Then           Throw New Exception("Resume Description in use")         End If         Dim OutputFile As FileStream         Try           Dim Filename As String = "C:\Jobs\Resumes\" & Category & _           Â "\" & Description & ".RTF"           OutputFile = File.Open(Filename, FileMode.Create, _           Â FileAccess.Write)           OutputFile.Write(Data, 0, Data.Length)           OutputFile.Close()         Catch Ex As Exception           Throw New Exception("Error writing RTF file")         End Try       Catch Ex As Exception         Throw New Exception(Ex.Message)       End Try       PostRTFResume = True     Else       Throw New Exception("Invalid username and password specified")     End If End Function <WebMethod()> Public Function PostRTFJobOpening(ByVal Username _ Â  As String, ByVal Password As String, ByVal Company As String, _ Â  ByVal Description As String, ByVal Data As Byte()) As Boolean     If Not Context.Request.IsSecureConnection() Then       Throw New Exception("Method requires secure connection")     End If     If (ValidatePassword(Username, Password)) Then       Try         If (Not Directory.Exists("C:\Jobs\Postings\" & Company)) Then           Directory.CreateDirectory("C:\Jobs\Postings\" & Company)         End If         If (File.Exists("C:\Jobs\Postings\" & Company & "\" & _         Â Description & ".RTF")) Then           Throw New Exception("Job description in use")         End If         Dim OutputFile As FileStream         Try           Dim Filename As String = "C:\Jobs\Postings\" & Company & _           Â "\" & Description & ".RTF"           OutputFile = File.Open(Filename, FileMode.Create, _           Â FileAccess.Write)           OutputFile.Write(Data, 0, Data.Length)           OutputFile.Close()         Catch Ex As Exception           Throw New Exception("Error writing RTF file")         End Try       Catch Ex As Exception         Throw New Exception(Ex.Message)       End Try       PostRTFJobOpening = True     Else       Throw New Exception("Invalid username and password specified")     End If End Function
end example

To begin, the PostRTFResume method first validates the category passed to it by the caller. If the category is valid, the code then checks if a resume already exists in the category folder with the same name as the description provided. If no conflicting file exists, the program writes the contents of the data array to the specified file. The PostRTFJobOpening method performs similar processing, creating an RTF file within the specified company folder.

In addition, the Jobs web service provides the ResumeCategories method that returns an array of character strings that contains the categories to which a user can post a resume:

<WebMethod()> Public Function ResumeCategories() As String()     Dim Categories(3) As String     Categories(0) = "Management"     Categories(1) = "Programmer"     Categories(2) = "Information_Technology"     Categories(3) = "Admin"     ResumeCategories = Categories End Function

Submitting RTF-Based Text to the Jobs Web Service

The Visual Basic .NET program in Listing 16.3, SubmitRTF.vb, illustrates how a program would submit an employee resume or job description to the Jobs web service. When you run the program, it will display a form, as shown in Figure 16.6, that contains fields you can use to enter a username, password, category or company name, as well as the RTF-based content.

click to expand
Figure 16.6: Using a form to submit RTF-based content to the Jobs web service

Because the user is sending a username and password to the Jobs web service, the program will use the SSL-based encryption to protect the data it exchanges with the web service. As you will recall from Chapter 9, to create an encrypted SSL link to the web service, the code simply uses https:// when it specifies the location of the service’s WSDL content. To create the SubmitRTF.vb program, perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. Within the New Project dialog box Project Types list, click Visual Basic Projects. Then, within the Templates field, click Windows Application. Finally, within the Location field, specify the folder within which you want to store the program and the program name SubmitRTFResume. Select OK. Visual Studio .NET will display a form onto which you can drag and drop the program’s controls (label, buttons, and text box).

  3. Using the Toolbox, drag and drop the button and text boxes, list box, label, and hyperlink label shown in Figure 16.6 onto the form.

  4. Select the Project menu Add Web Reference option. Visual Studio .NET will display the Add Web Reference dialog box.

  5. Within the Address field, type https://localhost/Jobs/Service1.asmx?WSDL (note the use of the https: prefix that establishes an SSL-based encrypted connection) and press Enter. The dialog box will load the file’s contents. Click the Add Reference button.

  6. Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code add the program statements in Listing 16.3.

Listing 16.3 SubmitRTFResume.vb

start example
Imports System.IO Private Sub Page_Load(ByVal sender As System.Object, _ Â ByVal e As System.EventArgs) Handles MyBase.Load    Try      Dim WS As New localhost.Service1()      Dim Categories() As String      Categories = WS.ResumeCategories()      Dim Str As String      If (ListBox1.Items.Count = 0) Then        For Each Str In Categories           ListBox1.Items.Add(Str)        Next      End If    Catch Ex As Exception      TextBox4.Text = Ex.Message    End Try End Sub Private Sub Button1_Click(ByVal sender As System.Object, _ ÂByVal e As System.EventArgs) Handles Button1.Click   If (TextBox1.Text.Length = 0) Then     TextBox4.Text = "Must specify username"   ElseIf (TextBox2.Text.Length = 0) Then     TextBox4.Text = "Must specify password"   ElseIf (ListBox1.SelectedItem Is Nothing) Then     TextBox4.Text = "Must select category"   ElseIf (TextBox3.Text.Length = 0) Then     TextBox4.Text = "Must specify description"   ElseIf (RichTextBox1.Text.Length = 0) Then     TextBox4.Text = "Must post a resume"   Else     TextBox4.Text = ""     Form.ActiveForm.Refresh()     Try       Dim WS As New localhost.Service1()       RichTextBox1.SaveFile("Temp.RTF")       Dim InputFile As FileStream       Dim FileLength As Long       InputFile = File.Open("TEMP.RTF", FileMode.Open, _       ÂFileAccess.Read)       FileLength = InputFile.Length       Dim DataBuffer(FileLength - 1) As Byte       InputFile.Read(DataBuffer, 0, FileLength)       InputFile.Close()       If WS.PostRTFResume(TextBox1.Text, TextBox2.Text, _       ÂListBox1.SelectedItem, TextBox3.Text, DataBuffer) Then          TextBox4.Text = "Resume posted"       Else          TextBox4.Text = "Error posting resume"       End If     Catch Ex As Exception       TextBox4.Text = "Error in processing " & Ex.Message     End Try   End If End Sub
end example

After the user enters a username, password, category, description, and a resume, and then clicks the Submit Resume button, the program temporarily stores the rich-text format content in a file named Temp.RTF. Then, the program opens the file and reads the file’s contents into a byte array that the program can pass to the remote web service.

Note 

The program passes the data using the byte array because the .NET environment generates an exception when the program tries to pass a string containing the RTF data to the remote server. Hopefully, in a future release of Visual Studio .NET, the program can eliminate this requirement and pass a string that contains the RTF data to the remote web service.

As you might imagine, a program that wants to submit an RTF-based job description to the web service would perform similar processing.

Posting a Resume or Job Description Manually

As discussed, many job sites on the Web let users manually build a resume using a form whose content the user then submits to a remote server. In a similar way, a site may let a company use a form to manually create a job posting. The Jobs web service provides two methods that let an employee or company submit a string that contains XML-based content that describes a resume or job opportunity:

boolean PostManualResume(string Username, string Password, Âstring Category, string Description, string XMLData) boolean PostManualJobOpening(string Username, string Password, Âstring Company, string Description, string XMLData)

When the web service receives the data from the user or employee, the web service might parse the XML entries and store the data values within the corresponding fields of a database. The Jobs web service simply stores the XML entries in a file with the .xml extension. Later, when a user wants to retrieve and display a resume or posting, the web service will read the XML content and build an RTF file on the fly that it returns to the client program.

Listing 16.4 implements the PostManualResume and PostManualJobOpening methods.

Listing 16.4 Jobs.asmx.vb

start example
<WebMethod()> Public Function PostManualResume(ByVal Username As _     String, ByVal Password As String, ByVal Category As String, _     ByVal Description As String, ByVal XMLData As String) As Boolean     If Not Context.Request.IsSecureConnection() Then       Throw New Exception("Method requires secure connection")     End If     If (ValidatePassword(Username, Password)) Then       Try         Dim ValidCategory As Boolean = False         If (UCase(Category) = "MANAGEMENT") Then           ValidCategory = True         ElseIf (UCase(Category) = "PROGRAMMER") Then           ValidCategory = True         ElseIf (UCase(Category) = "INFORMATION_TECHNOLOGY") Then           ValidCategory = True         ElseIf (UCase(Category) = "ADMIN") Then           ValidCategory = True         End If         If (Not ValidCategory) Then           Throw New Exception("Invalid resume category specified")         End If         If (File.Exists("C:\Jobs\Resumes\" & Category & "\" & _         Â  Description & ".RTF")) Then           Throw New Exception("Resume Description in use")         End If         Dim Filename As String = "C:\Jobs\Resumes\" & _            Category & "\" & Description & ".XML"         Try           Dim OutputFile As New StreamWriter(Filename)           OutputFile.Write(XMLData)           OutputFile.Close()         Catch Ex As Exception           Throw New Exception("Error writing XML file")         End Try       Catch Ex As Exception         Throw New Exception(Ex.Message)       End Try       PostManualResume = True     Else       Throw New Exception("Invalid username and password specified")     End If End Function <WebMethod()> Public Function PostManualJobOpening(ByVal Username As _ Â   String, ByVal Password As String, ByVal Company As String, _ Â   ByVal Description As String, ByVal XMLData As String) As Boolean      If Not Context.Request.IsSecureConnection() Then        Throw New Exception("Method requires secure connection")      End If      If (ValidatePassword(Username, Password)) Then        Try          If (Not Directory.Exists("C:\Jobs\Postings\" & Company)) Then            Directory.CreateDirectory("C:\Jobs\Postings\" & Company)          End If          If (File.Exists("C:\Jobs\Postings\" & Company & "\" & _          Â Description & ".RTF")) Then            Throw New Exception("Job description in use")          End If          Dim Filename As String = "C:\Jobs\Postings\" & _          Â Company & "\" & Description & ".XML"          Try            Dim OutputFile As New StreamWriter(Filename)            OutputFile.Write(XMLData)            OutputFile.Close()          Catch Ex As Exception            Throw New Exception("Error writing XML file")          End Try        Catch Ex As Exception          Throw New Exception(Ex.Message)        End Try        PostManualJobOpening = True      Else        Throw New Exception("Invalid username and password specified")      End If End Function
end example

To begin, the PostManualResume method first validates the category passed to it by the caller. If the category is valid, the code then checks if a resume already exists in the category folder with the same name as the description provided. If no conflicting file exists, the program writes the XML contents of the data string to the specified file. The PostManualJobOpening method performs similar processing, creating an RTF file within the specified company folder.

Submitting a Manual Resume or Job Description

XML is a very powerful tool in that it lets developers provide data to a program that is self-describing. In other words, the data not only contains values, but also, information about the fields themselves. The Visual Basic .NET program in Listing 16.5, SubmitJobDescription.vb, provides a form, as shown in Figure 16.7, that a company can use to define a job opportunity.

click to expand
Figure 16.7: Using a form to manually build a job opportunity

After the user enters the data and clicks the Submit Job Opportunity button, the program groups the data fields within the XML tags. Assume, for example, the user entered the data shown in Figure 16.7. The program, in turn, would group the data within the XML tags:

<Dataset>   <Table>      <Title>Senior Programmer</Title>      <Salary>$60,000</Salary>      <Education>MS in Computer Science</Education>      <Experience>Four years of object-oriented programming</Experience>      <Skills>C++ and Java. Windows and Linux</Skills>      <Contact>Jim@ComputerDesign.com</Contact>   </Table> </Dataset> 

To create the SubmitJobDescription.vb program (Listing 16.5), perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. Within the New Project dialog box Project Types list, click your mouse on Visual Basic Projects. Then, within the Templates field, click your mouse on Windows Application. Finally, within the Location field, specify the folder within which you want to store the program and the program name SubmitJobDescription. Select OK. Visual Studio .NET will display a form onto which you can drag and drop the program’s controls (label, buttons, and text box).

  3. Using the Toolbox, drag and drop the button and text boxes, list box, label, and hyperlink label previously shown in Figure 16.7 onto the form.

  4. Select the Project menu Add Web Reference option. Visual Studio .NET will display the Add Web Reference dialog box.

  5. Within the Address field, type https://localhost/Jobs/Service1.asmx?WSDL (note the use of the https: prefix that establishes an SSL-based encrypted connection) and press Enter. The dialog box will load the file’s contents. Click your mouse on the Add Reference button.

  6. Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code add the program statements in Listing 16.5.

Listing 16.5 SubmitJobDescription.vb

start example
Private Sub Button1_Click(ByVal sender As System.Object, _ Â ByVal e As System.EventArgs) Handles Button1.Click    Dim XMLData As String    If (TextBox1.Text.Length = 0) Then      TextBox6.Text = "Must specify username"    ElseIf (TextBox2.Text.Length = 0) Then      TextBox6.Text = "Must specify password"    ElseIf (TextBox3.Text.Length = 0) Then      TextBox6.Text = "Must specify company"    ElseIf (TextBox3.Text.Length = 0) Then      TextBox6.Text = "Must specify description"    ElseIf (TextBox4.Text.Length = 0) Then      TextBox6.Text = "Must specify job posting details"    Else      TextBox6.Text = ""      Form.ActiveForm.Refresh()      Try        Dim WS As New localhost.Service1()        XMLData = "<Dataset><Table>"        XMLData = XMLData & "<Title>" & TextBox5.Text & "</Title>"        XMLData = XMLData & "<Salary>" & TextBox7.Text & "</Salary>"        XMLData = XMLData & "<Education>" & TextBox8.Text & _        Â"</Education>"        XMLData = XMLData & "<Experience>" & TextBox9.Text & _        Â"</Experience>"        XMLData = XMLData & "<Skills>" & TextBox10.Text & "</Skills>"        XMLData = XMLData & "<Contact>" & TextBox11.Text & "</Contact>"        XMLData = XMLData & "</Table></Dataset>"        If WS.PostManualJobOpening(TextBox1.Text, TextBox2.Text, _        Â TextBox3.Text, TextBox4.Text, XMLData) Then          TextBox6.Text = "Resume posted"        Else          TextBox6.Text = "Error posting resume"        End If      Catch Ex As Exception        TextBox6.Text = "Error in processing " & Ex.Message      End Try    End If End Sub 
end example

When the user clicks the Submit button, the following happens:

  1. The code insures the user has specified values for each of the required fields (username, password, company, and description).

  2. The program builds a string that contains XML-based entries that describe each of the fields and the field’s corresponding data.

  3. The program uses the Jobs web service to post the job on the server.

As you can guess, the steps to build a resume manually would be quite similar, with the program building a string that contained XML entries that described the user’s resume content.

Deleting a Resume or Job Listing

Over time, an employee or a company may want to delete a resume or job posting. To allow them to do so, the Jobs web service provides the following methods:

boolean DeleteResume(string Username, string Password, string     Category, string Description) boolean DeleteJobPosting(string Username, string Password, string     Company, string Description)

As you can see, each method requires a username and password and information about the entry to delete. Listing 16.6 implements the DeleteResume and DeleteJobPosting methods.

Listing 16.6 Jobs.asmx.vb

start example
<WebMethod()> Public Function DeleteResume(ByVal Username As _      ÂString, ByVal Password As String, ByVal Category As String, _      ÂByVal Description As String) As Boolean      If Not Context.Request.IsSecureConnection() Then        Throw New Exception("Method requires secure connection")      End If      If (ValidatePassword(Username, Password)) Then        If (File.Exists("C:\Jobs\Resume\" & Category & "\" & _        Â Description & ".RTF")) Then          File.Delete("C:\Jobs\Resume\" & Category & "\" & _        Â Description & ".RTF")          DeleteResume = True        Else          DeleteResume = False        End If      Else        Throw New Exception("Invalid username and password specified")      End If  End Function  <WebMethod()> Public Function DeleteJobPosting(ByVal Username _     ÂAs String, ByVal Password As String, ByVal Company As String, _     Â ByVal Description As String) As Boolean     If Not Context.Request.IsSecureConnection() Then       Throw New Exception("Method requires secure connection")     End If     If (ValidatePassword(Username, Password)) Then       If (File.Exists("C:\Jobs\Postings\" & Company & "\" & _       Â Description & ".RTF")) Then         File.Delete("C:\Jobs\Postings\" & Company & "\" & _       Â  Description & ".RTF")         DeleteJobPosting = True       Else         DeleteJobPosting = False       End If     Else       Throw New Exception("Invalid username and password specified")     End If End Function
end example

After each method validates the username and password, the methods then verify that the specified resume or job posting exists and if so, uses the File class Delete method to delete the corresponding file.




. NET Web Services Solutions
.NET Web Services Solutions
ISBN: 0782141722
EAN: 2147483647
Year: 2005
Pages: 161
Authors: Kris Jamsa

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net