# #-- # Copyright (c) 2005-2006, John Mettraux, OpenWFE.org # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # . Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # . Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # . Neither the name of the "OpenWFE" nor the names of its contributors may be # used to endorse or promote products derived from this software without # specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. #++ # # $Id: worklistclient.rb 3454 2006-10-08 16:51:00Z jmettraux $ # # # "hecho en Costa Rica" # require 'definitions' require 'restclient' require 'codec' module OpenWFE class WorklistClient < RestClient def initialize (server, port, username, password) super('http://'+server+':'+port.to_s(), username, password) end def initialize (url, username, password) super(url, username, password) end # # Returns the list of stores the worklist hosts # def listStores () r = self.get('listStores', nil, nil) return decode(r) end # # A synonym for listStores() # def getStoreNames () return listStores() end # # Returns the headers of a given store. # def getHeaders (storeName, limit=1000) params = {} params["limit"] = limit return decode(self.get('getHeaders', storeName, params)) end def findFlowInstance (storeName, workflowInstanceId) params = {} params["id"] = workflowInstanceId return decode(self.get('findFlowInstance', storeName, params)) end # # Launches a flow (on a given engine and with a given launchitem). # The 'engineId' corresponds to an engine's participant name # (see etc/engine/participant-map.xml) # def launchFlow (engineId, launchitem) eli = OpenWFE.encode(launchitem) params = {} params[ENGINEID] = engineId return decode(self.post('launchFlow', nil, params, eli)) end # # Returns a workitem (but doesn't put a lock on it, thus modifications # to it cannot be communicated with saveWorkitem() or forwardWorkitem() # to the worklist) # def getWorkitem (storeName, flowExpressionId) return getItem('getWorkitem', storeName, flowExpressionId) end # # Returns a workitem and makes sure it's locked in the worklist. Thus, # the usage of the methods saveWorkitem() and forwardWorkitem() is # possible. # def getAndLockWorkitem (storeName, flowExpressionId) #puts "...getAndLockWorkitem() for #{flowExpressionId}" return getItem('getAndLockWorkitem', storeName, flowExpressionId) end # # Given a queryMap (a dict of keys and values), locks and returns # the first workitem matching. # def queryAndLockWorkitem (storeName, queryMap) hs = getHeaders(storeName) hs.each do |h| #puts "...h.id #{h.flowExpressionId}" #h.attributes.each do |k, v| # puts "......h '#{k}' => '#{v}'" #end ok = true id = nil queryMap.each do |key, value| #puts "...'#{key}' => '#{h.attributes[key]}' ?= '#{value}'" ok = (ok and h.attributes[key] == value) # # the parenthesis are very important #puts " .ok is #{ok}" #puts " .id is #{h.flowExpressionId}" break if not ok end #puts " .id is #{h.flowExpressionId}" return getAndLockWorkitem(storeName, h.flowExpressionId) if ok end return nil end # # Notifies the worklist that the given workitem has to be unlocked # any local (client-side) modification to it are ignored. # def releaseWorkitem (workitem) return postItem('releaseWorkitem', workitem) end # # Saves back the workitem in the worklist (and releases it) # def saveWorkitem (workitem) return postItem('saveWorkitem', workitem) end # # Returns the workitem to the worklist so that it can resume # its flow (changes to the workitem are saved). # def proceedWorkitem (workitem) return postItem('forwardWorkitem', workitem) end # # Returns the list of flow URLs the user owning this session may # launch. # def listLaunchables () params = {} #return decode(post('listLaunchables', nil, params, nil)) return decode(get('listLaunchables', nil, params)) end # # Delegate the workitem (transfer it to another store). # def delegate (workitem, targetStoreName) ewi = OpenWFE.encode(workitem) params = {} params[TARGETSTORE] = targetStoreName return decode(post('delegate', workitem.store, params, ewi)) end # # Delegate the workitem (ask the worklist to deliver it to # another participant). # def delegateToParticipant (workitem, targetParticipantName) ewi = OpenWFE.encode(workitem) params = {} params[TARGETPARTICIPANT] = targetParticipantName return decode(post('delegate', workitem.store, params, ewi)) end #def queryStore (storeName, query) #end alias list_stores listStores alias get_store_names getStoreNames alias get_headers getHeaders alias find_flow_instance findFlowInstance alias launch_flow launchFlow alias get_workitem getWorkitem alias get_and_lock_workitem getAndLockWorkitem alias query_and_lock_workitem queryAndLockWorkitem alias release_workitem releaseWorkitem alias save_workitem saveWorkitem alias proceed_workitem proceedWorkitem alias forwardWorkitem proceedWorkitem alias forward_workitem proceedWorkitem alias list_launchables listLaunchables alias delegate_to_participant delegateToParticipant protected def decode (reply) xml = REXML::Document.new(reply.body) return OpenWFE.decode(xml.root) end def getItem (restMethodName, storeName, flowExpressionId) fei = OpenWFE.encode(flowExpressionId) fei = OpenWFE.docToString(fei, false) params = {} wi = decode(self.post(restMethodName, storeName, params, fei)) wi.store = storeName if wi return wi end def postItem (restMethodName, workitem) ewi = OpenWFE.encode(workitem) params = {} return decode(post(restMethodName, workitem.store, params, ewi)) end end end