CREATE OR REPLACE FUNCTION set_object_name(oid INTEGER) RETURNS BOOLEAN AS $$
 DECLARE
  otype TEXT;
 BEGIN
  SELECT objecttypeid INTO STRICT otype FROM object WHERE objectid = oid;

  IF NOT FOUND THEN 
    RETURN FALSE;
  END IF;

  IF otype = 'structure' THEN
    UPDATE object SET name = structure.name, 
      name_vector = setweight(to_tsvector('english', COALESCE(umlaut_flatten(structure.name_en), '')), 'A') ||
      setweight(to_tsvector('english', COALESCE(umlaut_flatten(structure.address), '')), 'B') ||
      setweight(to_tsvector('english', COALESCE(umlaut_flatten(structure.name), '')), 'A') ||
      setweight(to_tsvector('english', COALESCE(umlaut_flatten(names.others), '')), 'B') 
      FROM structure,
        (SELECT textcat_all(value || ' ') AS others FROM data WHERE datatypeid = 'name_other' AND data.objectid = oid AND visible = TRUE) AS names
      WHERE id = objectid AND id = oid;
  ELSEIF otype = 'street' THEN
    UPDATE object SET name = street.name, 
      name_vector = setweight(to_tsvector('english', COALESCE(umlaut_flatten(street.name_en), '')), 'A') ||
      setweight(to_tsvector('english', COALESCE(umlaut_flatten(street.name), '')), 'A') 
      FROM street WHERE id = objectid AND id = oid;
  ELSEIF otype = 'company' THEN
    UPDATE object SET name = company.name, 
      name_vector = setweight(to_tsvector('english', COALESCE(umlaut_flatten(company.name), '')), 'A') ||
      setweight(to_tsvector('english', COALESCE(umlaut_flatten(company.name_en), '')), 'A') 
      FROM company WHERE id = objectid AND id = oid;
  ELSEIF otype = 'region' THEN
    UPDATE object SET name_en = region.name_en,
      name_vector = setweight(to_tsvector('english', COALESCE(umlaut_flatten(region.name_en), '')), 'C') ||
        setweight(to_tsvector('english', COALESCE(umlaut_flatten(region.short_name), '')), 'C') ||
        setweight(to_tsvector('english', COALESCE(umlaut_flatten(names.others), '')), 'C') 
      FROM region, 
        (SELECT textcat_all(value || ' ') AS others FROM data WHERE datatypeid = 'name_other' AND data.objectid = oid AND visible = TRUE) AS names
    WHERE id = objectid AND id = oid;
    UPDATE region SET name_vector = to_tsvector(
      coalesce(short_name, '') || ' ' ||
      coalesce(name_ka, '') || ' ' || coalesce(name_sq, '') || ' ' || coalesce(name_ur, '') || ' ' ||
      coalesce(name_sw, '') || ' ' || coalesce(name_ta, '') || ' ' || coalesce(name_sl, '') || ' ' ||
      coalesce(name_tl, '') || ' ' || coalesce(name_fa, '') || ' ' || coalesce(name_he, '') || ' ' ||
      coalesce(name_uk, '') || ' ' || coalesce(name_el, '') || ' ' || coalesce(name_sr, '') || ' ' ||
      coalesce(name_hr, '') || ' ' || coalesce(name_vi, '') || ' ' || coalesce(name_lt, '') || ' ' ||
      coalesce(name_lv, '') || ' ' || coalesce(name_et, '') || ' ' || coalesce(name_ca, '') || ' ' ||
      coalesce(name_ro, '') || ' ' || coalesce(name_hu, '') || ' ' || coalesce(name_sv, '') || ' ' ||
      coalesce(name_bg, '') || ' ' || coalesce(name_th, '') || ' ' || coalesce(name_tr, '') || ' ' ||
      coalesce(name_id, '') || ' ' || coalesce(name_hi, '') || ' ' || coalesce(name_ko, '') || ' ' ||
      coalesce(name_ar, '') || ' ' || coalesce(name_ct, '') || ' ' || coalesce(name_zh, '') || ' ' ||
      coalesce(name_cs, '') || ' ' || coalesce(name_da, '') || ' ' || coalesce(name_pl, '') || ' ' ||
      coalesce(name_it, '') || ' ' || coalesce(name_ru, '') || ' ' || coalesce(name_ja, '') || ' ' ||
      coalesce(name_nl, '') || ' ' || coalesce(name_fi, '') || ' ' || coalesce(name_pt, '') || ' ' ||
      coalesce(name_es, '') || ' ' || coalesce(name_fr, '') || ' ' || coalesce(name_de, '') || ' ' ||
      coalesce(name_en, ''))
    WHERE id = oid;
  ELSE
    EXECUTE 'UPDATE object SET name_en = '||quote_ident(otype)||'.name_en,
      name_vector = setweight(to_tsvector(''english'', COALESCE(umlaut_flatten('||quote_ident(otype)||'.name_en), '''')), ''A'') 
      FROM '||quote_ident(otype)||' WHERE id = objectid AND id = '||oid;
  END IF;

  RETURN TRUE;
 END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION set_object_location(oid INTEGER) RETURNS BOOLEAN AS $$
 DECLARE
  otype TEXT;
 BEGIN
  SELECT objecttypeid INTO STRICT otype FROM object WHERE objectid = oid;

  IF NOT FOUND THEN 
    RETURN FALSE;
  END IF;

  IF otype = 'street' THEN
    EXECUTE 'UPDATE object SET location_vector = setweight(to_tsvector(''english'', COALESCE(umlaut_flatten('||quote_ident(otype)||'.name_en), '''')), ''A'') ||
      setweight(to_tsvector(''english'', COALESCE(umlaut_flatten('||quote_ident(otype)||'.name), '''')), ''A'') ||
      setweight(to_tsvector(''english'', COALESCE(umlaut_flatten((
        SELECT textcat_all(region.name_en || '' '') FROM object_region INNER JOIN region ON (region.id = object_region.regionid) WHERE object_region.objectid = '||oid
      ||')), '''')), ''B'')
      FROM '||quote_ident(otype)||' WHERE id = object.objectid AND id = '||oid;
  ELSEIF otype = 'region' THEN
    UPDATE object SET location_vector = object.name_vector ||
      setweight(to_tsvector('english', COALESCE(umlaut_flatten(region.name_de), '')), 'C') ||
      setweight(to_tsvector('english', COALESCE(umlaut_flatten(region.name_fr), '')), 'C') ||
      setweight(to_tsvector('english', COALESCE(umlaut_flatten(region.name_it), '')), 'C') ||
      setweight(to_tsvector('english', COALESCE(umlaut_flatten(region.name_es), '')), 'C') ||
      setweight(to_tsvector('english', COALESCE(umlaut_flatten(region.name_ru), '')), 'C') ||
      setweight(to_tsvector('english', COALESCE(umlaut_flatten(region.name_pt), '')), 'C') ||
      setweight(to_tsvector('english', COALESCE(umlaut_flatten(region.name_sv), '')), 'C') ||
      setweight(to_tsvector('english', COALESCE(umlaut_flatten(region.name_pl), '')), 'C') ||
      COALESCE(parent.location_vector, ''::tsvector)
      FROM region LEFT JOIN object AS parent ON (region.parent_id = parent.objectid) WHERE region.id = object.objectid AND region.id = oid;
  ELSEIF otype = 'structure' THEN
    UPDATE object SET location_vector = 
      (SELECT tsvectorcat_all(regionob.name_vector) FROM object_region 
        INNER JOIN object AS regionob ON (regionob.objectid = object_region.regionid) WHERE object_region.objectid = oid)
      FROM structure WHERE id = object.objectid AND id = oid;
  ELSEIF otype = 'company' THEN
    UPDATE object SET location_vector = 
      setweight(to_tsvector('english', COALESCE(umlaut_flatten(company.address), '')), 'B') ||
      (SELECT tsvectorcat_all(regionob.name_vector) FROM object_region 
        INNER JOIN object AS regionob ON (regionob.objectid = object_region.regionid) WHERE object_region.objectid = oid)
      FROM company WHERE id = object.objectid AND id = oid;
  ELSE
    EXECUTE 'UPDATE object SET location_vector = setweight(to_tsvector(''english'', COALESCE(umlaut_flatten('||quote_ident(otype)||'.name_en), '''')), ''A'') 
      FROM '||quote_ident(otype)||' WHERE id = objectid AND id = '||oid;
  END IF;

  RETURN TRUE;
 END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION set_object_properties(oid INTEGER) RETURNS BOOLEAN AS $$
 DECLARE
  otype TEXT;
 BEGIN
  SELECT objecttypeid INTO STRICT otype FROM object WHERE objectid = oid;

  IF NOT FOUND THEN 
    RETURN FALSE;
  END IF;

  IF otype = 'structure' THEN
    UPDATE object SET properties_vector = setweight(to_tsvector('english', COALESCE(umlaut_flatten(strform.name_en), '')),'C') || 
        setweight(to_tsvector('english', COALESCE(usage, '')),'C') || 
        setweight(to_tsvector('english', COALESCE(styl.name_en, '')),'B') ||
        setweight(to_tsvector('english', COALESCE(color, '')),'D') FROM structure 
      LEFT JOIN category AS strform ON (strform.categoryid = structural_form) LEFT JOIN category AS styl ON (styl.categoryid = style),
      (SELECT array_to_string(array_agg(category.name_en), ' ') AS usage FROM structure INNER JOIN category ON (categoryid = ANY(usage_main)) WHERE id = oid) AS usg,
      (SELECT array_to_string(array_agg(category.name_en), ' ') AS color FROM structure INNER JOIN category ON (categoryid = ANY(facade_color)) WHERE id = oid) AS fcolor
       WHERE objectid = oid AND id = oid;
  ELSEIF otype = 'region' THEN
    UPDATE object SET properties_vector = setweight(to_tsvector('english', COALESCE(umlaut_flatten(regiontype.name_en), '')),'C') || 
        setweight(to_tsvector('english', COALESCE(umlaut_flatten(regiontype.name_de), '')),'C') || 
        setweight(to_tsvector('english', COALESCE(umlaut_flatten(regiontype.name_fr), '')),'C') || 
        setweight(to_tsvector('english', COALESCE(umlaut_flatten(regiontype.name_it), '')),'C') || 
        setweight(to_tsvector('english', COALESCE(umlaut_flatten(regiontype.name_es), '')),'C') || 
        setweight(to_tsvector('english', COALESCE(umlaut_flatten(regiontype.name_pt), '')),'C') || 
        setweight(to_tsvector('english', COALESCE(umlaut_flatten(regiontype.name_ru), '')),'C') || 
        setweight(to_tsvector('english', COALESCE(umlaut_flatten(regiontype.name_sv), '')),'C') || 
        setweight(to_tsvector('english', COALESCE(umlaut_flatten(regiontype.name_pl), '')),'C')
      FROM region INNER JOIN regiontype ON (region.regiontypeid = regiontype.id)
       WHERE objectid = oid AND region.id = oid;
  ELSEIF otype = 'company' THEN
    UPDATE object SET properties_vector = location_vector WHERE objectid = oid;
  ELSE
    UPDATE object SET properties_vector = to_tsvector('english', '') WHERE objectid = oid;
  END IF;

  RETURN TRUE;
 END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION set_object_vectors(oid INTEGER) RETURNS BOOLEAN AS $$
 DECLARE
  retvalue BOOLEAN;
 BEGIN   
  SELECT (set_object_properties(oid) AND set_object_name(oid) AND set_object_location(oid)) INTO retvalue;
  UPDATE object SET all_vector = name_vector || location_vector || properties_vector WHERE objectid = oid;
  RETURN retvalue;
 END;
$$ LANGUAGE plpgsql;

