PageRenderTime 19ms CodeModel.GetById 15ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 1ms

/core/resource.cfc

http://github.com/atuttle/Taffy
ColdFusion CFScript | 141 lines | 121 code | 20 blank | 0 comment | 14 complexity | efe5c710f556d51ea40cde04ab00e772 MD5 | raw file
  1<cfcomponent hint="base class for taffy REST components">
  2
  3	<cffunction name="forceString">
  4		<cfargument name="data" required="true" hint="the data that is being forced to serialize as a string" />
  5		<cfreturn chr(2) & arguments.data />
  6	</cffunction>
  7
  8	<cfset variables.encode = structNew() />
  9	<cfset variables.encode.string = forceString />
 10
 11	<!--- helper functions --->
 12	<cffunction name="representationOf" access="public" output="false" hint="returns an object capable of serializing the data in a variety of formats">
 13		<cfargument name="data" required="true" hint="any simple or complex data that should be returned for the request" />
 14		<cfreturn getRepInstance().setData(arguments.data) />
 15	</cffunction>
 16
 17	<cffunction name="rep" access="public" output="false" hint="alias for representationOf">
 18		<cfargument name="data" required="true" />
 19		<cfreturn representationOf(arguments.data) />
 20	</cffunction>
 21
 22	<cffunction name="noData" access="private" output="false" hint="use this function to return only headers to the consumer, no data">
 23		<cfreturn getRepInstance().noData() />
 24	</cffunction>
 25
 26	<cffunction name="noContent" access="private" output="false" hint="use this function to return only headers to the consumer, no data">
 27		<cfreturn getRepInstance().noContent() />
 28	</cffunction>
 29
 30	<cffunction name="streamFile" access="private" output="false" hint="Use this function to specify a file name (eg c:\tmp\kitten.jpg) to be streamed to the client. When you use this method it is *required* that you also use .withMime() to specify the mime type.">
 31		<cfargument name="fileName" required="true" hint="fully qualified file path (eg c:\tmp\kitten.jpg)" />
 32		<cfreturn getRepInstance().setFileName(arguments.fileName) />
 33	</cffunction>
 34
 35	<cffunction name="streamBinary" access="private" output="false" hint="Use this function to stream binary data, like a generated PDF object, to the client. When you use this method it is *required* that you also use .withMime() to specify the mime type.">
 36		<cfargument name="binaryData" required="true" hint="binary file data (eg a PDF object) that you want to return to the client" />
 37		<cfreturn getRepInstance().setFileData(arguments.binaryData) />
 38	</cffunction>
 39
 40	<cffunction name="streamImage" access="private" output="false" hint="Use this function to stream binary data, like a generated PDF object, to the client. When you use this method it is *required* that you also use .withMime() to specify the mime type.">
 41		<cfargument name="binaryData" required="true" hint="binary file data (eg a PDF object or image data) that you want to return to the client" />
 42		<cfreturn getRepInstance().setImageData(arguments.binaryData) />
 43	</cffunction>
 44
 45	<cffunction name="saveLog">
 46		<cfargument name="exception" />
 47		<cfset logger = createObject("component", application._taffy.settings.exceptionLogAdapter).init(
 48				application._taffy.settings.exceptionLogAdapterConfig
 49		) />
 50		<cfset logger.saveLog(exception) />
 51	</cffunction>
 52
 53	<cffunction name="queryToArray" access="private" returntype="array" output="false">
 54		<cfargument name="q" type="query" required="yes" />
 55		<cfargument name="cb" type="any" required="no" />
 56		<cfscript>
 57			var local = {};
 58			if (structKeyExists(server, "railo") or structKeyExists(server, "lucee")) {
 59				local.Columns = listToArray(arguments.q.getColumnList(false));
 60			}
 61			else {
 62				local.Columns = arguments.q.getMetaData().getColumnLabels();
 63			}
 64			local.QueryArray = ArrayNew(1);
 65			for (local.RowIndex = 1; local.RowIndex <= arguments.q.RecordCount; local.RowIndex++){
 66				local.Row = {};
 67				local.numCols = ArrayLen( local.Columns );
 68				for (local.ColumnIndex = 1; local.ColumnIndex <= local.numCols; local.ColumnIndex++){
 69					local.ColumnName = local.Columns[ local.ColumnIndex ];
 70					if( local.ColumnName NEQ "" ) {
 71						local.Row[ local.ColumnName ] = arguments.q[ local.ColumnName ][ local.RowIndex ];
 72					}
 73				}
 74				if ( structKeyExists( arguments, "cb" ) ) {
 75					local.Row = cb( local.Row );
 76				}
 77				ArrayAppend( local.QueryArray, local.Row );
 78			}
 79			return( local.QueryArray );
 80		</cfscript>
 81	</cffunction>
 82
 83	<cffunction name="queryToStruct" access="private" returntype="struct" output="false">
 84		<cfargument name="q" type="query" required="yes" />
 85		<cfargument name="cb" type="any" required="no" />
 86		<cfset var local = {} />
 87
 88		<cfif q.recordcount gt 1>
 89			<cfthrow message="Unable to convert query resultset with more than one record to a simple struct, use queryToArray() instead" />
 90		</cfif>
 91
 92		<cfscript>
 93			if (structKeyExists(server, "railo") or structKeyExists(server, "lucee")) {
 94				local.Columns = listToArray(arguments.q.getColumnList(false));
 95			}
 96			else {
 97				local.Columns = arguments.q.getMetaData().getColumnLabels();
 98			}
 99
100			local.QueryStruct = {};
101			local.numCols = ArrayLen( local.Columns );
102
103			for (local.ColumnIndex = 1; local.ColumnIndex <= local.numCols; local.ColumnIndex++){
104				local.ColumnName = local.Columns[ local.ColumnIndex ];
105				if( local.ColumnName NEQ "" ) {
106					if ( structKeyExists( arguments, "cb" ) ) {
107						local.QueryStruct[ local.ColumnName ] = cb( local.ColumnName, arguments.q[ local.ColumnName ][1] );
108					} else {
109						local.QueryStruct[ local.ColumnName ] = arguments.q[ local.ColumnName ][1];
110					}
111				}
112			}
113
114			return( local.QueryStruct );
115		</cfscript>
116	</cffunction>
117
118	<!---
119		function that gets the representation class instance
120		-- if the argument is blank, we use the default from taffy settings
121		-- if the argument is a beanName, the bean is returned from the factory;
122		-- otherwise it is assumed to be a cfc path and that cfc instance is returned
123	--->
124	<cffunction name="getRepInstance" access="private" output="false">
125		<cfargument name="repClass" type="string" default="" />
126		<cfif repClass eq "">
127			<!--- recursion not the most efficient path here, but it's damn readable --->
128			<cfreturn getRepInstance(application._taffy.settings.serializer) />
129		<cfelseif application._taffy.factory.containsBean(arguments.repClass)>
130			<cfreturn application._taffy.factory.getBean(arguments.repClass) />
131		<cfelse>
132			<cfreturn createObject("component", arguments.repClass) />
133		</cfif>
134	</cffunction>
135	
136	<cffunction name="addDebugData" access="package" output="false">
137		<cfargument name="data" type="any" />
138		<cfset request.debugData = arguments.data />
139	</cffunction>
140
141</cfcomponent>