2
\$\begingroup\$

This script was previously reviewed here: Part 1: Create or update record via HTTP request

I've made the changes that were suggested by @Reinderien.

Now that the code has been significantly refactored, are there any other improvements that could be made?


Description:

I have an external system that sends an HTTP request to a Jython script (in IBM's Maximo Asset Management platform).

The Jython 2.7.0 script does this:

  1. Accepts an HTTP request: http://server:port/maximo/oslc/script/CREATEWO?_lid=wilson&_lpwd=wilson&f_wonum=LWO0382&f_description=LEGACY WO&f_classstructureid=1666&f_status=APPR&f_wopriority=1&f_assetnum=LA1234&f_worktype=CM
  2. Loops through parameters:
    • Searches for parameters that are prefixed with f_ ('f' is for field-value)
    • Puts the parameters in a list
    • Removes the prefix from the list values (so that the parameter names match the database field names).
  3. Updates or creates records via the parameters in the list:
    • If there is an existing record in the system with the same work order number, then the script updates the exiting record with the parameter values from the list.
    • If there isn't an existing record, then a new record is created (again, from the parameter values from the list).
  4. Finishes by returning a message to the external system (message: updated, created, or other (aka an error)).

from psdi.mbo import SqlFormat
from psdi.server import MXServer
from psdi.mbo import MboSet
IGNORE_RULES = 2L
params = [
 param for param in request.getQueryParams()
 if param.startswith('f_')
]
paramdict = {
 p[2:]: request.getQueryParam(p)
 for p in params
}
resp='' 
woset = MXServer.getMXServer().getMboSet("workorder",request.getUserInfo())
try:
 #Prevents SQL injection
 sqf = SqlFormat("wonum=:1")
 sqf.setObject(1,"WORKORDER","WONUM",request.getQueryParam("f_wonum"))
 woset.setWhere(sqf.format())
 woset.reset()
 woMbo = woset.moveFirst()
 if woMbo is None:
 woMbo=woset.add()
 verb = 'Created'
 else:
 verb = 'Updated'
 for k,v in paramdict.items():
 woMbo.setValue(k,v,2L)
 resp = verb + ' workorder ' + request.getQueryParam("f_wonum")
 woset.save()
 woset.clear()
 woset.close()
finally:
 woset.close()
responseBody = resp
Peilonrayz
44.4k7 gold badges80 silver badges157 bronze badges
asked Jul 6, 2020 at 12:13
\$\endgroup\$
1
  • \$\begingroup\$ I have rolled back your edit since it invalidates an answer. \$\endgroup\$ Commented Jul 6, 2020 at 13:53

1 Answer 1

2
+50
\$\begingroup\$

You haven't used this:

IGNORE_RULES

in your call to setValue.

Also, you have a double call to close; delete the first one.

resp does not benefit from being pre-initialized to an empty string; you can delete that line.

The only other thing I see is that this:

params = [
 param for param in request.getQueryParams()
 if param.startswith('f_')
]
paramdict = {
 p[2:]: request.getQueryParam(p)
 for p in params
}

can be condensed to only the dictionary comprehension:

paramdict = {
 p[2:]: request.getQueryParam(p)
 for p in request.getQueryParams()
 if p.startswith('f_')
}
answered Jul 6, 2020 at 13:40
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.