From: <ndi...@us...> - 2002-09-14 01:22:10
|
Update of /cvsroot/modus/org/bacfug/modus/persistence In directory usw-pr-cvs1:/tmp/cvs-serv8624/bacfug/modus/persistence Added Files: simplefilesystempersister.cfc Log Message: this is the basic file system persister that we are going to use as the default persister --- NEW FILE: simplefilesystempersister.cfc --- <cfcomponent extends="org.bacfug.modus.persistence.basePersister" displayname="fileSytemByType" hint="The basic component for storing object instances under the WEB-INF directory on the file system."> <!--- information about the base directory ---> <!--- this should really be in the config!! ---> <cfparam name="instance.baseModusDir" default="#expandPath("/WEB-INF/modus/")#"> <cfparam name="instance.objectStoreDir" default="#instance.baseModusDir#objectstore/"> <!--- make sure the base directory exists ---> <cfif NOT directoryExists(instance.objectStoreDir)> <cfdirectory action="create" directory="#instance.objectStoreDir#"> </cfif> <cffunction name="save" access="public" output="no" returnType="void" hint="The method to save data"> <cfargument name="contentObject" required="yes"> <cfset var objectToSave = ""> <cfset var packet = ""> <!--- make sure we're dealing with a contentObject ---> <cfif isContentObject(arguments.contentObject)> <cfscript> //make the storage struct packet = contentObjectToWddx(arguments.contentObject); </cfscript> <!--- write the file ---> <cffile action="write" file="#getPathFromObject(arguments.contentObject)#" output="#packet#"> <!--- put the object in the cache ---> <cfset cachePutObject(arguments.contentObject)> <!--- if it's not a good contentObject ---> <cfelse> <cfthrow type="modus.badContentObject" message="Bad ContentObject" detail="A bad contentObject was passed to save() in #getMetaData(this).name#"> </cfif> </cffunction> <cffunction name="get" access="public" output="no" returnType="org.bacfug.modus.baseContentObject" hint="The method to get a particular data instance"> <cfargument name="id" type="string" required="yes"> <cfset var objectRetrieved = ""> <cfset var getQuery = ""> <cfset var packet = ""> <cfset var dirToCheck = getBaseStorageDirectory()> <!--- if this object is cached, return that one ---> <cfscript> if(isObjectCached(arguments.id)){ objectRetrieved = cacheGetObject(arguments.id); if(structCount(arguments) GT 1) return contentObjectPopulateFromInstance(arguments[2],objectRetrieved); else return objectRetrieved; } </cfscript> <!--- try getting this instance ---> <cfdirectory action="list" directory="#dirToCheck#" name="getQuery" filter="#arguments.id#*"> <cfscript> //if we found one, grab the object if(getQuery.recordCount){ objectRetrieved = getObjectFromFile(getQuery.name[1]); //if there is a second argument, it means we need to load that instance if(structCount(arguments) GT 1){ contentObjectPopulateFromInstance(arguments[2],objectRetrieved); } } //if none found, that's a bad error else{ throwError("modus.badObjectID","Bad Object ID","The ID ""#arguments.id#"" does not correspond to a known contentObject instance available from #getMetaData(this).name#"); } </cfscript> <cfreturn objectRetrieved> </cffunction> <cffunction name="getAll" access="public" returntype="array" output="no" hint="The method to get all data instances of a particular type"> <cfargument name="type" required="no" default=""> <cfset var getQuery = ""> <cfset var objectArray = arrayNew(1)> <cfset var packet = ""> <cfset var filter = "*"> <cfset var dirToCheck = getBaseStorageDirectory()> <cfscript> //if a type is passed, set the filter if(len(trim(arguments.type))){ filter = "*." & arguments.type; } </cfscript> <!--- get the appropriate files into a query ---> <cfdirectory action="list" directory="#dirToCheck#" name="getQuery" filter="#filter#"> <!--- loop through files, parsing them and putting the contents into the objectArray ---> <cfloop query="getQuery"> <cfset objectArray[currentRow] = getObjectFromFile(name)> </cfloop> <!--- return the array of objects ---> <cfreturn objectArray> </cffunction> <!--- a method to get an object from the name of its storage file ---> <cffunction name="getObjectFromFile" access="private" returnType="org.bacfug.modus.baseContentObject" output="no" hint="returns an object instance based on the name of the file"> <cfargument name="fileName" type="string" required="yes"> <cfset var packet = ""> <cftry> <cffile action="read" file="#getBaseStorageDirectory()##arguments.fileName#" variable="packet"> <cfcatch> <cfthrow type="modus.badFile" message="File does not exist" detail="The requested file ""#arguments.fileName#"" does not exist."> </cfcatch> </cftry> <!--- if there is a second argument, it means we're loading an existing object ---> <cfif structCount(arguments) GT 1> <cfreturn wddxToContentObject(packet,arguments[2])> <!--- otherwise, just return the object ---> <cfelse> <cfreturn wddxToContentObject(packet)> </cfif> </cffunction> <!--- a method to get the directory path based on an object ---> <cffunction name="getPathFromObject" access="private" returnType="string" output="no" hint="returns a full path based on an object"> <cfargument name="contentObject" type="org.bacfug.modus.baseContentObject" required="yes"> <cfset var directoryPath = getBaseStorageDirectory()> <cfset var fullPath = ""> <cfscript> //make sure the directory exists directoryInit(directoryPath); //set the full path fullPath = directoryPath & arguments.contentObject.getID() & "." & arguments.contentObject.getType(); </cfscript> <!--- now, be sure ---> <cfreturn fullPath> </cffunction> <!--- a method to get the base directory for storage ---> <cffunction name="getBaseStorageDirectory" access="private" returnType="string" output="no" hint="returns the path to the base storage directory"> <cfreturn instance.objectStoreDir> </cffunction> <!--- a method for make sure directories exist in the storage directory ---> <cffunction name="directoryInit" access="private" returnType="void" output="no" hint="a method for make sure directories exist in the storage directory"> <cfargument name="directory" required="yes" type="string"> <!--- if the directory does not exist, create it ---> <cfif NOT directoryExists(arguments.directory)> <cfdirectory action="create" directory="#arguments.directory#"> </cfif> </cffunction> <!--- a method to throw an error (DAMN YOU CFSCRIPT FOR NOT HAVING THIS FUNCTION ALREADY! ---> <cffunction name="throwError" access="private" returnType="void" output="no"> <cfargument name="type" required="yes"> <cfargument name="message" requied="yes"> <cfargument name="detail" required="no" default=""> <cfthrow type="#arguments.type#" message="#arguments.message#" detail="#arguments.detail#"> </cffunction> </cfcomponent> |