Saturday, October 8, 2011

Beautiful Hunk of Java

I know, the title reads to some as an oxymoron, especially in light of Java's gnarly syntax requirements for closures. Still, some things are made beautiful partly because of the ugliness of the constituent material. Imagine a sculpture of Jessica Biel made of excised warts.
public enum SubstitutionVariable
{
AdjusterEstimator("Adjuster / Estimator", new ValueProvider()
{
public String get(Database db, OrderRec hdr) throws Exception
{
return hdr.selectAdjusterName(db) + " / " + hdr.selectEstimatorName(db);
}
}),
CustomerName("Customer Name", new ValueProvider()
{
public String get(Database db, OrderRec hdr) throws Exception
{
return hdr.selectCustomerName(db);
}
}),
InsuredName("Insured Name", new ValueProvider()
{
public String get(Database db, OrderRec hdr) throws Exception
{
return hdr.oInsured;
}
}),
WorkOrderClaim("Work Order / Claim", new ValueProvider()
{
public String get(Database db, OrderRec hdr) throws Exception
{
return hdr.oWorkOrder + " / " + hdr.oClaim;
}
});
interface ValueProvider
{
String get(Database db, OrderRec hdr) throws Exception;
}
//
//...some boring details left out...
//
private final String description;
private final ValueProvider valueProvider;
public static String substitute(String p, Database db, OrderRec hdr) throws Exception
{
for (SubstitutionVariable v : sortedByLengthDescending)
{
p = v.replace(p, db, hdr);
}
return p;
}
//
private SubstitutionVariable(String description, ValueProvider performer)
{
this.description = description;
this.valueProvider = performer;
}
//
public String getDescription()
{
return description;
}
//
private String replace(String p, Database db, OrderRec hdr) throws Exception
{
return StringKit.replace(p, "%" + name() + "%", valueProvider.get(db, hdr));
}
}

Sunday, March 27, 2011

Web Non-Framework

I recently finished a project to produce a web site described with PhotoShop for deployment to a content management system. To force myself to focus on interface design, I chose to limit the server-side facilities used to includes only.


Includes Only
While I had already reached the conclusion intellectually, the exercise reinforced my impression there is seldom a good reason to build HTML in server-side code.


HTTP ↑ JSON ↓
I have now built two small applications that do not include any HTML building or any sort of "application model" on the server side. Other than includes used as needed, at the top of each page is enough server-side code to invoke security checks and retrieve data formatted in JSON that will be used in all cases. Retrieval of anything optional is left to ajax invocations. The applications in question include sending e-mail, file uploading, image manipulation, and reporting with PDF, so they're not just sunny-day tests.


Tools
On the client side, I have tools to exchange data among JSON objects, form elements and ajax request parameters, and a facility for presenting error messages from client-side validations. On the server side, I have a persistence kit, a flexible model to map requests to data dispensers and updaters and a tool to deliver messages from server-side validations.


Sample


//I really can't imagine that anyone in my intended audience doesn't already know
//exactly how to code everything I've described, so why bother with a sample?

(Preliminary?) Conclusion
As long as the client-side script is kept in good order, I can't yet see where this approach will break down, while I can definitely say that working with this non-framework feels more enjoyable than with any web framework I've used so far.

Friday, October 9, 2009

Academic Excercise 1 for Java

So I was asking myself, "Self, would it be possible in Java, like in some of the coolboy languages, in one statement, to wrap an arbitrary hunk of code with resource allocation and cleanup?" It turns out there's a not-too-crazy way as long as your arguments can be marked final:

/**
* Provides resource-safe services for client code.
*/
public class DbKit {
protected DbKit()
{
}
interface DbTask<E>
{
abstract E run(Db db) throws Exception;
}

/**
* Along with DbTask, provides a means to wrap instantiation, opening,
* try { do } finally{close} around any task.
*/
private static <E> E run(DbTask<E> t) throws Exception
{
Db db= new Db();
db.open();
try
{
return t.run(db);
}
finally
{
db.close();
}
}
public static SomeRec insertSomeRec(final long lv,final String sv) throws Exception
{
return run(new DbTask<SomeRec>(){
public SomeRec run(Db db) throws Exception
{
SomeRec r = new SomeRec ();
r.setLongVal(lv);
r.setStrVal(sv);
db.insert(r);
return r;
}});
}
public static void updateRec(final long kv,final String newStatus) throws Exception
{
run(new DbTask<Object>(){
public Object run(Db db) throws Exception
{
SomeRec.update(db,kv,newStatus);
return null;
}});
}
}

Saturday, September 26, 2009

What to Work on Next

Testing, 1, 2, 3, the missiles are flying. My first post is a cod liver oil pill to be choked down before I begin to do what I fear I must do next. Lemons from lemonade - we'll explore the current situation and propose a maxim.

Suppose, like me, you're a programmer/developer/contractor/ISV who has the occasional luxury of choosing what task to work on, and that we like our profession, even find it extremely engaging at times - here's the maxim:


• if you're thinking about it in the shower - you must work on it

..or while exercising, or trying to sleep... even, alas, if it's a banal consulting assignment working with unfashionable technology; somehow it's driving your stray thoughts and must be purged before you can effectively return to those pursuits judged more important/interesting/(dare i say it:lucrative!) by your conscious mind.

I have my own business, two modestly profitable products, a promising product in early construction, an open-sourced creation for two platforms, a nascent interest in alternative programming languages, an urge to further explore native/embedded/mobile device products, and a desire to blog/teach/train/proselytize, and what's occupying my stray thoughts right now? A hunk of J2EE middleware to help BigCorp1217 move orders from xml to a dbms!

What could I do, Pancho Villa, he had a gun.