| 1 | package CGI::AppBuilder::Common; | ||
| 2 | |||
| 3 | # Perl standard modules | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Getopt::Std; | ||
| 7 | use POSIX qw(strftime); | ||
| 8 | use Carp; | ||
| 9 | use CGI; | ||
| 10 | use CGI::AppBuilder; | ||
| 11 | use CGI::AppBuilder::Message qw(:echo_msg); | ||
| 12 | use File::Path; | ||
| 13 | use Net::Rexec 'rexec'; | ||
| 14 | use File::Basename; | ||
| 15 | |||
| 16 | our $VERSION = 0.12; | ||
| 17 | require Exporter; | ||
| 18 | our @ISA = qw(Exporter CGI::AppBuilder); | ||
| 19 | our @EXPORT = qw(); | ||
| 20 | our @EXPORT_OK = qw(new_form disp_task_form disp_help | ||
| 21 | set_args cc_andwhere | ||
| 22 | ); | ||
| 23 | our %EXPORT_TAGS = ( | ||
| 24 | usrforms => [qw(disp_usr_form)], | ||
| 25 | all => [@EXPORT_OK] | ||
| 26 | ); | ||
| 27 | |||
| 28 | =head1 NAME | ||
| 29 | |||
| 30 | CGI::AppBuilder::Common - Create or display Task froms | ||
| 31 | |||
| 32 | =head1 SYNOPSIS | ||
| 33 | |||
| 34 | use CGI::AppBuilder::Common; | ||
| 35 | |||
| 36 | my $sec = CGI::AppBuilder::Common->new(); | ||
| 37 | |||
| 38 | |||
| 39 | =head1 DESCRIPTION | ||
| 40 | |||
| 41 | This class provides methods for reading and parsing configuration | ||
| 42 | files. | ||
| 43 | |||
| 44 | =cut | ||
| 45 | |||
| 46 | =head2 new (ifn => 'file.cfg', opt => 'hvS:') | ||
| 47 | |||
| 48 | This is a inherited method from CGI::AppBuilder. See the same method | ||
| 49 | in CGI::AppBuilder for more details. | ||
| 50 | |||
| 51 | =cut | ||
| 52 | |||
| 53 | sub new { | ||
| 54 | my ($s, %args) = @_; | ||
| 55 | return $s->SUPER::new(%args); | ||
| 56 | } | ||
| 57 | |||
| 58 | sub disp_help { | ||
| 59 | my ($s, $ar) = @_; | ||
| 60 | |||
| 61 | my $prg = 'AppBuilder::Common->disp_help'; | ||
| 62 | my $ifn = $s->set_param('ifn', $ar); # root init file: jp2.txt | ||
| 63 | my $ds = $s->set_param('dir_sep', $ar); # directory separator | ||
| 64 | my $pid = $s->set_param('pid', $ar); # project id | ||
| 65 | my $tsk = $s->set_param('task', $ar); # task name | ||
| 66 | my $tmp = $ifn; $tmp =~ s/\.(\w+)$//; | ||
| 67 | my $tf2 = join $ds, $tmp, $pid, "$tsk.txt"; # task file name: jp2/ckpt/tsk1.txt | ||
| 68 | my $p = {}; | ||
| 69 | if (! -f $tf2) { | ||
| 70 | $s->echo_msg("Warn: ($prg) did not find - $tf2.", 0); | ||
| 71 | } else { | ||
| 72 | $p = $s->read_init_file($tf2); | ||
| 73 | } | ||
| 74 | my $k = "${tsk}_help"; | ||
| 75 | if (! exists $p->{$k}) { | ||
| 76 | $s->echo_msg("Warn: ($prg) no help is defined for this task ($tsk).", 1); | ||
| 77 | } else { | ||
| 78 | $p->{$k} =~ s/(declare|begin)/$1<br>\n/ig; | ||
| 79 | $p->{$k} =~ s/;/;<br>\n/g; | ||
| 80 | print $p->{$k}; | ||
| 81 | } | ||
| 82 | print "</body>\n</html>\n"; | ||
| 83 | } | ||
| 84 | |||
| 85 | =head2 cc_andwhere ($str,$obj,$typ,$esc) | ||
| 86 | |||
| 87 | Input variables: | ||
| 88 | |||
| 89 | $str - string containing object pattern | ||
| 90 | $obj - object name such as column name | ||
| 91 | $typ - type: where, and or andnot | ||
| 92 | $esc - where to escape the character | ||
| 93 | |||
| 94 | Variables used or routines called: | ||
| 95 | |||
| 96 | None | ||
| 97 | |||
| 98 | How to use: | ||
| 99 | |||
| 100 | Return: None | ||
| 101 | |||
| 102 | =cut | ||
| 103 | |||
| 104 | sub cc_andwhere { | ||
| 105 | my ($s, $str, $obj, $typ, $esc) = @_; | ||
| 106 | |||
| 107 | return "" if !$str || !$obj; | ||
| 108 | $str =~ s/\s*,\s*/','/g; # remove space and replace "," with "','" | ||
| 109 | $typ = uc $typ; | ||
| 110 | my $whr = ""; | ||
| 111 | if ($typ =~ /^AND$/i) { | ||
| 112 | $whr .= " $typ $obj "; | ||
| 113 | } elsif ($typ =~ /^ANDNOT/i) { | ||
| 114 | if (index($str,',') < 0 && index($str,'%') < 0) { | ||
| 115 | $whr .= " AND $obj "; | ||
| 116 | } else { | ||
| 117 | $whr .= " AND $obj NOT "; | ||
| 118 | } | ||
| 119 | } else { | ||
| 120 | $whr .= " $typ $obj "; | ||
| 121 | } | ||
| 122 | if (index($str,',') > -1 ) { | ||
| 123 | $whr .= " IN ('$str') "; | ||
| 124 | } elsif (index($str,'%') > -1) { | ||
| 125 | $whr .= " LIKE '$str' "; | ||
| 126 | } else { | ||
| 127 | $whr .= ($typ =~ /^ANDNOT/i) ? " <> UPPER('$str') " : " = UPPER('$str') "; | ||
| 128 | } | ||
| 129 | $whr .= " ESCAPE '$esc' " if ($esc && index($str,'%') > 0); | ||
| 130 | return $whr; | ||
| 131 | } | ||
| 132 | |||
| 133 | =head2 new_form ($q,$ar) | ||
| 134 | |||
| 135 | Input variables: | ||
| 136 | |||
| 137 | $q - CGI class | ||
| 138 | $ar - array ref containing the following variables: | ||
| 139 | |||
| 140 | Variables used or routines called: | ||
| 141 | |||
| 142 | None | ||
| 143 | |||
| 144 | How to use: | ||
| 145 | |||
| 146 | Return: None | ||
| 147 | |||
| 148 | History: mm/dd/yyyy (developer) - description | ||
| 149 | |||
| 150 | 03/28/2011 (htu) - added $add_check, $f_ir, $f_ip, and $add_test | ||
| 151 | 04/02/2012 (htu) - added $f_if in new_form | ||
| 152 | 07/22/2013 (htu) - added id for <tr> and JS:<js_code> type | ||
| 153 | |||
| 154 | =cut | ||
| 155 | |||
| 156 | sub new_form { | ||
| 157 | my ($s, $q, $ar) = @_; | ||
| 158 | |||
| 159 | my $prg = 'AppBuilder::Common->new_form'; | ||
| 160 | my $tsk = $ar->{new_task}; # task: add_study | ||
| 161 | my $amg = eval $s->set_param('arg_msgs',$ar); # arg msgs | ||
| 162 | |||
| 163 | if (exists $ar->{guid}) { | ||
| 164 | my ($usr_sid,$usr_uid,$usr_tmo) = split /:/, $ar->{guid}; | ||
| 165 | $ar->{user_sid} = $usr_sid if $usr_sid =~ /^\d+$/; | ||
| 166 | $ar->{user_uid} = $usr_uid if $usr_uid; | ||
| 167 | $ar->{user_tmo} = $usr_tmo if $usr_tmo; | ||
| 168 | } | ||
| 169 | $ar->{encoding} = 'multipart/form-data' if ($tsk && $tsk =~ /^upload_file/i); | ||
| 170 | |||
| 171 | if (!exists $amg->{$tsk}) { | ||
| 172 | $s->echo_msg("ERR: ($prg) could not find new task - $tsk.",0); | ||
| 173 | return; | ||
| 174 | } | ||
| 175 | my ($pid,$sid,$t,$t1,$t2) = (); | ||
| 176 | $pid = $ar->{pid} if (exists $ar->{pid} && $ar->{pid}); | ||
| 177 | $sid = $ar->{sid} if (exists $ar->{sid} && $ar->{sid}); | ||
| 178 | $sid = $ar->{study_id} if (!$sid && exists $ar->{study_id}); | ||
| 179 | $sid = $ar->{sel_sn1} if (!$sid && exists $ar->{sel_sn1}); | ||
| 180 | my $usr_gid = (exists $ar->{guid}) ? $ar->{guid} : ""; | ||
| 181 | my $ksb = eval $s->set_param('var2sub',$ar); | ||
| 182 | my $far = $amg->{$tsk}; # form message | ||
| 183 | my $cls = eval $s->set_param('code_lists',$ar); # code lists | ||
| 184 | |||
| 185 | my $f_in = "<input name=\"%s\" value=\"%s\" />"; | ||
| 186 | my $f_ih = " <input type=\"hidden\" name=\"%s\" value=\"%s\" />\n"; | ||
| 187 | my $f_ir = " <input name=\"%s\" value=\"%s\" readonly/>\n"; | ||
| 188 | my $f_ip = " <input type=\"password\" name=\"%s\" value=\"%s\" />\n"; | ||
| 189 | my $f_if = " <input type=\"%s\" name=\"%s\" />\n"; | ||
| 190 | my $f_st = "\n<select name=\"\%s\" class='formField' %s>\n%s</select>\n "; | ||
| 191 | my $f_sm = "\n<select name=\"\%s\" multiple='multiple' %s>\n%s</select>\n "; | ||
| 192 | my $f_op = " <option value=\"%s\">%s</option>\n"; | ||
| 193 | my $f_os = " <option selected value=\"%s\">%s</option>\n"; | ||
| 194 | my $f_tr = "<tr id='%s'>\n <td>%s</td>\n <td>%s</td>\n <td>%s</td>\n</tr>\n"; | ||
| 195 | my $f_tb = "<table align=center>\n<caption>%s</caption>\n%s\n</table>\n"; | ||
| 196 | my $f_fm = "<form method=\"$ar->{method}\" action=\"$ar->{action}?\" "; | ||
| 197 | $f_fm .= "enctype=\"$ar->{encoding}\" name=\"oraForm\" "; | ||
| 198 | $f_fm .= "target=\"%s\">\n%s\n</form>\n"; | ||
| 199 | |||
| 200 | my $title = $tsk; $title =~ s/_/ /g; $title = '<b>' . uc($title) . '</b>'; | ||
| 201 | my $add_check = 0; | ||
| 202 | my $add_test = 0; | ||
| 203 | my $test_label = ''; | ||
| 204 | my $chk_label = ''; | ||
| 205 | |||
| 206 | # $s->disp_param($ar); | ||
| 207 | |||
| 208 | for my $i (0..$#$far) { # each variable | ||
| 209 | my $k = $far->[$i][0]; # name/key: study_id | ||
| 210 | my $m = $far->[$i][1]; # message | ||
| 211 | my $d = $far->[$i][2]; # default value | ||
| 212 | my $n = $far->[$i][3]; # desc/required | ||
| 213 | my ($k1,$k2) = ($n =~ /^([^:]+):?(.*)?/); | ||
| 214 | |||
| 215 | if ($k =~ /^a/i && $n =~ /^check/i) { | ||
| 216 | ++$add_check; $chk_label = ucfirst($n); next; | ||
| 217 | } | ||
| 218 | if ($k =~ /^a/i && $n =~ /^test/i) { | ||
| 219 | ++$add_test; $test_label = ucfirst($n); next; | ||
| 220 | } | ||
| 221 | if ($n && $n =~ /^hidden/i) { | ||
| 222 | $d = (!$d && exists $ar->{$k}) ? $ar->{$k} : $d; | ||
| 223 | $t .= sprintf $f_ih, $k, $d; next; | ||
| 224 | } | ||
| 225 | if (exists $ksb->{$k}) { | ||
| 226 | my $sub = $ksb->{$k}; | ||
| 227 | my $kkk = $s->$sub($ar); | ||
| 228 | $cls->{$k} = $kkk; | ||
| 229 | # print "Sub: $sub, $k, $kkk<br>\n"; | ||
| 230 | # $s->disp_param($cls->{$k}); | ||
| 231 | } | ||
| 232 | if (exists $cls->{$k}) { # check code list | ||
| 233 | my $a2 = $cls->{$k}; # array ref | ||
| 234 | $t2 = ''; | ||
| 235 | my $n1 = ($n =~ /^multiple/i) ? 1 : 0; | ||
| 236 | for my $j ($n1..$#$a2) { # key,value,default? | ||
| 237 | # [1 , 'Yes' , 1], | ||
| 238 | my $v0 = $a2->[$j][0]; $v0 =~ s/\n*$//g; | ||
| 239 | my $v1 = $a2->[$j][1]; $v1 =~ s/\n*$//g; | ||
| 240 | if (exists $a2->[$j][2] && $a2->[$j][2]) { | ||
| 241 | $t2 .= sprintf $f_os, $v0, $v1; | ||
| 242 | } else { | ||
| 243 | $t2 .= sprintf $f_op, $v0, $v1; | ||
| 244 | } | ||
| 245 | } | ||
| 246 | my $k3 = ($k1 && $k1 =~ /^(js|javascript)/i) ? $k2 : ''; | ||
| 247 | if ($n && $n =~ /^multiple/i) { | ||
| 248 | $t1 = sprintf $f_sm, $k, $k3, $t2; | ||
| 249 | } else { | ||
| 250 | $t1 = sprintf $f_st, $k, $k3, $t2; | ||
| 251 | } | ||
| 252 | $n = ($k1 && $k1 =~ /^(js|javascript)/i) ? '' : $n; | ||
| 253 | } else { | ||
| 254 | # print "$k = $ar->{$k}<br>\n"; | ||
| 255 | my $v = (exists $ar->{$k} && ($ar->{$k}||$ar->{$k} =~ /^\d+$/)) ? $ar->{$k} : ''; | ||
| 256 | $v = $d if !$v && $d; | ||
| 257 | # $s->echo_msg("WARN: ($prg) no value provided for $k", 1) if !$v && $n =~/^\*/; | ||
| 258 | if ($n =~ /^readonly/i) { | ||
| 259 | $t1 = sprintf $f_ir, $k, $v; | ||
| 260 | } elsif ($n =~ /^(pwd|password)/i) { | ||
| 261 | $t1 = sprintf $f_ip, $k, $v; | ||
| 262 | } elsif ($n =~ /^(js|javacript)/i) { | ||
| 263 | $t1 = sprintf $f_ip, $k, $v; | ||
| 264 | } elsif ($n =~/^(file)/i) { | ||
| 265 | $t1 = sprintf $f_if, $1, $k; | ||
| 266 | } else { | ||
| 267 | $t1 = sprintf $f_in, $k, $v; | ||
| 268 | } | ||
| 269 | # print "n=$n; K1=$k1; K2=$k2\n"; | ||
| 270 | $n = ($k2) ? $k2 : ( | ||
| 271 | ($k1 && $k1 =~ /^(pwd|password|readonly|js|javascript|file)/i) ? '' : $n); | ||
| 272 | } | ||
| 273 | $t .= sprintf $f_tr, "tr_$k", $m, $t1, $n; | ||
| 274 | } | ||
| 275 | |||
| 276 | $t .= "<tr align=center>\n <td colspan=3>\n"; | ||
| 277 | $t .= "* indicates required fields"; | ||
| 278 | $t .= " <input type='submit' name='a' value='Go' />\n"; | ||
| 279 | $t .= " <input type='reset' name='.reset' />\n"; | ||
| 280 | $t .= " <input type='submit' name='a' value='Help' />\n"; | ||
| 281 | $t .= " <input type='submit' name='a' value='$chk_label' />\n" | ||
| 282 | if ($add_check || $tsk =~ /^(run_cptable|run_cfgstudy)/i); | ||
| 283 | $t .= " <input type='submit' name='a' value='$test_label' />\n" | ||
| 284 | if $add_test; | ||
| 285 | $t .= " </td>\n</tr>\n"; | ||
| 286 | # $t .= "<tr>\n <td colspan=3>* indicates required fields</td>\n</tr>\n"; | ||
| 287 | my $tb = sprintf $f_tb, $title, $t; | ||
| 288 | $t1 = sprintf $f_ih, "pid", $pid if $pid || $pid =~ /^0$/; | ||
| 289 | $t1 .= sprintf $f_ih, "sel_sn1", $sid if $sid || $sid =~ /^0$/; | ||
| 290 | $t1 .= sprintf $f_ih, "task", $tsk; | ||
| 291 | $t1 .= sprintf $f_ih, "no_dispform", 1; | ||
| 292 | $t1 .= sprintf $f_ih, "guid", $usr_gid if $usr_gid; | ||
| 293 | print $q->header("text/html"); | ||
| 294 | print $q->start_html(%{$ar->{html_header}}); | ||
| 295 | printf $f_fm, "R", "$t1$tb"; | ||
| 296 | $ar->{bottom_nav} = ''; | ||
| 297 | } | ||
| 298 | |||
| 299 | =head2 disp_task_form($q,$ar,$txt) | ||
| 300 | |||
| 301 | Input variables: | ||
| 302 | |||
| 303 | $q - CGI class | ||
| 304 | $ar - array ref containing the following variables: | ||
| 305 | pid : project id such as ckpt, owb, dba, etc. | ||
| 306 | task : task name required such as task1,task2,etc. | ||
| 307 | target(sel_sn1) : select one (DB/server name) defining sid | ||
| 308 | args(sel_sn2) : select two (Arguments) | ||
| 309 | task_fn : task file name containing all the tasks defined | ||
| 310 | svr_conn : host/server connection info | ||
| 311 | db_conn : db connection info for each target/server | ||
| 312 | task_conn : special connection for tasks. | ||
| 313 | It overwrites db_conn for the task | ||
| 314 | |||
| 315 | Variables used or routines called: | ||
| 316 | |||
| 317 | None | ||
| 318 | |||
| 319 | How to use: | ||
| 320 | |||
| 321 | |||
| 322 | Return: $pr will contain the parameters adn output from running the PL/SQL. | ||
| 323 | |||
| 324 | |||
| 325 | =cut | ||
| 326 | |||
| 327 | sub disp_task_form { | ||
| 328 | my ($s, $q, $ar, $txt, $ret) = @_; | ||
| 329 | |||
| 330 | my $fmn = 'fm1'; | ||
| 331 | $fmn = $ar->{form_name} | ||
| 332 | if exists $ar->{form_name} && $ar->{form_name}; | ||
| 333 | my %fr = (-name => $fmn, -method=>uc $ar->{method}, | ||
| 334 | -action=>"$ar->{action}?", -enctype=>$ar->{encoding} ); | ||
| 335 | if (exists $ar->{hr_form} && $ar->{hr_form}) { | ||
| 336 | my $fr_hr = (ref($ar->{hr_form}) =~ /^HASH/) ? | ||
| 337 | $ar->{hr_form} : eval $ar->{hr_form}; | ||
| 338 | foreach my $k (keys %{$fr_hr}) { $fr{$k} = $fr_hr->{$k}; } | ||
| 339 | } | ||
| 340 | my $t = ""; | ||
| 341 | $t .= $q->start_form(%fr); | ||
| 342 | my $hvs = $s->set_param('vars_keep', $ar); | ||
| 343 | if ($hvs) { | ||
| 344 | foreach my $k (split /,/, $hvs) { | ||
| 345 | my $v = $s->set_param($k, $ar); | ||
| 346 | next if $v =~ /^\s*$/; | ||
| 347 | $t .= $q->hidden($k,$v); | ||
| 348 | } | ||
| 349 | } | ||
| 350 | $t .= "$txt\n"; | ||
| 351 | $t .= $q->end_form . "\n"; | ||
| 352 | print $t if !$ret; | ||
| 353 | return $t if ($ret); | ||
| 354 | } | ||
| 355 | |||
| 356 | |||
| 357 | =head2 set_args ($ar) | ||
| 358 | |||
| 359 | Input variables: | ||
| 360 | |||
| 361 | $ar - array ref containing the following variables: | ||
| 362 | |||
| 363 | Variables used or routines called: | ||
| 364 | |||
| 365 | None | ||
| 366 | |||
| 367 | How to use: | ||
| 368 | |||
| 369 | Return: None | ||
| 370 | |||
| 371 | =cut | ||
| 372 | |||
| 373 | sub set_args { | ||
| 374 | my ($s, $ar) = @_; | ||
| 375 | |||
| 376 | return if (exists $ar->{sel_sn2} && $ar->{sel_sn2}); | ||
| 377 | |||
| 378 | my $prg = "AppBuilder::Common->set_args"; | ||
| 379 | my $tsk = $ar->{task}; # task: add_study | ||
| 380 | my $amg = eval $s->set_param('arg_msgs',$ar); # arg msgs | ||
| 381 | my $dir = eval $s->set_param('dir_map',$ar); # dir map | ||
| 382 | |||
| 383 | |||
| 384 | if (!exists $amg->{$tsk}) { | ||
| 385 | $s->echo_msg("WARN: ($prg) could not find task - $tsk in arg_msgs.",0); return; | ||
| 386 | } | ||
| 387 | |||
| 388 | my $far = $amg->{$tsk}; # form message | ||
| 389 | my $sel_sn2 = ''; | ||
| 390 | for my $i (0..$#$far) { # each variable | ||
| 391 | my $k = $far->[$i][0]; # name/key: study_id | ||
| 392 | my $m = $far->[$i][1]; # message | ||
| 393 | my $d = $far->[$i][2]; # default | ||
| 394 | my $n = $far->[$i][3]; # desc/required | ||
| 395 | # print "K=$k,M=$m,N=$n<br>\n"; | ||
| 396 | if ($n =~ /^\*/ && (!exists $ar->{$k} || !$ar->{$k}) ) { | ||
| 397 | $s->echo_msg("ERR: ($prg) $k is required!",0); return; | ||
| 398 | } | ||
| 399 | my $v = (exists $ar->{$k}) ? $ar->{$k} : ''; | ||
| 400 | $v =~ s/:/_/g; | ||
| 401 | if ($k =~ /^(dir_name|file_name)/i && exists $ar->{$k}) { | ||
| 402 | my ($d1, $d2) = ($v =~ /^(\w_)(.+)/); | ||
| 403 | $d1 = uc $d1; | ||
| 404 | $d2 = dirname($d2) if $k =~ /^(dir_name)/i && $d2 =~ /\.(\w+)$/; | ||
| 405 | $v = $dir->{$d1} . $d2 if exists $dir->{$d1}; | ||
| 406 | } | ||
| 407 | $ar->{$k} = $v; | ||
| 408 | $sel_sn2 .= ($i == 0) ? $v : ":$v"; | ||
| 409 | } | ||
| 410 | # print "SEL_SN2: $sel_sn2<br>\n"; | ||
| 411 | $ar->{sel_sn2} = $sel_sn2; | ||
| 412 | } | ||
| 413 | |||
| 414 | |||
| 415 | 1; | ||
| 416 | |||
| 417 | =head1 HISTORY | ||
| 418 | |||
| 419 | =over 4 | ||
| 420 | |||
| 421 | =item * Version 0.10 | ||
| 422 | |||
| 423 | This version extracted from jp2.pl on 09/08/2010. | ||
| 424 | |||
| 425 | =item * Version 0.20 | ||
| 426 | |||
| 427 | 09/21/2010 (htu): start this PM | ||
| 428 | 10/16/2010 (htu): added get_jobid and get_hjobid | ||
| 429 | |||
| 430 | |||
| 431 | =cut | ||
| 432 | |||
| 433 | =head1 SEE ALSO (some of docs that I check often) | ||
| 434 | |||
| 435 | Oracle::Loader, Oracle::Trigger, CGI::AppBuilder, File::Xcopy, | ||
| 436 | CGI::AppBuilder::Message | ||
| 437 | |||
| 438 | =head1 AUTHOR | ||
| 439 | |||
| 440 | Copyright (c) 2009 Hanming Tu. All rights reserved. | ||
| 441 | |||
| 442 | This package is free software and is provided "as is" without express | ||
| 443 | or implied warranty. It may be used, redistributed and/or modified | ||
| 444 | under the terms of the Perl Artistic License (see | ||
| 445 | http://www.perl.com/perl/misc/Artistic.html) | ||
| 446 | |||
| 447 | =cut | ||
| 448 |