app/models/maestrano/connector/rails/concerns/entity.rb in maestrano-connector-rails-1.0.4 vs app/models/maestrano/connector/rails/concerns/entity.rb in maestrano-connector-rails-1.1.0

- old
+ new

@@ -57,10 +57,14 @@ def last_update_date_from_external_entity_hash(entity) raise "Not implemented" end + def creation_date_from_external_entity_hash(entity) + raise "Not implemented" + end + # Return a string representing the object from a connec! entity hash def object_name_from_connec_entity_hash(entity) raise "Not implemented" end @@ -152,25 +156,25 @@ # ---------------------------------------------- # Supported options: # * full_sync # * $filter (see Connec! documentation) # * $orderby (see Connec! documentation) - def get_connec_entities(last_synchronization) + def get_connec_entities(last_synchronization_date=nil) return [] if @opts[:skip_connec] || !self.class.can_read_connec? Maestrano::Connector::Rails::ConnectorLogger.log('info', @organization, "Fetching Connec! #{self.class.connec_entity_name}") entities = [] query_params = {} query_params[:$orderby] = @opts[:$orderby] if @opts[:$orderby] # Fetch first page page_number = 0 - if last_synchronization.blank? || @opts[:full_sync] + if last_synchronization_date.blank? || @opts[:full_sync] query_params[:$filter] = @opts[:$filter] if @opts[:$filter] else - query_params[:$filter] = "updated_at gt '#{last_synchronization.updated_at.iso8601}'" + (@opts[:$filter] ? " and #{@opts[:$filter]}" : '') + query_params[:$filter] = "updated_at gt '#{last_synchronization_date.iso8601}'" + (@opts[:$filter] ? " and #{@opts[:$filter]}" : '') end Maestrano::Connector::Rails::ConnectorLogger.log('debug', @organization, "entity=#{self.class.connec_entity_name}, fetching data with #{query_params.to_query}") uri = "#{self.class.normalized_connec_entity_name}?#{query_params.to_query}" response_hash = fetch_connec(uri, 0) @@ -217,16 +221,16 @@ end # ---------------------------------------------- # External methods # ---------------------------------------------- - def get_external_entities_wrapper(last_synchronization) + def get_external_entities_wrapper(last_synchronization_date=nil) return [] if @opts[:skip_external] || !self.class.can_read_external? - get_external_entities(last_synchronization) + get_external_entities(last_synchronization_date) end - def get_external_entities(last_synchronization) + def get_external_entities(last_synchronization_date=nil) Maestrano::Connector::Rails::ConnectorLogger.log('info', @organization, "Fetching #{Maestrano::Connector::Rails::External.external_name} #{self.class.external_entity_name.pluralize}") raise "Not implemented" end def push_entities_to_external(mapped_connec_entities_with_idmaps) @@ -317,10 +321,13 @@ return {connec_entities: mapped_connec_entities, external_entities: mapped_external_entities} end def consolidate_and_map_connec_entities(connec_entities, external_entities, references, external_entity_name) connec_entities.map{|entity| + # Entity has been created before date filtering limit + next nil if before_date_filtering_limit?(entity, false) && !@opts[:full_sync] + entity = Maestrano::Connector::Rails::ConnecHelper.unfold_references(entity, references, @organization) next nil unless entity connec_id = entity.delete(:__connec_id) if entity['id'].blank? @@ -330,18 +337,20 @@ idmap = self.class.find_or_create_idmap(external_id: entity['id'], organization_id: @organization.id, external_entity: external_entity_name.downcase, connec_id: connec_id) idmap.update(name: self.class.object_name_from_connec_entity_hash(entity)) next nil if idmap.external_inactive || !idmap.to_external || (!@opts[:full_sync] && not_modified_since_last_push_to_external?(idmap, entity)) - # Check for conflict with entities from external solve_conflict(entity, external_entities, external_entity_name, idmap) }.compact end def consolidate_and_map_external_entities(external_entities, connec_entity_name) external_entities.map{|entity| + # Entity has been created before date filtering limit + next nil if before_date_filtering_limit?(entity) && !@opts[:full_sync] + entity_id = self.class.id_from_external_entity_hash(entity) idmap = self.class.find_or_create_idmap(external_id: entity_id, organization_id: @organization.id, connec_entity: connec_entity_name.downcase) # Not pushing entity to Connec! next nil unless idmap.to_connec @@ -387,15 +396,15 @@ end # ---------------------------------------------- # After and before sync # ---------------------------------------------- - def before_sync(last_synchronization) + def before_sync(last_synchronization_date) # Does nothing by default end - def after_sync(last_synchronization) + def after_sync(last_synchronization_date) # Does nothing by default end # ---------------------------------------------- @@ -449,10 +458,14 @@ not_modified = idmap.last_push_to_external && idmap.last_push_to_external > entity['updated_at'] Maestrano::Connector::Rails::ConnectorLogger.log('info', @organization, "Discard Connec! #{self.class.connec_entity_name} : #{entity}") if not_modified not_modified end + def before_date_filtering_limit?(entity, external=true) + @organization.date_filtering_limit && @organization.date_filtering_limit > (external ? self.class.creation_date_from_external_entity_hash(entity) : entity['created_at']) + end + def is_connec_more_recent?(connec_entity, external_entity) connec_entity['updated_at'] > self.class.last_update_date_from_external_entity_hash(external_entity) end def solve_conflict(connec_entity, external_entities, external_entity_name, idmap) @@ -463,14 +476,14 @@ else keep_connec = is_connec_more_recent?(connec_entity, external_entity) end if keep_connec - Maestrano::Connector::Rails::ConnectorLogger.log('info', @organization, "Conflict between #{Maestrano::Connector::Rails::External::external_name} #{external_entity_name} #{external_entity} and Connec! #{self.class.connec_entity_name} #{connec_entity}. Entity from external kept") + Maestrano::Connector::Rails::ConnectorLogger.log('info', @organization, "Conflict between #{Maestrano::Connector::Rails::External::external_name} #{external_entity_name} #{external_entity} and Connec! #{self.class.connec_entity_name} #{connec_entity}. Entity from Connec! kept") external_entities.delete(external_entity) map_connec_entity_with_idmap(connec_entity, external_entity_name, idmap) else - Maestrano::Connector::Rails::ConnectorLogger.log('info', @organization, "Conflict between #{Maestrano::Connector::Rails::External::external_name} #{external_entity_name} #{external_entity} and Connec! #{self.class.connec_entity_name} #{connec_entity}. Entity from Connec! kept") + Maestrano::Connector::Rails::ConnectorLogger.log('info', @organization, "Conflict between #{Maestrano::Connector::Rails::External::external_name} #{external_entity_name} #{external_entity} and Connec! #{self.class.connec_entity_name} #{connec_entity}. Entity from external kept") nil end else map_connec_entity_with_idmap(connec_entity, external_entity_name, idmap) \ No newline at end of file