Tuesday, December 21, 2010

Oracle PL/SQL Recipes

Since the beginning of my career, I have worked with Oracle databases. I began as a database administrator, monitoring performance and creating database objects. From there, I became a more advanced DBA as I began writing PL/SQL code and creating triggers, functions, packages, and procedures in the database. After a while, I became interested in web application development because I was always receiving requests for creating database reports. Why write a SQL statement and re-run it every time someone wanted to see the output, when you could write a web query that accepted parameters as input and the user could run it on their own? That is the point when I discovered how to do web application development with PL/SQL and the Oracle PL/SQL web toolkit. My good friend and colleague, Matt Arena, showed me the ropes about developing PL/SQL web applications, and it was great! Now I was able to develop web-based queries whenever I had received a request for a report.

After a while, I began to write Java stored procedures because I was developing some sophisticated PL/SQL web applications by that time and I wanted to use some features that the Java language had to offer. Of course, this all worked very well...but I soon gained interest in developing Java Server Pages (JSP) based web Java web applications because they seemed a bit more versatile then PL/SQL web applications. I learned that I could more clearly separate business logic from display code using Java technologies, and began to learn more about it. Before long, I was a full-fledged Java application developer. I was developing Java enterprise applications for Oracle database, and using PL/SQL objects to help facilitate my applications where it made sense.

That brings me to where I am today. I still develop web applications for Oracle database using Java technologies and PL/SQL. I have broadened my horizons by using great languages such as Jython. Life couldn't be much better. This past year, I decided to author a book about PL/SQL that focuses on the basics...but also goes into advanced topics such as working with PL/SQL and Java to develop advanced solutions. I brought my highly respected coworker, Matt Arena, into the book as my co-author. Matt is the most advanced PL/SQL developer that I know, and he wrote the chapters that focus on PL/SQL web application development, collections, PL/SQL jobs, and performance. He taught me, and now he is sharing his great knowledge by teaching others. I authored many chapters that focus on the PL/SQL language fundamentals, and also some advanced chapters working with PL/SQL and Java application development.

Our book is entitled, Oracle PL/SQL Recipes - A Problem-Solution Approach. I recommend you picking it up if you are an Oracle applications developer. Learning PL/SQL is a great asset to any Oracle developer's toolbox. Even if you only work with Java applications, PL/SQL can help you to develop better-performing, and highly robust Oracle solutions. A big thanks goes to Apress and the entire editorial team, including my good friend Jonathan Gennick, who has done an excellent job editing this book. I look forward to working with him again on future projects. I also thank my colleague and great friend Matt Arena for showing me the ropes with PL/SQL web application development...leading my to where I am today in my career.

Oracle PL/SQL Recipes - A Problem-Solution Approach...publishing late December 2010.

Tuesday, October 12, 2010

JavaOne and Oracle Develop 2010 - A New Experience

I took my first trip to JavaOne this year. There were many deciding factors that made me choose to make the trip this year, but perhaps the biggest reason that I attended was due to the Oracle acquisition of Sun Microsystems that took place earlier this year. I wanted to see what Oracle's viewpoint was on the Java ecosystem, and what they planned to do with it over the next several years. I must say that I was pleasantly surprised with the message that I received at the conference, I think Java is alive and very well.

I attended several sessions each day, and the conference made for a very hectic week. I had never visited San Francisco before and I must say that I found it very similar to Chicago. Being a Chicago area person myself, I found nothing overly different about San Francisco as compared to Chicago, but then again, I did not take any opportunities to go sight seeing as I was completely focused on the conference itself.

Having never been to a JavaOne conference before, i think that it was fairly well organized. Now, JavaOne was spread across a few different hotels and one large tent...and in the past I believe that it was centered in the Moscone convention center. I thought that the hotel system worked, but it did make for some rather busy shuffles from session to session. I did like the fact that I had the opportunity to go outside in between sessions though, and since the Mason St. tent was in the center of it all, I could swing by the tent on the way to another session and pick up a coffee or chat with some interesting folk. This really enhanced the experience for me.

The sessions were well thought out and worthwhile. Of course, being a Jython fan I went to as many sessions that were centered on dynamic languages on the JVM as possible. Jim Baker and myself had put in for a Jython-specific talk at this year's JavaOne and unfortunately it was turned down. Our talk was going to focus on the overall picture of bringing dynamic languages to the JVM...and Jython was going to be the example-case. I wasn't too surprised when our session was turned down as there are thousands of submissions, but when I attended the conference it was clear to see why it was turned down...there were several excellent sessions regarding dynamic languages on the JVM already. To be quite truthful, the dynamic language sessions at JavaOne covered several different languages...and I feel that the most used languages on the JVM today were nicely represented. It would have been very nice to have a session devoted to Jython specifically, but the fact is that there are so many dynamic languages on the JVM nowadays, that there was just not enough room for a session on each.

To that end, I attended an excellent JRuby session as my first JavaOne session...it really started my experience off in a great way. The presentation was given by Charles Nutter and Thomas Enebo...a couple of JRuby architects. This was a solid session that really made me want to go and download JRuby afterwards and try it out. Having not tried JRuby prior to the session, I was really focusing more on seeing another dynamic language on the JVM and getting some ideas of how they implemented it. After the presentation, it was clear to see that the JRuby team has a focused effort and is doing very well in porting the Ruby language to the JVM...excellent session.

My second session also focused on languages on the JVM...it was entitled "Multiple Languages, One Virtual Machine". This talk was given by Brian Goetz and John Rose. They did an excellent job of discussing invoke dynamic and what the future holds for dynamic languages on the JVM. It was clear to see that Oracle is putting resource behind offering many different languages on the JVM...not just Java. They are behind the invoke dynamic effort, and it will be included in JDK 7...which is due out sometime in 2011.

I attended many sessions on Java EE 6, EJB, JSF, and other enterprise technologies as I am currently using EJB3 and JSF on many of my projects at work. After attending so many sessions on the EJB 3.1 Lite and JSF 2.0 features, it is clear to see that I need to update my code to take advantage of many of the new features that are available today. It is too easy to get a formula that works and just continue to develop applications using that formula. I fell into that trap over the last couple of years, mainly due to a shortage of time for learning the new stuff. Between my authoring of the Jython book and Oracle PL/SQL Recipes, I haven't enough time to devote an hour a day to learning EJB 3.1, JSF 2.0 or CDI. However, I am making a goal for myself to learn how to migrate my EJB 3.0 code bases to take advantage of the new technologies as I can see from the JavaOne sessions that the newest implementations of the APIs are clearly more productive and easier to manage.

And then there was JavaFX. This technology has always been in the back of my mind as something that I want to learn and begin to use in my projects. I had purchased and read the JavaFX Script book by Jim Weaver a while back...excellent book. As everyone knows by now, it was announced at JavaOne that JavaFX Script will be going away and that the JavaFX API will be changing so that it can be used directly with Java code. This is HUGE news for me as a Java and Jython developer. I attended a great session by Jonathan Giles and Stephen Chin regarding the development of JavaFX applications using alternate languages. They covered JRuby, Closure, Scala, and Groovy in the session. It really looks like the new JavaFX API is going to be great and I am looking forward to writing a JavaFX application using Jython. As a matter of fact, I spoke with Stephen Chin in the Mason St. tent later on, and he was also interested in developing a Jython demo. I hope to see something soon, and plan to develop Jython and JavaFX applications when the new JavaFX API is available.

My parting session was an introduction to Scala. I am glad that I attended this session as well as I have been in the dark about this language until now. I had been hearing lots of good things about Scala, but hadn't found the time to take a look. Attending this session gave me a great overview of the language and really gave me the bug that I needed to put it on my priority list as a language to learn in the coming weeks. Now I need to learn both JRuby and Scala...my time is really going to be growing thin!

Other activities that I took part in included:

Oracle Publisher's Seminar - Since I am writing the PL/SQL Recipes book, I was able to attend this great seminar. It gave me a good opportunity to learn about some Oracle strategies from the team leads themselves, and also meet other Oracle authors.

Jython BoF - Oti Humbel and myself gave a Jython BoF on Wednesday of the conference in the Parc 55 hotel. The BoF was not very well attended, but the appreciation event was also that evening and I suspect that had something to do with it. Overall, Oti and I were able to muddle through the list of open bugs that need to be repaired prior to a Jython 2.5.2 final release. I had a great time meeting with Oti and look forward to working with him on Jython for future releases. We also met with Frank Wierzbicki, the Jython project lead and one of my co-authors for the Jython book. It was great to talk Jython for a while with a couple of the core devs, and to strategize about future Jython developments.

Keynotes - The JavaOne keynotes were okay...lots of hype around the future of Java and JavaFX. Glad I attended them for the experience, but nothing too earth shattering. My favorite keynote was the Oracle Develop keynote by Tom Kyte. I have been a big fan of Tom Kyte for several years as he is a lead in Oracle database technologies and PL/SQL. Excellent keynote with some great insight on Oracle 11gR2 and some of it's new features (some that I have not used yet!).

JavaPosse - Perhaps the highlight of the conference for me was the JavaPosse BoF and rooftop party at the Passion Cafe. I listen to the JavaPosse religiously, and finally having a chance to see the crew in person and have drinks with them after the BoF was a great experience. They are truly great people who do an excellent job with the podcast. Thanks JavaPosse crew, excellent show at JavaOne.

So JavaOne 2010 was a new experience for me. I think that it was a great experience and I would be happy to attend another JavaOne at some point in the future. I think that given the atmosphere and the dynamics of the conference, it was a new experience for even those who had attended in previous years. It is nice to see that Java is in good hands with Oracle, and I am looking forward to a bright future.

Wednesday, March 24, 2010

Working with Images Using Django on Jython

If you've tried to use Django on Jython along with an ImageField, you know well that Django on Jython does not support this field. This is because the ImageField in Django relies on the Python Imaging Library (PIL). Jython does not support the PIL since it is C based, and there is not currently a version of the PIL that has been ported to Java.

That being said, how do we make use of images in Django on Jython? Since the ImageField is not usable from Django on Jython, we need to use an alternate method for uploading and displaying images. The following is a solution I've been using for one of my applications. This is certainly not a perfect solution, nor do I think we should modify Django on Jython to support this solution out-of-the-box. However, it works and gets the job done.

In order to use images with Django on Jython, you need to make use of the FileField. Since the FileField does not display in a template as an image, it is also a requirement to tweak the template which will be displaying the images with an ordinary "img" tag. Also, to make things work nicely, it is important to code a variable...we'll call it "image_src"...with the path to the image that will be displayed. This is done within your views in views.py. Of course, images will take the name attribute of your FileField, but you must prefix that name with the path to your web server. So my solution for this is to create another variable, say PHOTO_ROOT, in the settings.py and we will set it equal to the web server's MEDIA_ROOT location.

Lots of steps to make a usable image? Not really, it may seem daunting at first glance, but it does work easily once you've got it down.

Steps for using images with Django Jython:

1) Create a FileField in your model to contain your image information. Be sure to indicate where you wish the image to be uploaded to by specifying the "upload_to" attribute.

For example, in the following case the "images" directory which is specified by the 'upload_to' attribute exists directly under the MEDIA_ROOT root:

class MyTestModel(models.Model):
photo = models.FileField(upload_to='images', blank=True)


2) Create a variable within settings.py to contain the path to your web server's MEDIA_ROOT location. Please note that this will need to be changed when you deploy to different environments (ie: development or production).


PHOTO_ROOT = 'http://localhost:8000/site_media/'


3) Add a variable to your views through which the image path will be exposed. This variable will need to contain the PHOTO_ROOT you specified in settings.py, as well as the appended file.name for the FileField containing your image. Therefore, you will need to code a line in each view you wish to expose the image through.

In order to make your UI slick, it is a good idea to use a default image for any records that may not have an image uploaded. You can assign the path for that default image to your variable if your FileField does not contain anything.


# Be sure to import your settings file
from django.conf import settings
...
# This code contains relevant portions of a detail view for displaying or modifying
# a record
def detail(request, record_id=0):
...
record = get_object_or_404(MyTestModel, pk=record_id)
form = MyForm(instance=record)

# Check to see if the FileField (photo) contains anything
if record.photo:
image_src = '%s/%s' % (settings.PHOTO_ROOT, record.photo.name)
else:
image_src = '%s/images/nophoto.jpg' % settings.PHOTO_ROOT

...
return render_to_response('detail.html',
{'form':form,
'image_src':image_src})



4) Add an "img" tag to your template in order to display your image. You must also add the "form.photo" field to the template in order to have the ability to upload an image.



some_content
...


{{ form.photo }}

<img title="Photo"
src="{{ image_src }}"/>


...
more_content


That is basically it. When you view the record initially, the default image should appear. After an image is uploaded successfully, it should replace the default image.

Wednesday, March 10, 2010

Received Author Copies of The Definitive Guide to Jython

For the past few weeks since The Definitive Guide to Jython had been published, I've been checking my mail often in anticipation of seeing our book in print for the first time. Well, today my copies finally arrived and it is great. It is excellent to read the printed copy...and it will definitely be nice to have on my desk while I'm coding some Jython.

Thanks again to all of my co-authors, the technical reviewers, editors, and everyone else involved in the publishing of the book. It is a great resource and I am glad to be a part of it. Looking forward to working on the second edition in the not too distant future!

Now, onto more coding...

Wednesday, February 03, 2010

Jython Book - Working with the Sources

The open source version of 'The Definitive Guide to Jython' (aka: Jython book) is available online, both in restructuredText format and HTML via Sphinx. Apress was good enough to send me the final versions for all of our chapters and appendicies in MS-Word format. It is my task to convert them from MS Word format into restructuredText...which is a slow and sometimes painful process.

Overview of Conversion Process

1) Open the MS-Word document in Open Office (my preference) and remove headers and footers from each page

2) Save the document in open office format

3) Try to apply the apress_odt_to_rst.py script that was donated by James Gardner. If that doesn't work, then save document in HTML format and use pandoc to convert.

Command: pandoc -t rst filename.htm > filename.rst

4) Manually parse through each rst file and repair issues (and there are usually a lot...especially with code markup and tables)

Interested in building the sources?

If you are interested in building the sources, you can check the out from bitbucket.org at the following link: http://bitbucket.org/javajuneau/jythonbook/ and then build them using Sphinx

View the Open Source Book

If you'd like to simply view the open source book, it can be found at http://jythonbook.com in Sphinx format. We of course recommend that you purchase a copy of the book from Apress to keep handy as well so that you can mark it up, make notes, etc. However, the open source version will be continually updated and it will be great for using as a quick reference while online.

Monday, February 01, 2010

The Definitive Guide to Jython Is Published

The Definitive Guide to Jython: Python for the Java Platform has been published. This is a work in which five authors: Jim Baker, Victor Ng, Leo Soto, Frank Wierzbicki, and myself, began to author early in 2009. The book covers much detail on the Jython language and it's usage. It was a much needed addition to the library of Jython books that are available today as this book focuses on Jython 2.5.1...the most current release to-date. Many methodologies that hadn't been formally documented previously, such as the object factory pattern, with-statement, django and jython, and concurrency, have been documented in detail in this text.

If you are interested in developing with Jython, I recommend you take a look at this book today. There is also an online open source version of the book that can be found at http://jythonbook.com, but all of it's contents have not yet been updated to include finalized versions of all chapters. I am working on converting the .doc formatted final versions into restructured text at this time, but it is a lengthy process.

Thanks to all of my fellow authors and to the team at Apress for all of their hard work!