Kawai Wiki

Compare Revisions

Ignore whitespace Rev 77 → Rev 78

/trunk/src/main/java/org/kawai/model/Revision.java
New file
0,0 → 1,98
package org.kawai.model;
 
public class Revision {
private int id;
private int revision;
private String name;
private int languageId;
private int userId;
private User user;
private String oldTitle;
private String newTitle;
private String oldBody;
private String newBody;
public Revision() { }
 
public int getId() {
return id;
}
 
public void setId(int id) {
this.id = id;
}
 
public int getRevision() {
return revision;
}
 
public void setRevision(int revision) {
this.revision = revision;
}
 
public String getName() {
return name;
}
 
public void setName(String name) {
this.name = name;
}
 
public int getLanguageId() {
return languageId;
}
 
public void setLanguageId(int languageId) {
this.languageId = languageId;
}
 
public int getUserId() {
return userId;
}
 
public void setUserId(int userId) {
this.userId = userId;
}
 
public User getUser() {
return user;
}
 
public void setUser(User user) {
this.user = user;
}
 
public String getOldTitle() {
return oldTitle;
}
 
public void setOldTitle(String oldTitle) {
this.oldTitle = oldTitle;
}
 
public String getNewTitle() {
return newTitle;
}
 
public void setNewTitle(String newTitle) {
this.newTitle = newTitle;
}
 
public String getOldBody() {
return oldBody;
}
 
public void setOldBody(String oldBody) {
this.oldBody = oldBody;
}
 
public String getNewBody() {
return newBody;
}
 
public void setNewBody(String newBody) {
this.newBody = newBody;
}
}
/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);
}
}
}
/trunk/src/main/java/org/kawai/AppManager.java
10,6 → 10,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.DBTypes;
import org.mentabean.util.SQLUtils;
127,6 → 128,19
.field("createdOn", "created_on", DBTypes.NOW_ON_INSERT_TIMESTAMP)
.field("name", DBTypes.STRING)
.field("languageId", "language_id", DBTypes.INTEGER);
bean(Revision.class, "Revisions")
.pk("id", DBTypes.AUTOINCREMENT)
.field("userId", "user_id", DBTypes.INTEGER)
.field("newTitle", "new_title", DBTypes.STRING)
.field("newBody", "new_body", DBTypes.STRING)
.field("oldTitle", "old_title", DBTypes.STRING)
.field("oldBody", "old_body", DBTypes.STRING)
.field("createdOn", "created_on", DBTypes.AUTOTIMESTAMP)
.field("name", DBTypes.STRING)
.field("languageId", "language_id", DBTypes.INTEGER)
.field("revision", DBTypes.INTEGER);
 
}
@Override
/trunk/src/main/java/org/kawai/action/PageAction.java
8,6 → 8,7
import org.kawai.model.Language;
import org.kawai.model.Page;
import org.kawai.model.Preview;
import org.kawai.model.Revision;
import org.mentawai.filter.AuthenticationFree;
import org.mentawai.rule.MethodRule;
import org.mentawai.rule.RegexRule;
223,10 → 224,17
if (preview != null) {
Page page = pageDAO.loadByName(name, lang);
if (page != null) {
String oldTitle = page.getTitle();
String oldBody = page.getBody();
page.setTitle(preview.getTitle());
page.setBody(preview.getBody());
page.setModifiedById(preview.getUserId());
pageDAO.update(page);
insertRevision(page, oldTitle, oldBody);
pageDAO.deletePreview(userId, name, lang);
} else {
page = preview.getPage();
238,6 → 246,18
return SUCCESS;
}
private void insertRevision(Page page, String oldTitle, String oldBody) {
Revision r = new Revision();
r.setName(page.getName());
r.setLanguageId(page.getLanguageId());
r.setUserId(sessionUser.getId());
r.setNewTitle(page.getTitle());
r.setNewBody(page.getBody());
r.setOldTitle(oldTitle);
r.setOldBody(oldBody);
pageDAO.insertRevision(r);
}
public String edit() {
if (!isPost()) return INDEX;
String name = input.getString("name");
248,12 → 268,21
}
Page page = pageDAO.loadByName(name, Language.fromString(lang));
if (page != null) {
String oldTitle = page.getTitle();
String oldBody = page.getBody();
input.inject(page);
page.setModifiedById(sessionUser.getId());
if (input.getBoolean("isPreview")) {
pageDAO.insertPreview(sessionUser.getId(), page);
} else {
pageDAO.update(page);
insertRevision(page, oldTitle, oldBody);
// and now you have to delete the preview!
pageDAO.deletePreview(sessionUser.getId(), page.getName(), page.getLanguage());
}