On the fly localization based on database query

Hi!
I have a situation where I need localization or number translation on destination number based on a database query result.
For example, when a customer (from SIP device) dials any number starting with 01, the dialed number needs to be searched in a database. If found, the switch needs to change the number by cutting 0 and adding 880N1 (this 880N1 code will be returned by the DB query). And then switch will route the call to the trunk found from the termination rate (the termination rate will have 880N1 prefix and not the 01 prefix). On the other hand, if the dialed number is not found in the database, switch will change the dialed number based on third digit of the dialed number. For example, if the dialed number is 01X, the translated number will be 880Y11X.
Can anyone suggest where to start if I want to achieve this using ASTPP?
TIA,
Jeet