22 code snippet_web_services_2

847 views
770 views

Published on

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

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
847
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

22 code snippet_web_services_2

  1. 1. CODE SNIPPETS FOR BWM – MOTOR FRANCHISE PROJECT Created by Team Members Reviewed by Team MembersModule: Web Services Created Date 19 April 2012Assignment: Team Assignment Revised Date 27 April 2012 Revision No. 1.2Team Name: SPOCK Document Name F03-001
  2. 2. DATA VALIDATION 2
  3. 3. 3 1.VALIDATION BY CONTROLS Register User Register Vehicle Required Field Validators
  4. 4. 4 2.VALIDATION BY CONTROLS Validate Price Validate Price and Mileage Price must be number Price and mileage must more than 0
  5. 5. 5 3.VALIDATION BY CONTROLS Validate E-mail Regular Expression Validator User must fill right format of e-mail
  6. 6. 6 4. CODE VALIDATION Date ValidationValidate Date ex. 31 April 2012 is a incorrect date
  7. 7. CONSUMING WEB SERVICES FROM BWM SITE 7
  8. 8. 8 1.REGISTER VEHICLE Consuming Web Services ======================================================== 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
  9. 9. 9 2. UPDATE VEHICLE ======================================================== 2.2) UPDATE TO BWM WEBSERVICE ======================================================== ReturnFunction = MyProxy.UpdateAVehicle(BodyStyle, Model,MyUtils.ConvertDateForBwmWebService(DateRegistered), RegNumber, Mileage,FuelType, CarDescription, "SPOCK-" & UserName)
  10. 10. 3. UPDATE SOLD VEHICLE10 (VEHICLE FOR SALES) Consuming Web Service ========================================================*Option if want mark SOLD in garage DB and Delete in BWM website 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)
  11. 11. 11 4. DELETE VEHICLE (NON-SELL VEHICLE USING SECURED XML) Consuming Web Service ======================================================== 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)
  12. 12. WEB SERVICESPROVIDED BY GARAGE (SPOCK) 12
  13. 13. 13 1. GET VEHICLE INFORMATION • Parameter: Vehicle Register No • Return: DataSet (0 or 1 record) Only basic vehicle information transferred to BWM website
  14. 14. 14 2. GET VEHICLE INFORMATION (ALL) • Parameter: Vehicle Register No • Return: DataSet (0 or 1 record) All Information collected in garage database
  15. 15. 3. GET ORDER INFORMATION15 (SOLD CAR) • Parameter: From Date and To Date • Return: DataSet • Only: SOLD Vehicle This function used for BWM to monitor vehicle sold by garage
  16. 16. CLASSES UI (aspx files) We separate data layer from business logic by using three classes: Car, User and UtilsBusiness Logic (VB files) Our design - SQL queries are used only in classes (Except to quick search feature) Data Layer (Class files) - 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 DB 16
  17. 17. 17 1. CLASS “CAR” Variable in class Functions in class
  18. 18. 18 2. CLASS “USER” Use SQL DataReader to collect dataAuthentication and account functions Authentication User
  19. 19. 19 3. CLASS “UTILS”Class Utils used for general functions Benefits: Reusability
  20. 20. DATA ACCESS (EXAMPLES) 20
  21. 21. 21 1. REGISTER VEHICLE (FUNCTION IN CLASS “CAR”) ================================================================ ============================================================= FUNCTION#1: REGISTER CAR_INFO TO DB 3) EXECUTE INSERT COMMAND ================================================================ ============================================================= Function RegisterCar() As Boolean Dim cnString As String = Global.Resources.Resource.cnString ============================================================ Dim ds As New SqlDataSource 1) DECLAR VARIABLE Try ============================================================ ds.ConnectionString = cnString Dim ReturnFunction As Boolean = True ds.InsertCommand = SqlCmd Dim MyUtils As New Utils ds.Insert() Dim SqlCmd As String = "" ReturnFunction = True ============================================================ Catch 2) PREPARE INSERT COMMAND ReturnFunction = False ============================================================ End Try SqlCmd = "INSERT INTO TM_CAR " ============================================================ SqlCmd = SqlCmd & "(RegNo, BodyStyle, Model, DateRegistered, Mileage, 4) RETURN VARIABLEFuelType, CarDescription, IsEnable, DateAdded, AddedBy, DateUpdated, ============================================================UpdatedBy, DateSold, SoldBy, Price, SellingStatus, SynStatus, SynTime, Return ReturnFunctionTransmission, Colour, InteriorDetails, AdditionalEquipments, StandardEquipments, End FunctionTechnicalSpecification, CustFullName, CustAddress, CustContactNo, CustEmail,Comment1, Comment2) " SqlCmd = SqlCmd & " VALUES (" & GetNextRegisterNo() & " ," & BodyStyle &" ," & Model & " ," & MyUtils.ConvertDateForSQL(DateRegistered) & " ," SqlCmd = SqlCmd & Mileage & " ," & FuelType & "," & CarDescription & " ," SqlCmd = SqlCmd & IsEnable & " ," & Use SqlDataSource to register vehicleMyUtils.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 & " ," & Register Types (SellingStatus Column)CustEmail & " ," & Comment1 & " ," & Comment2 & ") " 1. SELL: Register For Sales 2. NON-SALES: Register a Vehicle 3. SOLD: Sold Vehicle
  22. 22. 2. UPDATE VEHICLE 22 (FUNCTION IN CLASS “CAR”)================================================================ ============================================================ FUNCTION#6: UPDATE SELLING_CAR_INFO TO DB 2.3) FILL DATA TO DATASET================================================================ ============================================================ Public Function UpdateSellingCarInfoByDataSet(ByVal RegNumber As String) As ds.Tables(0).Rows(0).Item("DateUpdated") = NowBoolean ds.Tables(0).Rows(0).Item("UpdatedBy") = UpdatedBy ============================================================ ds.Tables(0).Rows(0).Item("DateSold") = DateSold 1) DECLARE VARIABLE ds.Tables(0).Rows(0).Item("SoldBy") = SoldBy ============================================================ ds.Tables(0).Rows(0).Item("Price") = Price Dim MyUtils As New Utils ds.Tables(0).Rows(0).Item("SellingStatus") = SellingStatus Dim cn As SqlConnection ds.Tables(0).Rows(0).Item("SynStatus") = SynStatus Dim da As SqlDataAdapter ds.Tables(0).Rows(0).Item("SynTime") = SynTime Dim ds As New DataSet ds.Tables(0).Rows(0).Item("CustFullName") = CustFullName Dim cnString As String = Global.Resources.Resource.cnString ds.Tables(0).Rows(0).Item("CustAddress") = CustAddress Dim sqlCmd As String = "SELECT * FROM TM_CAR WHERE RegNo = " & ds.Tables(0).Rows(0).Item("CustContactNo") = CustContactNoRegNumber & "" ds.Tables(0).Rows(0).Item("CustEmail") = CustEmail ============================================================ ds.Tables(0).Rows(0).Item("Comment2") = Comment2 2) GET DATA AND UPDATE DATA TO DB ============================================================ ============================================================ Try 4) UPDATE DATA IN DB ======================================================== 2.1) FILL DATA FROM DATABASE TO DATASET ============================================================ ======================================================== Dim cmb As New SqlCommandBuilder(da) cn = New SqlConnection(cnString) da.Update(ds, "TM_CAR") da = New SqlDataAdapter(sqlCmd, cn) ds.AcceptChanges() da.Fill(ds, "TM_CAR") ======================================================== ============================================================ 2.2) NOT FOUND REG_NO 5) UPDATE SUCCESSFUL ======================================================== If ds.Tables(0).Rows.Count <> 1 Then ============================================================ Return False Return True End If Catch ex As Exception ============================================================ 6) UPDATE ERROR Use DataSet and SQLDataAdaptor to ============================================================ update database Return False End Try End Function
  23. 23. 3. DELETE VEHICLE 23 (FUNCTION IN CLASS “CAR”)================================================================ ================================================================ FUNCTION#8: DELETE CAR_INFO FROM DATABASE FUNCTION#10: DELETE VEHICLE FROM REGISTER TABLE (CALL WEBSERVICE) ================================================================ ================================================================ Function DeleteVehicleFromRegisterTableAtBwm(ByVal RegNumber As String) As Boolean Public Function DeleteCarInfo(ByVal RegNumber As String) As Boolean ============================================================ ============================================================ 1) RETURN VARIABLE 1) RETURN VARIABLE ============================================================ ============================================================ Dim ReturnFunction As Boolean = False Dim ReturnFunction As Boolean = True Dim MyUtils As New Utils Dim MyProxy = New BMWHOWebService.BMWHOWebService Dim MyUtils As New Utils ============================================================ ============================================================ 2) PREPARE INSERT COMMAND 2) PREPARE INSERT COMMAND ============================================================ ============================================================ Try Dim SqlCmd As String = "" ======================================================== SqlCmd = "DELETE TM_CAR " 2.1) AUTHEN TO BWM SITE ======================================================== SqlCmd = SqlCmd & "WHERE RegNo = " & RegNumber & " " Dim Auth As New AuthenticationHeader() ============================================================ Auth.UserName = "tutor" 3) EXECUTE DELETE COMMAND Auth.PassWord = "password" ============================================================ MyProxy.AuthenticationHeaderValue = Auth Dim cnString As String = Global.Resources.Resource.cnString ======================================================== Dim ds As New SqlDataSource 2.2) DELETE VEHICLE FROM REGISTER TABLE (CALL WEBSERVICE) ======================================================== Try ReturnFunction = MyProxy.DeleteVehicleFromRegisterTable(RegNumber) ds.ConnectionString = cnString ============================================================ ds.DeleteCommand = SqlCmd 3) UPDATE SYN_STATUS OF UPDATING TO BWM_WEBSERVICE (AYSN) ds.Delete() ============================================================ ReturnFunction = True ============================================================ Catch 3.1) MARK "COMPLETED" INTO DB ============================================================ ReturnFunction = False If ReturnFunction = True Then End Try UpdateSynWebServiceStatus(RegNumber, "COMPLETED") ============================================================ End If 4) RETURN VARIABLE ============================================================ ============================================================ 3.2) MARK "INCOMPLETED" INTO DB Return ReturnFunction ============================================================ If ReturnFunction = False Then End Function UpdateSynWebServiceStatus(RegNumber, "INCOMPLETED") End If Catch ======================================================== 2.2) UPDATE TO WS INCOMPLETEED ======================================================== Use SQLDataSource and SQL ReturnFunction = False End Try ============================================================ Command to delete data 4) RETURN VARIABLE ============================================================ Return ReturnFunction
  24. 24. 4. GET CAR INFORMATION 24 (FUNCTION IN CLASS “CAR”) ============================================================================================================= B) GET DATA ============================================= FUNCTION#4: GET CAR_INFO FROM DB While (dr.Read) ================================================================ RegNo = dr.GetValue(0) Public Function GetCarInfo(ByVal RegNumber As String) As Boolean BodyStyle = dr.GetValue(1) Model = dr.GetValue(2) ============================================================ DateRegistered = dr.GetValue(3) 1) DECLAR VARIABLE Mileage = dr.GetValue(4) ============================================================ FuelType = dr.GetValue(5) CarDescription = dr.GetValue(6) Dim ReturnFunction As Boolean = True IsEnable = dr.GetValue(7) ============================================================ 2) OPEN DATABASE CONNECTION DateAdded = dr.GetValue(8) AddedBy = dr.GetValue(9) Use SQLDataReader DateUpdated = dr.GetValue(10) ============================================================ Dim cn As New System.Data.SqlClient.SqlConnection(Global.Resources.Resource.cnString) UpdatedBy = dr.GetValue(11) DateSold = dr.GetValue(12) to collect data Try SoldBy = dr.GetValue(13) Price = dr.GetValue(14) ======================================================== SellingStatus = dr.GetValue(15) 3) CONNECT DATA SynStatus = dr.GetValue(16) ======================================================== SynTime = dr.GetValue(17) Transmission = dr.GetValue(18) Dim sql As String = "" Colour = dr.GetValue(19) sql = sql & "SELECT * FROM TM_CAR " InteriorDetails = dr.GetValue(20) sql = sql & "WHERE UPPER(RegNo) = UPPER(" & RegNumber.ToString & ") " AdditionalEquipments = dr.GetValue(21) StandardEquipments = dr.GetValue(22) Dim cmd As New System.Data.SqlClient.SqlCommand(sql, cn) TechnicalSpecification = dr.GetValue(23) cn.Open() CustFullName = dr.GetValue(24) ======================================================== CustAddress = dr.GetValue(25) CustContactNo = dr.GetValue(26) 4) RETRIVE DATA CustEmail = dr.GetValue(27) ======================================================== Comment1 = dr.GetValue(28) Dim dr As System.Data.SqlClient.SqlDataReader = cmd.ExecuteReader Comment2 = dr.GetValue(29) End While ======================================================== End If 5.1) NOT FOUND USERID ============================================= ======================================================== 6) CLOSE DATABASE ============================================= If Not dr.HasRows Then cn.Close() ReturnFunction = False ============================================= End If 7)RETURN VALUE ============================================= ======================================================== Return ReturnFunction 5.2) FOUND USERID Catch ex As Exception ======================================================== ======================================================== RETURN FALSE If dr.HasRows Then ======================================================== =================================================== Return False A) FOUND USER ID ======================================================== ANY ERROR CASE, CLOSE DB CONNECTION =================================================== ======================================================== ReturnFunction = True cn.Close() End Try Return True End Function
  25. 25. 5 . U SER A U T HEN IC AT ION & R EG IST ER AT IO N 25 (F U N C T IO N IN C L A SS “ U SER ” )================================================================ FUNCTION#1: LOGIN ================================================================ ================================================================ FUNCTION#3: RESGISTER_USER (PERMISSION ONLY ADMIN) Function Login(ByVal UserName As String, ByVal UserPassword As String) As Boolean ================================================================ ============================================================ Function RegisterUser() As Boolean OPEN DATABASE CONNECTION ============================================================ ============================================================ RETURN VARIABLE Dim cn As New System.Data.SqlClient.SqlConnection(Global.Resources.Resource.cnString) Try ============================================================ ======================================================== Dim ReturnFunction As Boolean = True CONNECT DATA ============================================================ ======================================================== PREPARE INSERT COMMAND Dim sql As String = "" ============================================================ sql = sql & "SELECT * FROM TM_USER " Dim MyUtils As New Utils sql = sql & "WHERE UPPER(UserId) = UPPER(" & UserName.ToString & ") AND " Dim SqlCmd As String = "" sql = sql & "UPPER(UserPassword) = UPPER(" & UserPassword.ToString & ")" Dim cmd As New System.Data.SqlClient.SqlCommand(sql, cn) SqlCmd = "INSERT INTO TM_USER " cn.Open() SqlCmd = SqlCmd & ======================================================== "(UserId,UserPassword,UserRole,UserFirstName,UserLastName,UserEmail,UserDat SELECT VALUE eRegistered) " ======================================================== SqlCmd = SqlCmd & " VALUES (" & UserId & " ," & UserPassword & " ," & Dim dr As System.Data.SqlClient.SqlDataReader = cmd.ExecuteReader UserRole & " ," & UserFirstName & " ," & UserLastName & " ," & UserEmail & " ," ======================================================== & MyUtils.ConvertDateForSQL(UserDateRegistered) & ")" WRONG USER & PASSWORD ======================================================== If Not dr.HasRows Then ================================================================= Return False EXECUTE INSERT COMMAND End If ======================================================== ================================================================= CORRECT USER & PASSWORD Dim cnString As String = Global.Resources.Resource.cnString ======================================================== Dim ds As New SqlDataSource If dr.HasRows Then Return True Try ds.ConnectionString = cnString End If cn.Close() Catch ex As Exception ds.InsertCommand = SqlCmd ds.Insert() Use ======================================================== RETURN FALSE Use ReturnFunction = True Catch SQLDataSoruce ======================================================== Return False ======================================================== DataReader ReturnFunction = False End Try and SQL ============================================================ ANY ERROR CASE, CLOSE DB CONNECTION to collect data ======================================================== RETURN VARIABLE Command to cn.Close() ============================================================ End Try Return True Return ReturnFunction End Function insert data End Function
  26. 26. CONFIGURATION FILES 26
  27. 27. 27 WEB.CONFIG Connection Strings for .net controls in aspx files Web Service References
  28. 28. 28 RESOURCE.RESX Flexible to change connection String by resource.resx file Connection Strings for source code in VB files 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

×