lib/ruote/dm/storage.rb in ruote-dm-2.1.10 vs lib/ruote/dm/storage.rb in ruote-dm-2.1.11

- old
+ new

@@ -43,10 +43,11 @@ property :ide, String, :key => true, :length => 255, :required => true property :rev, Integer, :key => true, :required => true property :typ, String, :key => true, :required => true property :doc, Text, :length => 2**32 - 1, :required => true, :lazy => false + property :wfid, String, :index => true property :participant_name, String, :length => 512 def to_h Rufus::Json.decode(doc) end @@ -173,26 +174,27 @@ def get_many (type, key=nil, opts={}) q = { :typ => type } - if l = opts[:limit] - q[:limit] = l - end + if l = opts[:limit]; q[:limit] = l; end + if s = opts[:skip]; q[:offset] = s; end - if key - q[:ide.like] = if m = key.source.match(/(.+)\$$/) - "%#{m[1]}" - elsif m = key.source.match(/^\^(.+)/) - "#{m[1]}%" - else - "%#{key.source}%" - end - end + keys = key ? Array(key) : nil + q[:wfid] = keys if keys && keys.first.is_a?(String) DataMapper.repository(@repository) do - Document.all(q).collect { |d| d.to_h } + + return Document.all(q).count if opts[:count] + + docs = Document.all(q) + docs = docs.reverse if opts[:descending] + docs = docs.collect { |d| d.to_h } + + keys && keys.first.is_a?(Regexp) ? + docs.select { |doc| keys.find { |key| key.match(doc['_id']) } } : + docs end end def ids (type) @@ -240,19 +242,19 @@ end # A provision made for workitems, allow to query them directly by # participant name. # - def by_participant (type, participant_name) + def by_participant (type, participant_name, opts) raise NotImplementedError if type != 'workitems' - Document.all( + query = { :typ => type, :participant_name => participant_name - ).collect { |d| - d.to_h - } + }.merge(opts) + + Document.all(query).collect { |d| d.to_h } end # Querying workitems by field (warning, goes deep into the JSON structure) # def by_field (type, field, value=nil) @@ -266,20 +268,22 @@ Document.all(:typ => type, :doc.like => like.join).collect { |d| d.to_h } end def query_workitems (criteria) + cr = { :typ => 'workitems' } + + return Document.all(cr).count if criteria['count'] + offset = criteria.delete('offset') limit = criteria.delete('limit') wfid = criteria.delete('wfid') pname = criteria.delete('participant_name') || criteria.delete('participant') - cr = { :typ => 'workitems' } - cr[:ide.like] = "%!#{wfid}" if wfid cr[:offset] = offset if offset cr[:limit] = limit if limit cr[:participant_name] = pname if pname @@ -288,11 +292,11 @@ end cr[:conditions] = [ ([ 'doc LIKE ?' ] * likes.size).join(' AND '), *likes ] unless likes.empty? - Document.all(cr).collect { |d| d.to_h } + Document.all(cr).collect { |d| Ruote::Workitem.new(d.to_h) } end protected def insert (doc, rev) @@ -302,11 +306,17 @@ :rev => rev, :typ => doc['type'], :doc => Rufus::Json.encode(doc.merge( '_rev' => rev, 'put_at' => Ruote.now_to_utc_s)), + :wfid => extract_wfid(doc), :participant_name => doc['participant_name'] ).save! + end + + def extract_wfid (doc) + + doc['wfid'] || (doc['fei'] ? doc['fei']['wfid'] : nil) end def do_get (type, key) Document.first(:typ => type, :ide => key, :order => :rev.desc)