function Create(staffData as structStaff, projectdata as structProject, stageData as ArrayList) as boolean

   dim myDSN As String = ConfigurationSettings.AppSettings("ProjectPalDSN")
   dim myConn As new SqlConnection(myDSN)
   dim myCommand As SqlCommand
   dim myParm As SqlParameter
   dim myStaff AS new projectpal.bo.boStaff()
        dim myTran As SqlTransaction

try   
   myConn.Open()
   myTran = myConnection.BeginTransaction()


   'przed stworzeniem menedera projektu (PM) naley sprawdzi, czy ju nie ma takiego w systemie
   if myStaff.Authenticate (staffData.email, staffData.password) then
      'w bazie danych jest ju taki PM - naley pobrac jego dane
      staffData = myStaff.Detail(staffData.email)
      projectData.pmid = ctype(staffData.id, integer)
   else
      'nie ma takiego PM - naley go utworzy
      myCommand = New SqlCommand("spCreateStaff", myConn)
      myCommand.CommandType = CommandType.StoredProcedure
      myCommand.Transaction = myTran
      myParm = myCommand.Parameters.Add("@staffdata", SqlDbType.varchar, 250)
      myParm.Value =  staffData.toinsertstring()

      myParm  = myCommand.Parameters.Add("@staffid", SqlDbType.int)
      myParm.Direction = ParameterDirection.Output

      myCommand.ExecuteNonQuery()
   
      projectData.pmid = ctype(myCommand.Parameters("@staffid").Value, integer)
   end if

   'tworzenie projektu
   myCommand = New SqlCommand("spCreateProject", myConn)
   myCommand.CommandType = CommandType.StoredProcedure
   myCommand.Transaction = myTran
   myParm = myCommand.Parameters.Add("@projectdata", SqlDbType.varchar, 2048)
   myParm.Value =  projectData.toinsertstring()

   myParm  = myCommand.Parameters.Add("@projectid", SqlDbType.int)
   myParm.Direction = ParameterDirection.Output

   myCommand.ExecuteNonQuery()

   
   'tworzenie etapu
   Dim myEnum As System.Collections.IEnumerator = stageData.GetEnumerator()
   Dim myProjectid as integer = ctype(myCommand.Parameters("@projectid").Value, integer)

   While myEnum.MoveNext()
      
      if ctype(myEnum.Current(), string).length > 0 then

         myCommand = New SqlCommand("spCreateStage", myConn)
         myCommand.CommandType = CommandType.StoredProcedure
         myCommand.Transaction = myTran      
         myParm = myCommand.Parameters.Add("@projectid", SqlDbType.int)
         myParm.Value = myProjectid

         myParm = myCommand.Parameters.Add("@stageData", SqlDbType.varchar, 50)
         myParm.Value = ctype(myEnum.Current(), string)
         myCommand.ExecuteNonQuery()
      
      end if

   End While
      
   myTran.commit()


catch e as SqlException
   myTran.rollback()
   throw e
   
finally
   myConn.Close

end try


return(true)

end function
