Google Baseを使ったサンプル(1)

元記事はこちらです。

■Application.cfm

<cfprocessingdirective pageEncoding = "UTF-8">
<cfsetting enableCFoutputOnly="no">

<cfapplication name="#hash( getCurrentTemplatePath() )#"
    sessionmanagement="yes"
    sessiontimeout=#CreateTimeSpan(1, 0, 0, 0)# >

<cfset request.url = thisURL()>
<cfparam name="session.GToken" default="">

<cfset request.proxyServer  = "">
<cfset request.proxyPort    = "80">
<cfset request.g_key = "xxx">
<!---==========================================
    thisURL
        実行されたURL。ただしファイル名は削除。
        例)http://hoge.com/
===========================================--->
<cffunction name="thisURL" access="public" returnType="string" output="no">
    <cfset var local = structNew()>

    <cfset local.server_name = cgi.server_name>
    <cfif cgi.server_port neq 80>
        <cfset local.server_name = local.server_name & ":" & cgi.server_port>
    </cfif>

    <cfset local.url = "#lcase(ListFirst(cgi.server_protocol,"/"))#://#local.server_name##cgi.script_name#">
    <cfset local.url = ListDeleteAt(local.url, ListLen(local.url, "/"), "/") & "/">

    <cfreturn local.url>
</cffunction>

■index.cfm

<cfprocessingdirective pageEncoding = "UTF-8">
<cfparam name="url.p"       default="">
<cfparam name="url.token"   default="">
<cfparam name="url.page"    default="0">

<!--- session.GToken --->
<cfif (session.GToken eq "")>
    <cfif (url.token neq "")>
        <cfset session.GToken = cst_authSubSessionToken(url.token)>
    </cfif>
<cfelse>
    <cfif (url.p eq "init")>
        <cfset session.GToken = "">
    </cfif>
</cfif>

<!--- 処理 --->
<cfset content = "<a href='index.cfm?p=list'>一覧</a> <a href='index.cfm?p=add'>登録</a><br><br>">
<cfswitch expression = "#url.p#">
    <!--- 一覧 --->
    <cfcase value="list">
        <cfset data = cst_getData(url.page)>
        <cfset content = content & cst_dspList(data)>
    </cfcase>

    <!--- add --->
    <cfcase value="add">
        <cfset content = content & cst_dspAddFm()>
    </cfcase>

    <!--- addEnd --->
    <cfcase value="addEnd">
        <cfset form.id = "http://www.google.com/base/feeds/items/">
        <cfset data = cst_setXmlForm()>
        <cfset content = content & cst_actionData(form.id, data, "post")>
    </cfcase>

    <!--- upd --->
    <cfcase value="upd">
        <cfset data = cst_setXmlForm()>
        <cfset content = content & cst_actionData(form.id, data, "put")>
    </cfcase>

    <!--- del --->
    <cfcase value="del">
        <cfset content = content & cst_actionData(form.id, "", "delete")>
    </cfcase>

    <!--- 指定処理以外 --->
    <cfdefaultcase>
        <cfif (session.GToken eq "")>
            <!--- 初期画面表示 --->
            <cfset content = "Please <a href='https://www.google.com/accounts/AuthSubRequest?next=#request.url#index.cfm&scope=http%3A%2F%2Fwww.google.com%2Fbase%2Ffeeds%2F&session=1&secure=0'>click here</A> to authorize.">
        </cfif>
    </cfdefaultcase>
</cfswitch>
<cfoutput>#content#</cfoutput>
<!---===============================
cst_authSubSessionToken
================================--->
<cffunction name="cst_authSubSessionToken" access="public" returnType="string" output="no">
    <cfargument name="token" type="string" required="yes">

    <cfhttp method="get" url="http://www.google.com/accounts/AuthSubSessionToken"
            proxyServer ="#request.proxyServer#"
            proxyPort   ="#request.proxyPort#"
    >
        <cfhttpparam type="header" name="Content-Type" value="application/atom+xml"> 
        <cfhttpparam type="header" name="Authorization" value="AuthSub token=#arguments.token#">
        <cfhttpparam name="X-Google-Key" type="Header" value="key=#request.g_key#">
    </cfhttp>

    <cfreturn ListLast(cfhttp.filecontent, "=")>
</cffunction>
<!---===============================
cst_getData
================================--->
<cffunction name="cst_getData" access="public" returnType="struct" output="no">
    <cfargument name="page"     type="string" required="yes">
    <cfargument name="maxRow"   type="string" default="15">

    <cfset var local    = structNew()>
    <cfset var ret      = structNew()>

    <cfset local.start = 1 + arguments.page * arguments.maxRow>

    <!--- Initial query string setup --->
    <cfset local.queryString="http://www.google.com/base/feeds/items?start-index=#local.start#&max-results=#arguments.maxRow#">

    <cfhttp method="Get" url="#local.queryString#"
        proxyServer ="#request.proxyServer#"
        proxyPort   ="#request.proxyPort#"
    >
        <cfhttpparam type="header" name="Authorization" value="AuthSub token=#session.GToken#">
        <cfhttpparam name="X-Google-Key" type="Header" value="key=#request.g_key#">
    </cfhttp>

    <cfset local.data = xmlparse(cfhttp.filecontent)>

    <cfset ret.totalResults = local.data.feed["openSearch:totalResults"].XmlText>
    <cfif (ret.totalResults neq 0)>
        <cfset ret.qry = QueryNew("id,title,item_type","varchar,varchar,varchar")>

        <cfloop index="local.idx" from="1" to="#arrayLen(local.data.feed.entry)#">
            <cfset local.currentData = local.data.feed.entry[local.idx]>

            <cfset QueryAddRow(ret.qry)>
            <cfset QuerySetCell(ret.qry, "id",          local.currentData.id.XmlText)>
            <cfset QuerySetCell(ret.qry, "title",       local.currentData.title.XmlText)>
            <cfset QuerySetCell(ret.qry, "item_type",   local.currentData.item_type.XmlText)>
        </cfloop>
    </cfif>

    <cfreturn ret>
</cffunction>
<!---===============================
cst_dspList
================================--->
<cffunction name="cst_dspList" access="public" returnType="string" output="no">
    <cfargument name="data" type="struct" required="yes">

    <cfset var local    = structNew()>
    <cfset var qry      = "">
    <cfset var ret      = "">

    <cfsavecontent variable = "ret">
    <cfoutput>
    <cfif (arguments.data.totalResults eq 0)>
    no data
    <cfelse>
        <cfset qry = arguments.data.qry>

        <table>
            <tr>
                <td>title</td>
                <td>item_type</td>
            </tr>

            <cfloop query="qry">
                <form name="frm_#qry.currentRow#" method="post">
                <input type="hidden" name="id" value="#qry.id#">

                <tr>
                    <td><input type="text" name="title"     value="#qry.title#"></td>
                    <td>#qry.item_type#</td>
                    <input type="hidden" name="item_type"   value="#qry.item_type#">
                    <td><input type="button" value="upd" onClick="js_upd(#qry.currentRow#)"></td>
                    <td><input type="button" value="del" onClick="js_del(#qry.currentRow#)"></td>
                </tr>
                </form>
            </cfloop>
        </table>
    </cfif>

    <script language="JavaScript">
    <!--
    function js_upd(pID){
        with(eval("document.frm_"+pID)){
            action = "index.cfm?p=upd";
            submit();
        }
    }
    function js_del(pID){
        with(eval("document.frm_"+pID)){
            action = "index.cfm?p=del";
            submit();
        }
    }
    //-->
    </script>

    </cfoutput>
    </cfsavecontent>

    <cfreturn ret>
</cffunction>
<!---===============================
cst_dspAddFm
================================--->
<cffunction name="cst_dspAddFm" access="public" returnType="string" output="no">
    <cfset var ret = "">

    <cfsavecontent variable = "ret">
    <cfoutput>
    <form name="frm" action="index.cfm?p=addEnd" method="post">
    <table>
    <tr>
        <td>title</td>
        <td>item_type</td>
    </tr>
    <tr>
        <td><input type="text" name="title"></td>
        <td><input type="text" name="item_type"></td>
    </tr>
    </table>
    <input type="button" value="add" onClick="js_add()">
    </form>

    <script language="JavaScript">
    <!--
    function js_add(){
        with(document.frm){
            if((title.value=="")||(item_type=="")){
                alert("please input all data");
                return;
            }
            submit();
        }
    }
    //-->
    </script>

    </cfoutput>
    </cfsavecontent>

    <cfreturn ret>
</cffunction>
<!---===============================
cst_setXmlForm
================================--->
<cffunction name="cst_setXmlForm" access="public" returnType="string" output="no">
    <cfset var ret = "">

    <!---xml --->
    <cfxml variable="ret">
    <cfoutput>
    <?xml version='1.0'?>
    <entry  xmlns='http://www.w3.org/2005/Atom'
            xmlns:g='http://base.google.com/ns/1.0'>
    <title type="text">#form.title#</title>
    <g:item_type type="text">#form.item_type#</g:item_type>
    </entry>
    </cfoutput>
    </cfxml>

    <cfreturn toString(ret)>
</cffunction>
<!---===============================
cst_actionData
================================--->
<cffunction name="cst_actionData" access="public" returnType="string" output="no">
    <cfargument name="id"       type="string" required="yes">
    <cfargument name="data"     type="string" required="yes">
    <cfargument name="action"   type="string" required="yes">

    <cfhttp url="#arguments.id#" method="#arguments.action#" redirect="no"
        proxyServer ="#request.proxyServer#"
        proxyPort   ="#request.proxyPort#"
        >
        <cfhttpparam type="header" name="Content-Type" value="application/atom+xml"> 
        <cfhttpparam type="header" name="Authorization" value="AuthSub token=#session.GToken#">
        <cfhttpparam name="X-Google-Key" type="Header" value="key=#request.g_key#">
        <cfhttpparam type="Body" value="#arguments.data#">
    </cfhttp>

    <cfif (left(cfhttp.Responseheader.status_code,2) neq "20")>
        <cfreturn HTMLEditFormat(cfhttp.filecontent)>
    </cfif>

    <cfreturn "">
</cffunction>