Busca customizada no Oracle PORTAL

Link da documentacao:
http://www.oracle.com/technology/products/ias/portal/html/plsqldoc/pldoc1014/wwsrc_api.html

Temos uma funcao para efetuar uns testes:

create or replace procedure search_results(searchterm varchar2, score number) as
x varchar2(100);
y number;
l_results wwsrc_api.items_result_array_type;
l_count number;
l_scores wwsrc_api.number_list_type;
begin
x := searchterm;
y := score;
l_results := wwsrc_api.item_search(
p_mainsearch => x,
p_out_count => l_count,
p_out_scores => l_scores
);
htp.p('Number of total hits: ' || l_count);

htp.p('resultado : ' || l_results.count);
htp.p('resultado : ' || l_results(1).url);
htp.p('resultado : ' || l_results(2).url) ;

htp.p('
');

htp.p('
');

for i in 1..l_results.count loop
-- if (l_scores(i) > y) then
htp.p('' || i || ' -
'">' || l_results(i).display_name || '
');

htp.p('
');

htp.p('score = ' || l_scores(i));
htp.p('
');

-- end if;
end loop;
exception when others then
null;
end;



Mas ai tive que dar uma melhorada e criei a funcao:

CREATE OR REPLACE PROCEDURE PESQUISA(p_ATTRIBUTE_TEXT varchar2,
P_TEMA varchar2,
P_ASSUNTO varchar2,
P_AUTOR varchar2,
P_DAT_INI DATE,
P_DAT_FIM DATE,

P_TIPOTERMO VARCHAR2 default 'ANY')

is
/*

FALTA VALIDAR SE O ITEM FOI APROVADOR
e PUBLICADO....

Limitar a pesquisa a 3 caracteres e no maximo 128 caracteres.

*/

l_results wwsrc_api.items_result_array_type;
l_count number;
l_scores wwsrc_api.number_list_type;
l_attributes wwsrc_runtime_attr_varray;
l_pggroups wwsrc_api.number_list_type;

v_contador number default 1;
v_databusca date;

v_sqldinamico varchar2(4000);

type tpCursor is REF CURSOR;

cCursor tpCursor;
v_sql varchar2(4000) default v_sqldinamico;

-- Mostrar na tela retorno do cursor.

v_tema portal.WWSBR_ALL_CATEGORIES.display_name%TYPE;
v_temaanterior portal.WWSBR_ALL_CATEGORIES.display_name%TYPE;

v_assunto portal.WWSBR_ALL_CATEGORIES.display_name%TYPE;
v_titulo portal.WWSBR_ALL_CATEGORIES.display_name%type;
v_DATADEPUBLICACAO date;
v_urlpage varchar2(4000);
v_descricao varchar2(4000);
v_tipotermo varchar2(4000);
v_nome varchar2(4000);

-- solucao temporaria
v_pgname portal.WWSBR_ALL_CONTENT_AREAS.name%TYPE;

begin
--
-- Verificar se foram designados parametros.
--
if length(NVL(p_ATTRIBUTE_TEXT, '') ) <>
length(p_ATTRIBUTE_TEXT ) >= 128 then
htp.print('Refaça a pesquisa. Informe acima de 3 caracteres e no maximo 128 caracteres para a busca.');
else
--
-- Se foram passados parametros
-- entao estaremos
-- com os parametros para informar na busca.
--
if p_tipotermo = 'ANY' then
v_tipotermo := portal.wwsrc_api.CONTAINS_ANY;
elsif p_tipotermo = 'ALL' then
v_tipotermo := portal.wwsrc_api.CONTAINS_ALL;

elsif p_tipotermo = 'TO' then
v_tipotermo := portal.wwsrc_api.EQUALS_TO;
end if;

-- Build up attribute object with author criteria.
--
-- Quais os page groups para a busca? OU quais os portais...
--
l_pggroups(1) := 813; --813-- Portal Publico
-- l_pggroups(2) := 833; -- Portal Publico

--
-- Para pegar o nome do portal DEFAULT
-- vou pesquisar o nome para passar como parametro fixo
-- Visto que a consulta somente retorna os itens da pagina LIB
-- chamado aberto com a oracle para resolver.
--
select name
into v_pgname
from portal.WWSBR_ALL_CONTENT_AREAS
where portal.WWSBR_ALL_CONTENT_AREAS.id = 813;


if p_ATTRIBUTE_TEXT is not null then
portal.wwsrc_api.specify_attributes(p_id => portal.wwsbr_api.ATTRIBUTE_KEYWORDS,
p_value => p_ATTRIBUTE_TEXT,
p_operator => v_tipotermo,
p_in_out_attr_varray => l_attributes);

portal.wwsrc_api.specify_attributes(p_id => portal.wwsbr_api.ATTRIBUTE_TITLE,
p_value => p_ATTRIBUTE_TEXT,
p_operator => v_tipotermo,
p_in_out_attr_varray => l_attributes);

portal.wwsrc_api.specify_attributes(p_id => portal.wwsbr_api.ATTRIBUTE_URL,
p_value => p_ATTRIBUTE_TEXT,
p_operator => v_tipotermo,
p_in_out_attr_varray => l_attributes);
end if;

--
-- Primeiro tentar converter o texto de procura para a data inicial
--
if P_DAT_INI is not null then

begin
v_databusca := to_char(p_ATTRIBUTE_TEXT, 'DD-MON-RRRR');
exception
when others then
v_databusca := null;
end;

begin
v_databusca := to_char(p_ATTRIBUTE_TEXT, 'DDMMYYYY');
exception
when others then
v_databusca := null;
end;

begin
v_databusca := to_char(p_ATTRIBUTE_TEXT, 'DD\MM\YYYY');
exception
when others then
v_databusca := null;
end;

begin
v_databusca := to_char(p_ATTRIBUTE_TEXT, 'DD\MM\YY');
exception
when others then
v_databusca := null;
end;

begin
v_databusca := to_char(p_ATTRIBUTE_TEXT, 'DDMMYY');
exception
when others then
v_databusca := null;
end;

end if;

--
-- Tentar converter o formato da data INICIAL
--

if P_DAT_INI is not null then

begin
v_databusca := to_char(P_DAT_INI, 'DD-MON-RRRR');
exception
when others then
v_databusca := null;
end;

begin
v_databusca := to_char(P_DAT_INI, 'DDMMYYYY');
exception
when others then
v_databusca := null;
end;

begin
v_databusca := to_char(P_DAT_INI, 'DD\MM\YYYY');
exception
when others then
v_databusca := null;
end;

begin
v_databusca := to_char(P_DAT_INI, 'DD\MM\YY');
exception
when others then
v_databusca := null;
end;

begin
v_databusca := to_char(P_DAT_INI, 'DDMMYY');
exception
when others then
v_databusca := null;
end;

end if;

if v_databusca is not null then
dbms_session.set_nls('NLS_DATE_FORMAT', '''DD-MON-RRRR''');
portal.wwsrc_api.specify_attributes(p_id => portal.wwsbr_api.ATTRIBUTE_PUBLISHDATE, --portal.wwsbr_api.
p_value => v_databusca,
p_operator => portal.wwsrc_api.GREATER_THAN,
p_in_out_attr_varray => l_attributes);

end if;


--
-- Colocar a validacao da data final
--

if P_DAT_FIM is not null then

begin
v_databusca := to_char(P_DAT_FIM, 'DD-MON-RRRR');
exception
when others then
v_databusca := null;
end;

begin
v_databusca := to_char(P_DAT_FIM, 'DDMMYYYY');
exception
when others then
v_databusca := null;
end;

begin
v_databusca := to_char(P_DAT_FIM, 'DD\MM\YYYY');
exception
when others then
v_databusca := null;
end;

begin
v_databusca := to_char(P_DAT_FIM, 'DD\MM\YY');
exception
when others then
v_databusca := null;
end;

begin
v_databusca := to_char(P_DAT_FIM, 'DDMMYY');
exception
when others then
v_databusca := null;
end;

end if;

if P_DAT_FIM is not null then
v_databusca := to_char(P_DAT_INI, 'DD-MON-RRRR');
portal.wwsrc_api.specify_attributes(p_id => portal.wwsbr_api.ATTRIBUTE_PUBLISHDATE, --portal.wwsbr_api.
p_value => v_databusca,
p_operator => portal.wwsrc_api.LESS_THAN,
p_in_out_attr_varray => l_attributes);
end if;

--
-- Filtrar pelo AUTOR
--
if P_AUTOR is not null then
portal.wwsrc_api.specify_attributes(p_id => portal.wwsbr_api.ATTRIBUTE_AUTHOR, --portal.wwsbr_api.
p_value => P_AUTOR,
p_operator => v_tipotermo,
p_in_out_attr_varray => l_attributes);
end if;

--
-- Filtrar pelo TEMA
--
if P_TEMA is not null then

portal.wwsrc_api.specify_attributes(p_id => portal.wwsbr_api.ATTRIBUTE_CATEGORY,
p_value => P_TEMA,
p_operator => v_tipotermo,
p_in_out_attr_varray => l_attributes);
end if;


--
-- Filtrar pelo ASSUNTO
--

if P_ASSUNTO is not null then

portal.wwsrc_api.specify_attributes(p_id => portal.wwsbr_api.ATTRIBUTE_CATEGORY,
p_value => P_ASSUNTO,
p_operator => v_tipotermo,
p_in_out_attr_varray => l_attributes);
end if;

-- The search needs to be performed by a User in the Portal
-- The is the Portal UI password for the portal user.
-- portal.wwctx_api.set_context('portal', 'senha');
-- Perform the search.
portal.wwctx_api.set_context('andre.rocha', 'senha');

begin

l_results := portal.wwsrc_api.item_search(p_page_groups => l_pggroups, --p_mainsearch => 'portalpublicodl',
p_attributes => l_attributes,
p_attributesmatch => v_tipotermo,
p_out_count => l_count,
p_out_scores => l_scores);

exception
when others then
htp.print('Erro na pesquisa. Favor contactar o analista responsável. Erro: ' || sqlerrm);
end;
-- dbms_output.put_line('Number of results: ' || l_count);

v_sqldinamico := null;
for i in 1 .. l_results.count loop

-- Preencher dados do ID do item para que eu possa retornar a pagina.

-- Mostrar os IDs
--
-- htp.print(' FOLDER_ID ' || l_results(i).FOLDER_ID);
-- htp.print(' CAID ' || l_results(i).CAID);

if v_sqldinamico is null then
v_sqldinamico := '(' || l_results(i).id;
if i = l_results.count then
v_sqldinamico := v_sqldinamico || ') ';
end if;
else

if i = l_results.count then
v_sqldinamico := v_sqldinamico || ',' || l_results(i).id || ') ';
else
v_sqldinamico := v_sqldinamico || ',' || l_results(i).id;
end if;

end if;

end loop;

--
-- Mostrar os ids de pesquisa
--

--htp.print(v_sqldinamico);

if nvl(l_results.count, 0) > 0 then

-- agora filtrar tb a categoria com os itens e paginas.
-- Montar esta consulta dinamica para conseguir
-- listar ASSUNTO x TEMA x resultados.

v_sql := ' ';

v_sql := v_sql ||
' select rownum linha, substr(categoria.descricao, ';
v_sql := v_sql || '1, ';
v_sql := v_sql || ' decode(instr(categoria.descricao, ' || '''' || '-' || '''' ||
' ), ';
v_sql := v_sql || ' 0,';
v_sql := v_sql || ' 100,';
v_sql := v_sql || ' instr(categoria.descricao, ' || '''' || '-' || '''' ||
') - 2)) tema,';
v_sql := v_sql || ' categoria.descricao assunto,';
v_sql := v_sql || ' a.display_name titulo,';
v_sql := v_sql || ' a.name nome,';

v_sql := v_sql || ' case';
v_sql := v_sql || ' when a.ITEMTYPE = ' || '''' || 'basefile' || '''' ||
' then';
v_sql := v_sql || ' a.url';
v_sql := v_sql || ' when a.ITEMTYPE = ' || '''' || 'baseimage' || '''' ||
' then';
v_sql := v_sql || ' a.url';
v_sql := v_sql || ' when a.ITEMTYPE = ' || '''' || 'baseimagemap' || '''' ||
' then';
v_sql := v_sql || ' a.url';
v_sql := v_sql || ' when a.ITEMTYPE = ' || '''' ||
'baseloginlogout' || '''' || ' then';
v_sql := v_sql || ' a.url';
v_sql := v_sql || ' when a.ITEMTYPE = ' || '''' || 'basemaplink' || '''' ||
' then ';
v_sql := v_sql || ' a.url';
v_sql := v_sql || ' when a.ITEMTYPE = ' || '''' || 'basepagelink' || '''' ||
' then';
v_sql := v_sql || ' a.url';
v_sql := v_sql || ' when a.ITEMTYPE = ' || '''' || 'basepagepath' || '''' ||
' then';
v_sql := v_sql || ' a.url';
v_sql := v_sql || ' when a.ITEMTYPE = ' || '''' || 'baseplsql' || '''' ||
' then';
v_sql := v_sql || ' a.url';
v_sql := v_sql || ' when a.ITEMTYPE = ' || '''' || 'baseportallink' || '''' ||
' then';
v_sql := v_sql || ' a.url';
v_sql := v_sql || ' when a.ITEMTYPE = ' || '''' ||
'baseportletinstance' || '''' || ' then';
v_sql := v_sql || ' a.url';
v_sql := v_sql || ' when a.ITEMTYPE = ' || '''' || 'basesearch' || '''' ||
' then';
v_sql := v_sql || ' a.url';
v_sql := v_sql || ' when a.ITEMTYPE = ' || '''' || 'basesmarttext' || '''' ||
' then';
-- v_sql := v_sql ||
-- ' bhtrans_publico.FRETORNAURLPAGE(substr(a.text, 1, 350), pgs.id, pg.name)';
v_sql := v_sql ||
' bhtrans_publico.FRETORNAURL(substr(a.text, 1, 350), FRETORNAPGIDPUBLICA(a.id,' ||
l_pggroups(1) || '), ' || '''' || v_pgname || '''' || ') ';

v_sql := v_sql || ' when a.ITEMTYPE = ' || '''' || 'basetab' || '''' ||
' then';
v_sql := v_sql || ' a.url';
v_sql := v_sql || ' when a.ITEMTYPE = ' || '''' || 'basetext' || '''' ||
' then';
v_sql := v_sql ||
' bhtrans_publico.FRETORNAURL(substr(a.text, 1, 350), FRETORNAPGIDPUBLICA(a.id,' ||
l_pggroups(1) || '), ' || '''' || v_pgname || '''' || ') ';
-- ' bhtrans_publico.FRETORNAURLPAGE(substr(a.text, 1, 350), pgs.id, pg.name)';

v_sql := v_sql || ' when a.ITEMTYPE = ' || '''' || 'baseurl' || '''' ||
' then';
v_sql := v_sql || ' a.url ';
v_sql := v_sql || ' end DESCRICAO,';

--
-- TEMPORARIA
-- Solucao para retornar a pagina do portal PUBLICO
-- e nao a do portal LIB

-- v_sql := v_sql ||
-- ' a.publish_date DATADEPUBLICACAO, bhtrans_publico.FRETORNAURLPAGE(substr(a.text, 1, 100), pgs.id, pg.name) URLPAGE ';

v_sql := v_sql ||
' a.publish_date DATADEPUBLICACAO, bhtrans_publico.FRETORNAURL(substr(a.text, 1, 100), FRETORNAPGIDPUBLICA(a.id,' ||
l_pggroups(1) || '), ' || '''' || v_pgname || '''' ||
') URLPAGE ';

v_sql := v_sql || ' from portal.wwsbr_all_items a,';
v_sql := v_sql || ' portal.WWSBR_ALL_CONTENT_AREAS pg,';
v_sql := v_sql || ' portal.wwsbr_user_folders pgs,';
v_sql := v_sql || ' (select c.id,';
v_sql := v_sql || ' substr(replace(SYS_CONNECT_BY_PATH(' || '''' || ' ' || '''' ||
' || c.display_name,';
v_sql := v_sql || ' ' || '''' ||
'; -' || '''' || '),';
v_sql := v_sql || ' ' || '''' || ';' || '''' || ',';
v_sql := v_sql || ' null),';
v_sql := v_sql || ' 4) as descricao ';
v_sql := v_sql || ' from portal.WWSBR_ALL_CATEGORIES c ';
v_sql := v_sql ||
' where c.language = Portal.wwctx_api.get_nls_language()' ||
' and c.id in ' ||
' (select distinct x.category_id
from portal.wwsbr_all_items x
where x.id in ' || v_sqldinamico || ')';

v_sql := v_sql || ' start with c.PARENTID = 0';
v_sql := v_sql || ' connect by prior c.ID = c.PARENTID';
v_sql := v_sql || ' and prior c.CAID = c.CAID';
v_sql := v_sql || ' and prior c.language = c.language' ||
' ) categoria,';
v_sql := v_sql || ' portal.WWSBR_ALL_CATEGORIES d ';
v_sql := v_sql || ' where a.category_id = categoria.id';
v_sql := v_sql ||
' and a.language = Portal.wwctx_api.get_nls_language()';
v_sql := v_sql || ' and a.caid = pg.id';
v_sql := v_sql || ' and a.caid = pgs.caid';
v_sql := v_sql || ' and pgs.caid = pg.id';
v_sql := v_sql || ' and a.FOLDER_ID = pgs.id';
v_sql := v_sql || ' and a.active = 1';
v_sql := v_sql || ' and a.is_current_version = 1';

v_sql := v_sql || ' and a.category_id = d.id';
v_sql := v_sql || ' and a.id in ' || v_sqldinamico;

v_sql := v_sql || ' order by 2, 3 ';

--htp.print('CONSULTA: ' || v_SQL);

open cCursor for v_SQL;

-- Mostrar o SQL pronto
htp.print('CONSULTA: ' || v_SQL);

v_temaanterior := ' ';
loop

fetch cCursor
into v_contador, v_tema, v_assunto, v_titulo,v_nome, v_descricao, v_DATADEPUBLICACAO, v_urlpage;

exit when cCursor%notfound;
if v_urlpage is null then

if v_temaanterior <> v_tema then
v_temaanterior := v_tema;
htp.print('');
htp.print(' ');
htp.print(' ');
htp.print(' ');
htp.print('
CATEGORIA: ' || v_tema || '
');

end if;

htp.print('');
htp.print(' ');
htp.print(' ');
htp.print(' ');

-- v_returnurl:= '' || v_returnurl || '';


htp.print(' ');
htp.print(' ');

htp.print(' ');
htp.print(' ');



htp.print(' ');
htp.print(' ');
htp.print(' ');
htp.print(' ');

htp.print(' ');
htp.print(' ');
htp.print(' ');
htp.print(' ');
htp.print('
' || v_contador || ' -' || nvl(v_titulo,v_nome) || '

' || FRETORNANEGRITO(v_descricao, trim(p_ATTRIBUTE_TEXT))|| '

');
htp.print('


Publicado em ' ||
to_char(v_DATADEPUBLICACAO, 'DD/MM/YYYY') || '

');


end if;
-- DBMS_OUTPUT.PUT_LINE(trim(v_tema));

end loop;

close cCursor;

else
htp.print('Não foram encontrados registros em sua busca, favor especifique novos valores.');

end if;
end if;

exception
when others then
htp.print('Error in Search Execution!');
dbms_output.put_line('Error in Search Execution!');
dbms_output.put_line(sqlerrm);

end;

Comentários