Class: ErpForms::ErpApp::Desktop::DynamicForms::DataController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/erp_forms/erp_app/desktop/dynamic_forms/data_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

create a dynamic data record


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
# File 'app/controllers/erp_forms/erp_app/desktop/dynamic_forms/data_controller.rb', line 118

def create
  begin
    check_file_upload_size

    form_data = JSON.parse(params[:form_data_json])
    form_data[:dynamic_form_id] = params[:dynamic_form_id]
    form_data[:model_name] = params[:model_name]
    form_data.symbolize_keys!

    @record = DynamicFormModel.get_instance(params[:model_name])

    form_data[:created_by] = current_user unless current_user.nil?
    form_data[:created_with_form_id] = params[:dynamic_form_id] if params[:dynamic_form_id]
    @record = @record.save_all_attributes(form_data, ErpForms::ErpApp::Desktop::DynamicForms::BaseController::IGNORED_PARAMS)
    is_secure = (@record.file_security_default == 'private')
    save_file_asset(form_data, is_secure) unless params[:file].nil?

    data = @record.data.sorted_dynamic_attributes
    result_hash = {
      :success => true, 
      :id => @record.id, 
      :model_name => params[:model_name], 
      :form_id => form_data[:created_with_form_id], 
      :data => data, 
      :metadata => , 
      :comments => get_comments, 
      :has_file_assets => has_file_assets?
    }
    render :inline => @record ? result_hash.to_json : {:success => false}.to_json
  rescue Exception => e
    Rails.logger.error e.message
    Rails.logger.error e.backtrace.join("\n")
    render :inline => {
      :success => false,
      :message => e.message
    }.to_json             
  end
end

#deleteObject

delete a dynamic data record


197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'app/controllers/erp_forms/erp_app/desktop/dynamic_forms/data_controller.rb', line 197

def delete
  begin
    @record = DynamicFormModel.get_constant(params[:model_name])
    @record.destroy(params[:id])
    render :json => {:success => true}
  rescue Exception => e
    Rails.logger.error e.message
    Rails.logger.error e.backtrace.join("\n")
    render :inline => {
      :success => false,
      :message => e.message
    }.to_json             
  end
end

#delete_fileObject

file tree


271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'app/controllers/erp_forms/erp_app/desktop/dynamic_forms/data_controller.rb', line 271

def delete_file
  messages = []

  nodes_to_delete = (params[:selected_nodes] ? JSON(params[:selected_nodes]) : [params[:node]])

  begin
    @record = DynamicFormModel.get_constant(params[:model_name]).find(params[:id])
    result = false
    nodes_to_delete.each do |path|
      path = "#{path}/" if params[:leaf] == 'false' and path.match(/\/$/).nil?                
      begin
        name = File.basename(path)
        result, message, is_folder = @file_support.delete_file(File.join(@file_support.root,path))
        if result and !is_folder
          file = @record.files.find(:first, :conditions => ['name = ? and directory = ?', ::File.basename(path), ::File.dirname(path)])
          file.destroy
        end
        messages << message
      rescue Exception => e
        Rails.logger.error e.message
        Rails.logger.error e.backtrace.join("\n")
        render :json => {:success => false, :error => "Error deleting #{name}"} and return
      end
    end # end nodes_to_delete.each
    if result
      render :json => {:success => true, :message => messages.join(',')}
    else
      render :json => {:success => false, :error => messages.join(',')}
    end
  rescue ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability => ex
    render :json => {:success => false, :message => ex.message}
  end
end

#getObject

get a single record with sorted_dynamic_attributes


97
98
99
100
101
102
103
104
# File 'app/controllers/erp_forms/erp_app/desktop/dynamic_forms/data_controller.rb', line 97

def get
  @record = DynamicFormModel.get_constant(params[:model_name]).find(params[:id])

  data = @record.data.sorted_dynamic_attributes
  result_hash = {:success => true, :data => data, :metadata => , :comments => get_comments, :has_file_assets => has_file_assets?}

  render :json => (@record ? result_hash : {:success => false})
end

#get_filesObject

file tree


213
214
215
216
217
218
219
220
221
# File 'app/controllers/erp_forms/erp_app/desktop/dynamic_forms/data_controller.rb', line 213

def get_files
  @record = DynamicFormModel.get_constant(params[:model_name]).find(params[:id])
  if @record.nil?
    render :json => []
  else
    set_root_node(params)
    render :json => @file_support.build_tree(base_path, :file_asset_holder => @record, :preload => true)
  end
end

get a single record with dynamic_attributes_with_related_data


107
108
109
110
111
112
113
114
115
# File 'app/controllers/erp_forms/erp_app/desktop/dynamic_forms/data_controller.rb', line 107

def get_with_related_data
  @record = DynamicFormModel.get_constant(params[:model_name]).find(params[:id])

  related_fields = @record.form.related_fields
  data = @record.data.dynamic_attributes_with_related_data(related_fields, true)
  result_hash = {:success => true, :data => data, :metadata => , :comments => get_comments, :has_file_assets => has_file_assets?}

  render :json => (@record ? result_hash : {:success => false})
end

#indexObject

get dynamic data records


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/controllers/erp_forms/erp_app/desktop/dynamic_forms/data_controller.rb', line 50

def index
  begin
    sort  = (params[:sort] || 'created_at').downcase
    dir   = (params[:dir] || 'desc').downcase
    query_filter = params[:query_filter].strip rescue nil

    myDynamicObject = DynamicFormModel.get_constant(params[:model_name])

    if ErpForms.use_solr? and myDynamicObject.is_searchable?
      solr_search_results = myDynamicObject.search do
        keywords query_filter unless params[:query_filter].blank?
        paginate(:page => page, :per_page => per_page)
        order_by(sort.to_sym, dir.to_sym)
      end
      dynamic_records = solr_search_results.results
    else     
      dynamic_records = myDynamicObject.paginate(:page => page, :per_page => per_page, :order => "#{sort} #{dir}")
      dynamic_records = dynamic_records.joins(:dynamic_data).where("UPPER(dynamic_data.dynamic_attributes) LIKE UPPER('%#{query_filter}%')") unless params[:query_filter].blank?
    end

    related_fields = dynamic_records.first.form.related_fields rescue []

    wi = []
    dynamic_records.each do |i|
      wihash = i.data.dynamic_attributes_with_related_data(related_fields, false)
      wihash[:id] = i.id
      wihash[:created_username] = i.data.created_by.nil? ? '' : i.data.created_by.username
      wihash[:updated_username] = i.data.updated_by.nil? ? '' : i.data.updated_by.username
      wihash[:created_at] = i.data.created_at.getlocal.strftime(@@datetime_format)
      wihash[:updated_at] = i.data.updated_at.getlocal.strftime(@@datetime_format)
      wihash[:form_id] = (i.data.updated_with_form_id ? i.data.updated_with_form_id : i.data.created_with_form_id)
      wihash[:model_name] = params[:model_name]
      wi << wihash
    end

    render :inline => "{ total:#{dynamic_records.total_entries}, data:#{wi.to_json} }"
  rescue Exception => e
    Rails.logger.error e.message
    Rails.logger.error e.backtrace.join("\n")
    render :inline => {
      :success => false,
      :message => e.message
    }.to_json             
  end
end

#setupObject

setup dynamic data grid


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/erp_forms/erp_app/desktop/dynamic_forms/data_controller.rb', line 6

def setup
  begin
    form = DynamicForm.get_form(params[:model_name]) 
    raise "No Default Form found for this model." if form.nil?   
    definition = form.definition_object

    columns = []
    definition.each do |field_hash|
      if field_hash[:display_in_grid]
        # for some reason grid column widths are greater than form field widths
        field_hash[:width] = (field_hash[:width].to_f * 0.56).round.to_i unless field_hash[:width].nil?
        columns << DynamicGridColumn.build_column(field_hash)
      end
    end

    columns << DynamicGridColumn.build_column({ :fieldLabel => "Created By", :name => 'created_username', :xtype => 'textfield', :width => 100 })
    columns << DynamicGridColumn.build_column({ :fieldLabel => "Created At", :name => 'created_at', :xtype => 'datefield', :width => 115 })
    columns << DynamicGridColumn.build_column({ :fieldLabel => "Updated By", :name => 'updated_username', :xtype => 'textfield', :width => 100 })
    columns << DynamicGridColumn.build_column({ :fieldLabel => "Updated At", :name => 'updated_at', :xtype => 'datefield', :width => 115 })

    definition << DynamicFormField.textfield({ :fieldLabel => "Created By", :name => 'created_username' })
    definition << DynamicFormField.datefield({ :fieldLabel => "Created At", :name => 'created_at' })
    definition << DynamicFormField.textfield({ :fieldLabel => "Updated By", :name => 'updated_username' })
    definition << DynamicFormField.datefield({ :fieldLabel => "Updated At", :name => 'updated_at' })
    definition << DynamicFormField.hiddenfield({ :fieldLabel => "ID", :name => 'id' })
    definition << DynamicFormField.hiddenfield({ :fieldLabel => "Form ID", :name => 'form_id' })
    definition << DynamicFormField.hiddenfield({ :fieldLabel => "Model Name", :name => 'model_name' })

    render :inline => "{
      \"success\": true,
      \"columns\": [#{columns.join(',')}],
      \"fields\": #{definition.to_json}
    }"
  rescue Exception => e
    Rails.logger.error e.message
    Rails.logger.error e.backtrace.join("\n")
    render :inline => {
      :success => false,
      :message => e.message
    }.to_json             
  end
end

#updateObject

update a dynamic data record


158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'app/controllers/erp_forms/erp_app/desktop/dynamic_forms/data_controller.rb', line 158

def update
  begin
    check_file_upload_size

    form_data = JSON.parse(params[:form_data_json])
    form_data[:dynamic_form_id] = params[:dynamic_form_id]
    form_data[:model_name] = params[:model_name]
    form_data.symbolize_keys!

    @record = DynamicFormModel.get_constant(params[:model_name]).find(params[:id])

    form_data[:updated_by] = current_user unless current_user.nil?
    form_data[:updated_with_form_id] = params[:dynamic_form_id] if params[:dynamic_form_id]      
    @record = @record.save_all_attributes(form_data, ErpForms::ErpApp::Desktop::DynamicForms::BaseController::IGNORED_PARAMS)
    save_file_asset(form_data, nil) unless params[:file].nil?

    data = @record.data.sorted_dynamic_attributes
    result_hash = {
      :success => true, 
      :id => params[:id], 
      :model_name => params[:model_name], 
      :form_id => form_data[:updated_with_form_id], 
      :data => data, 
      :metadata => , 
      :comments => get_comments, 
      :has_file_assets => has_file_assets?
    }
    render :inline => @record ? result_hash.to_json : {:success => false}.to_json
  rescue Exception => e
    Rails.logger.error e.message
    Rails.logger.error e.backtrace.join("\n")
    render :inline => {
      :success => false,
      :message => e.message
    }.to_json             
  end
end

#update_file_securityObject

toggle security on file


249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'app/controllers/erp_forms/erp_app/desktop/dynamic_forms/data_controller.rb', line 249

def update_file_security
  begin
    path   = params[:path]
    secure = params[:secure]
    
    @record = DynamicFormModel.get_constant(params[:model_name]).find(params[:id])
    file = @record.files.find(:first, :conditions => ['name = ? and directory = ?', ::File.basename(path), ::File.dirname(path)])

    set_file_security(file, (secure == 'true'))

    render :json =>  {:success => true}
  rescue Exception => e
    Rails.logger.error e.message
    Rails.logger.error e.backtrace.join("\n")
    render :inline => {
      :success => false,
      :message => e.message
    }.to_json             
  end
end

#upload_fileObject

for plupload via filetree


224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'app/controllers/erp_forms/erp_app/desktop/dynamic_forms/data_controller.rb', line 224

def upload_file
  result = {}
  upload_path = params[:directory]
  name = params[:name]
  data = request.raw_post

  begin
    @record = DynamicFormModel.get_constant(params[:model_name]).find(params[:id])
    set_root_node(params)
    file = @record.add_file(data, File.join(@file_support.root,base_path,name))

    is_secure = (@record.file_security_default == 'private')
    set_file_security(file, is_secure)

    result = {:success => true}
  rescue Exception => e
    Rails.logger.error e.message
    Rails.logger.error e.backtrace.join("\n")
    result = {:success => false, :error => "Error uploading file. #{e.message}"}
  end

  render :inline => result.to_json
end