版权声明:本文为博主汪子熙原创文章,未经博主允许不得转载。 https://cloud.tencent.com/developer/article/1439493
Output parameter definition:
Input parameter definition:
Procedure script:
/********* Begin Procedure Script ************/
BEGIN
/*Procedure output is a table with structure of "SocialPostUUID" + "SentimentCode"(e.g. SP, WP...)*/
ET_SENTIMENT =
SELECT UUID,
CASE WHEN TA_NUM = 0 THEN 'NE'
WHEN TA_NUM <= 1 AND TA_NUM > 0 THEN 'WP'
WHEN TA_NUM <= 2 AND TA_NUM > 1 THEN 'SP'
WHEN TA_NUM < 0 AND TA_NUM >= -1 THEN 'WN'
WHEN TA_NUM < -1 AND TA_NUM >= -2 THEN 'SN'
WHEN TA_NUM = 3 THEN 'NA'
ELSE 'NA'END AS SENTI_CODE
FROM (
/*Secondly assign an integer value (from -2 to 2) to each TA_TYPE.
For no TA_TYPE entries(NULL), assign 3 as an indicator. Afterward calculate average for each post*/
SELECT UUID, SUM (CASE WHEN TA_TYPE = 'NeutralSentiment' OR TA_TYPE = 'NeutralEmoticon' THEN 0
WHEN TA_TYPE = 'StrongNegativeSentiment' OR TA_TYPE = 'MajorProblem' OR TA_TYPE = 'StrongNegativeEmoticon' THEN -2
WHEN TA_TYPE = 'StrongPositiveSentiment' OR TA_TYPE = 'StrongPositiveEmoticon' THEN 2
WHEN TA_TYPE = 'WeakNegativeSentiment' OR TA_TYPE = 'MinorProblem' OR TA_TYPE = 'WeakNegativeEmoticon' THEN -1
WHEN TA_TYPE = 'WeakPositiveSentiment' OR TA_TYPE = 'WeakPositiveEmoticon' THEN 1
WHEN TA_TYPE IS NULL THEN 3
ELSE 0 END)/COUNT(2) AS TA_NUM
FROM (
/*firstly join input UUID with $TA table and pick up entries only with specified TA_TYPE*/
SELECT A.UUID, B.TA_TYPE
FROM :IT_UUID A LEFT JOIN "$TA_INDEXVOICEOFCUST" B ON A.UUID = B.SOCIALDATAUUID
AND B.TA_TYPE in (
'NeutralSentiment' ,
'StrongNegativeSentiment' ,
'StrongPositiveSentiment',
'WeakNegativeSentiment' ,
'WeakPositiveSentiment',
'MinorProblem',
'MajorProblem',
'StrongPositiveEmoticon',
'WeakPositiveEmoticon',
'WeakNegativeEmoticon',
'NeutralEmoticon',
'StrongNegativeEmoticon')
)
GROUP BY UUID
);
END;
/********* End Procedure Script ************/
method if_soc_senti_badi~calculate_sentiment.
*The prerequisite of this BAdI implementation is a pre-defined HANA database procedure as well as the generated procedure proxy.
*In the following example, the HANA database procedure is ZDP_SENTIMENT_ASSIGNMENT.
*The relevant procedure proxy is ZDPP_SENTIMENT_ASSIGNMENT, and its interface ZIF_ZDPP_SENTIMENT_ASSIGNMENT is defined as:
*interface ZIF_ZDPP_SENTIMENT_ASSIGNMENT public.
*types: begin of it_uuid,
* uuid type c length 32,
* end of it_uuid.
*types: begin of et_sentiment,
* uuid type c length 32,
* senti_code type c length 2,
* end of et_sentiment.
*endinterface .
DATA: lt_uuid TYPE STANDARD TABLE OF zif_zdpp_sentiment_assignment=>it_uuid,
ls_sentiment TYPE zif_zdpp_sentiment_assignment=>et_sentiment,
lt_sentiment TYPE STANDARD TABLE OF zif_zdpp_sentiment_assignment=>et_sentiment.
*prepare uuid list of social posts to be calculated for sentiment.
LOOP AT ct_social_data ASSIGNING FIELD-SYMBOL(<line>).
APPEND <line>-uuid TO lt_uuid.
ENDLOOP.
*Wait 5 seconds for HANA TA processing
WAIT UP TO 5 SECONDS.
*Pass uuid list to database procedure proxy. database procedure/proxy is defined in HDB
TRY .
CALL DATABASE PROCEDURE zdpp_sentiment_assignment
EXPORTING it_uuid = lt_uuid
IMPORTING et_sentiment = lt_sentiment.
CATCH cx_sy_db_procedure_sql_error.
* DB procedure has issue or does not exist at all!
ENDTRY.
*Fill sentiment into changing table.
*If entry is missing in lt_sentiment(this case rarely happens), fill sentiment 'NA' - Not Applicable.
LOOP AT ct_social_data ASSIGNING <line>.
READ TABLE lt_sentiment INTO ls_sentiment WITH KEY uuid = <line>-uuid.
IF sy-subrc = 0.
<line>-sentiment = ls_sentiment-senti_code.
ELSE.
<line>-sentiment = 'NA'.
ENDIF.
ENDLOOP.