openwferu-extras 0.9.12.863

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,198 @@
1
+ #
2
+ #--
3
+ # Copyright (c) 2007, John Mettraux, OpenWFE.org
4
+ # All rights reserved.
5
+ #
6
+ # Redistribution and use in source and binary forms, with or without
7
+ # modification, are permitted provided that the following conditions are met:
8
+ #
9
+ # . Redistributions of source code must retain the above copyright notice, this
10
+ # list of conditions and the following disclaimer.
11
+ #
12
+ # . Redistributions in binary form must reproduce the above copyright notice,
13
+ # this list of conditions and the following disclaimer in the documentation
14
+ # and/or other materials provided with the distribution.
15
+ #
16
+ # . Neither the name of the "OpenWFE" nor the names of its contributors may be
17
+ # used to endorse or promote products derived from this software without
18
+ # specific prior written permission.
19
+ #
20
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
+ # POSSIBILITY OF SUCH DAMAGE.
31
+ #++
32
+ #
33
+ # $Id$
34
+ #
35
+
36
+ #
37
+ # "made in Japan"
38
+ #
39
+ # John Mettraux at openwfe.org
40
+ #
41
+
42
+ #
43
+ # this participant requires atom-tools from
44
+ #
45
+ # http://code.necronomicorp.com/trac/atom-tools
46
+ #
47
+ # atom-tools' license is X11/MIT
48
+ #
49
+
50
+ require 'monitor'
51
+
52
+ require 'rubygems'
53
+
54
+ begin
55
+ require 'atom/collection'
56
+ rescue LoadError
57
+ #
58
+ # soft dependency on 'atom-tools'
59
+ #
60
+ puts
61
+ puts
62
+ puts "'atom/collection' is missing. You can install with :"
63
+ puts
64
+ puts " [sudo] gem install atom-tools"
65
+ puts
66
+ puts
67
+ exit 1
68
+ end
69
+
70
+ require 'openwfe/participants/participant'
71
+
72
+
73
+ module OpenWFE
74
+ module Extras
75
+
76
+ #
77
+ # Stores the incoming workitem into an 'atom feed'
78
+ #
79
+ # An example :
80
+ #
81
+ # feed0 = AtomParticipant.new(
82
+ # 20, # no more than 20 entries are kept
83
+ # """
84
+ # <p>
85
+ # <h1>${f:colour}</h1>
86
+ # </p>
87
+ # """) # the template for each entry
88
+ #
89
+ # The 'template' parameter may contain an instance of File instead of
90
+ # an instance of String.
91
+ #
92
+ # feed0 = AtomParticipant.new(
93
+ # 20, File.new("path/to/my/atom/template.txt")
94
+ #
95
+ # The template can be passed as the second parameter (after the max entry
96
+ # count) or as a block :
97
+ #
98
+ # feed1 = AtomParticipant.new(20) do |flow_expression, atom_participant, workitem|
99
+ # #
100
+ # # usually only the workitem parameter is used
101
+ # # but the other two allow for advanced tricks...
102
+ #
103
+ # atom_participant.content_type = "xml"
104
+ # # by default, it's "xhtml"
105
+ #
106
+ # s = "<task>"
107
+ # s << "<name>#{workitem.task_name}</name>"
108
+ # s << "<assignee>#{workitem.task_assignee}</assignee>"
109
+ # s << "<duedate>#{workitem.task_duedate}</duedate>"
110
+ # s << "</task>"
111
+ #
112
+ # # the block is supposed to 'return' a string which is the
113
+ # # effective template
114
+ # end
115
+ #
116
+ # This participant uses
117
+ # "atom-tools" from http://code.necronomicorp.com/trac/atom-tools
118
+ #
119
+ #
120
+ class AtomParticipant
121
+ include LocalParticipant, MonitorMixin
122
+
123
+ attr_accessor \
124
+ :content_type # blocks may manipulate them
125
+
126
+ def initialize (max_item_count, template=nil, &block)
127
+
128
+ super()
129
+
130
+ @template = template
131
+ @max_item_count = max_item_count
132
+ @block_template = block
133
+
134
+ @feed = Atom::Collection.new("http://localhost")
135
+ @content_type = "xhtml"
136
+ end
137
+
138
+ def consume (workitem)
139
+
140
+ e = Atom::Entry.new
141
+
142
+ e.id = \
143
+ "#{workitem.fei.workflow_instance_id}--" +
144
+ "#{workitem.fei.expression_id}"
145
+
146
+ e.title = workitem.atom_entry_title
147
+ e.content = render(workitem)
148
+
149
+ e.content["type"] = @content_type
150
+
151
+ @feed << e
152
+
153
+ @feed = @feed[0, @max_item_count] \
154
+ if @feed.length > @max_item_count
155
+
156
+ publish workitem
157
+
158
+ reply_to_engine workitem
159
+ end
160
+
161
+ protected
162
+
163
+ def render (workitem)
164
+
165
+ fe = get_flow_expression(workitem)
166
+
167
+ template = if @block_template
168
+ #@block_template.call(fe, self, workitem)
169
+ call_block @block_template, workitem
170
+ elsif @template
171
+ if @template.kind_of? File
172
+ @template.readlines
173
+ else
174
+ @template.to_s
175
+ end
176
+ else
177
+ "(no template given)"
178
+ end
179
+
180
+ OpenWFE::dosub(template, fe, workitem)
181
+ end
182
+
183
+ #
184
+ # For the moment, just dumps the feed into a file.
185
+ #
186
+ def publish (workitem)
187
+ synchronize do
188
+ filename = "work/atom_#{workitem.participant_name}.xml"
189
+ f = File.open(filename, "w")
190
+ f << @feed.to_s
191
+ f.close()
192
+ end
193
+ end
194
+ end
195
+
196
+ end
197
+ end
198
+
@@ -0,0 +1,129 @@
1
+ #
2
+ #--
3
+ # Copyright (c) 2007, John Mettraux, OpenWFE.org
4
+ # All rights reserved.
5
+ #
6
+ # Redistribution and use in source and binary forms, with or without
7
+ # modification, are permitted provided that the following conditions are met:
8
+ #
9
+ # . Redistributions of source code must retain the above copyright notice, this
10
+ # list of conditions and the following disclaimer.
11
+ #
12
+ # . Redistributions in binary form must reproduce the above copyright notice,
13
+ # this list of conditions and the following disclaimer in the documentation
14
+ # and/or other materials provided with the distribution.
15
+ #
16
+ # . Neither the name of the "OpenWFE" nor the names of its contributors may be
17
+ # used to endorse or promote products derived from this software without
18
+ # specific prior written permission.
19
+ #
20
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
+ # POSSIBILITY OF SUCH DAMAGE.
31
+ #++
32
+ #
33
+ # $Id$
34
+ #
35
+
36
+ #
37
+ # "made in Japan"
38
+ #
39
+ # John Mettraux at openwfe.org
40
+ #
41
+
42
+ require 'openwfe/utils'
43
+ require 'openwfe/util/dollar'
44
+ require 'openwfe/participants/participant'
45
+ require 'openwfe/extras/util/csvtable'
46
+
47
+ include OpenWFE
48
+
49
+
50
+ module OpenWFE
51
+ module Extras
52
+
53
+ #
54
+ # Using CSV files to transform workitems
55
+ # This concept is called "decision participant" in OpenWFEja, here
56
+ # it's simply called "csv participant".
57
+ #
58
+ # See CsvTable for an explanation of the core concept behind a
59
+ # CsvParticipant
60
+ #
61
+ # An example :
62
+ #
63
+ # class TestDefinition0 < ProcessDefinition
64
+ # def make
65
+ # process_definition :name => "test0", :revision => "0" do
66
+ # sequence do
67
+ # set :field => "weather", :value => "cloudy"
68
+ # set :field => "month", :value => "may"
69
+ # decision
70
+ # _print "${f:take_umbrella?}"
71
+ # end
72
+ # end
73
+ # end
74
+ # end
75
+ #
76
+ #
77
+ # csvParticipant = CsvParticipant.new(
78
+ # """
79
+ # in:weather, in:month, out:take_umbrella?
80
+ # ,,
81
+ # raining, , yes
82
+ # sunny, , no
83
+ # cloudy, june, yes
84
+ # cloudy, may, yes
85
+ # cloudy, , no
86
+ # """)
87
+ #
88
+ # engine.register_participant("decision", csvParticipant)
89
+ #
90
+ # # ...
91
+ #
92
+ # engine.launch(new OpenWFE::LaunchItem(TestDefinition0)
93
+ #
94
+ # Note that the CsvParticipant constructor also accepts a block.
95
+ #
96
+ class CsvParticipant
97
+ include LocalParticipant
98
+
99
+ attr_accessor \
100
+ :csv_table
101
+
102
+ #
103
+ # Builds a new CsvParticipant instance, csv_data or the block
104
+ # may contain a File instance, a String or an Array of Array of
105
+ # String instances.
106
+ #
107
+ def initialize (csv_data=nil, &block)
108
+
109
+ super()
110
+
111
+ csv_data = block.call if block
112
+
113
+ @csv_table = CsvTable.new(csv_data)
114
+ end
115
+
116
+ #
117
+ # This is the method called by the engine (actually the
118
+ # ParticipantExpression) when handling a workitem to this participant.
119
+ #
120
+ def consume (workitem)
121
+ fe = get_flow_expression(workitem)
122
+ workitem = @csv_table.transform_wi(fe, workitem)
123
+ reply_to_engine(workitem)
124
+ end
125
+ end
126
+
127
+ end
128
+ end
129
+
@@ -0,0 +1,124 @@
1
+ #
2
+ #--
3
+ # Copyright (c) 2007, John Mettraux, OpenWFE.org
4
+ # All rights reserved.
5
+ #
6
+ # Redistribution and use in source and binary forms, with or without
7
+ # modification, are permitted provided that the following conditions are met:
8
+ #
9
+ # . Redistributions of source code must retain the above copyright notice, this
10
+ # list of conditions and the following disclaimer.
11
+ #
12
+ # . Redistributions in binary form must reproduce the above copyright notice,
13
+ # this list of conditions and the following disclaimer in the documentation
14
+ # and/or other materials provided with the distribution.
15
+ #
16
+ # . Neither the name of the "OpenWFE" nor the names of its contributors may be
17
+ # used to endorse or promote products derived from this software without
18
+ # specific prior written permission.
19
+ #
20
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
+ # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24
+ # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25
+ # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
+ # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
+ # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
+ # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
+ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
+ # POSSIBILITY OF SUCH DAMAGE.
31
+ #++
32
+ #
33
+ #
34
+
35
+ #
36
+ # "made in Japan"
37
+ #
38
+ # John Mettraux at openwfe.org
39
+ #
40
+
41
+ require 'yaml'
42
+ require 'base64'
43
+
44
+ #require 'openwfe/utils'
45
+ #require 'openwfe/rudefinitions'
46
+ require 'openwfe/participants/participant'
47
+
48
+
49
+ module OpenWFE
50
+ module Extras
51
+
52
+ #
53
+ # This participant dispatches its workitem to an Amazon SQS queue.
54
+ #
55
+ # If the queue doesn't exist, the participant will create it.
56
+ #
57
+ # a small example :
58
+ #
59
+ # # ...
60
+ # engine.register_participant(:sqs0, SqsParticipant.new("workqueue0"))
61
+ # # ...
62
+ #
63
+ # For more details about SQS :
64
+ # http://aws.amazon.com
65
+ #
66
+ class SqsParticipant
67
+ include LocalParticipant
68
+
69
+ attr_reader :queue, :queue_service
70
+
71
+ #
72
+ # Builds an SqsParticipant instance pointing to a given queue.
73
+ # (Refer to the SQS service on how to set up AWS key ids).
74
+ #
75
+ # By default the host_name is 'queue.amazonaws.com'
76
+ #
77
+ def initialize (queue_name, host_name=nil)
78
+
79
+ @queue_name = queue_name
80
+
81
+ @queue_service = SQS::QueueService.new(host_name)
82
+
83
+ @queue_service.create_queue(@queue_name)
84
+ # make sure the queue exists
85
+
86
+ @queue = @queue_service.get_queue(@queue_name)
87
+ end
88
+
89
+ #
90
+ # The method called by the engine when it has a workitem for this
91
+ # participant.
92
+ #
93
+ def consume (workitem)
94
+
95
+ msg = encode_workitem(workitem)
96
+
97
+ msg_id = @queue_service.put_message(@queue, msg)
98
+
99
+ ldebug do
100
+ "consume() msg sent to queue #{@queue.path} id is #{msg_id}"
101
+ end
102
+ end
103
+
104
+ protected
105
+
106
+ #
107
+ # Turns the workitem into a Hash, pass it through YAML and
108
+ # encode64 the result.
109
+ #
110
+ # Override this method as needed.
111
+ #
112
+ # Something of 'text/plain' flavour should be returned.
113
+ #
114
+ def encode_workitem (wi)
115
+
116
+ msg = wi.to_h
117
+ msg = YAML.dump(msg)
118
+ msg = Base64.encode64(msg)
119
+ msg
120
+ end
121
+ end
122
+
123
+ end
124
+ end