1 |
williamc |
1.1 |
#
|
2 |
|
|
# User Interface --- very basic command line input interface
|
3 |
|
|
#
|
4 |
|
|
# Interface
|
5 |
|
|
# ---------
|
6 |
|
|
# new() : a new interface object
|
7 |
|
|
# runquery(type, parameter,args) : Call the appropriate query handling
|
8 |
|
|
# routine for the given type
|
9 |
williamc |
1.4 |
# runcheckedquery(checkobj, type, parameter, args) : as runquery but input
|
10 |
|
|
# is checked on input
|
11 |
williamc |
1.1 |
# startform() : Indicate a new form to start
|
12 |
|
|
# endform(parameterstore) : Close a form and transfer parameters
|
13 |
|
|
# (call setparam() on object)
|
14 |
williamc |
1.3 |
# messageout(text) : send a message to the user
|
15 |
williamc |
1.1 |
#
|
16 |
|
|
# ------------------------------------------------------------
|
17 |
|
|
# ---- required processing routines ---
|
18 |
|
|
# freechoice( param , commentstring, optionref )
|
19 |
|
|
# : display and ask user for input
|
20 |
|
|
# strictchoice( param , commentstring, optionref )
|
21 |
|
|
# : display and ask user for input
|
22 |
|
|
# basic(param, commentstring) : Simply user input query
|
23 |
|
|
|
24 |
|
|
package ActiveDoc::UserInterface_basic;
|
25 |
|
|
require 5.001;
|
26 |
|
|
|
27 |
|
|
sub new {
|
28 |
|
|
my $class=shift;
|
29 |
|
|
$self={};
|
30 |
|
|
bless $self, $class;
|
31 |
|
|
$self->init(@_); # override if reqd
|
32 |
|
|
return $self;
|
33 |
|
|
}
|
34 |
|
|
|
35 |
|
|
sub init {
|
36 |
|
|
my $self=shift;
|
37 |
|
|
@{$self->{allowed_commands}}=qw(freechoice basic strictchoice);
|
38 |
|
|
}
|
39 |
|
|
|
40 |
williamc |
1.5 |
sub expandvars {
|
41 |
|
|
my $self=shift;
|
42 |
|
|
my $string=shift;
|
43 |
williamc |
1.7 |
print "String in : $string\n";
|
44 |
|
|
$string=~s/\$\((.*?)\)/$self->{paramhash}{$1}/g;
|
45 |
williamc |
1.5 |
$string=~s/\$(.*?)( |\/|\Z)/$self->{paramhash}{$1}$2/g;
|
46 |
williamc |
1.7 |
print "String Out in : $string\n";
|
47 |
williamc |
1.5 |
return $string;
|
48 |
|
|
|
49 |
|
|
}
|
50 |
|
|
|
51 |
williamc |
1.4 |
sub runcheckedquery() {
|
52 |
|
|
my $self=shift;
|
53 |
williamc |
1.7 |
my $checker=shift;
|
54 |
williamc |
1.4 |
my @args=@_;
|
55 |
|
|
my $type=shift;
|
56 |
|
|
my $param=shift;
|
57 |
|
|
|
58 |
|
|
$self->runquery(@args);
|
59 |
williamc |
1.7 |
while ( ($self->{paramhash}{$param} ne "" ) &&
|
60 |
|
|
($checker->check($self->expandvars($self->{paramhash}{$param})))) {
|
61 |
|
|
$self->printout("Input Error : ".$checkobj->errormessage()."\n");
|
62 |
|
|
$self->runquery(@args);
|
63 |
williamc |
1.4 |
}
|
64 |
|
|
}
|
65 |
|
|
|
66 |
williamc |
1.1 |
sub runquery() {
|
67 |
|
|
my $self=shift;
|
68 |
|
|
my $type=shift;
|
69 |
|
|
my $param=shift;
|
70 |
|
|
my @args=@_;
|
71 |
|
|
my $found='false';
|
72 |
|
|
|
73 |
|
|
my $rv;
|
74 |
|
|
|
75 |
|
|
foreach $command ( @{$self->{allowed_commands}} ) {
|
76 |
|
|
if ( $command=~/^$type/ ) {
|
77 |
|
|
$rv=$self->$command( $param, @args ); $found='true';
|
78 |
|
|
last;
|
79 |
|
|
}
|
80 |
|
|
}
|
81 |
|
|
if ( ! ( $found=~/true/ ) ) {
|
82 |
|
|
# default to basic
|
83 |
|
|
$rv=$self->basic( $param, @args );
|
84 |
|
|
}
|
85 |
|
|
$self->{paramhash}{$param}=$rv;
|
86 |
williamc |
1.5 |
$self->printout("\n");
|
87 |
williamc |
1.1 |
}
|
88 |
|
|
|
89 |
williamc |
1.3 |
sub messageout {
|
90 |
|
|
my $self=shift;
|
91 |
|
|
my $text=shift;
|
92 |
|
|
|
93 |
|
|
print $text;
|
94 |
|
|
}
|
95 |
williamc |
1.1 |
|
96 |
|
|
# --------------- Request Implementations -----------------------------
|
97 |
|
|
#
|
98 |
|
|
|
99 |
|
|
sub basic {
|
100 |
|
|
my $self=shift;
|
101 |
|
|
my $param=shift;
|
102 |
|
|
my $string=shift;
|
103 |
williamc |
1.4 |
|
104 |
|
|
my @args=shift;
|
105 |
williamc |
1.1 |
|
106 |
|
|
$self->printout($string);
|
107 |
williamc |
1.4 |
return $self->prompt("Please Enter Value :");
|
108 |
williamc |
1.1 |
}
|
109 |
|
|
|
110 |
|
|
sub freechoice {
|
111 |
|
|
my $self=shift;
|
112 |
|
|
my $param=shift;
|
113 |
|
|
my $string=shift;
|
114 |
|
|
my $choiceref=shift;
|
115 |
|
|
|
116 |
|
|
my $menuchoice;
|
117 |
|
|
my $rv;
|
118 |
|
|
|
119 |
williamc |
1.5 |
$self->printout($string."\n");
|
120 |
williamc |
1.1 |
my $num=1;
|
121 |
|
|
foreach $item ( @{$choiceref} ) {
|
122 |
|
|
$self->printout($num.". ".$item."\n");
|
123 |
|
|
$num++;
|
124 |
|
|
}
|
125 |
williamc |
1.2 |
if ( $num == 1 ) {
|
126 |
|
|
$rv=$self->basic($param,"Please enter a value\n");
|
127 |
|
|
}
|
128 |
|
|
else {
|
129 |
williamc |
1.1 |
$self->printout($num.". Enter Alternative Value\n");
|
130 |
|
|
$self->printout("Please Enter a Number from the list above :");
|
131 |
|
|
# Error check
|
132 |
|
|
while ( (($menuchoice=$self->prompt()) > $num) || ( $menuchoice < 1) ) {
|
133 |
|
|
$self->printout("Please Select a number Between 1 and $num :");
|
134 |
|
|
}
|
135 |
|
|
# Deal with alternative value
|
136 |
|
|
if ( $menuchoice == $num ) {
|
137 |
|
|
$rv=$self->basic($param,"Please enter new value\n");
|
138 |
|
|
}
|
139 |
|
|
else {
|
140 |
|
|
$rv=$$choiceref[$menuchoice-1];
|
141 |
|
|
$self->printout( $rv." selected\n");
|
142 |
|
|
}
|
143 |
williamc |
1.2 |
}
|
144 |
williamc |
1.1 |
return $rv;
|
145 |
|
|
}
|
146 |
|
|
|
147 |
|
|
sub strictchoice {
|
148 |
|
|
my $self=shift;
|
149 |
|
|
my $param=shift;
|
150 |
|
|
my $string=shift;
|
151 |
|
|
my $choiceref=shift;
|
152 |
|
|
|
153 |
|
|
my $menuchoice;
|
154 |
|
|
my $rv;
|
155 |
|
|
|
156 |
|
|
$self->printout($string);
|
157 |
|
|
my $num=0;
|
158 |
|
|
foreach $item ( @{$choiceref} ) {
|
159 |
|
|
$num++;
|
160 |
|
|
$self->printout($num.". ".$item."\n");
|
161 |
|
|
}
|
162 |
williamc |
1.2 |
if ( $num!=0 ) {
|
163 |
williamc |
1.1 |
$self->printout("Please Enter a Number from the list above :");
|
164 |
|
|
# Error check
|
165 |
|
|
while ( (($menuchoice=$self->prompt()) > $num) || ( $menuchoice < 1) ) {
|
166 |
|
|
$self->printout("Please Select a number Between 1 and $num :");
|
167 |
|
|
}
|
168 |
|
|
$rv=$$choiceref[$menuchoice-1];
|
169 |
|
|
$self->printout( $rv." selected\n");
|
170 |
williamc |
1.2 |
}
|
171 |
williamc |
1.1 |
return $rv;
|
172 |
|
|
}
|
173 |
|
|
# ----------------- Support Routines ----------------------------------
|
174 |
|
|
|
175 |
|
|
sub printout {
|
176 |
|
|
my $self=shift;
|
177 |
|
|
my $string=shift;
|
178 |
|
|
|
179 |
|
|
print $string;
|
180 |
|
|
}
|
181 |
|
|
|
182 |
|
|
sub prompt {
|
183 |
|
|
my $self=shift;
|
184 |
williamc |
1.4 |
my $prompt=shift;
|
185 |
williamc |
1.1 |
my $input;
|
186 |
|
|
|
187 |
williamc |
1.4 |
if ( defined $prompt ) {
|
188 |
|
|
print $prompt;
|
189 |
|
|
}
|
190 |
williamc |
1.1 |
$input = <STDIN>;
|
191 |
|
|
chomp $input;
|
192 |
|
|
return $input;
|
193 |
|
|
}
|
194 |
|
|
|
195 |
|
|
sub startform {
|
196 |
|
|
my $self=shift;
|
197 |
williamc |
1.4 |
my $string=shift;
|
198 |
williamc |
1.1 |
undef %$self->{paramhash};
|
199 |
|
|
$self->printout("-----------------------------------------------------".
|
200 |
|
|
"---------------\n");
|
201 |
williamc |
1.4 |
$self->printout($string."\n\n");
|
202 |
williamc |
1.1 |
}
|
203 |
|
|
|
204 |
|
|
sub endform {
|
205 |
|
|
my $self=shift;
|
206 |
|
|
my $parameterobject=shift;
|
207 |
|
|
|
208 |
|
|
# copy the parameter hash across
|
209 |
williamc |
1.6 |
foreach $key ( keys %{$self->{'paramhash'}} ) {
|
210 |
williamc |
1.5 |
$parameterobject->setparam($key, $self->{paramhash}{$key});
|
211 |
williamc |
1.1 |
}
|
212 |
|
|
}
|