Class: RPCManager

Inherits:
Object
  • Object
show all
Defined in:
lib/RPCManager.rb

Instance Method Summary collapse

Constructor Details

#initialize(request = nil) ⇒ RPCManager

<summary>

  Process the request with the model.
</summary>  

<params>
  request: posted request parameters
  model: the object that is mapped to the table
</params>


26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/RPCManager.rb', line 26

def initialize(request=nil)        
  # if is not wrapped in a transaction then we'll wrap it to make unified handling of the request  

  if !check_transaction(request)   
req_hash = HashWithIndifferentAccess.new
req_hash[:transaction] = HashWithIndifferentAccess.new
req_hash[:transaction][:transactionNum] = -1        
req_list = Array.new
req_list << request
req_hash[:transaction][:operations] = req_list
@request = req_hash
  else
@request = request
  end   
end

Instance Method Details

#check_advanced_criteria(data) ⇒ Object

<summary> Helper method to decide if request contains an advanced criteria or not

</summary>

<param name=“req”></param>

<returns></returns>


51
52
53
54
55
56
57
# File 'lib/RPCManager.rb', line 51

def check_advanced_criteria(data)  
  if data.include?(:_constructor)
    return true
  else
    return false
  end
end

#check_transaction(request) ⇒ Object

<summary> Returns true if the request has transaction support

</summary>
<returns></returns>


64
65
66
67
68
69
70
# File 'lib/RPCManager.rb', line 64

def check_transaction(request)
  if request.include?(:transaction)# and request.include?(:operations) and request.include?(:transactionNum) 

    return true
  else
    return false
  end
end

#get_datasourceObject

<summary> Select the model by the datasource

</summary>
<returns>Datasource name</returns>


90
91
92
93
94
95
96
# File 'lib/RPCManager.rb', line 90

def get_datasource
  if @request.include?(:transaction)
    return @request[:transaction][:operations][0][:dataSource]
  else
    return @request[:dataSource]
  end
end

#model=(model) ⇒ Object



41
42
43
# File 'lib/RPCManager.rb', line 41

def model=(model)
  @model = model
end

#parse_advanced_criterias(operations) ⇒ Object



169
170
171
172
173
174
175
# File 'lib/RPCManager.rb', line 169

def parse_advanced_criterias(operations)
  data = operations[:data]     
  if check_advanced_criteria(data)
    return data
  end
  return nil   
end

#processRequestObject

<summary>

    Transforms a object object into a Json. Will setup the serializer with the        
    appropriate converters, attributes,etc.
</summary>
    <param name="dsresponse">the object object to be transformed to json</param>
    <returns>the created json object</returns>


79
80
81
82
83
# File 'lib/RPCManager.rb', line 79

def processRequest
  response = processTransaction    
  @result = { :response => response } 
  return @result  
end

#processTransactionObject

<summary> Process the transaction request for which this RPCManager was created for

</summary>
<returns></returns>


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/RPCManager.rb', line 103

def processTransaction    
  # retrieve the requests with data in form

  transaction_request = @request[:transaction]     
  # store transaction num, we'll use it later to see if there was a transaction or not

  transaction_num = transaction_request[:transactionNum]
  # fetch the operations

  operations = transaction_request[:operations]
  
  # response list

  res_list = Array.new     
  # transaction progress

  @model.transaction do                  
    begin       
      operations.each do |op|   
        # parase advanced criterias, if any

        advanced_criteria = parse_advanced_criterias(op)
         
        req = DSRequest.new(op, @model)           
        unless advanced_criteria == nil
          req.advancedCriteria = advanced_criteria
        end 
        # execute the request and get the response

        res = req.execute              
        if res == nil
          res = DSResponse.new
          res.status = -1
        end
        
      
          
        # store the response for later

        res_list << res           
      end      
    rescue ActiveRecord::RecordInvalid
      # if it occurs exception

      raise ActiveRecord::Rollback
    end
  end
  
  # if we have only one object, send directly the DSResponse

  if transaction_num  == -1
    response = DSResponse.new
    response.data = res_list[0].data
    response.startRow = res_list[0].startRow
    response.endRow = res_list[0].endRow
    response.totalRow = res_list[0].totalRow
    response.status = res_list[0].status
    
    return response
  end
  
  # iterate over the responses and create a instance of an anonymous class which mimics the required json

  responses = Array.new
  
  res_list.each do | response |      
    res = DSResponse.new
    res.data = response.data      
    res.startRow = response.startRow
    res.endRow = response.endRow
    res.totalRow = response.totalRow
    res.status = response.status      
    responses << res
  end 
  return responses
end