from Products.ZSQLCatalog.SQLCatalog import Query, SimpleQuery

portal = context.getPortalObject()
portal_catalog = portal.portal_catalog
object = state_change['object']

if object.getPortalType() == "Data Set":
  data_set = object
  context.logEntry("Invalidating data set '%s' and dependencies." % data_set.getReference())
  reference_query = Query(**{'reference': data_set.getReference()+'/%'})
  kw_dict = {"portal_type": "Data Stream",
             "query": reference_query}
  for data_stream in portal_catalog(**kw_dict):
    if data_stream.getReference().startswith(data_set.getReference()+'/') and not data_stream.getReference().endswith("_invalid"):
      portal.ERP5Site_invalidateIngestionObjects(data_stream.getReference())
  data_set.setReference(data_set.getReference() + "_invalid")
elif object.getPortalType() == "Data Stream":
  data_stream = object
  portal.ERP5Site_invalidateIngestionObjects(data_stream.getReference())
