Zend Framework, Existst Validator


I've started to use Zend Framework in one of my web project. And here is existst Validator. I've noticed that ZF doesn't have that basic validator as exists.



This validator check if value existst in database. Two parametrs have to be given in constructor:


  • tableName - name of model class in which validator check value

  • fieldName - field name to compare in specified table


Validator use model class insted of database table name.



/**
* @author: teodor
* @date: 2008-07-04
* @description: PHP Class
*/

class Debra_Validate_Exists extends Zend_Validate_Abstract {
const MSG_EXISTS = "msg_exists";

protected $_messageTemplates = array(
self::MSG_EXISTS => "'%value%' already exists"
);

public $tableName;
public $fieldName;
/**
* Create Exists Validator. Check if spec value exists in database
*
* @param string $tableName
* @param string $fieldName
*/
public function __construct($tableName,$fieldName) {
$this->tableName = $tableName;
$this->fieldName = $fieldName;
}
/**
* Check if $value is valid
*
* @param unknown_type $value
* @return unknown
*/
public function isValid($value) {
$this->_setValue($value);
$tableName = new $this->tableName;
$select = $tableName->select()->where($this->fieldName . ' = ?',$value);
$rows = $tableName->fetchRow($select);
if(!empty($rows)) {
$this->_error();
return false;
}
return true;
}

}

Problem with SVN


It looks like that I forget about this blog. Well in some part this is true. I've got a lot to do on my studies and apart of them. But today it' s beautiful day, 30 ÂșC in Poznan, very nice weather, you know ;) every geeks and nerds love that weather. Back to root topic



Yesterday I was fighting with very strange bug. I create new repository


svnadmin create /home/teodor/svnroot

and next try to commit something to this repo I got some nasty and unclear bug

svn: Commit failed (details follow):
svn: Can't open activity db: APR does not understand this error code



in apache logs there was of course nothing special, like everytime. But previous I got error
that it couldn't save something in db/transaction path. I've change persmisons and that bug was fixed, so also this time this bug was linked with file persmions. svn running under mod_dav module have to be able to store some files under transaction/ directory.

  1. First we have to add www-data user to the same group like svnroot have. In my case it was teodor group (I working under Debian OS, every user has own group, it's very nice construction for admin e.g. www servers), so I added www-data to teodor group
  2. Next we have to change owners of all files under svnroot/,
    chown -R teodor:www-data *
    in svnroot/ directory
  3. And last add read/write permision to user/group :
    chmod -R 0770 *
    in svnroot/
That's all, after that everything should work correct. Have nice day ;)

Microphone on Ubuntu Gutsy and HP 6710s

Yesterday at last I've figured how to properly configure microphone on ubuntu gutsy. Yes, it's sounds ridiculous, what is difficult in configuring microphone. Indeed it not so simple, if we can't find what's going on. I have HP 6710s laptop with sound card (lspci -v)
00:1b.0 Audio device: Intel Corporation 82801H (ICH8 Family) HD Audio Controller (rev 03)
and Ubuntu 7.10 Gutsy. Here are my settings from gnome-volume-manager (screenshots) that works with skype.


Arithmetic operation on a date


Next post about very obvious thing, but I've lost about 30 minutes to find out how to do it. So this post is some kind of note, for my information.
The problem was, how to get yesterday date from datefield. I use very nice component DataPicker from Microba Controls library which deliver calendar. Ok but back to topic. How to get a day before some date selected on that component, or day after, or back to today's date. Example:



// -1 day
try {
Calendar c = Calendar.getInstance();
c.setTime(dateField.getDate()); // set calendar date from field
c.add(Calendar.DATE, -1); // substract one day
dateField.setDate(c.getTime());
} catch (PropertyVetoException ex) {
ex.printStackTrace();
}
// today
dateField.setDate(new Date());
// + 1 day
try {
Calendar c = Calendar.getInstance();
c.setTime(dateField.getDate()); // set calendar date from field
c.add(Calendar.DATE, 1); // add one day
dateField.setDate(c.getTime());
} catch (PropertyVetoException ex) {
ex.printStackTrace();
}

JPA, Postgresql and auto increment id attribute


I'm still writing this stupid frontend for database. Really it's confusing when have to write piece of software only for student project. Never mind. I've left idea with web app written in jsp/jsf (god's know what else) and 've started a new project. It's also would be tv schedule, but now it would be a desktop app written using netbeans, swing, postgresql and jpa.
So, todays problem. How to insert new object into database? I've written the front controller for a model, with method addStacja that get a Stacjtv object as an argument.



public boolean addStacjatv(Stacjatv st) {
em = getEntityManager();
try {
em.getTransaction().begin();
em = getEntityManager();
try {
em.getTransaction().begin();
em.persist(st);
em.getTransaction().commit();
return true;
} finally {
em.close();
} em.persist(st);
em.getTransaction().commit();
return true;
} finally {
em.close();
}
}


simple code, nothing special. But every time, when I invoke it, I get exception:


Internal Exception: org.postgresql.util.PSQLException: ERROR: null value in column "id" violates not-null constraint
Error Code: 0
Call: INSERT INTO stacjatv (id, url, opis, nazwa) VALUES (?, ?, ?, ?)
bind => [null, fff, fff, ff]

well, Postgresql make auto increment using sequence. JPA doesn't know about it. I've googled and found solution in JPA Toplink Documentation. We have to tell JPA that we want to use sequence to generate next id value of primary key, so to solve the problem, we have to make some modification in model class.
I had to change Stacjatv.java which was generated by Netbeans creator. New version looks like it:

...
@Id
// start modyfication
@GeneratedValue(generator="stacjatv_id_seq",strategy=GenerationType.SEQUENCE)
// end
@Column(name = "id", nullable = false)
private Integer id;
...