COMPILE FUNCTION obj_Note_Ptrs(Method,Parms)
/*
	Methods for NOTE_PTRS (User InBox) table
    
	08/02/2006 JCH - Initial Coding
     
    Properties:
     
    Methods:
    
    AddNote()				;* Checks NOTES_SENT table for undelivered NOTE_IDs and adds them to the users inbox.
	
*/


DECLARE FUNCTION Get_Status, Msg, Utility, obj_Tables, NextKey, obj_Notes_Sent
DECLARE SUBROUTINE  Set_Status, Msg, obj_Tables, RList, ErrMsg, obj_Notes_Sent, Update_Index

$INSERT LOGICAL
$INSERT MSG_EQUATES
$INSERT NOTES_EQU
$INSERT NOTE_PTRS_EQU

EQU TARGET_ACTIVELIST$ TO 5

ErrTitle = 'Error in Stored Procedure "obj_Note_Ptrs"'
ErrorMsg = ''

IF NOT(ASSIGNED(Method))	THEN ErrorMsg = 'Unassigned parameter "Method" passed to subroutine'
IF NOT(ASSIGNED(Parms))		THEN Parms = ''

IF ErrorMsg NE '' THEN
	Set_Status(-1,ErrTitle:@SVM:ErrorMsg)
	RETURN ''
END

Result = ''

BEGIN CASE
	CASE Method = 'AddNotes'		; GOSUB AddNotes
	CASE 1
		ErrorMsg = 'Unknown Method ':QUOTE(Method):' passed to object.'

END CASE

IF ErrorMsg NE '' THEN
	Set_Status(-1,ErrTitle:@SVM:ErrorMsg)
	RETURN ''
END

RETURN Result


* * * * * * *
AddNotes:
* * * * * * *

UserID	= Parms[1,@RM]

IF UserID = '' THEN ErrorMsg = 'Unassigned Parameter "UserID" passed to object. (':Method:')'


IF ErrorMsg NE '' THEN RETURN

OtParms = 'NOTE_PTRS':@RM:UserID

NPRec = obj_Tables('ReadRec',OtParms)	;* If not found then returns null, otherwise reads and sets the lock
 
IF LEN(NPRec) > 60000 THEN
	* Dump the oldest note pointers
	PtrCnt = COUNT(NPRec<note_ptrs_from$>, @vm) + (NPRec<note_ptrs_from$> NE '')
	FOR N = PtrCnt TO (PtrCnt - 100) STEP -1
		NPRec = Delete(NPRec, note_ptrs_subject$, N, 0)
		NPRec = Delete(NPRec, note_ptrs_from$, N, 0)
		NPRec = Delete(NPRec, note_ptrs_date$, N, 0)
		NPRec = Delete(NPRec, note_ptrs_time$, N, 0)
		NPRec = Delete(NPRec, note_ptrs_new$, N, 0)
		NPRec = Delete(NPRec, note_ptrs_note_ids$, N, 0)
		NPRec = Delete(NPRec, note_ptrs_attachment$, N, 0)
	NEXT N
	TLen = LEN(NPRec)
END	

NotesSentKeys = obj_Notes_Sent('GetUserKeys',UserID)

IF NotesSentKeys NE '' THEN
	FOR I = 1 TO COUNT(NotesSentKeys,@VM) + (NotesSentKeys NE '')

		NotesSentKey = NotesSentKeys<1,I>
		NoteID = FIELD(NotesSentKey,'*',2)
		
		LOCATE NoteID IN NPRec<NOTE_PTRS_NOTE_IDS$> USING @VM SETTING POS ELSE
		
			NoteRec = XLATE('NOTES',NoteID,'','X')
			
			SentFrom		= NoteRec<NOTES_FROM$>
			SentDate		= NoteRec<NOTES_ENTRY_DATE$>
			SentTime		= NoteRec<NOTES_ENTRY_TIME$>
			AttachWindow	= NoteRec<NOTES_ATTACH_WINDOW$>
			AttachKeys		= NoteRec<NOTES_ATTACH_KEYS$>
			Subject			= NoteRec<NOTES_SUBJECT$>
			
			IF AttachWindow NE '' AND AttachKeys NE '' THEN
			    Attachment = 'Yes'
			END ELSE
				Attachment = 'No'
			END	
			
			NPRec = INSERT( NPRec, note_ptrs_subject$, 1, 0, Subject )		;* Add the subject

			ConvSentFrom = OCONV(SentFrom,'[XLATE_CONV,LSL_USERS*FIRST_LAST]')
			IF ConvSentFrom NE '' THEN SentFrom = ConvSentFrom

			NPRec = INSERT( NPRec, note_ptrs_from$, 1, 0, SentFrom )
			NPRec = INSERT( NPRec, note_ptrs_date$, 1, 0, OCONV(SentDate,'D2/') )
			NPRec = INSERT( NPRec, note_ptrs_time$, 1, 0, OCONV(SentTime,'MTH') )
			NPRec = INSERT( NPRec, note_ptrs_new$, 1, 0, 'Yes' )
			NPRec = INSERT( NPRec, note_ptrs_note_ids$, 1, 0, NoteID )
			NPRec = INSERT( NPRec, note_ptrs_attachment$, 1, 0, Attachment )
		END

	NEXT I

	OtParms = FIELDSTORE(OtParms,@RM,4,0,NPRec)
	obj_Tables('WriteRec',OtParms)

	IF Get_Status(errCode) THEN
		ErrMsg(errCode)
	END ELSE
		obj_Notes_Sent('Delete',NotesSentKeys)
		
		Update_Index('NOTES_SENT','USER_ID', False$, True$)
	END
END ELSE
	obj_Tables('UnlockRec',OtParms)
END


RETURN

