13.07.2015 Views

Software Engineering for Internet Applications - Student Community

Software Engineering for Internet Applications - Student Community

Software Engineering for Internet Applications - Student Community

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

give their site a unique look and feel. Eve expected that laws andaccounting procedures regarding sales tax would change. So sheencapsulated the looking up of sales tax by state, the figuring out ifthat state charges tax on shipping, and the multiplication of tax rateby price into an Oracle PL/SQL function:create or replace function ec_tax(v_price IN number, v_shipping IN number,v_order_id IN integer)return numberIStaxes ec_sales_tax_by_state%ROWTYPE;tax_exempt_p ec_orders.tax_exempt_p%TYPE;BEGINSELECT tax_exempt_p INTO tax_exempt_pFROM ec_ordersWHERE order_id = v_order_id;IF tax_exempt_p = 't' THENreturn 0;END IF;SELECT t.* into taxesFROM ec_orders o, ec_addresses a,ec_sales_tax_by_state tWHERE o.shipping_address=a.address_idAND a.usps_abbrev=t.usps_abbrev(+)AND o.order_id=v_order_id;IF nvl(taxes.shipping_p,'f') = 'f' THENreturn nvl(taxes.tax_rate,0) * v_price;ELSEreturn nvl(taxes.tax_rate,0) * (v_price + v_shipping);END IF;END;The Web script or other PL/SQL procedure that calls this functionneed only know the proposed cost of an item, the proposed shippingcost, and the order ID to which this item might be added (these arethe three arguments to ec_tax). That sales taxes <strong>for</strong> each state arestored in the ec_sales_tax_by_state table, <strong>for</strong> example, ishidden from the rest of the application. If an organization thatadopted this software decided to switch to using third-party software<strong>for</strong> calculating tax, that organization would need only to change thisone function rather than wading through hundreds of Web scriptslooking <strong>for</strong> tax-related code.know how many programs you have to examine and modify after apersonnel change. With magic numbers in the code it is tough toknow if rules are being en<strong>for</strong>ced consistently site-wide.Where should you store parameters such as these? Except <strong>for</strong> thedatabase username and password, an obvious answer would seemto be "in the database." There are a bunch of keys (the parameternames) and a bunch of values (the parameters). This is the veryproblem <strong>for</strong> which a database management system is ideal.-- use Oracle's unique key generatorcreate sequence config_param_seq start with 1;create table config_param_keys (config_param_key_id integer primary key,key_name varchar(4000) not null,param_comment varchar(4000));create table config_param_values (config_param_key_idnot nullreferences config_param_keys,value_index integer default 1 not null,param_value varchar(4000) not null);-- we use the Oracle operator "nextval" to get the next-- value from the sequence generatorinsert into config_param_keysvalues(config_param_seq.nextval, 'view_source_link_p', 'damn6.171 instructor is making me do this');-- we use the Oracle operator "currval" to get the last-- value from the sequence generator (so that rows-- inserted in this transaction will all have-- the same ID)insert into config_param_valuesvalues(config_param_seq.currval, 1, 't');commit;insert into config_param_keysvalues(config_param_seq.nextval, 'redirect','dropping the /wtr/ directory');insert into config_param_valuesvalues204145

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!