Skip to content

Commit

Permalink
HBW-61 Add support for Camunda
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Vershinin committed Sep 4, 2018
1 parent 33e4868 commit 3c2f1d1
Show file tree
Hide file tree
Showing 11 changed files with 292 additions and 127 deletions.
2 changes: 2 additions & 0 deletions config/hbw.default.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
hbw:
adapter: camunda

order:
entity_code_key: homsOrderCode

Expand Down
114 changes: 14 additions & 100 deletions hbw/app/models/hbw/activiti/adapter.rb
Original file line number Diff line number Diff line change
@@ -1,41 +1,17 @@
module HBW
module Activiti
# rubocop:disable Metrics/ClassLength
class Adapter
class Adapter < ::HBW::Common::Adapter
include HBW::Inject[:api, :config]

def entity_code_key(entity_class)
config.fetch(entity_class)[:entity_code_key]
end

# TODO: cache it until new user is added
def users
HBW::BPMUser.fetch_all
end

def user_exist?(user_email)
user = HBW::BPMUser.with_connection(api) do
HBW::BPMUser.fetch(user_email)
end

!user.nil?
end

def users_lookup(pattern)
d_pattern = pattern.mb_chars.downcase.to_s
users.select do |user|
user.values.find { |v| v.mb_chars.downcase.include?(d_pattern) }
end
end

def process_instances(entity_code, entity_class)
response = api.post(
'query/process-instances', variables: [
name: entity_code_key(entity_class),
value: entity_code,
operation: :equals,
type: :string
])
'query/process-instances',
variables: [
name: entity_code_key(entity_class),
value: entity_code,
operation: :equals,
type: :string
])
response.status == 200 && response.body['data'] || []
end

Expand All @@ -49,58 +25,12 @@ def submit(user_email, entity_class, task_id, form_data)
response.status == 200
end

# TODO: How to distinguish between running process instance and done
# TODO: Think of suspended process instances
def bp_running?(entity_code, entity_class, current_user_identifier)
!process_instances(entity_code, entity_class).empty? ||
!task_list_response(current_user_identifier, entity_code, entity_class, 1000, true).empty?
end

def start_process(bp_code,
user_email,
entity_code,
entity_class)
user = HBW::BPMUser.with_connection(api) do
HBW::BPMUser.fetch(user_email)
end
return false unless user

p_def = process_definition_for_key_like(bp_code)
return false unless p_def

variables = [
{ name: :initiator, value: user.id, type: :string },
{ name: :initiatorEmail, value: user.email, type: :string },
{ name: entity_code_key(entity_class), value: entity_code, type: :string }
]

response = start_process_response(p_def['id'], variables)
response.status == 201
end

def drop_processes(entity_code, entity_class)
ids = process_instances(entity_code, entity_class).map { |i| i['id'] }
ids.map do |id|
response = api.delete("runtime/process-instances/#{id}")
response.status == 204
end
ids.reject { |e| e }.empty?
end

def entity_task_list(user_email, entity_code, entity_class, size = 1000)
response = task_list_response(user_email, entity_code, entity_class, size)
::HBW::Task.wrap(response.body['data']) if response.status == 200
end

def task_list(email, entity_class, size = 1000)
task_list_response(email, '%', entity_class, size)
end

def form(user_email, entity_class, task_id)
task = task_for_email_and_task_id(user_email, entity_class, task_id)
HBW::Form.with_connection(api) do
HBW::Form.fetch(task, entity_class)
end
def get_variables(user, entity_class, entity_code)
{
:initiator => {value: user.id, type: :string},
:initiatorEmail => {value: user.email, type: :string},
entity_code_key(entity_class) => {value: entity_code, type: :string}
}
end

def process_definition_for_key_like(key)
Expand All @@ -113,22 +43,6 @@ def start_process_response(id, variables)
processDefinitionId: id,
variables: variables)
end

def task_list_response(email, entity_code, entity_class, size, for_all_users = false)
HBW::Task.with_connection(api) do
HBW::Task.fetch(email, entity_code, entity_class, size, for_all_users)
end
end

def task_for_email_and_task_id(user_email, entity_class, task_id)
task_list(user_email, entity_class).find { |task| task.id == task_id }
end

def process_instance_from(proc_inst_id)
response = api.get("runtime/process-instances/#{proc_inst_id}")
response.body if response.status == 200
end
end
# rubocop:enable Metrics/ClassLength
end
end
16 changes: 14 additions & 2 deletions hbw/app/models/hbw/bpm_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,27 @@ class BPMUser

class << self
def fetch(email)
definition = do_request(:get, 'identity/users', email: email).first
definition = do_request(:get, users_url, email: email).first
new(definition) if definition
end

def fetch_all
do_request(:get, 'identity/users', firstNameLike: '%').map do |definition|
do_request(:get, users_url, firstNameLike: '%').map do |definition|
new(definition) if definition
end.compact
end

def activiti?
HBW::Widget.config.fetch(:adapter) == 'activiti'
end

def users_url
if activiti?
'identity/users'
else
'user'
end
end
end

definition_reader :id, :email, :first_name, :last_name
Expand Down
50 changes: 50 additions & 0 deletions hbw/app/models/hbw/camunda/adapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module HBW
module Camunda
class Adapter < ::HBW::Common::Adapter
include HBW::Inject[:api, :config]

def process_instances(entity_code, entity_class)
response = api.post(
'/rest/process-instance',
variables: [
name: entity_code_key(entity_class),
value: entity_code,
operator: :eq
])
response.status == 200 && response.body || []
end


def submit(user_email, entity_class, task_id, form_data)
form_definition = form(user_email, entity_class, task_id)

variables = form_definition.extract_and_coerce_values(form_data).map do |key, value|
{ name: key, value: value }
end

variables = variables.map { |item| [item.delete(:name), item]}.to_h

response = api.post("/rest/task/#{task_id}/submit-form", variables: variables)
response.status == 204
end

def get_variables(user, entity_class, entity_code)
[
{ name: :initiator, value: user.id, type: :string },
{ name: :initiatorEmail, value: user.email, type: :string },
{ name: entity_code_key(entity_class), value: entity_code, type: :string }
]
end

def process_definition_for_key_like(key)
response = api.get('/rest/process-definition', keyLike: key, latestVersion: true)
response.body.first if response.status == 200
end

def start_process_response(id, variables)
api.post("process-definition/#{id}/start",
variables: variables)
end
end
end
end
109 changes: 109 additions & 0 deletions hbw/app/models/hbw/common/adapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
module HBW
module Common
class Adapter
include HBW::Inject[:api, :config]

def entity_code_key(entity_class)
config.fetch(entity_class)[:entity_code_key]
end

# TODO: cache it until new user is added
def users
HBW::BPMUser.fetch_all
end

def user_exist?(user_email)
user = HBW::BPMUser.with_connection(api) do
HBW::BPMUser.fetch(user_email)
end

!user.nil?
end

def users_lookup(pattern)
d_pattern = pattern.mb_chars.downcase.to_s

users.select do |user|
user.values.find { |v| v.mb_chars.downcase.include?(d_pattern) }
end
end

def process_instances(_, _)
raise NotImplementedError
end

def submit(_, _, _, _)
raise NotImplementedError
end

# TODO: How to distinguish between running process instance and done
# TODO: Think of suspended process instances
def bp_running?(entity_code, entity_class, current_user_identifier)
!process_instances(entity_code, entity_class).empty? ||
!task_list_response(current_user_identifier, entity_code, entity_class, 1000, true).empty?
end

def get_variables(_, _, _)
raise NotImplementedError
end

def start_process(bp_code,
user_email,
entity_code,
entity_class)
user = HBW::BPMUser.with_connection(api) do
HBW::BPMUser.fetch(user_email)
end
return false unless user

p_def = process_definition_for_key_like(bp_code)
return false unless p_def

variables = get_variables(user, entity_class, entity_code)

response = start_process_response(p_def['id'], variables)
response.status == 201
end

def drop_processes(entity_code, entity_class)
ids = process_instances(entity_code, entity_class).map { |i| i['id'] }
ids.map do |id|
response = api.delete("runtime/process-instances/#{id}")
response.status == 204
end
ids.reject { |e| e }.empty?
end

def entity_task_list(user_email, entity_code, entity_class, size = 1000)
response = task_list_response(user_email, entity_code, entity_class, size)
::HBW::Task.wrap(response.body['data']) if response.status == 200
end

def task_list(email, entity_class, size = 1000)
task_list_response(email, '%', entity_class, size)
end

def form(user_email, entity_class, task_id)
task = task_for_email_and_task_id(user_email, entity_class, task_id)
HBW::Form.with_connection(api) do
HBW::Form.fetch(task, entity_class)
end
end

def task_list_response(email, entity_code, entity_class, size, for_all_users = false)
HBW::Task.with_connection(api) do
HBW::Task.fetch(email, entity_code, entity_class, size, for_all_users)
end
end

def task_for_email_and_task_id(user_email, entity_class, task_id)
task_list(user_email, entity_class).find { |task| task.id == task_id }
end

def process_instance_from(proc_inst_id)
response = api.get("runtime/process-instances/#{proc_inst_id}")
response.body if response.status == 200
end
end
end
end
2 changes: 1 addition & 1 deletion hbw/app/models/hbw/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Definition
module ClassMethods
def definition_reader(*names)
names.each do |name|
key = name.to_s.first + name.to_s.camelize[1..-1]
key = name.to_s.camelize(:lower)
class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
def #{name}
definition.fetch('#{key}')
Expand Down
27 changes: 24 additions & 3 deletions hbw/app/models/hbw/deployment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,43 @@ module HBW
class Deployment
extend HBW::Remote
include HBW::Definition
include HBW::Inject[:config]

class << self
def fetch(process_definition)
deployment_id = process_definition.deployment_id
resources = do_request(:get, 'repository/deployments/%s/resources' % deployment_id)
new(do_request(:get, 'repository/deployments/%s' % deployment_id).merge(
resources = do_request(:get, "#{deployments_url_prefix}/%s/resources" % deployment_id)
new(do_request(:get, "#{deployments_url_prefix}/%s" % deployment_id).merge(
'resources' => resources
))
end

def activiti?
HBW::Widget.config.fetch(:adapter) == 'activiti'
end

def deployments_url_prefix
if activiti?
'repository/deployments'
else
'/rest/deployment'
end
end
end

definition_reader :resources

def resource(resource_id)
resources.find do |resource|
resource['id'] == resource_id
resource[resource_key] == resource_id
end
end

def resource_key
if config.fetch(:adapter) == 'activiti'
'id'
else
'name'
end
end
end
Expand Down
Loading

0 comments on commit 3c2f1d1

Please sign in to comment.