(FILECREATED "25-May-86 18:30:52" {QV}<NOTECARDS>1.3K>FGHPATCH044.;2 22040  

      changes to:  (VARS FGHPATCH044COMS)
		   (FNS NC.ConvertLinkFormat NC.GetBrowserSubstance NC.GetGraphSubstance 
			NC.PutBrowserSubstance)

      previous date: "25-May-86 18:05:21" {QV}<NOTECARDS>1.3K>FGHPATCH044.;1)


(* Copyright (c) 1986 by Xerox Corporation. All rights reserved.)

(PRETTYCOMPRINT FGHPATCH044COMS)

(RPAQQ FGHPATCH044COMS ((* * Small fixes to conversion. FROM NCBROWSERCARD and NCGRAPHCARD)
			  (FNS NC.GetBrowserSubstance NC.GetGraphSubstance NC.PutBrowserSubstance)
			  (* * Fix to function first defined in FGHPATHC041)
			  (FNS NC.ConvertLinkFormat)
			  (* * Fix to functions fixed in FGHPATCH041)
			  (FNS NC.ScavengeDatabaseFile)))
(* * Small fixes to conversion. FROM NCBROWSERCARD and NCGRAPHCARD)

(DEFINEQ

(NC.GetBrowserSubstance
  (LAMBDA (Card Length Stream VersionNum)                    (* fgh: "25-May-86 18:03")

          (* * Go get all the browser-specific info and then get the graph that is the browser's substance.)



          (* * rht 2/14/86: Added call to NC.ApplySupersFn)



          (* * rht 2/28/86: Added special handling for old version -1 style. I.e. pre 1.3k.)



          (* * fgh 5/1/86 Can't have negative versions, so changed old style to be 255 255.0 Also added defaults for old 
	  style browser specs.)



          (* * fgh 5/25/86 Undid preceeding change w.r.t. the 255 versus -1 Turns out -1 never came from file, only from 
	  converter as an arg.)


    (if (NOT (EQP VersionNum -1))
	then (NC.SetBrowserRootsInfo Card (NC.ReadBrowserRootsInfo Stream))
	       (NC.SetBrowserLinkLabels Card (NC.ReadBrowserLinkLabels Stream))
	       (NC.SetBrowserFormat Card (NC.ReadBrowserFormat Stream))
	       (NC.SetSpecialBrowserSpecs Card (NC.ReadSpecialBrowserSpecs Stream))
	       (NC.SetBrowserDepth Card (NC.ReadBrowserDepth Stream))
	       (NC.SetBrowserLinksLegend Card (NC.ReadBrowserLinksLegend Stream))
	       (NC.SetBrowserSavedLinkingInfo Card (NC.ReadBrowserSavedLinkingInfo Stream))
      else (NC.SetBrowserRootsInfo Card NIL)
	     (NC.SetBrowserLinkLabels Card NIL)
	     (NC.SetBrowserFormat Card NIL)
	     (NC.SetSpecialBrowserSpecs Card NIL)
	     (NC.SetBrowserDepth Card 0)
	     (NC.SetBrowserLinksLegend Card NIL)
	     (NC.SetBrowserSavedLinkingInfo Card NIL))
    (NC.ApplySupersFn GetFn Card Length Stream VersionNum)))

(NC.GetGraphSubstance
  (LAMBDA (Card Length Stream VersionNum)                    (* fgh: "25-May-86 18:04")

          (* * Read the Graph)



          (* * fgh 11/14/85 Updated to handle Card object.)



          (* * fgh 11/20/85 NoteCards now passes start and end ptrs down.)



          (* * fgh 11/21/85 Now passes length instead of start and end ptrs.)



          (* * rht 1/23/86: Now takes Stream as arg instead of computing from Card.)



          (* * rht 2/7/86: Now uses READGRAPH instead of HREAD)



          (* * rht 2/28/86: Added special handling for old version -1 style. I.e. pre 1.3k.)



          (* * fgh 5/1/86 Can't have negative version numbers, so changed old style versions to be 255.0)



          (* * fgh 5/25/86 Undid preceeding change. Turns out -1 never came from file only passed as Arg from converter.)


    (if (EQP VersionNum -1)
	then (HREAD Stream)
      else (READGRAPH Stream))))

(NC.PutBrowserSubstance
  (LAMBDA (Card Stream)                                      (* fgh: "25-May-86 18:02")

          (* * For each BrowserUID, clear its UID prop list. Otherwise HPRINT will die in PutGraphSubstance.)



          (* * rht 1/23/86: Now takes Stream as arg)



          (* * rht 2/1/86: Now saves old UID information on card's proplist for restoring when card is brought up again.
	  Note that this info will only exist for nodes connected to some other node with multiple links.)



          (* * fgh 2/5/86 Added call to NC.ApplySupersFn)



          (* * rht 2/6/86: Now writes down all browser info to substance rather than letting it live on prop list.)



          (* * rht 2/20/86: Now checks to see if saved linking info is cached before recomputing it.)



          (* * fgh 5/25/86 Added default depth paramter.)


    (NC.WriteBrowserRootsInfo Stream (NC.FetchBrowserRootsInfo Card))
    (NC.WriteBrowserLinkLabels Stream (NC.FetchBrowserLinkLabels Card))
    (NC.WriteBrowserFormat Stream (NC.FetchBrowserFormat Card))
    (NC.WriteSpecialBrowserSpecs Stream (NC.FetchSpecialBrowserSpecs Card))
    (NC.WriteBrowserDepth Stream (OR (NC.FetchBrowserDepth Card)
					 0))
    (NC.WriteBrowserLinksLegend Stream (NC.FetchBrowserLinksLegend Card))
    (NC.WriteBrowserSavedLinkingInfo Stream (OR (NC.FetchBrowserSavedLinkingInfo Card)
						    (NC.SetBrowserSavedLinkingInfo Card
										     (
								NC.ComputeBrowserSavedLinkingInfo
										       Card))))
    (NC.ApplySupersFn PutFn Card Stream)))
)
(* * Fix to function first defined in FGHPATHC041)

(DEFINEQ

(NC.ConvertLinkFormat
  (LAMBDA (ListOfLinks Version2HashArray)                    (* fgh: "25-May-86 18:08")

          (* * Convert links from NOTECARDLINK record to Link datatype. Note the conversion of NoSource from a dangling link 
	  to a property list item.)



          (* * fgh 5/21/86 First created.)


    (bind NewLink ConvertedLinks PropList Card for Link in ListOfLinks
       do (OR (EQ (CAR Link)
			(QUOTE NOTECARDLINK))
		  (SETQ Link (CONS (QUOTE NOTECARDLINK)
				       Link)))
	    (if (NEQ (fetch (NOTECARDLINK DESTINATIONID) of Link)
			 (QUOTE NC00000))
		then (SETQ NewLink (create Link
						 UID ←(NC.MakeUID)
						 SourceCard ←(GETHASH (fetch (NOTECARDLINK 
											 SOURCEID)
									   of Link)
									Version2HashArray)
						 DestinationCard ←(GETHASH (fetch (NOTECARDLINK
											DESTINATIONID)
										of Link)
									     Version2HashArray)
						 AnchorMode ←(fetch (NOTECARDLINK ANCHORMODE)
								of Link)
						 Label ←(fetch (NOTECARDLINK LINKLABEL)
							   of Link)
						 DisplayMode ←(fetch (NOTECARDLINK DISPLAYMODE)
								 of Link)))
		       (NC.CheckDisplayModeFormat NewLink)
		       (SETQ ConvertedLinks (CONS NewLink ConvertedLinks))
	      elseif (SETQ Card (GETHASH (fetch (NOTECARDLINK SOURCEID) of Link)
					       Version2HashArray))
		then 

          (* * This is the old form of NoSource. Convert to new form by putting on the prop list.)


		       (SETQ PropList (NC.FetchPropList (SETQ Card (GETHASH
								(fetch (NOTECARDLINK SOURCEID)
								   of Link)
								Version2HashArray))))
		       (NC.SetPropList Card (if PropList
						  then (LISTPUT PropList (QUOTE NoSource)
								    T)
							 PropList
						else (LIST (QUOTE NoSource)
							       T))))
       finally (RETURN ConvertedLinks))))
)
(* * Fix to functions fixed in FGHPATCH041)

(DEFINEQ

(NC.ScavengeDatabaseFile
  (LAMBDA (NoteFileOrFileName BadLinkLabelsFlg ListOfBoxesToReconstruct 
			      ListOfCardsNeedingGlobalLinksReconstructed)
                                                             (* fgh: "25-May-86 18:24")

          (* Scavenge the database FileName. Essentially throw away all of the information about From and ToLinks and 
	  recreate them by retrieving the link information from the substance of each card and from the list of global links 
	  from the card.)



          (* * rht 8/9/84: Now calls NC.OpenDatabaseFile to do the file open.)



          (* * rht 7/17/85: Changed so can take a stream argument. Also handles link labels. If BadLinkLabelsFlg is non-nil, 
	  then don't try to read current link labels. Just rebuild them from what's out there. Otherwise, only rebuild if 
	  find new any new ones.)



          (* * fgh 22-Jul-85 Takes a list of bad file box cards and reconstructs the file boxes from the From pointer lists 
	  of all the cards in the NoteFile.)



          (* * fgh 30-Jul-85 Takes a list of cards with bad global links and reconstructs the global links list from the From
	  pointer lists of all the cards in the NoteFile.)



          (* * rht 11/23/85: Updated to handle new notefile and card object formats.)



          (* * rht 12/1/85: Now calls NC.GetMainCardData and NC.GetLinks instead of NC.GetNoteCard.)



          (* * rht 12/19/85: Massive overhaul for sake of speed. Should be wizzier now.)



          (* * fgh 2/4/86 Now works on open NFs. No need to error check since this function should always be called from 
	  earlier phases of the inspect & repaier.)



          (* * fgh 5/21/86 Fixed bug in handling of global links.)


    (PROG (NoteFile FileName CardTotal NoteCardNumber OldLinkLabels DiscoveredLinkLabels 
		      ReconstructLinks ReconstructGlobalLinks ToBeFiledCards)

          (* * First, take care of checking stream's validity, etc.)


	    (SETQ FileName (if (type? NoteFile NoteFileOrFileName)
				 then (SETQ NoteFile NoteFileOrFileName)
					(fetch (NoteFile FullFileName) of NoteFileOrFileName)
			       else NoteFileOrFileName))   (* Try to open notefile.)
	    (if (NULL (OPENP FileName))
		then (if (NULL (SETQ NoteFile
				       (NC.OpenDatabaseFile FileName NIL T NIL NIL NIL NIL NIL T)))
			   then (NC.PrintMsg NIL NIL "Couldn't open " FileName "." (CHARACTER
						   13)
						 "Repair aborted."
						 (CHARACTER 13))
				  (RETURN NIL)))

          (* * If link labels aren't screwed up, then read them in.)


	    (OR BadLinkLabelsFlg (SETQ OldLinkLabels (NC.RetrieveLinkLabels NoteFile T)))

          (* * Mark every card that needs its global links or substance reconstructed so we don't have to search the lists so
	  much.)


	    (for Card in ListOfCardsNeedingGlobalLinksReconstructed do (NC.SetUserDataProp
									       Card
									       (QUOTE 
								 NeedsGlobalLinksReconstructedFlg)
									       T))
	    (for Box in ListOfBoxesToReconstruct do (NC.SetUserDataProp Box (QUOTE 
									   NeedsReconstructingFlg)
										T))

          (* Read through all NoteCard substances to find actual pointers. Use this to create the To Links list.
	  The list collection function checks to make sure each link is valid.)


	    (SETQ CardTotal (SUB1 (fetch (NoteFile NextIndexNum)
					       NoteFile)))
	    (NC.PrintMsg NIL T "Rebuilding notefile links." (CHARACTER 13)
			   "Collecting Links for item " 1 " out of " CardTotal "." (CHARACTER
			     13))
	    (SETQ NoteCardNumber 0)
	    (NC.MapCards
	      NoteFile
	      (FUNCTION (LAMBDA (Card)
		  (SETQ NoteCardNumber (ADD1 NoteCardNumber))
		  (AND (ZEROP (REMAINDER NoteCardNumber 10))
			 (NC.PrintMsg NIL T "Rebuilding notefile links." (CHARACTER 13)
					"Collecting Links for item " NoteCardNumber " out of " 
					CardTotal "." (CHARACTER 13)))
		  (if (NC.FetchUserDataProp Card (QUOTE NeedsReconstructingFlg))
		      then                                 (* Card substance and links will be reconstructed so 
							     no need to try to read substance.)
			     (NC.GetLinks Card)
			     (if (NOT (NC.FetchUserDataProp Card (QUOTE 
								 NeedsGlobalLinksReconstructedFlg)))
				 then (NC.SetUserDataProp Card (QUOTE ScavengerToLinks)
							      (NC.FetchGlobalLinks Card))
					(NC.SetUserDataProp Card (QUOTE ScavengerGlobalLinks)
							      (NC.FetchGlobalLinks Card)))
			     (NC.DeactivateCard Card T)
		    else (NC.GetMainCardData Card)
			   (NC.GetLinks Card)
			   (NC.ActivateCard Card)
			   (if (EQ (NC.FetchStatus Card)
				       (QUOTE ACTIVE))
			       then                        (* Collect links having active destinations.
							     Delete the others.)
				      (NC.SetUserDataProp
					Card
					(QUOTE ScavengerToLinks)
					(NCONC (for Link in (CAR (NC.CollectReferences
									   Card))
						    eachtime (BLOCK)
						    when (if (EQ (NC.FetchStatus
									 (fetch (Link 
										  DestinationCard)
									    of Link))
								       (QUOTE ACTIVE))
							     else (NC.DelReferencesToCard Card 
											     Link)
								    NIL)
						    collect Link)
						 (if (NC.FetchUserDataProp Card (QUOTE 
								 NeedsGlobalLinksReconstructedFlg))
						   else (NC.SetUserDataProp Card (QUOTE 
									     ScavengerGlobalLinks)
										(NC.FetchGlobalLinks
										  Card))
							  (NC.FetchGlobalLinks Card))))
				      (if (NC.FetchUserDataProp Card (QUOTE 
								 NeedsGlobalLinksReconstructedFlg))
					else (NC.SetUserDataProp Card (QUOTE 
									     ScavengerGlobalLinks)
								     (NC.FetchGlobalLinks Card)))

          (* If there are file boxes to be reconstructed, then look thru the From links to see if this card was filed in one 
	  of the to-be-reconstructed boxes)


				      (AND ListOfBoxesToReconstruct
					     (for Link in (NC.FetchFromLinks Card)
						eachtime (BLOCK)
						when (AND (NC.ChildLinkP Link)
							      (NC.FetchUserDataProp
								(fetch (Link SourceCard)
								   of Link)
								(QUOTE NeedsReconstructingFlg)))
						do (push ReconstructLinks Link)))

          (* If there are global links to be reconstructed, then look thru the From links to see if this card had a global 
	  link from a card whose global links need reconstructing.)


				      (AND ListOfCardsNeedingGlobalLinksReconstructed
					     (for Link in (NC.FetchFromLinks Card)
						eachtime (BLOCK)
						when (AND (NC.GlobalLinkP Link)
							      (NC.FetchUserDataProp
								(fetch (Link SourceCard)
								   of Link)
								(QUOTE 
								 NeedsGlobalLinksReconstructedFlg)))
						do (push ReconstructGlobalLinks Link)))
				      (NC.DeactivateCard Card T))))))

          (* * Reconstruct any cards as requested)


	    (for BoxToReconstruct in ListOfBoxesToReconstruct eachtime (BLOCK)
	       do                                          (* Make a new file box using the given card.)
		    (NC.MakeNoteCard (QUOTE FileBox)
				       NoteFile "Untitled: Reconstructed during repair" T NIL 
				       BoxToReconstruct)

          (* File cards whose from links indicate that they used to be filed in this file box. Also add these new links to 
	  collected ToLinks.)


		    (NC.SetUserDataProp BoxToReconstruct (QUOTE ScavengerToLinks)
					  (APPEND (NC.FetchUserDataProp BoxToReconstruct
									    (QUOTE ScavengerToLinks)
									    )
						    (for Link in ReconstructLinks
						       eachtime (BLOCK)
						       when (NC.SameCardP BoxToReconstruct
									      (fetch (Link 
										       SourceCard)
										 of Link))
						       collect (NC.MakeChildLink
								   (fetch (Link DestinationCard)
								      of Link)
								   BoxToReconstruct NIL))))
                                                             (* Put the card away)
		    (NC.PutMainCardData BoxToReconstruct)
		    (NC.DeactivateCard BoxToReconstruct T))

          (* * Reconstruct any global link lists as required)


	    (for Link in ReconstructGlobalLinks bind ThisCardsToLinks ThisCardsGlobalLinks 
							   SourceCard
	       eachtime (BLOCK)
	       do (SETQ SourceCard (fetch (Link SourceCard) of Link)) 
                                                             (* Add it to the GlobalLinks list for its source card 
							     unless it's already there.)
		    (if (for GlobalLink in (SETQ ThisCardsGlobalLinks (NC.FetchUserDataProp
						     SourceCard
						     (QUOTE ScavengerGlobalLinks)))
			     eachtime (BLOCK) never (NC.SameLinkP Link GlobalLink))
			then (NC.SetUserDataProp SourceCard (QUOTE ScavengerGlobalLinks)
						     (CONS Link ThisCardsGlobalLinks)))
                                                             (* Add it to the source card's ToLinks list unless 
							     it's already there)
		    (if (for ToLink in (SETQ ThisCardsToLinks (NC.FetchUserDataProp
						 SourceCard
						 (QUOTE ScavengerToLinks)))
			     eachtime (BLOCK) never (NC.SameLinkP Link ToLink))
			then (NC.SetUserDataProp SourceCard (QUOTE ScavengerToLinks)
						     (CONS Link ThisCardsToLinks))))

          (* * Compute the From Links list by "inverting" the To Links list)


	    (NC.PrintMsg NIL T "Repairing NoteFile." (CHARACTER 13)
			   "Inverting links for item " 1 " out of " CardTotal "." (CHARACTER 13))
	    (SETQ NoteCardNumber 0)
	    (NC.MapCards NoteFile (FUNCTION (LAMBDA (Card)
			       (SETQ NoteCardNumber (ADD1 NoteCardNumber))
			       (AND (ZEROP (REMAINDER NoteCardNumber 100))
				      (NC.PrintMsg NIL T "Repairing NoteFile." (CHARACTER 13)
						     "Inverting links for item " NoteCardNumber 
						     " out of "
						     CardTotal "." (CHARACTER 13)))
			       (if (EQ (NC.FetchStatus Card)
					   (QUOTE ACTIVE))
				   then (for Link in (NC.FetchUserDataProp Card (QUOTE
										     ScavengerToLinks)
										   )
					     bind DestinationCard LinkLabel eachtime (BLOCK)
					     do            (* Add this ToLink as a FromLink for the link's 
							     destination card.)
						  (NC.SetUserDataProp (SETQ DestinationCard
									  (fetch (Link 
										  DestinationCard)
									     of Link))
									(QUOTE ScavengerFromLinks)
									(CONS Link
										(
									     NC.FetchUserDataProp
										  DestinationCard
										  (QUOTE 
									       ScavengerFromLinks))))
                                                             (* Accumulate the link labels into a list.)
						  (if (NOT (FMEMB (SETQ LinkLabel
									  (fetch (Link Label)
									     of Link))
									DiscoveredLinkLabels))
						      then (push DiscoveredLinkLabels LinkLabel)))
				     ))))

          (* * Reset all of the To and From Links lists in the database)


	    (NC.PrintMsg NIL T "Repairing NoteFile." (CHARACTER 13)
			   "Rewriting links for item " 1 " out of " CardTotal "." (CHARACTER 13))
	    (SETQ NoteCardNumber 0)
	    (NC.MapCards NoteFile (FUNCTION (LAMBDA (Card)
			       (SETQ NoteCardNumber (ADD1 NoteCardNumber))
			       (AND (ZEROP (REMAINDER NoteCardNumber 10))
				      (NC.PrintMsg NIL T "Repairing NoteFile." (CHARACTER 13)
						     "Rewriting links for item " NoteCardNumber 
						     " out of "
						     CardTotal "." (CHARACTER 13)))
			       (if (EQ (NC.FetchStatus Card)
					   (QUOTE ACTIVE))
				   then (NC.SetGlobalLinks Card (NC.FetchUserDataProp
								 Card
								 (QUOTE ScavengerGlobalLinks)))
					  (NC.SetToLinks Card (NC.FetchUserDataProp Card
											(QUOTE
											  
										 ScavengerToLinks)))
					  (NC.SetFromLinks Card (NC.FetchUserDataProp
							       Card
							       (QUOTE ScavengerFromLinks)))
                                                             (* Check whether this card isn't filed anywhere.)
					  (if (AND (NOT (NC.UndeletableCardP Card))
						       (for Link in (NC.FetchFromLinks Card)
							  eachtime (BLOCK) never (
										    NC.ChildLinkP
											 Link)))
					      then (push ToBeFiledCards Card))
					  (NC.PutLinks Card))
                                                             (* Clean any junk off the card.)
			       (NC.DeactivateCard Card T)
			       (NC.SetUserDataPropList Card NIL))))

          (* * File any unfiled cards in the ToBeFiled box.)


	    (if ToBeFiledCards
		then (NC.PrintMsg NIL T "Filing " (LENGTH ToBeFiledCards)
				      " cards in ToBeFiled box ..."
				      (CHARACTER 13))
		       (NCP.FileCards ToBeFiledCards (fetch (NoteFile ToBeFiledCard)
							  of NoteFile)))
                                                             (* Rewrite link labels if we've found any new ones.)
	    (if (LDIFFERENCE DiscoveredLinkLabels OldLinkLabels)
		then (NC.StoreLinkLabels NoteFile (UNION DiscoveredLinkLabels OldLinkLabels)))
                                                             (* Clean up and get out.)
	    (NC.CheckpointDatabase NoteFile T)
	    (NC.ForceDatabaseClose NoteFile)
	    (NC.PrintMsg NIL T "Repair Completed for " (FULLNAME FileName)
			   "."
			   (CHARACTER 13))
	    (if ToBeFiledCards
		then (NC.PrintMsg NIL NIL "Filed " (LENGTH ToBeFiledCards)
				      " cards in ToBeFiled box.")))))
)
(PUTPROPS FGHPATCH044 COPYRIGHT ("Xerox Corporation" 1986))
(DECLARE: DONTCOPY
  (FILEMAP (NIL (833 5221 (NC.GetBrowserSubstance 843 . 2557) (NC.GetGraphSubstance 2559 . 3559) (
NC.PutBrowserSubstance 3561 . 5219)) (5279 7334 (NC.ConvertLinkFormat 5289 . 7332)) (7385 21958 (
NC.ScavengeDatabaseFile 7395 . 21956)))))
STOP