Added in basic folder support to data access framework (no editing folders, no level information)

New Scholar.Objects method for treeView: getTreeRows() -- returns mixed array of all Folder and Object objects in the proper order, type testable with instanceof (e.g. if (obj instanceof Scholar.Folder))

Scholar.Objects.get() and getAll() return arrays hashed by objectID again, since they're no longer used directly by treeView

Scholar.Objects.get() now returns the object directly if only one argument and scalar (i.e. one id)

Updated schema and sample data to handle folder ordering
This commit is contained in:
Dan Stillman 2006-03-23 08:51:05 +00:00
parent cf7cf17d7e
commit d21638c465
5 changed files with 194 additions and 39 deletions

View file

@ -696,8 +696,9 @@ Scholar.Object.prototype._loadObjectData = function(){
/*
* Primary interface for accessing Scholar objects
*/
Scholar.Objects = new function(){
// Private members
var _objects = new Array();
@ -705,6 +706,7 @@ Scholar.Objects = new function(){
// Privileged methods
this.get = get;
this.getAll = getAll;
this.getTreeRows = getTreeRows;
this.reload = reload;
this.reloadAll = reloadAll;
@ -712,6 +714,9 @@ Scholar.Objects = new function(){
* Retrieves (and loads, if necessary) an arbitrary number of objects
*
* Can be passed ids as individual parameters or as an array of ids, or both
*
* If only one argument and it's an id, return object directly;
* otherwise, return array indexed by objectID
*/
function get(){
var toLoad = new Array();
@ -722,7 +727,7 @@ Scholar.Objects = new function(){
}
var ids = Scholar.flattenArguments(arguments);
for (var i=0; i<ids.length; i++){
// Check if already loaded
if (!_objects[ids[i]]){
@ -735,9 +740,15 @@ Scholar.Objects = new function(){
_load(toLoad);
}
// Build return array
// If single id, return the object directly
if (arguments[0] && typeof arguments[0]!='Object'
&& typeof arguments[1]=='undefined'){
return _objects[arguments[0]];
}
// Otherwise, build return array
for (i=0; i<ids.length; i++){
loaded.push(_objects[ids[i]]);
loaded[ids[i]] = _objects[ids[i]];
}
return loaded;
@ -749,22 +760,53 @@ Scholar.Objects = new function(){
*/
function getAll(){
var sql = 'SELECT O.objectID FROM objects O '
+ 'LEFT JOIN objectCreators OC USING (objectID) '
+ 'LEFT JOIN creators C USING (creatorID) '
+ 'LEFT JOIN folders F ON (O.folderID=F.folderID) '
// Only get first creator
+ 'WHERE OC.orderIndex=0 '
// folderID=0 puts root folder items after folders
// TODO: allow folders to intermingle with items, order-wise
+ 'ORDER BY O.folderID=0, F.orderIndex, O.orderIndex';
+ 'LEFT JOIN treeOrder ORD ON (O.objectID=ORD.id AND isFolder=0) '
+ 'ORDER BY orderIndex';
var ids = Scholar.DB.columnQuery(sql);
return this.get(ids);
}
/*
* Returns an array of all folders and objects as
* Scholar.Folder and Scholar.Object instances
*
* Type can tested with instanceof (e.g. if (obj instanceof Scholar.Folder))
*/
function getTreeRows(){
var toReturn = new Array();
var sql = 'SELECT * FROM treeOrder WHERE id>0 ORDER BY orderIndex';
var tree = Scholar.DB.query(sql);
if (!tree){
throw ('treeOrder is empty');
}
_load('all');
for (var i=0, len=tree.length; i<len; i++){
if (parseInt(tree[i]['isFolder'])){
var obj = Scholar.Folders.get(tree[i]['id']);
if (!obj){
throw ('Folder ' + tree[i]['id'] + ' not found');
}
}
else {
var obj = Scholar.Objects.get(tree[i]['id']);
if (!obj){
throw ('Object ' + tree[i]['id'] + ' not found');
}
}
toReturn.push(obj);
}
return toReturn;
}
/*
* Reloads data for specified objects into internal array
*
@ -839,6 +881,86 @@ Scholar.Objects = new function(){
}
/*
* Constructor for Folder object
*
* Generally should be called from Scholar.Folders rather than directly
*/
Scholar.Folder = function(){
this._id;
this._name;
this._parent;
}
/*
* Build folder from database
*/
Scholar.Folder.prototype.loadFromID = function(id){
var sql = 'SELECT * FROM folders WHERE folderID=' + id;
var row = Scholar.DB.rowQuery(sql);
this.loadFromRow(row);
}
/*
* Populate folder data from a database row
*/
Scholar.Folder.prototype.loadFromRow = function(row){
this._id = row['folderID'];
this._name = row['folderName'];
this._parent = row['parentFolderID'];
}
Scholar.Folder.prototype.getID = function(){
return this._id;
}
Scholar.Folder.prototype.getName = function(){
return this._name;
}
/*
* Primary interface for accessing Scholar folders
*/
Scholar.Folders = new function(){
var _folders = new Array();
var _foldersLoaded = false;
this.get = get;
/*
* Returns a Scholar.Folder object for a folderID
*/
function get(id){
if (!_foldersLoaded){
_load();
}
return (typeof _folders[id]!='undefined') ? _folders[id] : false;
}
function _load(){
var sql = "SELECT * FROM folders WHERE folderID>0"; // skip 'root' folder
var result = Scholar.DB.query(sql);
if (!result){
throw ('No folders exist');
}
for (var i=0; i<result.length; i++){
var folder = new Scholar.Folder();
folder.loadFromRow(result[i]);
_folders[folder.getID()] = folder;
}
_foldersLoaded = true;
}
}
Scholar.Creators = new function(){
var _creators = new Array; // indexed by first%%%last%%%creatorTypeID hash
var _creatorsByID = new Array; // indexed by creatorID

View file

@ -411,11 +411,11 @@ Scholar.DB = new function(){
for (var i=parseInt(fromVersion) + 1; i<=toVersion; i++){
// For now, just wipe and recreate
if (i==2){
if (i==3){
_initializeSchema();
}
if (i==3){
if (i==4){
// do stuff
// _updateDBVersion(i);
}

View file

@ -1,7 +1,7 @@
const SCHOLAR_CONFIG = {
GUID: 'scholar@chnm',
DB_FILE: 'scholar.sqlite',
DB_VERSION: 2,
DB_VERSION: 3,
DB_REBUILD: false, // erase DB and recreate from schema
DEBUG_LOGGING: true,
DEBUG_TO_CONSOLE: false // dump debug messages to console rather than (much slower) Debug Logger

View file

@ -25,7 +25,7 @@ var treeView = {
setTree: function(treebox)
{
this.treebox = treebox;
this.dataObjects = Scholar.Objects.getAll();
this.dataObjects = Scholar.Objects.getTreeRows();
},
isContainer: function(row){ return false; },
isSeparator: function(row){ return false; },

View file

@ -1,4 +1,4 @@
-- 2
-- 3
DROP TABLE IF EXISTS version;
CREATE TABLE version (
@ -15,7 +15,6 @@
source TEXT,
rights TEXT,
folderID INT,
orderIndex INT,
FOREIGN KEY (folderID) REFERENCES folders(folderID)
);
CREATE INDEX folderID ON objects(folderID);
@ -105,14 +104,23 @@
DROP TABLE IF EXISTS folders;
CREATE TABLE folders (
folderID INTEGER PRIMARY KEY,
folderID INT,
folderName TEXT,
parentFolderID INT DEFAULT 0,
orderIndex INT,
PRIMARY KEY (folderID),
FOREIGN KEY (parentFolderID) REFERENCES folders(folderID)
);
CREATE INDEX parentFolderID ON folders(parentFolderID);
INSERT INTO folders VALUES (0, 'root', 0, 0);
INSERT INTO folders VALUES (0, 'root', 0);
DROP TABLE IF EXISTS treeOrder;
CREATE TABLE treeOrder (
id INT,
isFolder INT,
orderIndex INT,
PRIMARY KEY (id, isFolder)
);
INSERT INTO treeOrder VALUES (0, 1, 0);
-- Some sample data
INSERT INTO objectTypes VALUES (1,'Book');
@ -148,21 +156,21 @@
INSERT INTO objectTypeFields VALUES (2,3,3);
INSERT INTO objectTypeFields VALUES (2,8,4);
INSERT INTO "objects" VALUES(1, 1, 'Online connections: Internet interpersonal relationships', '2006-03-12 05:24:40', '2006-03-12 05:24:40', NULL, NULL, 0, 5);
INSERT INTO "objects" VALUES(2, 1, 'Computer-Mediated Communication: Human-to-Human Communication Across the Internet', '2006-03-12 05:25:50', '2006-03-12 05:25:50', NULL, NULL, 0, 2);
INSERT INTO "objects" VALUES(3, 2, 'Residential propinquity as a factor in marriage selection', '2006-03-12 05:26:37', '2006-03-12 05:26:37', NULL, NULL, 0, 1);
INSERT INTO "objects" VALUES(4, 1, 'Connecting: how we form social bonds and communities in the Internet age', '2006-03-12 05:27:15', '2006-03-12 05:27:15', NULL, NULL, 0, 4);
INSERT INTO "objects" VALUES(5, 1, 'Male, Female, Email: The Struggle for Relatedness in a Paranoid Society', '2006-03-12 05:27:36', '2006-03-12 05:27:36', NULL, NULL, 0, 3);
INSERT INTO "objects" VALUES(6, 2, 'Social Implications of Sociology', '2006-03-12 05:27:53', '2006-03-12 05:27:53', NULL, NULL, 0, 7);
INSERT INTO "objects" VALUES(7, 1, 'Social Pressures in Informal Groups: A Study of Human Factors in Housing', '2006-03-12 05:28:05', '2006-03-12 05:28:05', NULL, NULL, 0, 6);
INSERT INTO "objects" VALUES(8, 1, 'Cybersociety 2.0: Revisiting Computer-Mediated Community and Technology', '2006-03-12 05:28:37', '2006-03-12 05:28:37', NULL, NULL, 0, 9);
INSERT INTO "objects" VALUES(9, 2, 'The Computer as a Communication Device', '2006-03-12 05:29:03', '2006-03-12 05:29:03', NULL, NULL, 0, 8);
INSERT INTO "objects" VALUES(10, 2, 'What Does Research Say about the Nature of Computer-mediated Communication: Task-Oriented, Social-Emotion-Oriented, or Both?', '2006-03-12 05:29:12', '2006-03-12 05:29:12', NULL, NULL, 0, 13);
INSERT INTO "objects" VALUES(11, 1, 'The second self: computers and the human spirit', '2006-03-12 05:30:38', '2006-03-12 05:30:38', NULL, NULL, 0, 10);
INSERT INTO "objects" VALUES(12, 1, 'Life on the screen: identity in the age of the Internet', '2006-03-12 05:30:49', '2006-03-12 05:30:49', NULL, NULL, 0, 11);
INSERT INTO "objects" VALUES(13, 2, 'The computer conference: An altered state of communication', '2006-03-12 05:31:00', '2006-03-12 05:31:00', NULL, NULL, 0, 12);
INSERT INTO "objects" VALUES(14, 2, 'Computer Networks as Social Networks: Collaborative Work, Telework, and Community', '2006-03-12 05:31:17', '2006-03-12 05:31:17', NULL, NULL, 0, 14);
INSERT INTO "objects" VALUES(15, 1, 'The Internet in everyday life', '2006-03-12 05:31:41', '2006-03-12 05:31:41', NULL, NULL, 0, 15);
INSERT INTO "objects" VALUES(1, 1, 'Online connections: Internet interpersonal relationships', '2006-03-12 05:24:40', '2006-03-12 05:24:40', NULL, NULL, 0);
INSERT INTO "objects" VALUES(2, 1, 'Computer-Mediated Communication: Human-to-Human Communication Across the Internet', '2006-03-12 05:25:50', '2006-03-12 05:25:50', NULL, NULL, 0);
INSERT INTO "objects" VALUES(3, 2, 'Residential propinquity as a factor in marriage selection', '2006-03-12 05:26:37', '2006-03-12 05:26:37', NULL, NULL, 0);
INSERT INTO "objects" VALUES(4, 1, 'Connecting: how we form social bonds and communities in the Internet age', '2006-03-12 05:27:15', '2006-03-12 05:27:15', NULL, NULL, 0);
INSERT INTO "objects" VALUES(5, 1, 'Male, Female, Email: The Struggle for Relatedness in a Paranoid Society', '2006-03-12 05:27:36', '2006-03-12 05:27:36', NULL, NULL, 0);
INSERT INTO "objects" VALUES(6, 2, 'Social Implications of Sociology', '2006-03-12 05:27:53', '2006-03-12 05:27:53', NULL, NULL, 0);
INSERT INTO "objects" VALUES(7, 1, 'Social Pressures in Informal Groups: A Study of Human Factors in Housing', '2006-03-12 05:28:05', '2006-03-12 05:28:05', NULL, NULL, 0);
INSERT INTO "objects" VALUES(8, 1, 'Cybersociety 2.0: Revisiting Computer-Mediated Community and Technology', '2006-03-12 05:28:37', '2006-03-12 05:28:37', NULL, NULL, 0);
INSERT INTO "objects" VALUES(9, 2, 'The Computer as a Communication Device', '2006-03-12 05:29:03', '2006-03-12 05:29:03', NULL, NULL, 0);
INSERT INTO "objects" VALUES(10, 2, 'What Does Research Say about the Nature of Computer-mediated Communication: Task-Oriented, Social-Emotion-Oriented, or Both?', '2006-03-12 05:29:12', '2006-03-12 05:29:12', NULL, NULL, 0);
INSERT INTO "objects" VALUES(11, 1, 'The second self: computers and the human spirit', '2006-03-12 05:30:38', '2006-03-12 05:30:38', NULL, NULL, 0);
INSERT INTO "objects" VALUES(12, 1, 'Life on the screen: identity in the age of the Internet', '2006-03-12 05:30:49', '2006-03-12 05:30:49', NULL, NULL, 1241);
INSERT INTO "objects" VALUES(13, 2, 'The computer conference: An altered state of communication', '2006-03-12 05:31:00', '2006-03-12 05:31:00', NULL, NULL, 6856);
INSERT INTO "objects" VALUES(14, 2, 'Computer Networks as Social Networks: Collaborative Work, Telework, and Community', '2006-03-12 05:31:17', '2006-03-12 05:31:17', NULL, NULL, 6856);
INSERT INTO "objects" VALUES(15, 1, 'The Internet in everyday life', '2006-03-12 05:31:41', '2006-03-12 05:31:41', NULL, NULL, 7373);
INSERT INTO "objectData" VALUES(1, 7, 2001);
INSERT INTO "objectData" VALUES(1, 5, 'Cresskill, N.J.');
@ -206,3 +214,28 @@
INSERT INTO "objectCreators" VALUES(7, 7, 1);
INSERT INTO "objectCreators" VALUES(7, 8, 2);
INSERT INTO "objectCreators" VALUES(9, 11, 1);
INSERT INTO folders VALUES (1241, 'Test Folder', 0);
INSERT INTO folders VALUES (3262, 'Another Test Folder', 0);
INSERT INTO folders VALUES (6856, 'A Subfolder', 0);
INSERT INTO folders VALUES (7373, 'A Sub-Subfolder!', 3262);
INSERT INTO treeOrder VALUES (1, 0, 1);
INSERT INTO treeOrder VALUES (3262, 1, 2);
INSERT INTO treeOrder VALUES (2, 0, 3);
INSERT INTO treeOrder VALUES (3, 0, 4);
INSERT INTO treeOrder VALUES (4, 0, 5);
INSERT INTO treeOrder VALUES (5, 0, 6);
INSERT INTO treeOrder VALUES (6, 0, 7);
INSERT INTO treeOrder VALUES (7, 0, 8);
INSERT INTO treeOrder VALUES (8, 0, 9);
INSERT INTO treeOrder VALUES (9, 0, 10);
INSERT INTO treeOrder VALUES (6856, 1, 11);
INSERT INTO treeOrder VALUES (14, 0, 12);
INSERT INTO treeOrder VALUES (13, 0, 13);
INSERT INTO treeOrder VALUES (7373, 1, 14);
INSERT INTO treeOrder VALUES (15, 0, 15);
INSERT INTO treeOrder VALUES (11, 0, 16);
INSERT INTO treeOrder VALUES (10, 0, 17);
INSERT INTO treeOrder VALUES (1241, 1, 18);
INSERT INTO treeOrder VALUES (12, 0, 19);