Archive for October, 2008

Jedit: include macro

Monday, October 27th, 2008

Jedit macros can end up pretty verbose and complex and I really hate copy pasting. So, the solution: first set up a directory where the macros can be stored (see my previous post for that), then let’s define a require function in the startup.bsh file:

void require(View view, String filename) {
String script = file_get_contents(MACROLIBPATH + filename + ".bsh");
result = BeanShell.eval(view, BeanShell.getNameSpace(), script);
}

As you might have realized, I used file_get_contents, which is “not really java”; let’s fix it:

String file_get_contents(String filename) {
String s = "";
String ret = "";
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
while((s = br.readLine()) != null) { ret += s + "\n"; }
fr.close();
return ret;
}

Yes, I know, my beanshell scripting knowledge is not that great, but hey, it works…
Now for an example macro (call it example.bsh and save it into your macrolib directory):

prettyFunction(view, message) {
Macros.message(view, "Hi! You got a message: " + message);
}

String prettyMessage = "it works!";

Now this should work (in another macro):

require(view, "example");
prettyFunction(view, prettyMessage);

In case you know a solution to the scattered view parameters, let me know.

Jedit: platform agnostic file paths in macros

Monday, October 27th, 2008

Here’s a trick I use in my jedit beanshell macros to access common directories and files I use (for macro includes for example):

Place this into your startup.bsh:

String DS = "\\";
String DOTJEDITPATH = "Q:\\.jedit\\";
String OS = "windows";

if (System.getProperty("os.name").indexOf("Windows") == -1) {
DS = "/";
DOTJEDITPATH = "~/.jedit/";
OUTPATH = "~/.jedit/out/";
OS = "linux";
}

String OUTPATH = DOTJEDITPATH + "out" + DS;
String MACROLIBPATH = DOTJEDITPATH + "macros_libs" + DS;

My preferences are always stored in .jedit on the same drive/place (at my workplace I use subst to achieve that) which is just an extra for portability.

Getting started with Doctrine – with some headaches

Saturday, October 11th, 2008

I have downloaded Doctrine and started playing with it: no matter how bad I feel about ORMs (mostly for performance considerations), it’s pretty good. The documentation is overwhelming and not the best quality (but at least they do have documentation, which is not granted with all php projects) and sometimes the examples are not very clear, but so far it’s much a bit more fun than plain old CRUD stuff.

Just one fun example:

<?//DOCTRINE
$posts = Doctrine_Query::create()
->select('p.*, u.username, t.name')
->from('Post p')
->leftJoin('p.Tags t')
->leftJoin('p.User u')
->fetchArray();
/*MYSQL*/
SELECT post.id, post.title, user.username,
GROUP_CONCAT(tag.name SEPARATOR ', ') AS tags
FROM post_tag
JOIN tag ON tag_id = tag.id
JOIN post ON post_id = post.id
JOIN user ON post.user_id = user.id
GROUP BY post.id

While I like the MySQL code:

  • Doctrine queries are database agnostic (portability); in this case GROUP_CONCAT is cute, but you can start writing stored procedures in case you end up on MSSQL
  • Less pain with many-to-many relationships (though detect_relations didn’t help much with those)
  • Yaml everwhere: schemas, fixture imports, fixture exports

And some of my problems:

  • the documentation is really half finished (missing sections) and the examples sometimes are very annoying (naming schemes implicate different relationship, unnormalized forms on purpose etc.).
  • detect_relations as far as I know sill can not deal with plurals (if a User has many Posts) – this seems to be a small issue, but it is not, since most of the examples are trying to represent relatinships here.
  • the schema files‘ documentation is very basic and there are places where it contradicts with the Relations section.
  • now it’s may be me, but a slightly more complex table structure and groupbys are a good recipe for headache (the relevant DQL doc part is empty as of this writing).
  • probably it’s me again, but I miss the subqueris with non left joins – I guess it is because old habits die hard, but the object to relation mapping with Doctrine feels like an overkill: the generated queries while are not big or complex, they fail miserably in cases where I can write simple SQL single handedly.

VirtualBox virtual machine backup/restore – FAIL

Friday, October 10th, 2008

Okay, it may not be Sun, I really don’t care, but backing up my VDI images (which I used for testing) always worked. Restored the VDI, created a new vm and everything was back normal. After a couple of months now I do need some of the machines I saved, so let’s restore (in the meantime Sun aquired VirtualBox from/with Innotek): BSOD. Shall we have an “official” backup/restore mechanism, I would’ve used that, but there is no such thing;
dear Sun: instead of adding nice candy icons, please add features instead.

Doctrine vs Codeigniter (classname collitions)

Sunday, October 5th, 2008

Doctrine is really nice, but upon generating schemas the generated classes are a bit “general”. For example I have a User, a Post and a PostTag table, after building the schema I will end up with similar classnames (no, renaming does not work). Yes, this is cute on one hand (new User), but on the other if I happen to have a User class in my controllers (not terribly hard with Codeigniter) then whoops, I have a classname collition where finetuning the autoloader will not help. Possible solutions:

  • rename the generated models: not a good idea, will throw nice fat errors (‘Couldn’t find class User’), there maybe a solution for this, but I really doubt it
  • rename the controller: pain in the back with Codeigniter (and routing), no thanks
  • finetune CI so that Controllers have a distinct name (UserController), with routing made aware of this fact (/user/ routes to UserController); this is buried in the CI core code, no thanks again. Also User_Controller would be nice, Kohana seemed to understand this.
  • set up custom routes for conflicting classes: CI route-manager is a bit dumb, finetuning routing for all cases does hurt. One can set up url rewriting too, but that’s even more pain in the back, because of the internal routing and redirection schemes.
  • use namespaces, which is possible with PHP5.3 (currently alpha 2), but namespacing the whole Doctrine system and ironing out class declaration problems is beyond my knowledge (though I must admit it, I tried).