home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 February / CHIP_2_98.iso / software / pelne / optionp / iis4_07.cab / Advertisement.cls < prev    next >
Text File  |  1997-11-01  |  5KB  |  142 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "Advertisement"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = True
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = True
  10. '
  11. ' This class manages data associated with advertisements and press releases
  12. ' in the Exploration Air website.
  13. '
  14. ' Like all of the classes for the site, it has no global properties or
  15. ' variables. This is so that objects will be called by the application
  16. ' (i.e. the ASPs) and then released as soon as they have performed their
  17. ' function. That way, if many users are creating instances of the same objects,
  18. ' the server on which the objects are running will not become burdened with
  19. ' too many instances.
  20. '
  21. ' One of the ways in which this is made apparent is that each function must
  22. ' establish its own database connection. If a connection were made and stored
  23. ' as a global variable, the object could not be recycled as quickly. This would
  24. ' also hinder Microsoft Transaction Server from pooling database connections
  25. ' efficiently. That is why the first parameter of every function that talks
  26. ' to the database is a string containing the location of the file DSN.
  27. '
  28. Option Explicit
  29.  
  30. Function ListAll(ByVal strFileDSN As String)
  31.     
  32.     Dim objContext As ObjectContext
  33.     Set objContext = GetObjectContext
  34.  
  35.     On Error GoTo ErrorHandler
  36.     
  37.     Dim strSQL As String
  38.     Dim rst As New ADODB.Recordset
  39.     
  40.     strSQL = "SELECT AdID, Description, FileName " & _
  41.              "FROM Ads"
  42.              
  43.     rst.CursorLocation = adUseServer
  44.     rst.Open strSQL, "FILEDSN=" & strFileDSN, adOpenStatic, adLockReadOnly, adCmdText
  45.     
  46.     objContext.SetComplete
  47.     Set ListAll = rst
  48.     
  49.     Exit Function
  50.     
  51. ErrorHandler:
  52.     If Not rst Is Nothing Then Set rst = Nothing
  53.      
  54.     objContext.SetAbort
  55.     Err.Raise Err.Number, "Advertisement.ListAll()", Err.Description
  56.     
  57. End Function
  58.  
  59. Function ListForInterests(ByVal strFileDSN As String, ByVal lngAccountID As Long)
  60.     
  61.     Dim objContext As ObjectContext
  62.     Set objContext = GetObjectContext
  63.     
  64.     On Error GoTo ErrorHandler
  65.     
  66.     Dim strSQL As String
  67.     Dim rst As New ADODB.Recordset
  68.     Dim arrFileNames() As Variant
  69.     Dim arrTemp(), strTemp As String, fltTemp As Single
  70.     Dim n, m, i As Integer
  71.     
  72.     strSQL = "SELECT Ads.FileName " & _
  73.              "FROM " & _
  74.                 "(Ads INNER JOIN AdsInterests ON Ads.AdID = AdsInterests.AdID) " & _
  75.                 "LEFT JOIN MembersInterests ON MembersInterests.InterestID = AdsInterests.InterestID " & _
  76.              "WHERE MembersInterests.AccountID = " & lngAccountID
  77.       
  78.     rst.CursorLocation = adUseServer
  79.     rst.Open strSQL, "FILEDSN=" & strFileDSN, adOpenStatic, adLockReadOnly, adCmdText
  80.    
  81.     ' if no hits happened, then select all the ads
  82.     If rst.EOF Then
  83.         Set rst = Nothing
  84.         strSQL = "SELECT FileName FROM Ads"
  85.         rst.Open strSQL, "FILEDSN=" & strFileDSN, adOpenDynamic, adLockReadOnly, adCmdText
  86.     End If
  87.    
  88.     n = 0
  89.  
  90.     Do Until rst.EOF
  91.         For i = 0 To n - 1
  92.             If arrFileNames(i) = rst("FileName") Then
  93.                 rst.MoveNext
  94.                 i = 0
  95.                 If rst.EOF Then
  96.                     Exit Do
  97.                 End If
  98.             End If
  99.         Next
  100.         ReDim Preserve arrFileNames(n)
  101.         arrFileNames(n) = CVar(rst("FileName"))
  102.         n = n + 1
  103.         rst.MoveNext
  104.     Loop
  105.     
  106.     'change n back to reflect size of arrFileNames
  107.     n = n - 1
  108.         
  109.     ' Randomize the order of the array elements
  110.     ReDim arrTemp(n)
  111.     Randomize
  112.     For i = 0 To n
  113.         arrTemp(i) = Rnd
  114.     Next
  115.     For m = n - 1 To 1 Step -1
  116.         For i = 0 To m
  117.             If arrTemp(i) > arrTemp(i + 1) Then
  118.                 ' sorting the random numbers
  119.                 fltTemp = arrTemp(i)
  120.                 arrTemp(i) = arrTemp(i + 1)
  121.                 arrTemp(i + 1) = fltTemp
  122.                 ' sorting the array the same way
  123.                 strTemp = arrFileNames(i)
  124.                 arrFileNames(i) = arrFileNames(i + 1)
  125.                 arrFileNames(i + 1) = strTemp
  126.             End If
  127.         Next
  128.     Next
  129.     
  130.     objContext.SetComplete
  131.     ListForInterests = arrFileNames
  132.     
  133.     Exit Function
  134.     
  135. ErrorHandler:
  136.     If Not rst Is Nothing Then Set rst = Nothing
  137.  
  138.     objContext.SetAbort
  139.     Err.Raise Err.Number, "Advertisement.ListForInterests()", Err.Description
  140.     
  141. End Function
  142.