From: <ndi...@us...> - 2002-09-08 03:45:20
|
Update of /cvsroot/modus/org/bacfug/modus/persistence In directory usw-pr-cvs1:/tmp/cvs-serv10123/bacfug/modus/persistence Modified Files: basepersister.cfc Log Message: added in a method to get sorted objects of a given type Index: basepersister.cfc =================================================================== RCS file: /cvsroot/modus/org/bacfug/modus/persistence/basepersister.cfc,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** basepersister.cfc 8 Sep 2002 02:37:45 -0000 1.3 --- basepersister.cfc 8 Sep 2002 03:45:16 -0000 1.4 *************** *** 20,24 **** <cffunction name="getAll" access="public" output="no" hint="The method to get all data instances of a particular type. Should return an array of contentObjects."> <cfthrow message="getAll() method not implemented!" detail="You must defined a getAll() method for #getMetaData(this).name#."> ! </cffunction> <!--- a package function to determine if an object passed is, indeed, a baseContentObject ---> --- 20,82 ---- <cffunction name="getAll" access="public" output="no" hint="The method to get all data instances of a particular type. Should return an array of contentObjects."> <cfthrow message="getAll() method not implemented!" detail="You must defined a getAll() method for #getMetaData(this).name#."> ! </cffunction> ! ! <!--- a function for sorting ---> ! <!--- ! ! I'm really not at all satisfied with this, but it does work, for now. ! ! ---> ! <cffunction name="getAllSorted" access="public" output="no" hint="A method to sort all instances and sort them by one or more fields"> ! <cfargument name="sortFields" required="yes" type="string"> ! <cfargument name="contentObjectType" required="yes" type="string"> ! <cfset var tempQuery = ""> ! <cfset var sortQuery = ""> ! <cfset var objectArray = getAll(arguments.contentObjectType)> ! <cfset var sortedArray = arrayNew(1)> ! <cfset var objectCount = arrayLen(objectArray)> ! <cfset var sortFieldsArray = listToArray(arguments.sortFields)> ! <cfset var ii = 0> ! <cfset var ff = 0> ! <cfset var thisObject = ""> ! <cfset var thisFieldName = ""> ! <cfscript> ! //clean the sortFieldsArray, so we can have the sort order in there too ! for(ii = 1; ii LTE arrayLen(sortFieldsArray); ii = ii + 1){ ! sortFieldsArray[ii] = getToken(sortFieldsArray[ii],1); ! } ! //prime the tempQuery ! tempQuery = queryNew("index," & arrayToList(sortFieldsArray)); ! //resize the query to however many objects there are ! queryAddRow(tempQuery,objectCount); ! //loop through all objects, populating the appropriate columns of the query ! for(ii = 1; ii LTE objectCount; ii = ii + 1){ ! thisObject = objectArray[ii]; ! querySetCell(tempQuery,"index",ii,ii); ! //loop through whatever fields we are sorting on, adding the values of those fields to the query ! for(ff = 1; ff LTE arrayLen(sortFieldsArray); ff = ff + 1){ ! thisFieldName = getToken(sortFieldsArray[ff],1); ! querySetCell(tempQuery,thisFieldName,trim(thisObject.getField(thisFieldName).getValue()),ii); ! } ! } ! </cfscript> ! <!--- ok, now let's get the sortedQuery ---> ! <cfquery name="sortQuery" dbtype="query"> ! SELECT * ! FROM tempQuery ! ORDER BY #arguments.sortFields# ! </cfquery> ! <!--- and now, we'll populate the sortedArray ---> ! <cfscript> ! //resize the array, so it doesn't have to keep allocating more memory ! arrayResize(sortedArray,objectCount); ! for(ii = 1; ii LTE objectCount; ii = ii + 1){ ! sortedArray[ii] = objectArray[sortQuery.index[ii]]; ! } ! </cfscript> ! <!--- return the sorted Array ---> ! <cfreturn sortedArray> ! </cffunction> ! <!--- a package function to determine if an object passed is, indeed, a baseContentObject ---> |