Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extended props #230

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
rename namespace => tagspace and make opts optional arguments
  • Loading branch information
zenchild committed Apr 7, 2011
commit 633a33fd875d19b0ff378f4ca78873a2996375f0
14 changes: 7 additions & 7 deletions lib/model/generic_folder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ def get_events
# Find Items
def find_items(opts = {})
opts = opts.clone # clone the passed in object so we don't modify it in case it's being used in a loop
namespace = opts.delete(:namespace) || 'viewpoint_tags'
item_shape = opts.has_key?(:item_shape) ? opts.delete(:item_shape) : {:base_shape => 'Default'}
tagspace = opts.delete(:tagspace) || 'viewpoint_tags'
if item_shape.has_key?(:additional_properties)
aprops = item_shape[:additional_properties]
if aprops.has_key?(:field_uRI)
Expand All @@ -228,14 +228,14 @@ def find_items(opts = {})
end
if aprops.has_key?(:extended_field_uRI)
raise EwsBadArgumentError, ":extended_field_uRI val should be an Array instead of #{aprops[:extended_field_uRI].class.name}" unless aprops[:extended_field_uRI].is_a?(Array)
aprops[:extended_field_uRI] << [{:distinguished_property_set_id=>"PublicStrings", :property_name=>namespace, :property_type=>"StringArray"}]
aprops[:extended_field_uRI] << [{:distinguished_property_set_id=>"PublicStrings", :property_name=>tagspace, :property_type=>"StringArray"}]
else
aprops[:extended_field_uRI] = [{:distinguished_property_set_id=>"PublicStrings", :property_name=>namespace, :property_type=>"StringArray"}]
aprops[:extended_field_uRI] = [{:distinguished_property_set_id=>"PublicStrings", :property_name=>tagspace, :property_type=>"StringArray"}]
end
else
item_shape[:additional_properties] = {}
item_shape[:additional_properties][:field_uRI] = ['item:ParentFolderId']
item_shape[:additional_properties][:extended_field_uRI] = [{:distinguished_property_set_id=>"PublicStrings", :property_name=>namespace, :property_type=>"StringArray"}]
item_shape[:additional_properties][:extended_field_uRI] = [{:distinguished_property_set_id=>"PublicStrings", :property_name=>tagspace, :property_type=>"StringArray"}]
end
resp = (Viewpoint::EWS::EWS.instance).ews.find_item([@folder_id], 'Shallow', item_shape, opts)
if(resp.status == 'Success')
Expand All @@ -252,11 +252,11 @@ def find_items(opts = {})
end

# Find Items with a specific tag
def find_items_with_tag(tag, opts)
namespace = opts.delete(:namespace) || 'viewpoint_tags'
def find_items_with_tag(tag, opts = {})
tagspace = opts.delete(:tagspace) || 'viewpoint_tags'

restrict = { :restriction => {
:is_equal_to => [ {:extended_field_uRI=>{:distinguished_property_set_id=>"PublicStrings", :property_name=>namespace, :property_type=>"StringArray"}},
:is_equal_to => [ {:extended_field_uRI=>{:distinguished_property_set_id=>"PublicStrings", :property_name=>tagspace, :property_type=>"StringArray"}},
:field_uRI_or_constant => {:constant => {:value=>tag}} ]
} }

Expand Down
44 changes: 26 additions & 18 deletions lib/model/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def self.add_attachments(parent_id, attachments)
attachments.each do |a|
b64attach << {:name => {:text =>(File.basename a.path)}, :content => {:text => Base64.encode64(a.read)}}
end
resp = conn.ews.create_attachment(parent_id, b64attach)
resp = conn.ews.create_attachment(parent_id, b64attach)
(resp.status == 'Success') || (raise EwsError, "Could not create attachments. #{resp.code}: #{resp.message}")
{:id => resp.items.first[:attachment_id][:root_item_id], :change_key => resp.items.first[:attachment_id][:root_item_change_key]}
end
Expand Down Expand Up @@ -183,7 +183,7 @@ def deepen!
return true unless @shallow
conn = Viewpoint::EWS::EWS.instance
shape = {:base_shape => 'AllProperties', :body_type => (@text_only ? 'Text' : 'Best')}
resp = conn.ews.get_item([@item_id], shape)
resp = conn.ews.get_item([@item_id], shape)
resp = resp.items.shift
@ews_item = resp[resp.keys.first]
@shallow = false
Expand Down Expand Up @@ -250,7 +250,7 @@ def attachments
end

# Delete this item
# @param [Boolean] soft Whether or not to do a soft delete. By default EWS will do a
# @param [Boolean] soft Whether or not to do a soft delete. By default EWS will do a
# hard delete of this item. See the MSDN docs for more info:
# http://msdn.microsoft.com/en-us/library/aa562961.aspx
# @return [Boolean] Whether or not the item was deleted
Expand Down Expand Up @@ -281,26 +281,32 @@ def parent_folder

# Use ExtendedProperties to create tags
# @param [String] tag a tag to add to this item
def add_tag!(tag)
# @param [Hash] opts options to pass to add_tag!
# @option opts [String] :tagspace the namespace to add the tag to. (default: 'viewpoint_tags')
def add_tag!(tag, opts={})
@tags |= [tag.downcase]
set_tags!(@tags)
set_tags!(@tags, opts)
end

# @param [String] tag a tag to delete from this item
def remove_tag!(tag)
# @param [Hash] opts options to pass to remove_tag!
# @option opts [String] :tagspace the namespace to add the tag to. (default: 'viewpoint_tags')
def remove_tag!(tag, opts={})
@tags -= [tag.downcase]
if(@tags.blank?)
clear_all_tags!
else
set_tags!(@tags)
set_tags!(@tags, opts)
end
end

def clear_all_tags!(options)
namespace = options[:namespace] || 'viewpoint_tags'
# @param [Hash] opts options to pass to clear_all_tags!
# @option opts [String] :tagspace the namespace to add the tag to. (default: 'viewpoint_tags')
def clear_all_tags!(opts={})
tagspace = opts[:tagspace] || 'viewpoint_tags'
vtag = {:preformatted => []}
vtag[:preformatted] << {:delete_item_field =>
{:extended_field_uRI=>{:distinguished_property_set_id=>"PublicStrings", :property_name=>namespace, :property_type=>"StringArray"}}
vtag[:preformatted] << {:delete_item_field =>
{:extended_field_uRI=>{:distinguished_property_set_id=>"PublicStrings", :property_name=>tagspace, :property_type=>"StringArray"}}
}

if(self.update_attribs!(vtag))
Expand All @@ -312,8 +318,10 @@ def clear_all_tags!(options)
end

# @param [Array<String>] tags viewpoint_tags to set on this item
def set_tags!(tags, options)
namespace = options[:namespace] || 'viewpoint_tags'
# @param [Hash] opts options to pass to set_tags!
# @option opts [String] :tagspace the namespace to add the tag to. (default: 'viewpoint_tags')
def set_tags!(tags, opts={})
tagspace = opts[:tagspace] || 'viewpoint_tags'
i_type = self.class.name.split(/::/).last.ruby_case.to_sym

tag_vals = []
Expand All @@ -323,10 +331,10 @@ def set_tags!(tags, options)

vtag = {:preformatted => []}
vtag[:preformatted] << {:set_item_field => [
{:extended_field_uRI=>{:distinguished_property_set_id=>"PublicStrings", :property_name=>namespace, :property_type=>"StringArray"}},
{:extended_field_uRI=>{:distinguished_property_set_id=>"PublicStrings", :property_name=>tagspace, :property_type=>"StringArray"}},
{i_type => [
{:extended_property => [
{:extended_field_uRI=>{:distinguished_property_set_id=>"PublicStrings", :property_name=>namespace, :property_type=>"StringArray"}},
{:extended_field_uRI=>{:distinguished_property_set_id=>"PublicStrings", :property_name=>tagspace, :property_type=>"StringArray"}},
{:values => tag_vals}
]}
]}
Expand Down Expand Up @@ -365,13 +373,13 @@ def method_missing(m, *args, &block)
end
end

def parse_tags(options)
namespace = options[:namespace] || 'viewpoint_tags'
def parse_tags(opts={})
tagspace = opts[:tagspace] || 'viewpoint_tags'

return [] unless(@ews_item.has_key?(:extended_property) &&
@ews_item[:extended_property].has_key?(:extended_field_u_r_i) &&
@ews_item[:extended_property][:extended_field_u_r_i].has_key?(:property_name) &&
@ews_item[:extended_property][:extended_field_u_r_i][:property_name] == namespace)
@ews_item[:extended_property][:extended_field_u_r_i][:property_name] == tagspace)

tags = []
vals = @ews_item[:extended_property][:values][:value]
Expand Down