Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
mm_updateappextension.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Add or update an extension to an application component
4  @details A SAS Application (SoftwareComponent) is a great place to store app
5  specific parameters. There are two main places those params can be stored:
6  1) Configuration, and 2) Extensions. The second location will enable end
7  users to modify parameters even if they don't have the Configuration Manager
8  plugin in SMC. This macro can be used after creating an application with
9  the mm_createapplication.sas macro. If a parameter with the same name
10  exists, it is updated. If it does not, it is created.
11 
12  Usage:
13 
14  %mm_updateappextension(app=/my/metadata/path/myappname
15  ,paramname=My Param
16  ,paramvalue=My value
17  ,paramdesc=some description)
18 
19 
20  @param app= the BIP Tree folder path plus Application Name
21  @param paramname= Parameter name
22  @param paramvalue= Parameter value
23  @param paramdesc= Parameter description
24 
25  @param frefin= change default inref if it clashes with an existing one
26  @param frefout= change default outref if it clashes with an existing one
27  @param mDebug= set to 1 to show debug messages in the log
28 
29  @version 9.4
30  @author Allan Bowe
31 
32 **/
33 
34 %macro mm_updateappextension(app=
35  ,paramname=
36  ,paramvalue=
37  ,paramdesc=Created by mm_updateappextension
38  ,frefin=inmeta,frefout=outmeta
39  , mdebug=0);
40 
41 
42 /* first, check if app (and param) exists */
43 %local appuri exturi;
44 %let appuri=stopifempty;
45 %let exturi=createifempty;
46 
47 data _null_;
48  format type uri tsuri value $200.;
49  call missing (of _all_);
50  paramname=symget('paramname');
51  path="&app(Application)";
52  /* first, find the STP ID */
53  if metadata_pathobj("",path,"Application",type,uri)>0 then do;
54  /* we have an app in this location! */
55  call symputx('appuri',uri,'l');
56  cnt=1;
57  do while (metadata_getnasn(uri,"Extensions",cnt,tsuri)>0);
58  rc=metadata_getattr(tsuri,"Name",value);
59  put tsuri= value=;
60  if value=paramname then do;
61  putlog "&sysmacroname: found existing param - " tsuri;
62  rc=metadata_getattr(tsuri,"Id",value);
63  call symputx('exturi',value,'l');
64  stop;
65  end;
66  cnt+1;
67  end;
68  end;
69  else put (_all_)(=);
70 run;
71 
72 %if &appuri=stopifempty %then %do;
73  %put WARNING: &app.(Application) not found!;
74  %return;
75 %end;
76 
77 /* escape the description so it can be stored as XML */
78 data _null_;
79  length outstr $32767;
80  outstr=symget('paramdesc');
81  outstr=tranwrd(outstr,'&','&');
82  outstr=tranwrd(outstr,'<','&lt;');
83  outstr=tranwrd(outstr,'>','&gt;');
84  outstr=tranwrd(outstr,"'",'&apos;');
85  outstr=tranwrd(outstr,'"','&quot;');
86  outstr=tranwrd(outstr,'0A'x,'&#10;');
87  outstr=tranwrd(outstr,'0D'x,'&#13;');
88  outstr=tranwrd(outstr,'$','&#36;');
89  call symputx('paramdesc',outstr,'l');
90 run;
91 
92 filename &frefin temp;
93 
94 %if &exturi=createifempty %then %do;
95  /* write header XML */
96  data _null_;
97  file &frefin;
98  pname=quote(trim(symget('paramname')));
99  pdesc=quote(trim(symget('paramdesc')));
100  pvalue=quote(trim(symget('paramvalue')));
101  put "<UpdateMetadata><Reposid>$METAREPOSITORY</Reposid><Metadata>"/
102  " <SoftwareComponent id='&appuri' ><Extensions>" /
103  ' <Extension Name=' pname ' Desc=' pdesc ' value= ' pvalue ' />' /
104  ' </Extensions></SoftwareComponent>'/
105  '</Metadata><NS>SAS</NS><Flags>268435456</Flags></UpdateMetadata>';
106  run;
107 
108 %end;
109 %else %do;
110  data _null_;
111  file &frefin;
112  pdesc=quote(trim(symget('paramdesc')));
113  pvalue=quote(trim(symget('paramvalue')));
114  put "<UpdateMetadata><Reposid>$METAREPOSITORY</Reposid><Metadata>"/
115  " <Extension id='&exturi' Desc=" pdesc ' value= ' pvalue ' />' /
116  '</Metadata><NS>SAS</NS><Flags>268435456</Flags></UpdateMetadata>';
117  run;
118 %end;
119 
120 filename &frefout temp;
121 
122 proc metadata in= &frefin out=&frefout verbose;
123 run;
124 
125 %if &mdebug=1 %then %do;
126  /* write the response to the log for debugging */
127  data _null_;
128  infile &frefout lrecl=1048576;
129  input;
130  put _infile_;
131  run;
132 %end;
133 
134 %mend;