It is currently Thu Mar 28, 2024 2:37 pm


All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Unit 3 - Lesson 3: Copy, Print and Delete - Solution
PostPosted: Fri Aug 12, 2011 4:59 am 
User avatar

Joined: Wed Nov 17, 2010 8:37 am
Posts: 136
Real Name: Terry L. Wiechmann
Began Programming in MUMPS: 0- 0-1971
Routine: MP1PDATA
Code:
MP1PDATA ; Model side code for MP1 course
   ;
DELETE(IEN)   ; Delete the specified record and indices.
   ;
   ; IEN is Internal Entry Number of part record.
   ;
   New R,ERR
   ; Initialize error state to success.
   Set ERR="0;Record deleted successfully."
   ; Lock part record node with timeout of zero.
   Lock +^MP1PARTS(IEN):0
   ; If timed out, then lock unsuccessful. Issue error.
   Else  Set ERR="5;Record busy – record not deleted."
   ; If lock was successful, delete the part record and indices.
   If 'ERR Do
   . Set R=^MP1PARTS(IEN)
   . Kill ^MP1PARTS("B",$Piece(R,"^",1))
   . Kill ^MP1PARTS("C",$Piece(R,"^",2))
   . Kill ^MP1PARTS(IEN)
   . Lock -^MP1PARTS(IEN)
   ; Pass back error message to caller.
   Quit ERR
   ;
COPY(SIEN,NPNUM,NPDESC)   ; Copy source part record to target.
   ;
   ; SIEN is source record IEN - Required parameter
   ; NPNUM and NPDESC - New part number and description values - required parameters.
   ;
   ; Assumptions:
   ; 1) The source record exists,
   ; 2) The target part number and description do not exist.
   ;
   New R,ERR,REC
   ; Get default if source IEN not passed in.
   Set SIEN=$Get(SIEN,0)
   ; Check if source record exists - issue error if not.
   If '$Data(^MP1PARTS(SIEN)) Do
   . Set ERR="8;Record does not exist."
   ; Check for duplicate NPNUM and NPDESC records - not permitted.
   Else  If $$GET^MP1PDATA(NPNUM,"B") Do
   . Set ERR="1;Number "_NPNUM_" is already being used."
   Else  If $$GET^MP1PDATA(NPDESC,"C") Do
   . Set ERR="2;Description "_NPDESC_" is already being used."
   Else  Do
   . ; Get the source record.
   . Set REC=^MP1PARTS(SIEN)
   . ; Set new key values into the record string.
   . Set $Piece(REC,"^",1)=NPNUM
   . Set $Piece(REC,"^",2)=NPDESC
   . ; Store the record.
   . Set ERR=$$PUT^MP1PDATA(0,REC) ; Have put get new IEN.
   . ; If no error initialize error state to success.
   . If 'ERR Set ERR="0;Record copied successfully."
   ; Pass back error message to caller.
   Quit ERR
   ;
NEXT(Key,Index)   ; Return the next entry using the specified key and index.
   ;
   ; Key is the part number for the "B" index or description for the "C" index.
   ; Index is either "B" or "C". If null, it its defaulted to "B".
   ;
   ; Default to primary index "B".
   Set Index=$Get(Index,"B")
   ; Return the next key.
   Quit $Order(^MP1PARTS(Index,Key))
   ;
GET(Key,Index) ; Return record based on primary key look up.
   ;
   ; Key is the part number for the "B" index or description for the "C" index.
   ; Index is either "B" or "C". If null, its defaulted to "B".
   ;
   New REC
   ; If the index is null, default it to "B".
   Set Index=$Get(Index,"B")
   ; If the key is defined, then get it.
   If $Data(^MP1PARTS(Index,Key)) Do
   . ;Edit entry
   . Set IEN=$Order(^MP1PARTS(Index,Key,""))
   . Set REC=IEN_";"_^MP1PARTS(IEN)
   ; Else, create an empty record.
   Else  Do
   . Set REC="0;"
   ; Return the record.
   Quit REC
   ;
PUT(IEN,REC) ; File the Parts record.
   ;
   ; IEN is either the Internal Entry Number or zero (not defined).
   ; REC is the full record structure.
   ;
   New ERR,OPNUM,OPDESC
   ;Default return message to success.
   Set ERR="0;Record filed successfully." ; Initial error state to success.
   ; Get indice values.
   Set PNUM=$Piece(REC,"^",1)
   Set PDESC=$Piece(REC,"^",2)
   ; If entry does exist, get the old indice values.
   If IEN Do
   . Set OPNUM=$Piece(^MP1PARTS(IEN),"^",1)
   . Set OPDESC=$Piece(^MP1PARTS(IEN),"^",2)
   ; If entry does not exist, get a new Internal Entry Number (IEN).
   Else  Do
   . ; Init old values to null - new record.
   . Set (OPNUM,OPDESC)=""
   . Lock +^MP1PARTS(0):0 ; Increment lock count on node. Force timeout.
   . Else  Set ERR="3;Cannot get next node number – record not filed." Quit
   . If '$Data(^MP1PARTS(0)) Set ^MP1PARTS(0)=0
   . Set (IEN,^MP1PARTS(0))=^MP1PARTS(0)+1
   . Lock -^MP1PARTS(0) ; Decrement lock count on node.
   ; Make sure indices do not exist - multiple indices not permittted.
   If PNUM'=OPNUM,$Data(^MP1PARTS("B",PNUM)) Do
   . Set ERR="1;Number "_PNUM_" is already being used."
   If PDESC'=OPDESC,$Data(^MP1PARTS("C",PDESC)) Do
   . Set ERR="2;Description "_PDESC_" is already being used."
   If 'ERR Do  ;File if no errors.
   . ; Lock record node incrementally.
   . Lock +^MP1PARTS(IEN):0 ; Increment lock count on record. Force timeout.
   . ; If timeout occurred and no lock then pass back record busy message.
   . Else  Set ERR="4;Record busy – record not filed." Quit
   . ; Remove old indices.
   . Kill:OPNUM]""&(OPNUM'=PNUM) ^MP1PARTS("B",OPNUM)
   . Kill:OPDESC]""&(OPDESC'=PDESC) ^MP1PARTS("C",OPDESC)
   . ; Set the new record and indices nodes.
   . Set ^MP1PARTS(IEN)=REC
   . Set ^MP1PARTS("B",PNUM,IEN)=""
   . Set ^MP1PARTS("C",PDESC,IEN)=""
   . ; Incrementally unlock record node.
   . Lock -^MP1PARTS(IEN) ; Decrement lock count on node
   Quit ERR
   ;
SETREC(PNUM,PDESC,PQTY,PLVL,PRC,PSP) ; Construct Parts record.
   ; Return concatenated record.
   Quit PNUM_"^"_PDESC_"^"_PQTY_"^"_PLVL_"^"_PRC_"^"_PSP
   ;
PNUM(X) ;Check PNUM syntax
   Quit $Select(X?1.6N:1,1:0)
   ;
PDESC(X) ;Check PDESC syntax
   Quit $Select(X?1A.29NAP:1,1:0)
   ;
PQTY(X) ;Check PNUM syntax
   Quit $Select(X?1.N:1,1:0)
   ;
PLVL(X) ;Check PLVL syntax
   Quit $Select(X?1.N:1,1:0)
   ;
PRC(X) ;Check PRC syntax
   Quit $Select(X?.N.1".".2N:1,1:0)
   ;
PSP(X) ;Check PSP syntax
   Quit $Select(X?.N.1".".2N:1,1:0)
   ;

Review
  1. The COPY and DELETE extrinsic functions are displayed above as a part of the MP1PDATA routine.
Description
  1. Within a normal lecture/workshop class, the last exercise would be to build an assemblage structure for the Parts global. That is, you would be able to create a part called Bicycle and link it to all the parts used to create it (Frame, Handle Bars, etc.). If one of these parts is an assemblage (Front Wheel), it would contain all the links to those parts that made it up. In other words you would create a hierarchical record structure of the bicycle.
  2. However, since we are moving into the MUMPS Programming II course and it deals with common data structures (lists, stacks, etc.) that can be used in this exercise, we will delay it until then.

This concludes the MUMPS Programming I course.

_________________
Terry L. Wiechmann


Top
Offline Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 18 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Theme created StylerBB.net