home *** CD-ROM | disk | FTP | other *** search
/ Stickerpedia Stickerbook / Stickerbook.iso / pc / DATA / objects.cxt / 00071_Script_oDatabase < prev    next >
Text File  |  2003-03-19  |  16KB  |  438 lines

  1. --¨
  2. oDatabase
  3.  
  4.  
  5. global  oHQ
  6. global  oDatabase
  7. global  oDataScreen
  8. global  oFileIO
  9. global  oMarvin
  10. global  oUserManager
  11.  
  12. global  gPathDelim
  13.  
  14.  
  15.  
  16.  
  17.  
  18. property  pKWCodeToKWName  -- property list; format: ["k01": "Security", "k02": "Government", etc.]
  19. property  pRecNumToKW      -- property list; format: ["2001": ["k22", "k22"],etc]
  20. property  pKWToRecNum      -- all keyword combinations as properties; a list of associated recNumbers as values
  21. property  pKWtoKWP         -- property list. Props are single keywords, their values are all keyword pairs associated with them
  22. property  pKWToRecList     -- property list. Props are single keywords, their values are all records associated with them (via various kwps)
  23.  
  24.  
  25. property  pNumOfRecords    -- count of pRecNumToKW  (the number of records)
  26.  
  27.  
  28.  
  29. property  pNumOfKWPairs    -- the number of keyword pairs used
  30. property  pKWPairHits      -- property list; properties are the used keyword combinations; values are integer counters, which default to 0
  31. ----------------------------- it also serves as an indicator of whether a KW combination has been visited yet
  32. property  pKWHits          -- this list is used to reshuffle the list 'pKWPSortOrder' according to user preferences; 
  33. property  pKWPSortOrder    -- this list contains the keyword pairs in their sort order, which updates every time the user makes a selection
  34. property  pRecordStatus    -- keeps track of each record, and whether they are visited or not visited (#vtd, #utd)
  35. property  pRecordNumbers   -- contains a simple list of recorsd numbers so it can be double-checked whether a record number is legal
  36. property  pIllegalRecords  -- contains list of illegal records - gets built up during running of programme and gets saved to file as "illegalrecords.txt" in the debug folder
  37.  
  38.  
  39.  
  40.  
  41. on new me, updateMode
  42.   
  43.   addObject me
  44.   
  45.   
  46.   -- this list is good unless new keywords are added or keyword names are changed
  47.   pKWCodeToKWName = ["k01": "Security", "k02": "Government", "k03": "Communist Party", "k04": "Rulers", "k05": "Astronomy", ¨
  48.                      "k06": "Population", "k07": "External Relations", "k08": "Foreign Trade", "k09": "Tourism", "k10": "Communications", ¨
  49.                      "k11": "Cities", "k12": "City Life", "k13": "Modern architecture", "k14": "Infrastructure", "k15": "Transportation", ¨
  50.                      "k16": "Economy", "k17": "State Enterprise", "k18": "Engineering", "k19": "High Technology", "k20": "Information Technology", ¨
  51.                      "k21": "Industry", "k22": "Trade", "k23": "Commerce", "k24": "Employment", "k25": "Individuals", ¨
  52.                      "k26": "Education", "k27": "Writing", "k28": "Language/Literacy", "k29": "Family", "k30": "Traditions", ¨
  53.                      "k31": "Mind and Body", "k32": "Medicine", "k33": "Recreation", "k34": "Provincial Life", "k35": "Rural Life", ¨
  54.                      "k36": "Geography", "k37": "Food", "k38": "Agriculture", "k39": "Water Control", "k40": "Environment", ¨
  55.                      "k41": "Natural Resources", "k42": "Iron and steel", "k43": "Bronze", "k44": "Innovation", "k45": "Silk", ¨
  56.                      "k46": "Porcelain", "k47": "Crafts", "k48": "Printing", "k49": "Arts", "k50": "Mass Media", ¨
  57.                      "k51": "Mythology", "k52": "Daoism", "k53": "Ancestor worship", "k54": "Buddhism", "k55": "Christianity", ¨
  58.                      "k56": "Islam", "k57": "Philosophy", "k58": "Confucius", "k59": "History", "k60": "Archaeology", "k61": "Traditional Architecture"]
  59.   
  60.   -- this list is good unless more keywords are added
  61.   pKWHits         = ["k01": 0, "k02": 0, "k03": 0, "k04": 0, "k05": 0, "k06": 0, "k07": 0, "k08": 0, "k09": 0, "k10": 0, ¨
  62.                      "k11": 0, "k12": 0, "k13": 0, "k14": 0, "k15": 0, "k16": 0, "k17": 0, "k18": 0, "k19": 0, "k20": 0, ¨
  63.                      "k21": 0, "k22": 0, "k23": 0, "k24": 0, "k25": 0, "k26": 0, "k27": 0, "k28": 0, "k29": 0, "k30": 0, ¨
  64.                      "k31": 0, "k32": 0, "k33": 0, "k34": 0, "k35": 0, "k36": 0, "k37": 0, "k38": 0, "k39": 0, "k40": 0, ¨
  65.                      "k41": 0, "k42": 0, "k43": 0, "k44": 0, "k45": 0, "k46": 0, "k47": 0, "k48": 0, "k49": 0, "k50": 0, ¨
  66.                      "k51": 0, "k52": 0, "k53": 0, "k54": 0, "k55": 0, "k56": 0, "k57": 0, "k58": 0, "k59": 0, "k60": 0, "k61": 0]
  67.   
  68.   
  69.   
  70.   if updateMode = #update then
  71.     -- uncomment the following two handlers to generate necessary files
  72.     initialise me
  73.     writeAllListsToFile me
  74.     pIllegalRecords = []
  75.   else
  76.     getAllListsFromFile me
  77.   end if
  78.   
  79.   pNumOfRecords = pRecNumToKW.count
  80.   pNumOfKWPairs = count(pKWToRecNum)
  81.   
  82.   return me  
  83.   
  84. end
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94. on writeAllListsToFile me
  95.   
  96.   loFileIO = new(script "oFileIO")
  97.   
  98.   put "Writing pKWToRecNum.ini"
  99.   writeFile loFileIO, the pathname & "~lists" & gPathDelim & "database" & gPathDelim, "pKWToRecNum.ini",       string(pKWToRecNum)
  100.   put "Done"
  101.   
  102.   put "Writing pKWPairHits.ini"
  103.   writeFile loFileIO, the pathname & "~lists" & gPathDelim & "database" & gPathDelim, "pKWPairHits.ini",       string(pKWPairHits)
  104.   put "Done"
  105.   
  106.   put "Writing pKWPSortOrder.ini"
  107.   writeFile loFileIO, the pathname & "~lists" & gPathDelim & "database" & gPathDelim, "pKWPSortOrder.ini",     string(pKWPSortOrder)
  108.   put "Done"
  109.   
  110.   put "Writing pRecordStatus.ini"
  111.   writeFile loFileIO, the pathname & "~lists" & gPathDelim & "database" & gPathDelim, "pRecordStatus.ini",     string(pRecordStatus)
  112.   put "Done"
  113.   
  114.   put "Writing pRecNumToKW.ini"
  115.   writeFile loFileIO, the pathname & "~lists" & gPathDelim & "database" & gPathDelim, "pRecNumToKW.ini",       string(pRecNumToKW)
  116.   put "Done"
  117.   
  118.   put "Writing pKWtoKWP.ini"
  119.   writeFile loFileIO, the pathname & "~lists" & gPathDelim & "database" & gPathDelim, "pKWtoKWP.ini",          string(pKWtoKWP)
  120.   put "Done"
  121.   
  122.   put "Writing pRecordNumbers.ini"
  123.   writeFile loFileIO, the pathname & "~lists" & gPathDelim & "database" & gPathDelim, "pRecordNumbers.ini",    string(pRecordNumbers)
  124.   put "Done"
  125.   
  126.   
  127.   
  128.   loFileIO = void
  129.   
  130.   put "Updating oDatabase - Finished"
  131.   
  132.   tempText = tempText && "New oDatabase list files saved:" && the long time
  133.   
  134.   member("updating log internal", "objects").text = member("updating log internal", "objects").text & RETURN & RETURN & tempText
  135.   
  136. end
  137.  
  138.  
  139.  
  140.  
  141.  
  142. on getAllListsFromFile me
  143.   
  144.   loFileIO = new(script "oFileIO")
  145.   
  146.   pKWToRecNum      = value(getFile (loFileIO, the pathname & "~lists" & gPathDelim & "database" & gPathDelim, "pKWToRecNum.ini"))
  147.   pKWPairHits      = value(getFile (loFileIO, the pathname & "~lists" & gPathDelim & "database" & gPathDelim, "pKWPairHits.ini"))
  148.   pKWPSortOrder    = value(getFile (loFileIO, the pathname & "~lists" & gPathDelim & "database" & gPathDelim, "pKWPSortOrder.ini"))
  149.   pRecordStatus    = value(getFile (loFileIO, the pathname & "~lists" & gPathDelim & "database" & gPathDelim, "pRecordStatus.ini"))
  150.   pRecNumToKW      = value(getFile (loFileIO, the pathname & "~lists" & gPathDelim & "database" & gPathDelim, "pRecNumToKW.ini"))
  151.   pKWtoKWP         = value(getFile (loFileIO, the pathname & "~lists" & gPathDelim & "database" & gPathDelim, "pKWtoKWP.ini"))
  152.   pRecordNumbers   = value(getFile (loFileIO, the pathname & "~lists" & gPathDelim & "database" & gPathDelim, "pRecordNumbers.ini"))
  153.   pIllegalRecords  = value(getFile (loFileIO, the pathname & "~debug" & gPathDelim, "pIllegalRecords.txt"))
  154.   put "pIllegalRecords" && pIllegalRecords
  155.   loFileIO = void
  156. end
  157.  
  158.  
  159.  
  160.  
  161. on savePrefs me, theUsername
  162.   
  163.   loFileIO = new(script "oFileIO")
  164.   
  165.   thePath = (oUserManager.pPathname & theUsername & gPathDelim)
  166.   
  167.   put "thePath: " & thePath
  168.   
  169.   writeFile loFileIO, thePath,  "pKWPairHits.ini",     string(pKWPairHits)
  170.   writeFile loFileIO, thePath,  "pKWPSortOrder.ini",   string(pKWPSortOrder)
  171.   writeFile loFileIO, thePath,  "pRecordStatus.ini",   string(pRecordStatus)
  172.   
  173.   loFileIO = void
  174.   
  175. end
  176.  
  177.  
  178. on getDataBasePrefs me, theUsername
  179.   
  180.   loFileIO = new(script "oFileIO")
  181.   
  182.   thePath = (oUserManager.pPathname & theUsername & gPathDelim)
  183.   
  184.   pKWPairHits      = value(getFile (loFileIO, thePath, "pKWPairHits.ini"))
  185.   pKWPSortOrder    = value(getFile (loFileIO, thePath, "pKWPSortOrder.ini"))
  186.   pRecordStatus    = value(getFile (loFileIO, thePath, "pRecordStatus.ini"))
  187.   
  188.   loFileIO = void
  189.   put "pKWPairHits: " &  pKWPairHits
  190. end
  191.  
  192.  
  193.  
  194.  
  195. on initialise me
  196.   
  197.   -- updateProgressBar ("oDatabase: Initialising")
  198.   
  199.   
  200.   -- creating pRecNumToKW which is good for somethign, I swear, I just can't remember now what for
  201.   -- it's used as a base lsit for following lines of code - basically it is a list representation of the text file export from the database
  202.   -- make a list that looks like this: ["2001": ["k22", "k22"], "2002": ["k54", "k54"], etc. ]
  203.   -- properties are the recnumbers, their values are the keywords associated with it
  204.   
  205.   pRecNumToKW = [:]
  206.   theListText       = member("recordnumbers and keywords").text
  207.   numOfEntries      = theListText.lines.count
  208.   the itemDelimiter = ","
  209.   
  210.   repeat with n = 1 to numOfEntries
  211.     recNum   = value(theListText.line[n].item[1])
  212.     keyWord1 = value(theListText.line[n].item[2])
  213.     keyWord2 = value(theListText.line[n].item[3])
  214.     if keyWord1 = "na" then keyWord1 = "k99"
  215.     if keyWord2 = "na" then keyWord2 = "k99"
  216.     pRecNumToKW.addProp(recNum, [keyWord1, keyWord2])
  217.   end repeat
  218.   sort pRecNumToKW
  219.   --put pRecNumToKW
  220.   put RETURN & "******** Doublecheck this list for empty entries ********"
  221.   
  222.   
  223.   -- create list which has an entry for every keyword pair as a property
  224.   -- and a list of all records that are allocated to this pair as a value
  225.   -- updateProgressBar ("oDatabase: creating pKWToRecNum")
  226.   
  227.   pNumOfRecords = pRecNumToKW.count
  228.   pKWToRecNum = [:]
  229.   thisKWPair  = ""
  230.   thisRecNum  = ""
  231.   
  232.   repeat with pointer = 1 to pNumOfRecords
  233.     
  234.     thisKWP    = pRecNumToKW[pointer]
  235.     thisKWPID  = kwpLstToKWPStr(me, thisKWP)
  236.     thisRecNum = pRecNumToKW.getPropAt(pointer)
  237.     
  238.     
  239.     -- status info
  240.     member ("progress details", "shared").text = string(thisKWPID)
  241.     
  242.     if not pKWToRecNum.findPos(thisKWPID) then
  243.       -- if there is no entry for this kw combination, then add one
  244.       pKWToRecNum.addProp(thisKWPID, [thisRecNum])
  245.     else
  246.       pKWToRecNum[thisKWPID].add(thisRecNum)
  247.     end if
  248.     
  249.   end repeat
  250.   
  251.   -- updateProgressBar ("oDatabase: sorting pKWToRecNum")
  252.   
  253.   sort pKWToRecNum
  254.   
  255.   -- updateProgressBar ("oDatabase: checking pKWToRecNum integrity")
  256.   
  257.   -- check integrity of this list:
  258.   pNumOfKWPairs = count(pKWToRecNum)
  259.   numOfEntries  = 0
  260.   repeat with n = 1 to pNumOfKWPairs
  261.     numOfEntries = numOfEntries + pKWToRecNum[n].count
  262.   end repeat
  263.   
  264.   if numOfEntries <> pNumOfRecords then alert "numOfEntries (" & string(numOfEntries) &&  ") <> pNumOfRecords (" & string(pNumOfRecords) & ")"
  265.   
  266.   
  267.   -- updateProgressBar ("oDatabase: Integrity ok", "Status: ok" & RETURN & "Records found:" && numOfEntries & RETURN & "Keyword Combinations:" && string(pKWToRecNum.count))
  268.   
  269.   -- at this point we need to check whether there is a user record, which would determine the contents of the following lists
  270.   
  271.   -- creating pKWPairHits which counts the hits each keyword pair gets 
  272.   -- updateProgressBar ("oDatabase: creating pKWPairHits" )
  273.   pKWPairHits = [:]
  274.   repeat with pointer = 1 to pNumOfKWPairs
  275.     thisKWP = (pKWToRecNum.getPropAt(pointer))
  276.     if thisKWP = "9999" then exit repeat
  277.     pKWPairHits.addProp(thisKWP,0)
  278.   end repeat
  279.   
  280.   -- creating pKWPSortOrder determines the sort order of the keywords pairs combinations
  281.   -- updateProgressBar ("oDatabase: creating pKWPSortOrder" )
  282.   pKWPSortOrder = [:]
  283.   repeat with pointer = 1 to pNumOfKWPairs
  284.     thisKWP = pKWToRecNum.getPropAt(pointer)
  285.     if thisKWP = "9999" then exit repeat
  286.     pKWPSortOrder.addProp(  (    symbol(string(pointer)), string(thisKWP)     )
  287.   end repeat
  288.   
  289.   
  290.   -- creating pRecordStatus which keeps track of each record, and whether they are visited or not visited (#vtd, #utd)
  291.   -- updateProgressBar ("oDatabase: creating pRecordStatus" )
  292.   pRecordStatus = [:]
  293.   repeat with pointer = 1 to pNumOfRecords
  294.     thisRecord = pRecNumToKW.getPropAt(pointer)
  295.     pRecordStatus.addProp(thisRecord,#std)
  296.   end repeat
  297.   
  298.   
  299.   
  300.   -- creating pKWHits, which tracks how often a particular keyword has been hit - 
  301.   -- this doesn't get saved to disk, it needs to be copied and pasted from the message window into this script 
  302.   
  303.   pKWHits = [:]
  304.   numofEntries  = pKWCodeToKWName.count
  305.   repeat with n = 1 to numofEntries  
  306.     pKWHits.addProp(pKWCodeToKWName.getPropAt(n), 0)
  307.   end repeat
  308.   
  309.   
  310.   -- creating pKWtoKWP, which is a list in the following format: ["k01": [["k01", "k02"], ["k01", "k07"], ["k01", "k30"], ["k01", "k31"], ["k01", "k34"] etc
  311.   -- i.e. all keywords are properties, and the values associated with them are list of all existing key word pair combinations 
  312.   
  313.   pKWtoKWP = [:]
  314.   
  315.   
  316.   repeat with n = 1 to numofEntries  
  317.     pKWtoKWP.addProp(pKWCodeToKWName.getPropAt(n), [])
  318.   end repeat
  319.   
  320.   combinationsList = []
  321.   numofCombinationsUnDiscriminated =  pKWToRecNum.count
  322.   repeat with n = 1 to numofCombinationsUnDiscriminated
  323.     combinationsList.add(pKWToRecNum.getPropAt(n))
  324.   end repeat
  325.   
  326.   numOfCombinations = combinationsList.count
  327.   repeat with n = 1 to pKWtoKWP.count  
  328.     thisKW = pKWtoKWP.getPropAt(n).char[2..3]
  329.     tempList = []
  330.     repeat with i = 1 to numOfCombinations
  331.       thisKWCombination = combinationsList[i]
  332.       if thisKWCombination.char[1..2] = thisKW then
  333.         tempList.add([("k"& thisKWCombination.char[1..2]), ("k"& thisKWCombination.char[3..4])])
  334.       end if
  335.     end repeat
  336.     pKWtoKWP["k" & thisKW] = tempList
  337.   end repeat
  338.   
  339.   -- datascreen uses this list to double check display requests - there may always be a record request which is qctually illegal.
  340.   -- this list contains all legal records
  341.   pRecordNumbers = []
  342.   repeat with pointer = 1 to pNumOfRecords
  343.     thisRecord = pRecordStatus.getPropAt(pointer)
  344.     pRecordNumbers.add(thisRecord)
  345.   end repeat
  346.   -- put "pRecordNumbers" && pRecordNumbers
  347. end
  348.  
  349.  
  350.  
  351.  
  352.  
  353. on recordNumIsLegal me, recordNum
  354.   return pRecordNumbers.findPos(recordNum)  
  355. end
  356.  
  357. -- recordNumListIsLegal
  358.  
  359.  
  360. on legaliseRecNumList me, listOfRecs
  361.   
  362.   -- startTimer
  363.   
  364.   numOfRecs    = listOfRecs.count
  365.   thisCulpritList  = []
  366.   retVal       = listOfRecs
  367.   
  368.   repeat with n = 1 to numOfRecs
  369.     thisRec = listOfRecs[n]
  370.     if not recordNumIsLegal (me, thisRec) then thisCulpritList.add(thisRec)
  371.   end repeat
  372.   
  373.   numOfCultprits = thisCulpritList.count
  374.   
  375.   
  376.   repeat with curCulprit in thisCulpritList
  377.     
  378.     if not findPos(pIllegalRecords, curCulprit) then
  379.       pIllegalRecords.add(curCulprit)
  380.     end if
  381.     
  382.     -- put "curCulprit" && curCulprit
  383.     
  384.     repeat while retVal.findPos(curCulprit)
  385.       deleteOne retVal, curCulprit
  386.     end repeat
  387.   end repeat
  388.   
  389.   --  put "thisCulpritList" && thisCulpritList
  390.   --  put "the timer" && the timer
  391.   
  392.   -- if thisCulpritList.count then customAlert "There are" && string(numOfCultprits) &&  "illegal record numbers in the current list of requested records"& return & "Culprits: " && string(thisCulpritList)
  393.   -- if thisCulpritList.count then put  string(numOfCultprits) &&  "illegal records" & return & "Culprits: " && string(thisCulpritList)
  394.   
  395.   return retVal
  396. end
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404.  
  405.  
  406. on kwpLstToKWPStr me, KWPLst
  407.   -- turns a keyword list into a keyword string as used in pKWToRecNum and pKWPairHits
  408.   -- i.e. put kwpLstToKWPStr(oDataBase, ["k01", "k78"]) results in "0178"
  409.   if listP(KWPLst) then return KWPLst[1].char[2..3] & KWPLst[2].char[2..3]
  410.   else return KWPLst
  411. end
  412.  
  413.  
  414.  
  415. on KWPStrToKWPLst me, KWPstr
  416.   -- turns a keyword list into a keyword string as used in pKWToRecNum and pKWPairHits
  417.   -- i.e. put kwpLstToKWPStr(oDataBase, ["k01", "k78"]) results in "0178"
  418.   
  419.   return [("k" & KWPstr.char[1..2]) , ("k" & KWPstr.char[1..2]) ]
  420. end
  421.  
  422.  
  423.  
  424.  
  425. on recordStatusVisited me, recordNumber
  426.   pRecordStatus[recordNumber] = #vtd
  427. end
  428.  
  429.  
  430.  
  431. on showProps me
  432.   showProps oHQ, me
  433. end
  434.  
  435.  
  436.  
  437.  
  438.