Symphony Framework
Symphony RESTController v3.2
Symphony Templates > Symphony RESTController v3.2
Template file name Symphony_RESTController_v3_2.tpl

The Symphony_RESTController (version 3.2) provides the ability to create a powerful repository based Restful controller that can be made available through the Symphony Bridge REST API.

The generated class creates the required {HttpGet}, {HttpPost}, {HttpPut} and {HttpDelete} methods to provide complete access to the Synergy Repository based "resource".

For more information relating to the created Controller code please refer to the Symphony Bridge pages.

 

The generated classes are designed to be used in Synergy .Net developments.  They are designed to support exposing data and logic through Symphony Bridge.
It is recommended that your generated code from the Symphony_DataREST template is created into a project sub-folder called Controller.
 CodeGen Template Tokens
Token Comments
NAMESPACE Define the namespace within which the generated class will be placed (-n option).
MODELNAMESPACE Define the namespace when the associated data object is define.
PROCEDURENAMESPACE The class specification where store procedure methods are stored.
ASSEMBLY Specify the name of the assembly that contains the associated data object classes.
TABLEMAPPER The fully qualified method name of the table mapping routine used to convert the table name to the physical file name.
 Symphony Tokens
             

There are no Symphony tokens applicable to this template.

           
 CodeGen Command Examples
Data object
codegen e -r -s PART -t Symphony_RESTController_v3_2 -o "%ROOT%ControlLibrary\Controller" -n ControlLibrary.Controller -ut MODELNAMESPACE=ControlLibrary.Data -ut ASASSEMBLYNAMESEMBLY=ControlLibrary -ut TABLEMAPPER=ControlLibrary.Data.TableMapper.MapTableToFile -ut PROCEDURENAMESPACE=ControlLibrary.Procedures.StoredProcedures

 

 Example
;;****************************************************************************
;; WARNING: This code was code generated. Any changes that you
;;          make to this code will be overwritten if the code is regenerated!
;;
;; Template author: Richard C. Morris : RCP Consultants.
;;
;; Template Name:   Symphony Framework : SYMPHONY_RESTCONTROLLER_V3_2.tpl
;;
;;***************************************************************************
import System
import System.Collections.Generic
import System.Text
import System.Web.Http
import System.Net.Http
import System.Net
import System.Net.Http.Formatting
import Newtonsoft.Json
import SymphonyBridgeServerCore
import SymphonyBridgeServerCore.Controller

import ControlLibrary.Data

namespace ControlLibrary.Controller

    public partial class PartsController extends ApiController
    
        ;;; <summary>
        ;;; Enables the OPTIONS verb to return OK for CORS and pre-flight operations.
        ;;; </summary>
        {HttpOptions}
        public method Options        ,@HttpResponseMessage
            endparams
        proc
            data response = new HttpResponseMessage()
            response.StatusCode = HttpStatusCode.OK
            mreturn response
        endmethod

        ;;; <summary>
        ;;; Returns the requested resource
        ;;; </summary>
        ;;; <param name="id"></param>
        ;;; <param name="fields"></param>
        ;;; <param name="filter"></param>
        ;;; <param name="casing"></param>
        ;;; <param name="sort"></param>
        ;;; <param name="limit"></param>
        ;;; <param name="procedure"></param>
        ;;; <param name="param"></param>
        ;;; <returns></returns>
        {HttpGet}
        public method Get        ,@HttpResponseMessage
            in req id            ,string
            in req fields        ,string
            in req filter        ,string
            in req casing        ,boolean
            in req sort            ,string
            in req limit        ,int
            in req procedure    ,string
            in req param        ,string
            endparams

            record
                methodResponse            ,@HttpResponseMessage
            endrecord
        proc

            if (!string.IsNullOrEmpty(procedure)) then
            begin
                data controller = new ExecuteController()

                data executeCommand = "exec @ControlLibrary.Procedures.StoredProcedures." + procedure

                methodResponse  = controller.CustomGet(Request, executeCommand,
                &    '{"DataObjectItem":{"DataObjectClass":"ControlLibrary.Data.Part_DataREST, ControlLibrary","DataObjectData":{}}}',
                &    param)
            end
            else
            begin

                data localOrderByValue        ,string    ,""
                data localOrderDirection    ,string    ,""

                data localFilter            ,string    ,""

                if (!string.IsNullOrEmpty(filter))
                begin
                    if (string.IsNullOrEmpty(id)) then
                        localFilter = "Where " + filter
                    else
                        localFilter = "and " + filter
                end

                data caseSetting = string.Format('{{"SetCaseSensitivity":"{0}"}}', casing.ToString())

                if (!string.IsNullOrEmpty(sort))
                begin
                    if (sort(1:1) == "-") then
                        localOrderDirection = " DESC"
                    else
                        localOrderDirection = " ASC"

                    localOrderByValue = " Order By " +     sort.Replace("-", "").Replace("+", "")
                end


                data controller = new SelectController()

                data selectCommand    ,string

                selectCommand = "select "
                
                if (limit > 0 )
                begin
                    selectCommand = selectCommand + "TOP " + %string(limit)
                end
                
                selectCommand = selectCommand + fields + " from part " 

                data paramString    ,string    ,""
                if (!string.IsNullOrEmpty(id))
                begin
                    data params = new Dictionary<string, Object>()
                    
                    ;;split the key incase we have segments
                    data idSplit = id.Split(",")
                    try 
                    begin
                        selectCommand = selectCommand + " Where ID = :1"
                        params.Add('id', idSplit[1])
                    end
                    catch (e, @Exception)
                    begin
                        throw new Exception("All required primary segment data not provided")
                    end
                    endtry
                    data settings = new JsonSerializerSettings()
                    settings.ContractResolver = new Symphony.Harmony.Core.ContractResolver()
                    paramString = JsonConvert.SerializeObject(params, settings)
                end

                selectCommand = selectCommand + localFilter 

                if (!string.IsNullOrEmpty(localOrderByValue))
                begin
                    selectCommand = selectCommand + " Order By " + localOrderByValue + localOrderDirection
                end
                
                methodResponse  = controller.CustomGet(Request, selectCommand,
                &    '{"DataObjectItem":{"DataObjectClass":"ControlLibrary.Data.Part_DataREST, ControlLibrary","DataObjectData":{}}}',
                &    'ControlLibrary.Data.TableMapper.MapTableToFile',
                &    paramString,
                &    caseSetting)
            end

            mreturn methodResponse

        endmethod

        ;;; <summary>
        ;;; Insert passed resource.
        ;;; </summary>
        ;;; <param name="postItem"></param>
        ;;; <param name="procedure"></param>
        ;;; <param name="param"></param>
        ;;; <returns></returns>
        {HttpPost}
        public method Post        ,@HttpResponseMessage
            postItem            ,@Part_DataREST
            in req procedure    ,string
            in req param        ,string
            endparams

            record
                methodResponse            ,@HttpResponseMessage
            endrecord

        proc

            if (!string.IsNullOrEmpty(procedure)) then
            begin
                data controller = new ExecuteController()

                data executeCommand = "exec @ControlLibrary.Procedures.StoredProcedures." + procedure

                methodResponse  = controller.CustomGet(Request, executeCommand,
                &    '{"DataObjectItem":{"DataObjectClass":"ControlLibrary.Data.Part_DataREST, ControlLibrary","DataObjectData":{}}}',
                &    param)
            end
            else
            begin
                ;;we must get at least the primary key segments
                if (string.IsNullOrEmpty(postItem.id))
                    throw new Exception("ID is required.")

                ;;first construct the required "object"
                data restDataObject = new RESTObjectContainer()
                postItem.DefineSerialiazableDataElements(^null)
                restDataObject.DataObjectItem = postItem

                data settings = new JsonSerializerSettings()
                settings.ContractResolver = new Symphony.Harmony.Core.ContractResolver()

                data restDataObjectJSON    ,string ,JsonConvert.SerializeObject(restDataObject, settings)

                data command = "insert into part"

                data controllerInsert = new InsertController()

                methodResponse = controllerInsert.CustomPost(Request, command,
                &    restDataObjectJSON,
                &    'ControlLibrary.Data.TableMapper.MapTableToFile')

            end

            mreturn methodResponse

        endmethod

        ;;; <summary>
        ;;; Update (or insert) passed resource.
        ;;; </summary>
        ;;; <param name="putItem"></param>
        ;;; <param name="procedure"></param>
        ;;; <param name="param"></param>
        ;;; <returns></returns>
        {HttpPut}
        public method Put        ,@HttpResponseMessage
            in req putItem        ,@Part_DataREST
            in req procedure    ,string
            in req param        ,string
            endparams

            record
                methodResponse            ,@HttpResponseMessage
            endrecord

        proc

            if (!string.IsNullOrEmpty(procedure)) then
            begin
                data controller = new ExecuteController()

                data executeCommand = "exec @ControlLibrary.Procedures.StoredProcedures." + procedure

                methodResponse  = controller.CustomGet(Request, executeCommand,
                &    '{"DataObjectItem":{"DataObjectClass":"ControlLibrary.Data.Part_DataREST, ControlLibrary","DataObjectData":{}}}',
                &    param)
            end
            else
            begin
                ;;we must get at least the primary key segments
                if (string.IsNullOrEmpty(putItem.id))
                    throw new Exception("ID is required.")

                ;;first construct the required REST "object"
                data restDataObject = new RESTObjectContainer()
                putItem.DefineSerialiazableDataElements(^null)
                restDataObject.DataObjectItem = putItem
                data settings = new JsonSerializerSettings()
                settings.ContractResolver = new Symphony.Harmony.Core.ContractResolver()

                data restDataObjectJSON    ,string ,JsonConvert.SerializeObject(restDataObject, settings)

                ;;define parameters
                data params = new Dictionary<string, Object>()

                ;;define the update command
                data command = "update part set" 

                data fieldsToUpdate    ,[#]string    ,putItem.FieldList.Split(",")

                data fieldPos    ,int    ,0
                data fieldItem    ,string
                foreach fieldItem in fieldsToUpdate
                begin
                    if (fieldItem == "Groupid")
                    begin
                        incr fieldPos
                        command = command + " Groupid = :" + %string(fieldPos)
                        params.Add('Groupid', putItem.Groupid)
                    end
                    if (fieldItem == "Supplierid")
                    begin
                        incr fieldPos
                        command = command + " Supplierid = :" + %string(fieldPos)
                        params.Add('Supplierid', putItem.Supplierid)
                    end
                    if (fieldItem == "Description")
                    begin
                        incr fieldPos
                        command = command + " Description = :" + %string(fieldPos)
                        params.Add('Description', putItem.Description)
                    end
                    if (fieldItem == "Technical_info")
                    begin
                        incr fieldPos
                        command = command + " Technical_info = :" + %string(fieldPos)
                        params.Add('Technical_info', putItem.Technical_info)
                    end
                    if (fieldItem == "Quantity")
                    begin
                        incr fieldPos
                        command = command + " Quantity = :" + %string(fieldPos)
                        params.Add('Quantity', putItem.Quantity)
                    end
                    if (fieldItem == "Cost_price")
                    begin
                        incr fieldPos
                        command = command + " Cost_price = :" + %string(fieldPos)
                        params.Add('Cost_price', putItem.Cost_price)
                    end
                    if (fieldItem == "Part_status")
                    begin
                        incr fieldPos
                        command = command + " Part_status = :" + %string(fieldPos)
                        params.Add('Part_status', putItem.Part_status)
                    end
                end

                command  = command  + " where "
                incr fieldPos
                if (1 > 1)
                    command  = command  + " and "
                command  = command  + " id = :" + %string(fieldPos)
                params.Add('key_id', putItem.id)

                data controllerUpdate = new UpdateController()

                methodResponse = controllerUpdate.CustomPut(Request, command,
                &    restDataObjectJSON,
                &    'ControlLibrary.Data.TableMapper.MapTableToFile',
                &    JsonConvert.SerializeObject(params, settings),
                &    '{"SetCaseSensitivity":"true"}')

                ;;if we get back a "not found" response we need to try to "insert" the data.
                if (methodResponse.StatusCode == HttpStatusCode.NotFound)
                begin
                    methodResponse = this.Post(putItem, procedure, param)
                end
            end
            mreturn methodResponse

        endmethod

        ;;; <summary>
        ;;; Delete the identified resource.
        ;;; </summary>
        ;;; <param name="id"></param>
        ;;; <param name="procedure"></param>
        ;;; <param name="param"></param>
        ;;; <returns></returns>
        {HttpDelete}
        public method Delete    ,@HttpResponseMessage
            in req id            ,string
            in req procedure    ,string
            in req param        ,string
            endparams

            record
                methodResponse            ,@HttpResponseMessage
            endrecord

        proc

            if (!string.IsNullOrEmpty(procedure)) then
            begin
                data controller = new ExecuteController()

                data executeCommand = "exec @ControlLibrary.Procedures.StoredProcedures." + procedure

                methodResponse  = controller.CustomGet(Request, executeCommand,
                &    '{"DataObjectItem":{"DataObjectClass":"ControlLibrary.Data.Part_DataREST, ControlLibrary","DataObjectData":{}}}',
                &    param)
            end
            else
            begin
                ;;we must get at least the primary key segments
                if (string.IsNullOrEmpty(id))
                    throw new Exception("key value is required.")

                ;;define parameters
                data params = new Dictionary<string, Object>()

                ;;define the command
                data deleteCommand = "delete from part where " 
                
                ;;key segment connector
                data connector = ""
                
                ;;split the key incase we have segments
                data idSplit = id.Split(",")
                try 
                begin
                    deleteCommand = deleteCommand + connector + " ID = :1"
                    params.Add('id', idSplit[1])
                    connector = " AND "
                end
                catch (e, @Exception)
                begin
                    throw new Exception("All required primary segment data not provided")
                end
                endtry
                
                ;;delete controller
                data controller = new DeleteController()

                data settings = new JsonSerializerSettings()
                settings.ContractResolver = new Symphony.Harmony.Core.ContractResolver()

                methodResponse = controller.CustomDelete(Request, deleteCommand,
                &    '{"DataObjectItem":{"DataObjectClass":"ControlLibrary.Data.Part_DataREST, ControlLibrary","DataObjectData":{}}}',
                &    'ControlLibrary.Data.TableMapper.MapTableToFile',
                &    JsonConvert.SerializeObject(params, settings),
                &    '{"SetCaseSensitivity":"True"}')
            end
            
            mreturn methodResponse
            
        endmethod
    endclass

endnamespace
See Also