CREATE OR REPLACE FUNCTION recognition_channels() RETURNS trigger AS $$
 DECLARE
  rectype TEXT;
  ooid INTEGER;
 BEGIN

  -- green buildings
  -- DELETE FROM object_channel WHERE object_channel.objectid = NEW.objectid AND channelid IN (576707705);
  SELECT recognition_type INTO rectype FROM recognition WHERE id = NEW.recognitionid;
  IF FOUND AND rectype = 'recognition_type.2.1' THEN
    SELECT objectid INTO ooid FROM object_channel WHERE objectid = NEW.objectid AND channelid = 576707705;
    IF NOT FOUND THEN
      INSERT INTO object_channel (objectid, channelid) VALUES (NEW.objectid, 576707705);
    END IF;
  END IF;

  RETURN NULL;
 END
$$ LANGUAGE plpgsql;

DROP TRIGGER channel_updates ON recognition_connection;
CREATE TRIGGER channel_updates AFTER INSERT ON recognition_connection FOR EACH ROW EXECUTE PROCEDURE recognition_channels();

