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