28
CODE SNIPPETS FOR BWM – MOTOR FRANCHISE PROJECT Module: Web Services Assignment: Team Assignment Team Name: SPOCK Created by Team Members Reviewed by Team Members Created Date 19 April 2012 Revised Date 27 April 2012 Revision No. 1.2 Document Name F03-001

22 code snippet_web_services_2

Embed Size (px)

DESCRIPTION

Topic: Car Franchise Website (Document)Module: Web ServicesDate: Apr 2012Mark: 100%Web site: http://aces-web.aces.shu.ac.uk/students/b1046615/default.aspx

Citation preview

Page 1: 22 code snippet_web_services_2

CODE SNIPPETS

FORBWM – MOTOR FRANCHISE PROJECT

Module: Web Services

Assignment: Team Assignment

Team Name: SPOCK

Created by Team Members

Reviewed by Team Members

Created Date 19 April 2012

Revised Date 27 April 2012

Revision No. 1.2

Document Name F03-001

Page 2: 22 code snippet_web_services_2

DATA VALIDATION

2

Page 3: 22 code snippet_web_services_2

1.VALIDATION BY CONTROLS3

Register User Register Vehicle

Required Field Validators

Page 4: 22 code snippet_web_services_2

2.VALIDATION BY CONTROLS4

Validate Price Validate Price and Mileage

Price must be number Price and mileage must more than 0

Page 5: 22 code snippet_web_services_2

3.VALIDATION BY CONTROLS5

Validate E-mail

User must fill right format of e-mail

Regular Expression Validator

Page 6: 22 code snippet_web_services_2

4. CODE VALIDATION6

Date Validation

Validate Date ex. 31 April 2012 is a incorrect date

Page 7: 22 code snippet_web_services_2

CONSUMING WEB SERVICES FROM BWM

SITE

7

Page 8: 22 code snippet_web_services_2

1.REGISTER VEHICLE8

'======================================================== ' 2.2.2) REGISTER VEHICLE TO BWM '======================================================== '======================================================== ' 2.2.2.1) REGISTER VEHICLE TO BWM (FOR SELL) '======================================================== If SellingStatus = "SELL" Then ReturnFunction = MyProxy.RegisterVehicleForSale(BodyStyle, Model, MyUtils.ConvertDateForBwmWebService(DateRegistered), RegNumber, Mileage, FuelType, CarDescription, Price, DateAdded, "SPOCK-" & UserName) End If '======================================================== ' 2.2.2.2) REGISTER VEHICLE TO BWM (FOR NON-SELL) '======================================================== If SellingStatus = "NON-SELL" Then ReturnFunction = MyProxy.RegisterNewVehicle(BodyStyle, Model, MyUtils.ConvertDateForBwmWebService(DateRegistered), RegNumber, Mileage, FuelType, CarDescription, "SPOCK-" & UserName) End If

Consuming Web Services

Page 9: 22 code snippet_web_services_2

2. UPDATE VEHICLE9

'======================================================== ' 2.2) UPDATE TO BWM WEBSERVICE '======================================================== ReturnFunction = MyProxy.UpdateAVehicle(BodyStyle, Model, MyUtils.ConvertDateForBwmWebService(DateRegistered), RegNumber, Mileage, FuelType, CarDescription, "SPOCK-" & UserName)

Page 10: 22 code snippet_web_services_2

3. UPDATE SOLD VEHICLE(VEHICLE FOR SALES)

10

'======================================================== ' 2.2.1) UPDATE TO BWM WEBSERVICE '======================================================== ReturnFunction = MyProxy.SetVehicleAsSold(RegNumber, Price, UserName) '======================================================== ' 2.2.2) DELETE VEHICLE FROM SALES TABLE ' DELETE IS OPTION IN SELLING PAGE '======================================================== 'ReturnFunction = MyProxy.DeleteVehicleFromSalesTable(RegNumber)

Consuming Web Service

*Option if want mark SOLD in garage DB and Delete in BWM website

Page 11: 22 code snippet_web_services_2

4. DELETE VEHICLE(NON-SELL VEHICLE USING SECURED XML)

11

'======================================================== ' 2.1) AUTHEN TO BWM SITE '======================================================== Dim Auth As New AuthenticationHeader() Auth.UserName = "tutor" Auth.PassWord = "password" MyProxy.AuthenticationHeaderValue = Auth '======================================================== ' 2.2) DELETE VEHICLE FROM REGISTER TABLE (CALL WEBSERVICE) '======================================================== ReturnFunction = MyProxy.DeleteVehicleFromRegisterTable(RegNumber)

Consuming Web Service

Page 12: 22 code snippet_web_services_2

WEB SERVICES PROVIDED BY GARAGE (SPOCK)

12

Page 13: 22 code snippet_web_services_2

1. GET VEHICLE INFORMATION13

Only basic vehicle information transferred to BWM website

• Parameter: Vehicle Register No• Return: DataSet (0 or 1 record)

Page 14: 22 code snippet_web_services_2

2. GET VEHICLE INFORMATION (ALL)

14

All Information collected in garage database

• Parameter: Vehicle Register No• Return: DataSet (0 or 1 record)

Page 15: 22 code snippet_web_services_2

3. GET ORDER INFORMATION(SOLD CAR)

15

This function used for BWM to monitor vehicle sold by garage

• Parameter: From Date and To Date• Return: DataSet• Only: SOLD Vehicle

Page 16: 22 code snippet_web_services_2

CLASSES

16

We separate data layer from business logic by using three classes: Car, User and Utils

Our design- SQL queries are used only in classes (Except to quick search feature)- No SQL queries in VB, aspx, asmx files - User interfaces (aspx files) are separated from business logic (vb files)- Data layer is separated from business logic layer

UI (aspx files)

Business Logic (VB files)

Data Layer (Class files)

DB

Page 17: 22 code snippet_web_services_2

1. CLASS “CAR”17

Variable in class Functions in class

Page 18: 22 code snippet_web_services_2

2. CLASS “USER”18

Authentication and account functions Authentication User

Use SQL DataReader to collect data

Page 19: 22 code snippet_web_services_2

3. CLASS “UTILS”19

Class Utils used for general functionsBenefits: Reusability

Page 20: 22 code snippet_web_services_2

DATA ACCESS (EXAMPLES)

20

Page 21: 22 code snippet_web_services_2

21 1. REGISTER VEHICLE(FUNCTION IN CLASS “CAR”)

'================================================================ ' FUNCTION#1: REGISTER CAR_INFO TO DB '================================================================ Function RegisterCar() As Boolean '============================================================ ' 1) DECLAR VARIABLE '============================================================ Dim ReturnFunction As Boolean = True Dim MyUtils As New Utils Dim SqlCmd As String = "" '============================================================ ' 2) PREPARE INSERT COMMAND '============================================================ SqlCmd = "INSERT INTO TM_CAR " SqlCmd = SqlCmd & "(RegNo, BodyStyle, Model, DateRegistered, Mileage, FuelType, CarDescription, IsEnable, DateAdded, AddedBy, DateUpdated, UpdatedBy, DateSold, SoldBy, Price, SellingStatus, SynStatus, SynTime, Transmission, Colour, InteriorDetails, AdditionalEquipments, StandardEquipments, TechnicalSpecification, CustFullName, CustAddress, CustContactNo, CustEmail, Comment1, Comment2) " SqlCmd = SqlCmd & " VALUES ('" & GetNextRegisterNo() & "' ,'" & BodyStyle & "' ,'" & Model & "' ,'" & MyUtils.ConvertDateForSQL(DateRegistered) & "' ," SqlCmd = SqlCmd & Mileage & " ,'" & FuelType & "','" & CarDescription & "' ," SqlCmd = SqlCmd & IsEnable & " ,'" & MyUtils.ConvertDateForSQL(DateAdded) & "' ,'" & AddedBy & "' ,'" SqlCmd = SqlCmd & MyUtils.ConvertDateForSQL(DateUpdated) & "' ,'" & UpdatedBy & "' ,'" & MyUtils.ConvertDateForSQL(DateSold) & "' ,'" & SoldBy & "' ," SqlCmd = SqlCmd & Price & " ,'" & SellingStatus & "' ,'" & SynStatus & "' ,'" & SynTime & "' ,'" & Transmission & "' ,'" & Colour & "' ,'" & InteriorDetails & "' ,'" & AdditionalEquipments & "' ,'" & StandardEquipments & "' ,'" & TechnicalSpecification & "' ,'" & CustFullName & "' ,'" & CustAddress & "' ,'" & CustContactNo & "' ,'" & CustEmail & "' ,'" & Comment1 & "' ,'" & Comment2 & "') "

'============================================================= ' 3) EXECUTE INSERT COMMAND '============================================================= Dim cnString As String = Global.Resources.Resource.cnString Dim ds As New SqlDataSource Try ds.ConnectionString = cnString ds.InsertCommand = SqlCmd ds.Insert() ReturnFunction = True Catch ReturnFunction = False End Try '============================================================ ' 4) RETURN VARIABLE '============================================================ Return ReturnFunction End Function

Use SqlDataSource to register vehicle

Register Types (SellingStatus Column)

1. SELL: Register For Sales

2. NON-SALES: Register a Vehicle

3. SOLD: Sold Vehicle

Page 22: 22 code snippet_web_services_2

2. UPDATE VEHICLE(FUNCTION IN CLASS “CAR”)

22

'================================================================ ' FUNCTION#6: UPDATE SELLING_CAR_INFO TO DB '================================================================ Public Function UpdateSellingCarInfoByDataSet(ByVal RegNumber As String) As Boolean '============================================================ ' 1) DECLARE VARIABLE '============================================================ Dim MyUtils As New Utils Dim cn As SqlConnection Dim da As SqlDataAdapter Dim ds As New DataSet Dim cnString As String = Global.Resources.Resource.cnString Dim sqlCmd As String = "SELECT * FROM TM_CAR WHERE RegNo = '" & RegNumber & "'" '============================================================ ' 2) GET DATA AND UPDATE DATA TO DB '============================================================ Try '======================================================== ' 2.1) FILL DATA FROM DATABASE TO DATASET '======================================================== cn = New SqlConnection(cnString) da = New SqlDataAdapter(sqlCmd, cn) da.Fill(ds, "TM_CAR") '======================================================== ' 2.2) NOT FOUND REG_NO '======================================================== If ds.Tables(0).Rows.Count <> 1 Then Return False End If

'============================================================ ' 2.3) FILL DATA TO DATASET '============================================================ ds.Tables(0).Rows(0).Item("DateUpdated") = Now ds.Tables(0).Rows(0).Item("UpdatedBy") = UpdatedBy ds.Tables(0).Rows(0).Item("DateSold") = DateSold ds.Tables(0).Rows(0).Item("SoldBy") = SoldBy ds.Tables(0).Rows(0).Item("Price") = Price ds.Tables(0).Rows(0).Item("SellingStatus") = SellingStatus ds.Tables(0).Rows(0).Item("SynStatus") = SynStatus ds.Tables(0).Rows(0).Item("SynTime") = SynTime ds.Tables(0).Rows(0).Item("CustFullName") = CustFullName ds.Tables(0).Rows(0).Item("CustAddress") = CustAddress ds.Tables(0).Rows(0).Item("CustContactNo") = CustContactNo ds.Tables(0).Rows(0).Item("CustEmail") = CustEmail ds.Tables(0).Rows(0).Item("Comment2") = Comment2 '============================================================ ' 4) UPDATE DATA IN DB '============================================================ Dim cmb As New SqlCommandBuilder(da) da.Update(ds, "TM_CAR") ds.AcceptChanges() '============================================================ ' 5) UPDATE SUCCESSFUL '============================================================ Return True Catch ex As Exception '============================================================ ' 6) UPDATE ERROR '============================================================ Return False End Try End Function

Use DataSet and SQLDataAdaptor to update database

Page 23: 22 code snippet_web_services_2

3. DELETE VEHICLE(FUNCTION IN CLASS “CAR”)

23

'================================================================ ' FUNCTION#8: DELETE CAR_INFO FROM DATABASE '================================================================ Public Function DeleteCarInfo(ByVal RegNumber As String) As Boolean '============================================================ ' 1) RETURN VARIABLE '============================================================ Dim ReturnFunction As Boolean = True Dim MyUtils As New Utils '============================================================ ' 2) PREPARE INSERT COMMAND '============================================================ Dim SqlCmd As String = "" SqlCmd = "DELETE TM_CAR " SqlCmd = SqlCmd & "WHERE RegNo = '" & RegNumber & "' " '============================================================ ' 3) EXECUTE DELETE COMMAND '============================================================ Dim cnString As String = Global.Resources.Resource.cnString Dim ds As New SqlDataSource Try ds.ConnectionString = cnString ds.DeleteCommand = SqlCmd ds.Delete() ReturnFunction = True Catch ReturnFunction = False End Try '============================================================ ' 4) RETURN VARIABLE '============================================================ Return ReturnFunction End Function

'================================================================ ' FUNCTION#10: DELETE VEHICLE FROM REGISTER TABLE (CALL WEBSERVICE) '================================================================ Function DeleteVehicleFromRegisterTableAtBwm(ByVal RegNumber As String) As Boolean '============================================================ ' 1) RETURN VARIABLE '============================================================ Dim ReturnFunction As Boolean = False Dim MyUtils As New Utils Dim MyProxy = New BMWHOWebService.BMWHOWebService '============================================================ ' 2) PREPARE INSERT COMMAND '============================================================ Try '======================================================== ' 2.1) AUTHEN TO BWM SITE '======================================================== Dim Auth As New AuthenticationHeader() Auth.UserName = "tutor" Auth.PassWord = "password" MyProxy.AuthenticationHeaderValue = Auth '======================================================== ' 2.2) DELETE VEHICLE FROM REGISTER TABLE (CALL WEBSERVICE) '======================================================== ReturnFunction = MyProxy.DeleteVehicleFromRegisterTable(RegNumber) '============================================================ ' 3) UPDATE SYN_STATUS OF UPDATING TO BWM_WEBSERVICE (AYSN) '============================================================ '============================================================ ' 3.1) MARK "COMPLETED" INTO DB '============================================================ If ReturnFunction = True Then UpdateSynWebServiceStatus(RegNumber, "COMPLETED") End If '============================================================ ' 3.2) MARK "INCOMPLETED" INTO DB '============================================================ If ReturnFunction = False Then UpdateSynWebServiceStatus(RegNumber, "INCOMPLETED") End If Catch '======================================================== ' 2.2) UPDATE TO WS INCOMPLETEED '======================================================== ReturnFunction = False End Try '============================================================ ' 4) RETURN VARIABLE '============================================================ Return ReturnFunction

Use SQLDataSource and SQL Command to delete data

Page 24: 22 code snippet_web_services_2

4. GET CAR INFORMATION(FUNCTION IN CLASS “CAR”)

24

'================================================================ ' FUNCTION#4: GET CAR_INFO FROM DB '================================================================ Public Function GetCarInfo(ByVal RegNumber As String) As Boolean '============================================================ ' 1) DECLAR VARIABLE '============================================================ Dim ReturnFunction As Boolean = True '============================================================ ' 2) OPEN DATABASE CONNECTION '============================================================ Dim cn As New System.Data.SqlClient.SqlConnection(Global.Resources.Resource.cnString) Try '======================================================== ' 3) CONNECT DATA '======================================================== Dim sql As String = "" sql = sql & "SELECT * FROM TM_CAR " sql = sql & "WHERE UPPER(RegNo) = UPPER('" & RegNumber.ToString & "') " Dim cmd As New System.Data.SqlClient.SqlCommand(sql, cn) cn.Open() '======================================================== ' 4) RETRIVE DATA '======================================================== Dim dr As System.Data.SqlClient.SqlDataReader = cmd.ExecuteReader '======================================================== ' 5.1) NOT FOUND USERID '======================================================== If Not dr.HasRows Then ReturnFunction = False End If '======================================================== ' 5.2) FOUND USERID '======================================================== If dr.HasRows Then '=================================================== ' A) FOUND USER ID '=================================================== ReturnFunction = True

'============================================= ' B) GET DATA '============================================= While (dr.Read) RegNo = dr.GetValue(0) BodyStyle = dr.GetValue(1) Model = dr.GetValue(2) DateRegistered = dr.GetValue(3) Mileage = dr.GetValue(4) FuelType = dr.GetValue(5) CarDescription = dr.GetValue(6) IsEnable = dr.GetValue(7) DateAdded = dr.GetValue(8) AddedBy = dr.GetValue(9) DateUpdated = dr.GetValue(10) UpdatedBy = dr.GetValue(11) DateSold = dr.GetValue(12) SoldBy = dr.GetValue(13) Price = dr.GetValue(14) SellingStatus = dr.GetValue(15) SynStatus = dr.GetValue(16) SynTime = dr.GetValue(17) Transmission = dr.GetValue(18) Colour = dr.GetValue(19) InteriorDetails = dr.GetValue(20) AdditionalEquipments = dr.GetValue(21) StandardEquipments = dr.GetValue(22) TechnicalSpecification = dr.GetValue(23) CustFullName = dr.GetValue(24) CustAddress = dr.GetValue(25) CustContactNo = dr.GetValue(26) CustEmail = dr.GetValue(27) Comment1 = dr.GetValue(28) Comment2 = dr.GetValue(29) End While End If '============================================= ' 6) CLOSE DATABASE '============================================= cn.Close() '============================================= ' 7)RETURN VALUE '============================================= Return ReturnFunction Catch ex As Exception '======================================================== ' RETURN FALSE '======================================================== Return False '======================================================== ' ANY ERROR CASE, CLOSE DB CONNECTION '======================================================== cn.Close() End Try Return True End Function

Use SQLDataReader to collect data

Page 25: 22 code snippet_web_services_2

5. USER AUTHENICATION & REGISTERATION

(FUNCTION IN CLASS “USER”)25

'================================================================ ' FUNCTION#1: LOGIN '================================================================ Function Login(ByVal UserName As String, ByVal UserPassword As String) As Boolean '============================================================ ' OPEN DATABASE CONNECTION '============================================================ Dim cn As New System.Data.SqlClient.SqlConnection(Global.Resources.Resource.cnString) Try '======================================================== ' CONNECT DATA '======================================================== Dim sql As String = "" sql = sql & "SELECT * FROM TM_USER " sql = sql & "WHERE UPPER(UserId) = UPPER('" & UserName.ToString & "') AND " sql = sql & "UPPER(UserPassword) = UPPER('" & UserPassword.ToString & "')" Dim cmd As New System.Data.SqlClient.SqlCommand(sql, cn) cn.Open() '======================================================== ' SELECT VALUE '======================================================== Dim dr As System.Data.SqlClient.SqlDataReader = cmd.ExecuteReader '======================================================== ' WRONG USER & PASSWORD '======================================================== If Not dr.HasRows Then Return False End If '======================================================== ' CORRECT USER & PASSWORD '======================================================== If dr.HasRows Then Return True End If cn.Close() Catch ex As Exception '======================================================== ' RETURN FALSE '======================================================== Return False '======================================================== ' ANY ERROR CASE, CLOSE DB CONNECTION '======================================================== cn.Close() End Try Return True End Function

'================================================================ ' FUNCTION#3: RESGISTER_USER (PERMISSION ONLY ADMIN) '================================================================ Function RegisterUser() As Boolean '============================================================ ' RETURN VARIABLE '============================================================ Dim ReturnFunction As Boolean = True '============================================================ ' PREPARE INSERT COMMAND '============================================================ Dim MyUtils As New Utils Dim SqlCmd As String = "" SqlCmd = "INSERT INTO TM_USER " SqlCmd = SqlCmd & "(UserId,UserPassword,UserRole,UserFirstName,UserLastName,UserEmail,UserDateRegistered) " SqlCmd = SqlCmd & " VALUES ('" & UserId & "' ,'" & UserPassword & "' ,'" & UserRole & "' ,'" & UserFirstName & "' ,'" & UserLastName & "' ,'" & UserEmail & "' ,'" & MyUtils.ConvertDateForSQL(UserDateRegistered) & "')" '================================================================= 'EXECUTE INSERT COMMAND '================================================================= Dim cnString As String = Global.Resources.Resource.cnString Dim ds As New SqlDataSource Try ds.ConnectionString = cnString ds.InsertCommand = SqlCmd ds.Insert() ReturnFunction = True Catch ReturnFunction = False End Try '============================================================ ' RETURN VARIABLE '============================================================ Return ReturnFunction End Function

Use DataReader

to collect data

Use SQLDataSoruce

and SQL Command to

insert data

Page 26: 22 code snippet_web_services_2

CONFIGURATION FILES

26

Page 27: 22 code snippet_web_services_2

WEB.CONFIG27

Connection Strings for .net controls in aspx

files

Web Service References

Page 28: 22 code snippet_web_services_2

RESOURCE.RESX28

Benefit:Flexible to change variable of connection strings e.g. Server Name, Database Name, DB User and DB Password.

The cnString variable is used in Car and User classes

Flexible to change connection String by resource.resx file

Connection Strings for source code in

VB files