Skip to content

Commit 91a20de

Browse files
committed
Implements geometry support, fixes #24
Signed-off-by: Daniel Kastl <[email protected]>
1 parent edb5f3f commit 91a20de

File tree

9 files changed

+84
-4
lines changed

9 files changed

+84
-4
lines changed

app/controllers/subscription_issues_controller.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,35 @@ def create
1818
@issue.priority = @subscription_template.issue_priority
1919
@issue.fixed_version = @subscription_template.version
2020

21+
logger.info "################################################################################"
22+
logger.info "################################################################################"
23+
logger.info "Request received to create a new issue based on a subscription template"
24+
logger.info "Request parameters: #{params}"
25+
logger.info "################################################################################"
26+
27+
# If the redmine_gtt plugin is installed and enabled, and geometry data is provided,
28+
# try to convert it to an RGeo geometry object
29+
if Redmine::Plugin.installed?(:redmine_gtt) && @subscription_template.project.module_enabled?('gtt') && params[:geometry]
30+
begin
31+
@issue.geom = RedmineGtt::Conversions.to_geom(params[:geometry].to_json)
32+
rescue => e
33+
logger.warn "Failed to convert geometry data: #{e.message}"
34+
end
35+
end
36+
37+
logger.info "################################################################################"
38+
logger.info "Create issue key/value:"
39+
@issue.attributes.each do |key, value|
40+
logger.info "#{key}: #{value}"
41+
end
42+
logger.info "################################################################################"
43+
logger.info "Subscription template key/value pairs:"
44+
@subscription_template.attributes.each do |key, value|
45+
logger.info "#{key}: #{value}"
46+
end
47+
logger.info "################################################################################"
48+
logger.info "################################################################################"
49+
2150
if @issue.save
2251
# Respond with the newly created issue and a 201 status code
2352
render json: @issue.as_json(include: [:status, :tracker, :author, :assigned_to]), status: :created

app/controllers/subscription_templates_controller.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ def unpublish
110110
def prepare_payload
111111
@subscription_template = SubscriptionTemplate.find(params[:id])
112112
@broker_url = URI.join(@subscription_template.broker_url, "/v2/subscriptions").to_s
113+
@entity_url = URI.join(@subscription_template.broker_url, "/v2/entities").to_s
113114
@member = Member.find(@subscription_template.member_id)
114115

115116
httpCustom = {
@@ -121,8 +122,12 @@ def prepare_payload
121122
},
122123
method: "POST",
123124
json: {
125+
entity: "#{@entity_url}/${id}?type=${type}",
124126
subject: @subscription_template.subject,
125-
description: @subscription_template.description
127+
description: @subscription_template.description,
128+
attachments: @subscription_template.attachments,
129+
notes: @subscription_template.notes,
130+
geometry: @subscription_template.geometry
126131
}
127132
}
128133

@@ -208,7 +213,7 @@ def get_issue_priorities
208213

209214
def subscription_template_params
210215
params[:subscription_template][:alteration_types] ||= []
211-
params.require(:subscription_template).permit(:standard, :broker_url, :fiware_service, :fiware_servicepath, :subscription_id, :name, :expires, :status, :context, :entities_string, :attrs, :expression_query, :expression_georel, :expression_geometry, :expression_coords, :notify_on_metadata_change, :subject, :description, :attachments_string, :is_private, :project_id, :tracker_id, :version_id, :issue_status_id, :issue_category_id, :issue_priority_id, :member_id, :comment, alteration_types: [])
216+
params.require(:subscription_template).permit(:standard, :broker_url, :fiware_service, :fiware_servicepath, :subscription_id, :name, :expires, :status, :context, :entities_string, :attrs, :expression_query, :expression_georel, :expression_geometry, :expression_coords, :notify_on_metadata_change, :subject, :description, :attachments_string, :is_private, :project_id, :tracker_id, :version_id, :issue_status_id, :issue_category_id, :issue_priority_id, :member_id, :comment, :threshold_create, :threshold_create_hours, :notes, :geometry, :geometry_string, alteration_types: [])
212217
end
213218

214219
end

app/models/subscription_template.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,35 @@ class SubscriptionTemplate < ActiveRecord::Base
3030

3131
validate :name_uniqueness
3232
validate :take_json_entities
33+
validate :take_json_geometry
3334
validate :take_json_attachments
3435
validate :attrs_must_be_array_of_strings
3536
validate :geo_query_fields_must_be_all_or_none
3637

3738
before_save :serialize_alteration_types
3839
after_find :deserialize_alteration_types
3940

41+
attr_accessor :threshold_create_hours
42+
# Override the getter for threshold_create_hours
43+
def threshold_create_hours
44+
threshold_create / 3600 if threshold_create
45+
end
46+
47+
# Override the setter for threshold_create_hours
48+
def threshold_create_hours=(hours)
49+
self.threshold_create = hours.to_i * 3600
50+
end
51+
4052
attr_writer :entities_string
4153
def entities_string
4254
@entities_string ||= entities.present? ? JSON.pretty_generate(entities) : ''
4355
end
4456

57+
attr_writer :geometry_string
58+
def geometry_string
59+
@geometry_string ||= geometry.present? ? JSON.pretty_generate(geometry) : ''
60+
end
61+
4562
attr_writer :attachments_string
4663
def attachments_string
4764
@attachments_string ||= attachments.present? ? JSON.pretty_generate(attachments) : ''
@@ -63,6 +80,12 @@ def take_json_entities
6380
errors.add :entities_string, I18n.t(:error_invalid_json)
6481
end
6582

83+
def take_json_geometry
84+
self.geometry = JSON.parse(geometry_string)
85+
rescue JSON::ParserError
86+
errors.add :geometry_string, I18n.t(:error_invalid_json)
87+
end
88+
6689
def take_json_attachments
6790
return if attachments_string.blank?
6891

app/views/gtt_fiware/_settings.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
<p>
1717
<%= content_tag(:label, l(:field_fiware_broker_subscription_throttling)) %>
18-
<%= number_field_tag 'settings[request_timeout]',
18+
<%= number_field_tag 'settings[fiware_broker_subscription_throttling]',
1919
@settings['fiware_broker_subscription_throttling'], :min => 0, :step => 1 %>
2020
</p>
2121

app/views/subscription_templates/_form.html.erb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@
9494
<p><%= f.text_field :subject, required: true, size: 75, placeholder: l(:field_subscription_template_subject_placeholder) %></p>
9595
<p><%= f.text_area :description, required: true, rows: 6, class: 'wiki-edit', placeholder: l(:field_subscription_template_description_placeholder) %></p>
9696

97+
<p><%= f.text_area :notes, rows: 4, class: 'wiki-edit', placeholder: l(:field_subscription_template_notes_placeholder) %></p>
98+
<p><%= f.number_field :threshold_create_hours, min: 0, step: 1 %> <em><%= l(:field_subscription_template_threshold_create_hours_hint) %></em></p>
99+
100+
<p><%= f.text_area :geometry_string, rows: 4,
101+
placeholder: JSON.pretty_generate(JSON.parse(l(:field_subscription_template_geometry_placeholder))),
102+
value: (@subscription_template.geometry.presence ? JSON.pretty_generate(@subscription_template.geometry) : '') %></p>
103+
97104
<p><%= f.text_area :attachments_string, rows: 6,
98105
placeholder: JSON.pretty_generate(JSON.parse(l(:field_subscription_template_attachments_placeholder))),
99106
value: (@subscription_template.attachments.presence ? JSON.pretty_generate(@subscription_template.attachments) : '') %></p>
@@ -127,3 +134,4 @@
127134
</div>
128135

129136
<%= wikitoolbar_for 'subscription_template_description' %>
137+
<%= wikitoolbar_for 'subscription_template_notes' %>

config/locales/en.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ en:
6363
field_subscription_template_attachments_string: "Attachments (array of)"
6464
field_subscription_template_attachments_placeholder: "[{\"filename\": \"image.jpg\", \"url\": \"https://example.com/image.jpg\"}]"
6565
field_subscription_template_notification_user: "Sent from user"
66+
field_subscription_template_threshold_create_hours: "Threshold (h)"
67+
field_subscription_template_threshold_create_hours_hint: "Threshold to create new issue in hours."
68+
field_subscription_template_notes: "Issue notes"
69+
field_subscription_template_notes_placeholder: "Post an issue note instead of creating a new issue if an issue has been created within the threshold."
70+
field_subscription_template_geometry_string: "Issue geometry (GeoJSON)"
71+
field_subscription_template_geometry_placeholder: "{\"type\": \"Feature\", \"geometry\": \"${location}\"}"
6672

6773
link_to_insert_project_geometry: "Insert project boundary"
6874
link_to_insert_project_geometry_hint: "This will use the project boundary as the geospatial query for the subscription"

config/locales/ja.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ en:
6363
field_subscription_template_attachments_string: "Attachments (array of)"
6464
field_subscription_template_attachments_placeholder: "[{\"filename\": \"image.jpg\", \"url\": \"https://example.com/image.jpg\"}]"
6565
field_subscription_template_notification_user: "Sent from user"
66+
field_subscription_template_threshold_create_hours: "Threshold (h)"
67+
field_subscription_template_threshold_create_hours_hint: "Threshold to create new issue in hours."
68+
field_subscription_template_notes: "Issue notes"
69+
field_subscription_template_notes_placeholder: "Post an issue note instead of creating a new issue if an issue has been created within the threshold."
70+
field_subscription_template_geometry_string: "Issue geometry (GeoJSON)"
71+
field_subscription_template_geometry_placeholder: "{\"type\": \"Feature\", \"geometry\": \"${location}\"}"
6672

6773
link_to_insert_project_geometry: "Insert project boundary"
6874
link_to_insert_project_geometry_hint: "This will use the project boundary as the geospatial query for the subscription"

db/migrate/20240425141120_create_subscription_templates.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ def change
2222

2323
t.string :subject, null: false
2424
t.text :description
25+
t.text :notes
2526
t.jsonb :attachments
27+
t.jsonb :geometry
2628
t.boolean :is_private, default: false
2729

2830
t.references :project, index: true, foreign_key: true, null: false
@@ -34,6 +36,7 @@ def change
3436
t.references :member, index: true, foreign_key: true, null: false
3537

3638
t.text :comment
39+
t.integer :threshold_create, default: 0
3740
t.timestamps null: false
3841
end
3942
end

init.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
settings(
2222
default: {
2323
'ngsi_ld_format' => false,
24-
'fiware_broker_subscription_throttling' => '60',
24+
'fiware_broker_subscription_throttling' => '10',
2525
},
2626
partial: 'gtt_fiware/settings'
2727
)

0 commit comments

Comments
 (0)