main logo
Subject: Re: n-Tier apps with Codebook
Author: Gary Foster
Posted: 2002/08/15 11:29:06
 
View Entire Thread
New Search


Code sample 2 to illustrate the data broker object:

*************************************************************
EXAMPLE DATA BROKER CODE TO BUILD A COLLECTION

*-- Get the projects managed by this employee
SELECT curprojects && Cursor created previously
GO TOP
SCAN
ntop = helpdesk_project_id
*-- Jobticket is a custom "record object" class defined elsewhere
ojobchild = CREATEOBJECT("jobticket")

WITH ojobchild

cduedate = IIF(ISNULL(hp_due_date) OR EMPTY(hp_due_date),'None',DTOC(hp_due_date))
curgency = IIF(ISNULL(hp_priority),'0',ALLTRIM(STR(hp_priority)))
.cdescription = 'Due:'+ cduedate + ' Urgency:' + curgency + ' ' + ALLTRIM(hp_description)
.ckey = ALLTRIM(STR(helpdesk_project_id))
.cparentkey = cmanagerkey
.nimageindex = 0
.nrelationship = 4 && Always a child of something

*-- Flag a closed out job ticket
IF ISNULL(hp_date_completed)
.lclosed = .F.
ELSE
IF EMPTY(CTOD(DTOC(hp_date_completed)))
.lclosed = .F.
ELSE
.lclosed = .T.
ENDIF
ENDIF
ENDWITH

WITH ojob.jobticketchildren

*-- Add this record to the 'top level' array
.ADD(ojobchild)

ENDWITH

ENDSCAN

*************************************************************
EXAMPLE DATA BROKER METHODS TO UPDATE THE BACK END
*************************************************************

PROCEDURE SAVE
LOCAL nretval

WITH THIS
*-- Is this is a new record?
IF .lnewrecord
nretval = .savenew()
ELSE
nretval = .saveedited()
ENDIF

IF nretval != 1
MESSAGEBOX('Information did not save to database!' + CHR(13) + 'Try again',48,'Helpdesk Projects',1500)
ENDIF

*-- Enable the NEW button on the toolbar
.lnewrecord = .F.
.leditrecord = .F.

ENDWITH

RETURN nretval

*************************************************************
PROCEDURE savenew
LOCAL nretval

*-- Load the data object from the entry form field values
THIS.writetoobject()

*-- Save the object data to the helpdesk table
nretval = THIS.addnewrecord()


RETURN nretval

******************************************************
PROCEDURE addnew
LOCAL csql1
LOCAL nsqlresult
LOCAL cdatestarted
LOCAL cduedate
LOCAL cdatecompleted
LOCAL cdateentered
LOCAL cparentid
LOCAL crequestedby
LOCAL cmanagedby
LOCAL cpriority
LOCAL nparentid
LOCAL nrequestedby
LOCAL nmanagedby
LOCAL npriority
LOCAL cdescription
LOCAL crequestornote
LOCAL cmanagernote

*-- Make foreign key formats acceptable to the back end
nparentid = THIS.orecordobject.hp_parent_id
cparentid = IIF(ISNULL(nparentid),'NULL',ALLTRIM(STR(THIS.orecordobject.hp_parent_id)))

nrequestedby = THIS.orecordobject.hp_requested_by
crequestedby = IIF(nrequestedby = 0,'NULL',ALLTRIM(STR(THIS.orecordobject.hp_requested_by)))

nmanagedby = THIS.orecordobject.hp_managed_by
cmanagedby = IIF(nmanagedby = 0,'NULL',ALLTRIM(STR(THIS.orecordobject.hp_managed_by)))

*-- Make date formats acceptable to the back end
cdatestarted = DTOC(THIS.orecordobject.hp_date_started)
cdatestarted = IIF(EMPTY(CTOD(cdatestarted)),'NULL',[']+cdatestarted+['])

cduedate = DTOC(THIS.orecordobject.hp_due_date)
cduedate = IIF(EMPTY(CTOD(cduedate)),'NULL',[']+cduedate+['])

cdatecompleted = DTOC(THIS.orecordobject.hp_date_completed)
cdatecompleted = IIF(EMPTY(CTOD(cdatecompleted)),'NULL',[']+cdatecompleted+['])

cdateentered = DTOC(THIS.orecordobject.hp_date_entered)
cdateentered = IIF(EMPTY(CTOD(cdateentered)),'NULL',[']+cdateentered+['])

npriority = THIS.orecordobject.hp_priority
cpriority = IIF(ISNULL(npriority),'NULL',ALLTRIM(STR(THIS.orecordobject.hp_priority)))

cdescription = ['] + STRTRAN(ALLTRIM(THIS.orecordobject.hp_description),['], ['']) + [']
crequestornote = ['] + STRTRAN(ALLTRIM(THIS.orecordobject.hp_requestor_note),['], ['']) + [']
cmanagernote = ['] + STRTRAN(ALLTRIM(THIS.orecordobject.hp_manager_note),['], ['']) + [']

*-- Build the SQL INSERT statement
csql1 = [INSERT INTO helpdesk_project(] + ;
[hp_parent_id,] + ;
[hp_description,] + ;
[hp_requested_by,] + ;
[hp_requestor_note,] + ;
[hp_managed_by,] + ;
[hp_manager_note,] + ;
[hp_priority,] + ;
[hp_date_started,] + ;
[hp_due_date,] + ;
[hp_date_completed,] + ;
[hp_date_entered)] + ;
[ VALUES(]+ ;
cparentid + [,]+;
cdescription + [,]+;
crequestedby + [,]+;
crequestornote + [,]+;
cmanagedby + [,]+;
cmanagernote + [,]+;
cpriority + [,]+;
cdatestarted + [,]+;
cduedate + [,]+;
cdatecompleted + [,]+;
cdateentered + [)]

*-- Attempt the INSERT
nsqlresult = sqlexec(THIS.nconnection, csql1)

*-- Pass back the result code of the attempt.
RETURN nsqlresult




 
©2002 Gary Foster
<-- Prior Message New Search Next Message -->