DBA Data[Home] [Help]

PACKAGE BODY: APPS.WF_ENTITY_MGR

Source


1 package body WF_ENTITY_MGR as
2 /* $Header: WFEMGRB.pls 120.4 2010/03/09 21:02:56 alsosa ship $ */
3 ------------------------------------------------------------------------------
4 /*
5 ** InitCache - <private> Creates the 'CACHE_CHANGED' attribute for a specific
6 **                       entity_type/entity_key
7 */
8 procedure InitCache(p_entity_type in varchar2,
9                     p_entity_key_value in varchar2) is
10   pragma autonomous_transaction;
11 begin
12   insert into wf_attribute_cache (
13   entity_type,
14   entity_key_value,
15   attribute_name,
16   attribute_value,
17   last_update_date,
18   change_number)
19  values
20   (p_entity_type,
21    upper(p_entity_key_value),
22    'CACHE_CHANGED',
23    'PENDING',
24    sysdate,
25    NULL);
26 
27   --We commit the transaction so all sessions can see the attribute.
28   commit;
29 exception
30   when DUP_VAL_ON_INDEX then
31     --This can occur from race condition (two sessions tried to update at
32     --same time).
33     null;
34 end;
35 
36 /*
37 ** put - <private> construct cache rows
38 */
39 PROCEDURE put(p_entity_type      in varchar2,
40               p_entity_key_value in varchar2,
41               p_att              in varchar2,
42               p_value            in varchar2,
43               p_change_number    in number ) is
44 
45   l_cacheLocked boolean;
46 
47 begin
48 
49 
50   if (wf_log_pkg.level_procedure >= fnd_log.g_current_runtime_level) then
51 
52       WF_LOG_PKG.String(WF_LOG_PKG.LEVEL_PROCEDURE,
53          'wf.plsql.WF_ENTITY_MGR.put',
54                        'Begin put('||p_entity_type||', '||
55                        p_entity_key_value||', '||p_att||', '||
56                        p_value||', '||p_change_number||')');
57   end if;
58 
59   if (p_value = '*UNKNOWN*') then
60     return;
61   elsif (p_att <> 'CACHE_CHANGED') then
62     --First lock the cache or call InitCache() if the attribute
63     --does not exist..
64     l_cacheLocked := FALSE;
65     while not (l_cacheLocked) loop
66       update wf_attribute_cache
67       set    attribute_value = 'YES'
68       where  entity_type = p_entity_type
69       and    entity_key_value = upper(p_entity_key_value)
70       and    attribute_name = 'CACHE_CHANGED';
71 
72       if sql%notfound then
73         WF_ENTITY_MGR.InitCache(put.p_entity_type,
74                                 put.p_entity_key_value);
75       else
76         --We locked the cache so we can exit the loop.
77         l_cacheLocked := TRUE;
78       end if;
79     end loop;
80   end if;
81 
82   --We acquired a lock on the entity_type/entity_key so we can update/create
83   --Attributes.
84   update wf_attribute_cache
85   set    attribute_value = nvl(p_value, '*NULL*'),
86          last_update_date = sysdate,
87          change_number = nvl(p_change_number, change_number)
88   where  entity_type = p_entity_type
89   and    entity_key_value = upper(p_entity_key_value)
90   and    attribute_name = upper(p_att);
91 
92   --No need to worry about race condition here because we have a lock on the
93   --entity_type/entity_key.
94   if SQL%notfound then
95     insert into wf_attribute_cache (
96       entity_type,
97       entity_key_value,
98       attribute_name,
99       attribute_value,
100       last_update_date,
101       change_number)
102         values
103      (p_entity_type,
104       upper(p_entity_key_value),
105       upper(p_att),
106       nvl(p_value, '*NULL*'),
107       sysdate,
108       p_change_number);
109   end if;
110 
111   if (wf_log_pkg.level_procedure >= fnd_log.g_current_runtime_level) then
112 
113       WF_LOG_PKG.String(WF_LOG_PKG.LEVEL_PROCEDURE,
114          'wf.plsql.WF_ENTITY_MGR.put',
115                        'End put('||p_entity_type||', '||
116                        p_entity_key_value||', '||p_att||', '||
117                        p_value||', '||p_change_number||')');
118   end if;
119 
120 end;
121 ------------------------------------------------------------------------------
122 /*
123 ** process_changes - <described in WFEMGRS.pls>
124 */
125 PROCEDURE process_changes(p_entity_type      in varchar2,
126                           p_entity_key_value in varchar2,
127                           p_change_source    in varchar2,
128                           p_change_type      in varchar2 ,
129                           p_event_name       in varchar2 )
130 is
131   my_user_base varchar2(256);
132   my_ent_type  varchar2(50) := upper(p_entity_type);
133   my_parms     wf_parameter_list_t;
134   my_key_value varchar2(256);
135 begin
136 
137   if (wf_log_pkg.level_procedure >= fnd_log.g_current_runtime_level) then
138 
139       WF_LOG_PKG.String(WF_LOG_PKG.LEVEL_PROCEDURE,
140          'wf.plsql.WF_ENTITY_MGR.process_changes',
141                        'Begin process_changes('||p_entity_type||', '||
142                        p_entity_key_value||', '||p_change_source||', '||
143                        p_change_type||', '||p_event_name||')');
144   end if;
145 
146   if (wf_entity_mgr.get_attribute_value(my_ent_type, p_entity_key_value,
147                                        'CACHE_CHANGED') = 'YES') then
148     --
149     -- Tell everybody about the change by raising the appropriate event
150     --
151     -- First, mark this entity in the cache as unchanged so we can pick up
152     -- any actual subsequent changes
153     --
154     wf_entity_mgr.put_attribute_value(my_ent_type, p_entity_key_value,
155                                      'CACHE_CHANGED', 'NO');
156     wf_event.AddParameterToList('CHANGE_SOURCE', p_change_source, my_parms);
157     wf_event.AddParameterToList('CHANGE_TYPE', p_change_type, my_parms);
158 
159     if (p_event_name is null) then
160       wf_event.raise('oracle.apps.global.'||lower(my_ent_type)||'.change',
161                      upper(p_entity_key_value), null, my_parms);
162     else
163       wf_event.raise(p_event_name, upper(p_entity_key_value), null, my_parms);
164     end if;
165   end if;
166 
167   if (wf_log_pkg.level_procedure >= fnd_log.g_current_runtime_level) then
168 
169       WF_LOG_PKG.String(WF_LOG_PKG.LEVEL_PROCEDURE,
170          'wf.plsql.WF_ENTITY_MGR.process_changes',
171                        'End process_changes('||p_entity_type||', '||
172                        p_entity_key_value||', '||p_change_source||', '||
173                        p_change_type||', '||p_event_name||')');
174   end if;
175 
176 end;
177 ------------------------------------------------------------------------------
178 /*
179 ** get_attribute_value - <described in WFEMGRS.pls>
180 */
181 FUNCTION get_attribute_value(p_entity_type      in varchar2,
182                              p_entity_key_value in varchar2,
183                              p_attribute        in varchar2) return varchar2
184 is
185   my_att_val  varchar2(4000);
186 begin
187 
188   if (wf_log_pkg.level_procedure >= fnd_log.g_current_runtime_level) then
189 
190       WF_LOG_PKG.String(WF_LOG_PKG.LEVEL_PROCEDURE,
191          'wf.plsql.WF_ENTITY_MGR. get_attribute_value',
192                        'Begin  get_attribute_value('||p_entity_type||', '||
193                        p_entity_key_value||', '||
194                        p_attribute||')');
195   end if;
196 
197   select attribute_value into my_att_val
198   from   wf_attribute_cache
199   where  entity_type = upper(p_entity_type)
200   and    entity_key_value = upper(p_entity_key_value)
201   and    attribute_name = upper(p_attribute);
202 
203   if (wf_log_pkg.level_procedure >= fnd_log.g_current_runtime_level) then
204 
205       WF_LOG_PKG.String(WF_LOG_PKG.LEVEL_PROCEDURE,
206          'wf.plsql.WF_ENTITY_MGR. get_attribute_value',
207                        'End  get_attribute_value('||p_entity_type||', '||
208                        p_entity_key_value||', '||
209                        p_attribute||')');
210   end if;
211 
212   return my_att_val;
213 exception when no_data_found then
214   if (wf_log_pkg.level_unexpected >= fnd_log.g_current_runtime_level) then
215       WF_LOG_PKG.String(WF_LOG_PKG.LEVEL_UNEXPECTED,
216                        'wf.plsql.WF_ENTITY_MGR. get_attribute_value',
217                        'Exception: '||sqlerrm);
218   end if;
219 
220   return '*UNKNOWN*';
221 end;
222 ------------------------------------------------------------------------------
223 /*
224 ** put_attribute_value - <described in WFEMGRS.pls>
225 */
226 PROCEDURE put_attribute_value(p_entity_type      in varchar2,
227                               p_entity_key_value in varchar2,
228                               p_attribute        in varchar2,
229                               p_attribute_value  in varchar2)
230 is
231   old_att_val varchar2(4000);
232   my_ent_type varchar2(50) := upper(p_entity_type);
233 begin
234 
235  if (wf_log_pkg.level_procedure >= fnd_log.g_current_runtime_level) then
236 
237       WF_LOG_PKG.String(WF_LOG_PKG.LEVEL_PROCEDURE,
238          'wf.plsql.WF_ENTITY_MGR. put_attribute_value',
239                      'Begin put_attribute_value('||p_entity_type||', '||
240                      p_entity_key_value||', '||
241                      p_attribute||', '||p_attribute_value||')');
242  end if;
243 
244   if (p_attribute = 'CACHE_CHANGED') then
245     wf_entity_mgr.put(my_ent_type,
246                       p_entity_key_value,
247                       'CACHE_CHANGED',
248                       p_attribute_value,
249                       NULL);
250 
251   elsif (p_attribute_value = '*UNKNOWN*') then
252     null;
253 
254   elsif (p_attribute_value is null) then
255     null;  -- means "do not update" like for fndload --
256            -- if you want to null out a value, use *NULL* --
257   else
258     old_att_val := wf_entity_mgr.get_attribute_value(my_ent_type,
259                                                      p_entity_key_value,
260                                                      p_attribute);
261     if (p_attribute_value <> old_att_val) then
262       wf_entity_mgr.put(my_ent_type,
263                         p_entity_key_value,
264                         p_attribute,
265                         p_attribute_value,
266                         NULL);
267     end if;
268   end if;
269  if (wf_log_pkg.level_procedure >= fnd_log.g_current_runtime_level) then
270 
271     WF_LOG_PKG.String(WF_LOG_PKG.LEVEL_PROCEDURE,
272          'wf.plsql.WF_ENTITY_MGR. put_attribute_value',
273                        'End put_attribute_value('||p_entity_type||', '||
274                        p_entity_key_value||', '||
275                        p_attribute||', '||p_attribute_value||')');
276 
277  end if;
278 
279 end;
280 ------------------------------------------------------------------------------
281 /*
282 ** flush_cache - <described in WFEMGRS.pls>
283 */
284 PROCEDURE flush_cache(p_entity_type      in varchar2 ,
285                       p_entity_key_value in varchar2 )
286 is
287   pragma autonomous_transaction;
288 begin
289   if (p_entity_type = '*ALL*') then
290     purge_cache_attributes(sysdate);
291   elsif ((p_entity_type is NOT NULL) and (p_entity_key_value is NOT NULL)) then
292     delete from wf_attribute_cache
293     where  entity_type = upper(p_entity_type)
294     and    entity_key_value = upper(p_entity_key_value);
295   else
296     delete from wf_attribute_cache
297     where  ((entity_type is NULL) or (entity_type = upper(p_entity_type)))
298     and    ((entity_key_value is NULL) or
299             (entity_key_value = upper(p_entity_key_value)));
300   end if;
301 
302   commit;
303 exception
304   when others then
305     wf_core.context('WF_ENTITY_MGR', 'flush_cache', p_entity_type);
306     raise;
307 end;
308 ------------------------------------------------------------------------------
309 /*
310 ** get_entity_type - <described in WFEMGRS.pls>
311 */
312 FUNCTION get_entity_type(p_event_name in varchar2) return varchar2 is
313   a  number := instr(p_event_name,'.',-1,2)+1;
314   b  number := instr(p_event_name,'.',-1) - a;
315 begin
316   return upper( substr(p_event_name, a, b) );
317 end;
318 ------------------------------------------------------------------------------
319 /*
320 ** gen_xml_payload - <described in WFEMGRS.pls>
321 */
322 FUNCTION gen_xml_payload(p_event_name in varchar2,
323                          p_event_key  in varchar2) return clob
324 is
325   my_clob     clob;
326   my_ent_type varchar2(50) := wf_entity_mgr.get_entity_type(p_event_name);
327   found       boolean := FALSE;
328 
329   l_doc      xmldom.DOMDocument;
330   l_root     xmldom.DOMNode;
331   l_node     xmldom.DOMNode;
332 
333   cursor attribute_data is
334     select attribute_name aname,
335            attribute_value avalue
336     from   wf_attribute_cache
337     where  entity_type = my_ent_type
338     and    entity_key_value = upper(p_event_key)
339     and    attribute_name <> 'CACHE_CHANGED';
340 
341 begin
342   l_doc  := xmldom.newDOMDocument;
343   l_root := xmldom.makeNode(l_doc);
344   l_root := wf_event_xml.newtag(l_doc, l_root, my_ent_type);
345 
346   for atts in attribute_data loop
347     l_node := wf_event_xml.newtag(l_doc, l_root, atts.aname, atts.avalue);
348     found := TRUE;
349   end loop;
350 
351   if (found) then
352     dbms_lob.createtemporary(my_clob, FALSE);
353     xmldom.writeToClob(l_root, my_clob);
354     return my_clob;
355   else
356     return null;
357   end if;
358 end;
359 ---------------------------------------------------------------------------
360 /*
361 ** isChanged - <Described in WFEMGRS.pls>
362 */
363 FUNCTION isChanged(p_new_val in varchar2,
364                    p_old_val in varchar2) return boolean is
365   retval boolean := FALSE;
366 begin
367   if (p_new_val = '*UNKNOWN*') then
368     retval := FALSE;
369   elsif (p_new_val <> p_old_val) then
370     retval := TRUE;
371   end if;
372 
373   return retval;
374 end;
375 
376 ------------------------------------------------------------------------------
377 /*
378 ** purge_entities - Described in WFEMGRS.pls
379 */
380 PROCEDURE purge_cache_attributes (p_enddate date) is
381 
382 begin
383   delete from WF_ATTRIBUTE_CACHE
384   where LAST_UPDATE_DATE <= p_enddate;
385 exception
386   when no_data_found then
387     null;
388   when others then
389     wf_core.context('WF_ENTITY_MANAGER', 'purge_cache_attributes', p_enddate);
390     raise;
391 end;
392 ------------------------------------------------------------------------------
393 end WF_ENTITY_MGR;