Loading...
Searching...
No Matches
stagedata.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Sends a changeset to staging area
4 @details This is the service that is called when submitting a new edit.
5
6 <h4> Service Inputs </h4>
7 <h5> jsdata </h5>
8 This is the staged data table, plus an _____DELETE__THIS__RECORD_____ column
9
10 <h5> SASControlTable </h5>
11
12 |ACTION:$char4.|MESSAGE:$char1.|LIBDS:$char19.|
13 |---|---|---|
14 |LOAD|User-Provided message|LIBREF.DATASET_NAME|
15
16 <h4> SAS Macros </h4>
17 @li mf_getuser.sas
18 @li mf_nobs.sas
19 @li dc_assignlib.sas
20 @li mf_verifymacvars.sas
21 @li mf_mkdir.sas
22 @li mf_getuniquefileref.sas
23 @li mpe_loader.sas
24 @li mpe_filtermaster.sas
25 @li mp_abort.sas
26 @li mp_binarycopy.sas
27 @li mp_cntlout.sas
28 @li mp_ds2csv.sas
29 @li mf_getplatform.sas
30 @li removecolsfromwork.sas
31 @li mpeinit.sas
32
33
34 @version 9.2
35 @author 4GL Apps Ltd
36 @copyright 4GL Apps Ltd. This code may only be used within Data Controller
37 and may not be re-distributed or re-sold without the express permission of
38 4GL Apps Ltd.
39
40**/
41
42%mpeinit()
43
44%global approver; %let approver=;
45%global libref; %let libref=;
46%global dsn; %let dsn=;
47%global user; %let user=;
48%let user=%mf_getuser();
49
50/* load parameters */
51data _null_;
52 set work.sascontroltable;
53 call symputx('action',action);
54 /* sanitise message to prevent code injection */
55 message=compress(message, '&%;');
56 call symputx('message',message);
57 libds=upcase(libds);
58 call symputx('orig_libds',libds);
59 is_fmt=0;
60 if substr(cats(reverse(libds)),1,3)=:'CF-' then do;
61 libds=scan(libds,1,'-');
62 putlog "Format Catalog Captured";
63 libds='work.fmtextract';
64 call symputx('libds',libds);
65 is_fmt=1;
66 end;
67 else call symputx('libds',libds);
68 call symputx('is_fmt',is_fmt);
69 putlog (_all_)(=);
70run;
71
72%mp_cntlout(
73 iftrue=(&is_fmt=1)
74 ,libcat=&orig_libds
75 ,fmtlist=0
76 ,cntlout=work.fmtextract
77)
78
79/* stream back meta info, further jquery calls will return col metadata and
80 actual data */
81%let libref=%upcase(%scan(&libds,1,.));
82%let dsn=%upcase(%scan(&libds,2,.));
83%dc_assignlib(WRITE,&libref)
84%mp_abort(iftrue= (&syscc ne 0)
85 ,mac=&_program
86 ,msg=%str(syscc=&syscc - unable to assign library &libref)
87)
88
89%mp_abort(
90 iftrue=(%mf_verifymacvars(mpelocapprovals libds)=0)
91 ,mac=&_program
92 ,msg=%str(Missing: mpelocapprovals libds)
93)
94
95%put Verify that the upload does not violate Row Level Security checks:;
96%mpe_filtermaster(ULOAD,&libds,
97 dclib=&mpelib,
98 outref=filtref,
99 outds=work.query
100)
101
102%mp_abort(iftrue= (&syscc ne 0)
103 ,mac=&_program
104 ,msg=%str(syscc=&syscc during filtering process)
105)
106
107/* prepare inverse query */
108%let tempref=%mf_getuniquefileref();
109data _null_;
110 infile filtref end=eof;
111 file &tempref;
112 if _n_=1 then put 'where not(';
113 input;
114 put _infile_;
115 if eof then put ')';
116run;
117/* apply the query */
118data work.badrecords;
119 set work.jsdata;
120 %inc &tempref/source2;;
121 putlog (_all_)(=);
122run;
123%mp_abort(iftrue= (%mf_nobs(work.badrecords)>0)
124 ,mac=&_program
125 ,msg=%str(
126 Security Problem - %mf_nobs(work.badrecords) unauthorised records submitted
127 )
128)
129
130PROC FORMAT;
131 picture yymmddhhmmss other='%0Y%0m%0d_%0H%0M%0S' (datatype=datetime);
132RUN;
133
134/**
135 * Create package folder and redirect the log
136 */
137
138/* create a dataset key (datetime plus 6 digit random number plus PID) */
139%let mperef=DC%left(%sysfunc(datetime(),B8601DT19.3))_%substr(
140 %sysfunc(ranuni(0)),3,6)_%substr(%str(&sysjobid ),1,4);
141
142/* get web url */
143%global url;
144%let url=localhost/SASStoredProcess;
145%let platform=%mf_getplatform();
146%put &=platform;
147data _null_;
148 length url $128;
149
150%macro stagedata();
151 %if &platform=SASVIYA %then %do;
152 if symexist('_baseurl') then do;
153 url=symget('_baseurl');
154 if subpad(url,length(url)-9,9)='SASStudio'
155 then url=substr(url,1,length(url)-11);
156 else url="&systcpiphostname/SASJobExecution";
157 end;
158 else url="&systcpiphostname/SASJobExecution";
159 %end;
160 %else %if &platform=SASMETA %then %do;
161 rc=METADATA_GETURI("Stored Process Web App",url);
162 %end;
163%mend stagedata;
164%stagedata()
165
166 call symputx('url',url);
167 putlog url=;
168run;
169
170/* Create package folder */
171%let dir=&mpelocapprovals/&mperef;
172%mf_mkdir(&dir)
173
174/* redirect the log */
175%put; %put; %put log is being redirected;
176%put to retrieve, visit this url:; %put;%put;
177%let url=&url?_program=%substr(&_program
178 ,1,%length(&_program)-9)getlog%str(&)table=&mperef;
179%put &url;
180
181%mp_abort(iftrue= (&syscc ne 0)
182 ,mac=&_program
183 ,msg=%str(syscc=&syscc prior to log redirection)
184)
185
186proc printto log="&dir/weblog.txt";run;
187options notes mprint;
188libname approve "&dir";
189
190/* take copy of webin file */
191data _null_;
192 if symexist('_WEBIN_FILEREF1') then ref=symget('_WEBIN_FILEREF1');
193 else if symexist('sasjs_tables') then ref='0ref'; /* no fileref created */
194 else ref='indata1';
195 call symputx('ref',ref);
196 putlog ref=;
197run;
198
199%mp_binarycopy(inref=&ref,outloc="&dir/_WEBIN_FILEREF1.txt",iftrue=&ref ne 0ref)
200
201
202/* take copy of macvars */
203data _null_;
204 file "&dir/macvars.sas";
205 set sashelp.vmacro;
206 where scope='GLOBAL';
207 put '%let ' name '=' value ';';
208run;
209
210data approve.jsdset;
211 length _____DELETE__THIS__RECORD_____ $3;
212 set jsdata;
213run;
214
215/**
216 * mf_getvarXXX functions will fail if the target is locked - so take a copy
217 * and reference that (this will also explicitly throw the lock situation)
218 */
219%let dscopy=work.dscopy;
220data &dscopy;
221 set &libds;
222 stop;
223run;
224%mp_abort(iftrue= (&syscc ne 0)
225 ,mac=&_program
226 ,msg=%str(Issue getting lock on &libds)
227)
228
229%mp_ds2csv(approve.jsdset
230 ,dlm=COMMA
231 ,outfile="&dir/&orig_libds..csv"
232 ,outencoding="UTF-8"
233 ,headerformat=NAME
234 ,termstr=CRLF
235)
236
237%mp_abort(iftrue= (&syscc ne 0)
238 ,mac=&_program
239 ,msg=%str(syscc=&syscc when writing the CSV)
240)
241
242%mpe_loader(mperef=&mperef
243 ,submitted_reason_txt=%superq(message)
244 ,approver=%quote(%trim(&approver))
245 ,url=%superq(url)
246 ,dc_dttmtfmt=&dc_dttmtfmt
247)
248%mp_abort(mode=INCLUDE)
249
250%mp_abort(
251 iftrue=(%sysfunc(fileexist(%sysfunc(pathname(work))/mf_abort.error))=1)
252 ,mac=&_program..sas
253 ,msg=%str(mf_abort.error=1)
254)
255
256%mp_abort(iftrue= (&syscc ne 0)
257 ,mac=&_program..sas
258 ,msg=%str(syscc=&syscc)
259)
260
261/* send relevant SUCCESS values */
262data sasparams;
263STATUS='SUCCESS';
264DSID="&mperef";
265url="&url";
266run;
267
268%removecolsfromwork(___TMP___MD5)
269
270%webout(OPEN)
271%webout(OBJ,sasparams)
272%webout(CLOSE)
273
274%mpeterm()