Chunk: yet another Java Template EngineJan 01 2011 | 14:24:17 | No Comments

Simple, powerful templates for Java

Simple, powerful templates for Java

Q: Does the Java community really need yet another templating engine?

A: Not really, but back when I started Chunk, there wasn’t anything else out there like it. Now that it has matured, I am releasing it into the wild, and if somebody finds it useful, great!

Click below for an introduction to better code/layout separation:

http://www.x5software.com/chunk/wiki

Features:

  • Macros, Includes and Conditional Includes.
  • Flexible null-handling; template designer may specify default tag values.
  • Library of powerful predefined in-tag filters, including regex (regular expressions), sprintf.
  • Expose a subset of obj methods to template with a single line of code.
  • Define multiple snippets per template file.
  • Support for theme layers.
  • Highly optimized codebase.
  • Hooks for extending.
  • Eclipse Template Editor plugin available with syntax highlighting & more.

Visit the Chunk project home on Google Code to download the Chunk Templates jar and get started.

I don’t know if Chunk is better than Velocity, I don’t know if Chunk is better than FreeMarker, but I’ve been using it for years and it’s gotten pretty cool.

Hackers are encouraged to contribute enhancements back to the community.  Google is hosting the code on its SVN server so it’s easy to check out a copy of the source and make whatever improvements you need.

Fix or extend core Magento in an upgrade-safe wayNov 20 2010 | 09:22:03 | No Comments

Ah, open source.  The Magento core codebase, layouts and templates are right there, warts and all, just daring you to give in to temptation and ignore the “do not edit this file!” warning at the top of each distributed file.  We all know the guilty pleasure of instant gratification that comes with tweaking a word, or a layout, or a core class directly in the source and seeing our fix immediately on the site.  It seems especially rewarding after the hour or two it took just to hunt down the right cog in The Machine.  And really, who has time to learn entire frameworks and build a module, just to tweak the way a breadcrumb gets output?

But then it’s 500 (or 5,000!) lines of “little tweaks” later and you need to upgrade.  And you’re screwed.  And a couple extra xml files and a separate tree of folders and files doesn’t seem like such a bad idea anymore.

Well, okay, time to roll up your sleeves and learn this durn extension framework that the brainiacs at Varien/Magento have so lovingly constructed.

Layouts and Templates and Blocks, Oh My

classyllama.com has a great article on how to modify one or two things in a core layout without (1) modifying it directly in core or (2) having to make a copy of the whole layout file into your theme just to tweak one line:

http://classyllama.com/development/magento-development/the-better-way-to-modify-magento-layout/

Now that I understand this layout stuff, I’m doing a lot less messing around directly in theme templates.  By the way, if you are editing the factory-supplied Magento templates and layouts directly, for shame!  Set up a theme and make all your changes in the theme.

Update: another great article on navigating the Magento layout framework:
http://alanstorm.com/layouts_blocks_and_templates

OO PHP?  Isn’t that like oil and water?

The article linked below offers a lucid explanation of how to roll your own modules that extend the core Model, Controller, Resource, and Block classes (it’s a little different for each case) to override the core behavior in Magento. Not for beginners, you really need to have a handle on OO programming. Again, this is cool because you don’t have to edit a core file directly (where changes will get blown away in an upgrade) and you don’t have to copy the whole file just to tweak one function. The latter strategy prevents you from reaping the benefits of any bugfixes or new features in that file in the next upgrade, and could even cause serious problems/bugs as your copy gets out of sync with the core codebase.

http://prattski.com/2010/06/24/magento-overriding-core-files-blocks-models-resources-controllers/

For some reason that comprehensive article does not include instructions for overriding a function in a helper class (yet another special case). This article has an example for that:

http://blog.chapagain.com.np/magento-block-controller-model-helper-override/

Happy coding!

CSS Sucks (or, center this, microsoft)Aug 30 2010 | 09:20:01 | No Comments

CSS SucksCSS officially needs to be replaced. Centering a dropdown menu should NOT be this hard:

http://matthewjamestaylor.com/blog/centered-dropdown-menus

Thank goodness I found that article or I would still be pulling my hair out.

In my day, used to be you could stick this in one or two tables and you were done. We didn’t have no stinkin’ separation of content and layout and we liked it.

Calling Magento SOAP API from Perl with SOAP::LiteAug 02 2010 | 13:11:40 | 1 Comment

We had a legacy order fulfillment library written in Perl, so although accessing the API is extremely easy in PHP, when we migrated to Magento I had to figure out how to connect to the SOAP API from Perl.

Turns out the most popular CPAN module for doing SOAP in Perl (SOAP::Lite) is not particularly fun to use (see “State of the SOAP” at http://www.soaplite.com/ for a three-year-old call for volunteers to refactor).

Nevertheless, I did eventually emerge victorious. There may be a much easier way but after a lot of head-scratching and trial and error, this worked for me, YMMV.

The soapify routine was stolen from this page:
http://www.soaplite.com/2004/01/building_an_arr.html
Many thanks to soapify author Sandeep Satavlekar.

For passing the filter args to sales_order.list, I had to make the call in PHP first and then reverse-engineer the encoding of the nested array that actually got passed. There may be a way to get SOAP::Lite to encode nested arrays like this by default (or encode nested arrays in some other standard SOAPy way that Magento would also accept) but I chose not to waste any more time on this than I already had.

Enjoy!

UPDATE: Was getting sporadic “Access Denied” errors. Turns out you can fix this just by turning on cookies. Apparently this only affects you if Magento is using DB sessions. See new get_soap_session below for how to turn on cookies.

UPDATE TO THE UPDATE: Nah, that didn’t fix it. Still searching for a fix… runs fine in a session, fails about 80% of the time from cron.

The complete working example script is after the jump...


#!/usr/bin/perl

# replace these values with the soap account you set up in the admin
my $user = 'username';
my $pass = 'password';

# replace with your domain and path (we installed magento at /shop/)
my $soap_url = 'http://YOURDOMAIN/MAGENTO_PATH/index.php/api/index/index/';

my ($soap,$session_id) = &get_soap_session($user,$pass,$soap_url);

# simple example (one arg of type string)
my $oid = '1000001';
my $order = &get_order($soap,$session_id,$oid);

# gnarly example (argument is nested arrays)
my $orders = &get_recent_invoiced_orders($soap,$session_id);

foreach my $ord (@$orders) {
    my $due = $ord->{grand_total};
    my $paid_or_authorized = $ord->{total_invoiced};
    my $owed = $due - $paid_or_authorized;
    # ignore if not yet paid
    next if ($owed > .02);
    &fulfill_order($ord);
}

exit 0;
Continue reading »

Customizing MagentoJul 08 2010 | 15:07:35 | No Comments

Quick start - here are some pages that helped me get going quickly:


Defining your own attributes is easy.  Getting them to display in a template is not.  Cheat sheet:

http://www.sharpdotinc.com/mdost/2009/04/06/magento-getting-product-attributes-values-and-labels/


Don’t hack the Magento tree directly!  You’ll be sad when you upgrade and your customizations are gone. Do this instead:

http://www.exploremagento.com/magento/override-a-magento-core-block-class.php


Grab all the simple child products for a configurable product (and then display one of their other custom attributes):

http://snippi.net/taxonomy/term/10


Updating stock through the SOAP API is unbelievably slow.  The API doesn’t appear to accept more than one SKU per call! Use a script like this instead:

http://www.sonassi.com/knowledge-base/magento-knowledge-base/mass-update-stock-levels-in-magento-fast/


Some hints on how to get Magento to generate plain-text email:

http://www.magentocommerce.com/boards/viewthread/43928/


Kicking off an index process from the command line (using a custom script):

http://www.sonassi.com/knowledge-base/magento-knowledge-base/catalog-search-index-refresh-running-slow-or-haltingfreezing/


Finding Y-Wing (aka XY-Wing)Apr 08 2010 | 17:24:50 | No Comments

Y-Wing Fighter: the force is strong with this one.

Y-Wing Fighter: the force is strong with this one.

Y-Wing can be a very handy shortcut in a tough sudoku puzzle and it may even be the only way forward in some harder ones. It’s named after an ugly rebel fighter craft from the Star Wars movies.  True to its namesake, it is not as elegant or as powerful as its X-Wing brother, but it can still do some damage.

Y-Wing can be hard to spot, so today I’ll describe a method for quickly teasing out the best ones. It boils down to this — the best Y-Wing sets are the ones where two of the three squares in the set are in the same 3×3 box!

Really, don’t bother looking for a set that’s super spread-out. It’s very rare that you’ll find one and even if you do, it’s the weakest kind: you’ll only be able to remove one candidate from one square, and that’s if you’re lucky.

Y-What?

First, let’s make sure we all know what we’re looking for. The basic idea is, you find three squares that each have two remaining candidates. The “pivot” square can see both of the end squares.  All three squares share candidates from a pool of just three total numbers, ABC. So one end of the “V” is AB, the pivot has BC, and the other end is AC.

Important detail - no two squares in the set have the same pair of candidates.  So, if you whittle down three squares to 78, 48 and 47, you’re well on your way to a Y-Wing.  If you’re looking at 47, 48 and 47, no deal.

How to spot Y-Wing / XY-Wing

How to spot Y-Wing / XY-Wing

Any square that can see both ends of the Y-Wing may eliminate candidate A from its own candidate pool. It’s an easy proof, just look at the pivot — when the puzzle is done, the pivot will either be B or C.

If the pivot is B, then the first end is not B, and must be A. In the other “reality,” if pivot is C, then the other end is not C, and must be A. Since the pivot must be either B or C, at least one of those end squares will have A.  Here’s the neat part: squares that can “see” both end squares can never be A; not in the first case, and not in the second case (and there are only two cases total).

The Claw

Some people like to think of Y-Wing as a claw.  A simple way to visualize it is, the claw’s pincers (end squares) “pinch” candidate A out of any square they can both see.  The pivot is the “hinge” of the claw.

There are two kinds of Y-Wing. One can be very helpful, and one is usually not. If the three squares in your Y-Wing are all in different boxes, you’ve found the less useful Y-Wing variant (your claw is wide open, pincers at 90 degrees).  Only one square can see both pincers - so even if we are lucky, we can only eliminate/pinch one candidate from one square.

The kind I like is when one claw pincer and the pivot share a box. Then you’ll always have more than one “pinchable” square (up to five, in fact). Fortunately, finding this second type is easier anyway.

The Method

Look at each group of 9 squares (the nine large 3×3 boxes).  In each big box, look for two squares in that box with only two candidates left each. They must share exactly one candidate.  It’s okay if they are in the same row/column but you’ll cast a wider net for the other claw if they aren’t. Found a pair like this? Good. Now look at the two candidates they don’t have in common. Let’s call them AB.  We are looking for a third square, from a different box, that has only AB left in its candidate pool. It must be able to “see” one of our original squares (yep, either one).  The square that it sees from our original box becomes the pivot.

So, we really just scan four potential zones outside our box:

• the squares in the same row as square 1

• the squares in the same row as square 2

• the squares in the same column as square 1, and finally,

• the squares in the same column as square 2

Didn’t pan out?  Move along, find another potential pivot/claw pair, maybe in this box or probably the next.  You’d be surprised how often you can track down a set of three once you focus your search like this.  See if you can find the Y-Wing below.  hint: start in box 7.

Y-Wing Example

Y-Wing Example

Continue reading »

Fishy SudokuMar 29 2010 | 10:05:23 | No Comments

Fishy Sudoku

Fishy Sudoku: Challenging puzzles. Great Interface.

Well, it’s about time I came clean.  I am a Sudoku addict.  I do two fairly difficult puzzles a day, at a site that offers free daily puzzles.  I’m getting a little tired of the puzzles on that site because they tend to block forward progress until you find one or two X-Wing sets and then you’re done.  A bit predictable after a while, and once you get good at spotting X-Wing it gets kinda boring.

So about a year ago I started making my own puzzles.  I wrote a java program that generates random puzzles, and another java program that solves the puzzles by applying various techniques in a loop, in order of difficulty.  The second program rates the puzzles made by the first program.  I wrote a third program that lets the first two programs fight it out until they come up with a tough puzzle.

Hard puzzles should have one or two X-Wing sets.  Really hard puzzles, in my opinion, need an XY-Wing or a swordfish or even a jellyfish.  Thus the name of my new daily puzzle website, www.fishysudoku.com.

I put together a nice little sudoku puzzle interface in Flash.  It addresses all the things I found really annoying about other online sudoku sites.  First of all, the pencilmarks come up automatically.  My wife insists that Sudoku is a big waste of time, and she’s right, to an extent.  The interesting parts of solving a sudoku puzzle tend to just punctuate long stretches of tedium. God forbid you make a mistake during the pencil-mark tedium.  Anyone who has tried Sudoku has made a mistake at some point that went undiscovered for the better part of an hour.  This is not a rewarding feeling.

So in my interface, autohelp is on by default.  Glaring mistakes are immediately circled until they are fixed.  Obvious pencilmarks are done for you.  But most interfaces that do this prevent additional edits to the pre-determined marks.  So in my interface, you can grey out any pencilmarks that you know to be impossible.

I have plenty of features still to add — puzzle archive, access to the solver for hints & more — but I figured it was time to do an alpha launch.

To reach a broader audience, I also boxed it up as a facebook app. Add it to your facebook apps! I need at least five people to add it before I can get listed in the app directory.

Google Sketchup v. Punch BathroomMar 05 2010 | 15:23:35 | No Comments

Winner, Google Sketchup.  Punch Bathroom Design really sucks.

Spent $10 on Punch, Sketchup is free.  Sketchup rocks.  With no training and very little ramp-up on the interface, I had a working model of our master bath remodel and was able to mock up the custom shower stall that we want to build, found 3D models of toilets, sinks, vanities, clawfoot tubs, fixtures, you name it.

Caveats: Sketchup has no 2D view (although you can swing around to a bird’s-eye view and look through the ceiling without too much trouble once you get the hang of it).  Sketchup does require some patience when your toilet is 300 feet away from where you want it and you can’t quite get Sketchup to intuit what you are doing.  That said, nine times out of ten it is unbelievably good at intuiting what you want to do.  A lesser program would misinterpret most mouse movements, but there is some intelligent engine at work in Sketchup that seems to be aware of what you are seeing and almost always does what you mean.

Punch was infuriatingly dumb.  You had to tie everything you put into the space to a wall, and if something overlapped temporarily while you were adding something else or resizing a wall, the items on that wall just get deleted.  You couldn’t just grab things and move them with the mouse, and the 3D walkthrough was impossible to navigate.  Simple things like looking right or up or backing out, making walls temporarily see-through, etc. are SO easy in Sketchup and simply not possible in Punch.

A few tips to get you started in Sketchup — you definitely want to use a three button mouse with a mouse wheel.  Rolling the wheel zooms in and out, and holding down the wheel button while you move the mouse rotates/swings your 3D model around (this is a very powerful and satisfying feeling) no matter what tool is currently active.  Press H to get the “hand” cursor (grab and slide the model in any direction), Press space to select things, hold down shift to select multiple things.  Press M to grab things and move them.  Selecting and moving a face of a box will adjust the rest of the box as necessary to remain intact.

Hide walls, ceilings, whatever, by right clicking it and selecting ‘hide’ on the popup menu.  Sketch away!

Careful kid, they’ll break ya heaht…May 15 2009 | 09:02:45 | No Comments

I have cancelled my cable TV. Man did it feel good to tell Comcast to go **** it. Net gain $840/yr. Well, minus whatever it will cost to buy episodes of Top Chef for my wife (ok I admit it, she’s got me hooked too).

tim_thomasRabid sports fan that I am, I did not take the plunge until I discovered www.myp2p.eu and the wonders of p2p live streaming sports. So far this has turned out to be a mixed bag. When it’s good, it’s mid-to-low-res but you can follow the action. Veetle is pretty cool. When it’s bad it really stinks. The players and plays are fuzzy. The score is rarely legible. The keyframes drop out and for several seconds at a time blotchy pieces of the previous shot carry over for that “they’re playing in a vat of pea soup” effect. The best is when the feed stops for buffering just when Ray Allen has released the shot that will send the game into triple overtime.

But hey, the Bruins are in the playoffs (well, until they choked in OT last night), the Celtics are in the playoffs, and I have found that I will endure quite a lot of low quality picture to be connected to the hunt for a championship.

Some nights the only available feed is from Taiwan, with audio in Taiwanese Chinese (although I have to say, I don’t speak a word and the Taiwan commercials are still hilarious).

I find it ironic that in this age of higher and higher def, my own TV viewing quality experience is moving in the opposite direction. It’s consoling to think that a generation earlier I might be slapping the side of the TV fonzie-style before a big play to try to reduce the static from a bad over-the-air signal. Now I move around the room and adjust the angle of my laptop in the hopes that our wifi is the bottleneck.

However fuzzy the broadcast, last night proved to me that they can still break your heart, and that stings in hi-def. Bruins out of the hunt. Magic force game 7 on the Celts. Sox fall in the 12th. To cap it all off, the Yanks win. I guess we had it coming?

Baby Happy, Baby SadMay 05 2009 | 22:35:37 | No Comments

You can only stomach so much Green Eggs and Ham - unless you’re two, then it never gets old - but I’m well into my 30s so I’m always pretty stoked to find a good new children’s book to bring into the mix.

A new crop of talent is staking its ground in the children’s book landscape so long dominated by the likes of Dr. Seuss and Eric Carle.  It’s a good time to be a new dad.

Mo Willems is hit or miss, but when he’s on it’s pure gold. Dont Let the Pigeon Drive the Bus, for instance, is a clear winner.  Other pigeon books in the series don’t always measure up.  My Friend is Sad is one of a new Willems series featuring the new Piggie and Elephant duo (occasionally joined by a wise squirrel). This looks to be heavily in the rotation for some months to come.

The award, however, goes to Leslie Patricelli.  Nothing tops Yummy Yucky or No No Yes Yes except maybe her tour de force Baby Happy Baby Sad.  These board books are just the right length for the short toddler attention span, and just the right size for little hands and budding imaginations.  It’s only natural for our kids to imagine themselves as the baby protagonist in these juicy canvases with ultra saturated colors and friendly, thick black lines.

Honorable mention to Byron Barton.  I don’t know if girls would be quite so excited about the Boats / Planes / Trucks trilogy, but our two boys have a whole new appreciation for transportation thanks to Mr. Barton.