Kawai Wiki

Compare Revisions

Ignore whitespace Rev 77 → Rev 78

/trunk/src/main/java/org/kawai/dao/PageDAO.java
5,6 → 5,7
import org.kawai.model.Language;
import org.kawai.model.Page;
import org.kawai.model.Preview;
import org.kawai.model.Revision;
 
public interface PageDAO {
37,4 → 38,8
public Preview insertPreview(int userId, Page p);
public void deletePreview(int userId, String name, Language lang);
public Revision loadRevision(int revision, String name, Language lang);
public void insertRevision(Revision r);
}
/trunk/src/main/java/org/kawai/dao/jdbc/JdbcPageDAO.java
9,6 → 9,7
import org.kawai.model.Language;
import org.kawai.model.Page;
import org.kawai.model.Preview;
import org.kawai.model.Revision;
import org.kawai.model.User;
import org.mentabean.BeanSession;
 
295,4 → 296,48
session.delete(p);
}
}
@Override
public Revision loadRevision(int revision, String name, Language lang) {
Revision r = new Revision();
r.setRevision(revision);
r.setName(name);
r.setLanguageId(lang.getId());
return session.loadUnique(r);
}
@Override
public void insertRevision(Revision r) {
PreparedStatement stmt = null;
ResultSet rset = null;
try {
// first time, lock whole table
stmt = conn.prepareStatement("LOCK TABLES Revisions WRITE"); // this works as a synchronized block locked on the database... (I tested it!)
stmt.execute();
stmt.close();
stmt = conn.prepareStatement("select max(revision) from Revisions where name = ? and language_id = ?");
stmt.setString(1, r.getName());
stmt.setInt(2, r.getLanguageId());
rset = stmt.executeQuery();
rset.next();
int revisionNumber = rset.getInt(1) + 1;
rset.close();
stmt.close();
r.setRevision(revisionNumber);
session.insert(r);
 
stmt = conn.prepareStatement("UNLOCK TABLES");
stmt.execute();
} catch(Exception e) {
throw new RuntimeException(e);
} finally {
close(stmt, rset);
}
}
}