Add support for set() method to Key, and use fromString() for URLs

This way we have a consistent URL encoding for any entity key.

Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
Shawn O. Pearce
2008-12-29 09:52:42 -08:00
parent 2e3b5190b2
commit 1371e9c561
21 changed files with 168 additions and 44 deletions

View File

@@ -25,10 +25,9 @@ import com.google.gerrit.client.patches.PatchUnifiedScreen;
import com.google.gerrit.client.reviewdb.Account; import com.google.gerrit.client.reviewdb.Account;
import com.google.gerrit.client.reviewdb.Change; import com.google.gerrit.client.reviewdb.Change;
import com.google.gerrit.client.reviewdb.Patch; import com.google.gerrit.client.reviewdb.Patch;
import com.google.gerrit.client.reviewdb.PatchSet;
import com.google.gerrit.client.rpc.RpcUtil; import com.google.gerrit.client.rpc.RpcUtil;
import com.google.gerrit.client.ui.Screen; import com.google.gerrit.client.ui.Screen;
import com.google.gwt.http.client.URL; import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.HistoryListener; import com.google.gwt.user.client.HistoryListener;
public class Link implements HistoryListener { public class Link implements HistoryListener {
@@ -51,7 +50,7 @@ public class Link implements HistoryListener {
} }
public static String toChange(final Change.Id c) { public static String toChange(final Change.Id c) {
return "change," + c.get(); return "change," + c.toString();
} }
public static String toAccountDashboard(final AccountInfo acct) { public static String toAccountDashboard(final AccountInfo acct) {
@@ -59,7 +58,7 @@ public class Link implements HistoryListener {
} }
public static String toAccountDashboard(final Account.Id acct) { public static String toAccountDashboard(final Account.Id acct) {
return "dashboard," + acct.get(); return "dashboard," + acct.toString();
} }
public static String toPatchSideBySide(final Patch.Id id) { public static String toPatchSideBySide(final Patch.Id id) {
@@ -71,14 +70,18 @@ public class Link implements HistoryListener {
} }
public static String toPatch(final String type, final Patch.Id id) { public static String toPatch(final String type, final Patch.Id id) {
final PatchSet.Id psId = id.getParentKey(); return "patch," + type + "," + id.toString();
final Change.Id chId = psId.getParentKey();
final String encp = encodePath(id.get());
return "patch," + type + "," + chId.get() + "," + psId.get() + "," + encp;
} }
public void onHistoryChanged(final String token) { public void onHistoryChanged(final String token) {
final Screen s = select(token); Screen s;
try {
s = select(token);
} catch (RuntimeException err) {
GWT.log("Error parsing history token: " + token, err);
s = null;
}
if (s != null) { if (s != null) {
Gerrit.display(s); Gerrit.display(s);
} else { } else {
@@ -87,44 +90,44 @@ public class Link implements HistoryListener {
} }
private Screen select(final String token) { private Screen select(final String token) {
if (token == null) String p;
return null;
else if (SETTINGS.equals(token)) if (token == null) {
return null;
}
if (SETTINGS.equals(token)) {
return new AccountSettings(); return new AccountSettings();
}
else if (MINE.equals(token)) if (MINE.equals(token)) {
return new AccountDashboardScreen(RpcUtil.getAccountId()); return new AccountDashboardScreen(RpcUtil.getAccountId());
}
else if (MINE_STARRED.equals(token)) if (MINE_STARRED.equals(token)) {
return new MineStarredScreen(); return new MineStarredScreen();
else if (token.startsWith("patch,sidebyside,"))
return new PatchSideBySideScreen(patchId(token));
else if (token.startsWith("patch,unified,"))
return new PatchUnifiedScreen(patchId(token));
else if (token.matches("^change,\\d+$")) {
final String id = token.substring("change,".length());
return new ChangeScreen(Change.Id.fromString(id));
} }
else if (token.matches("^dashboard,\\d+$")) { p = "patch,sidebyside,";
final String id = token.substring("dashboard,".length()); if (token.startsWith(p))
return new AccountDashboardScreen(new Account.Id(Integer.parseInt(id))); return new PatchSideBySideScreen(Patch.Id.parse(skip(p, token)));
} p = "patch,unified,";
if (token.startsWith(p))
return new PatchUnifiedScreen(Patch.Id.parse(skip(p, token)));
p = "change,";
if (token.startsWith(p))
return new ChangeScreen(Change.Id.parse(skip(p, token)));
p = "dashboard,";
if (token.matches(p))
return new AccountDashboardScreen(Account.Id.parse(skip(p, token)));
return null; return null;
} }
private static Patch.Id patchId(final String token) { private static String skip(final String prefix, final String in) {
final String[] p = token.split(","); return in.substring(prefix.length());
final Change.Id cId = Change.Id.fromString(p[2]);
final PatchSet.Id psId = new PatchSet.Id(cId, Integer.parseInt(p[3]));
return new Patch.Id(psId, URL.decodeComponent(p[4]));
} }
private static native String encodePath(String path) /*-{ return encodeURIComponent(path).replace(/%20/g, "+").replace(/%2F/g, "/"); }-*/;
} }

View File

@@ -37,6 +37,18 @@ public final class Account {
public int get() { public int get() {
return id; return id;
} }
@Override
protected void set(int newValue) {
id = newValue;
}
/** Parse an Account.Id out of a string representation. */
public static Id parse(final String str) {
final Id r = new Id();
r.fromString(str);
return r;
}
} }
@Column @Column

View File

@@ -44,6 +44,11 @@ public final class AccountExternalId {
public String get() { public String get() {
return externalId; return externalId;
} }
@Override
protected void set(String newValue) {
externalId = newValue;
}
} }
@Column(name = Column.NONE) @Column(name = Column.NONE)

View File

@@ -37,6 +37,11 @@ public final class AccountGroup {
public String get() { public String get() {
return name; return name;
} }
@Override
protected void set(String newValue) {
name = newValue;
}
} }
/** Synthetic key to link to within the database */ /** Synthetic key to link to within the database */
@@ -55,6 +60,11 @@ public final class AccountGroup {
public int get() { public int get() {
return id; return id;
} }
@Override
protected void set(int newValue) {
id = newValue;
}
} }
@Column @Column

View File

@@ -46,6 +46,11 @@ public final class AccountSshKey {
public int get() { public int get() {
return seq; return seq;
} }
@Override
protected void set(int newValue) {
seq = newValue;
}
} }
@Column(name = Column.NONE) @Column(name = Column.NONE)

View File

@@ -35,6 +35,11 @@ public final class ApprovalCategory {
public String get() { public String get() {
return id; return id;
} }
@Override
protected void set(String newValue) {
id = newValue;
}
} }
@Column @Column

View File

@@ -44,6 +44,11 @@ public final class ApprovalCategoryValue {
public short get() { public short get() {
return value; return value;
} }
@Override
protected void set(short newValue) {
value = newValue;
}
} }
@Column(name = Column.NONE) @Column(name = Column.NONE)

View File

@@ -42,6 +42,11 @@ public final class Branch {
return branchName; return branchName;
} }
@Override
protected void set(String newValue) {
branchName = newValue;
}
@Override @Override
public Project.NameKey getParentKey() { public Project.NameKey getParentKey() {
return projectName; return projectName;
@@ -76,6 +81,11 @@ public final class Branch {
public int get() { public int get() {
return id; return id;
} }
@Override
protected void set(int newValue) {
id = newValue;
}
} }
@Column(name = Column.NONE) @Column(name = Column.NONE)

View File

@@ -38,9 +38,16 @@ public final class Change {
return id; return id;
} }
@Override
protected void set(int newValue) {
id = newValue;
}
/** Parse a Change.Id out of a string representation. */ /** Parse a Change.Id out of a string representation. */
public static Id fromString(final String str) { public static Id parse(final String str) {
return new Id(Integer.parseInt(str)); final Id r = new Id();
r.fromString(str);
return r;
} }
} }

View File

@@ -46,6 +46,11 @@ public final class ChangeMessage {
public String get() { public String get() {
return uuid; return uuid;
} }
@Override
protected void set(String newValue) {
uuid = newValue;
}
} }
@Column(name = Column.NONE) @Column(name = Column.NONE)

View File

@@ -38,6 +38,11 @@ public final class ContributorAgreement {
public int get() { public int get() {
return id; return id;
} }
@Override
protected void set(int newValue) {
id = newValue;
}
} }
@Column @Column

View File

@@ -44,6 +44,18 @@ public final class Patch {
public String get() { public String get() {
return fileName; return fileName;
} }
@Override
protected void set(String newValue) {
fileName = newValue;
}
/** Parse a Patch.Id out of a string representation. */
public static Id parse(final String str) {
final Id r = new Id();
r.fromString(str);
return r;
}
} }
public static enum ChangeType { public static enum ChangeType {

View File

@@ -46,6 +46,10 @@ public final class PatchContent {
return KeyUtil.encode(sha1); return KeyUtil.encode(sha1);
} }
public void fromString(final String in) {
sha1 = KeyUtil.decode(in);
}
public com.google.gwtorm.client.Key<?> getParentKey() { public com.google.gwtorm.client.Key<?> getParentKey() {
return null; return null;
} }

View File

@@ -46,6 +46,11 @@ public final class PatchLineComment {
public String get() { public String get() {
return uuid; return uuid;
} }
@Override
protected void set(String newValue) {
uuid = newValue;
}
} }
protected static final char STATUS_DRAFT = 'd'; protected static final char STATUS_DRAFT = 'd';

View File

@@ -44,6 +44,18 @@ public final class PatchSet {
public int get() { public int get() {
return patchSetId; return patchSetId;
} }
@Override
protected void set(int newValue) {
patchSetId = newValue;
}
/** Parse a PatchSet.Id out of a string representation. */
public static Id parse(final String str) {
final Id r = new Id();
r.fromString(str);
return r;
}
} }
@Column(name = Column.NONE) @Column(name = Column.NONE)

View File

@@ -44,6 +44,11 @@ public final class PatchSetAncestor {
public int get() { public int get() {
return position; return position;
} }
@Override
protected void set(int newValue) {
position = newValue;
}
} }
@Column(name = Column.NONE) @Column(name = Column.NONE)

View File

@@ -37,6 +37,11 @@ public final class Project {
public String get() { public String get() {
return name; return name;
} }
@Override
protected void set(String newValue) {
name = newValue;
}
} }
/** Synthetic key to link to within the database */ /** Synthetic key to link to within the database */
@@ -55,6 +60,11 @@ public final class Project {
public int get() { public int get() {
return id; return id;
} }
@Override
protected void set(int newValue) {
id = newValue;
}
} }
@Column @Column

View File

@@ -34,6 +34,11 @@ public final class SystemConfig {
public String get() { public String get() {
return VALUE; return VALUE;
} }
@Override
protected void set(final String newValue) {
assert get().equals(newValue);
}
} }
public static SystemConfig create() { public static SystemConfig create() {

View File

@@ -39,8 +39,9 @@ import java.util.ArrayList;
/** /**
* Recreates PatchSet and Patch entities for the changes supplied. * Recreates PatchSet and Patch entities for the changes supplied.
* <p> * <p>
* Takes on input strings of the form <code>change_id|patch_set_id</code>, such * Takes on input strings of the form <code>change_id|patch_set_id</code> or
* as might be created by the following PostgreSQL database dump: * <code>change_id,patch_set_id</code>, such as might be created by the
* following PostgreSQL database dump:
* *
* <pre> * <pre>
* psql reviewdb -tAc 'select change_id,patch_set_id from patch_sets' * psql reviewdb -tAc 'select change_id,patch_set_id from patch_sets'
@@ -52,17 +53,15 @@ import java.util.ArrayList;
public class ReimportPatchSets { public class ReimportPatchSets {
public static void main(final String[] argv) throws OrmException, public static void main(final String[] argv) throws OrmException,
XsrfException, IOException { XsrfException, IOException {
final GerritServer gs = GerritServer.getInstance();
final ArrayList<PatchSet.Id> todo = new ArrayList<PatchSet.Id>(); final ArrayList<PatchSet.Id> todo = new ArrayList<PatchSet.Id>();
final BufferedReader br = final BufferedReader br =
new BufferedReader(new InputStreamReader(System.in)); new BufferedReader(new InputStreamReader(System.in));
String line; String line;
while ((line = br.readLine()) != null) { while ((line = br.readLine()) != null) {
final String[] idstr = line.split("\\|"); todo.add(PatchSet.Id.parse(line.replace('|', ',')));
todo.add(new PatchSet.Id(Change.Id.fromString(idstr[0]), Integer
.parseInt(idstr[1])));
} }
final GerritServer gs = GerritServer.getInstance();
final ReviewDb db = gs.getDatabase().open(); final ReviewDb db = gs.getDatabase().open();
final ProgressMonitor pm = new TextProgressMonitor(); final ProgressMonitor pm = new TextProgressMonitor();
try { try {

View File

@@ -156,7 +156,7 @@ public class UrlRewriteFilter implements Filter {
return false; return false;
} }
final Change.Id id = Change.Id.fromString(m.group(1)); final Change.Id id = Change.Id.parse(m.group(1));
final StringBuffer url = toGerrit(req); final StringBuffer url = toGerrit(req);
url.append('#'); url.append('#');
url.append(Link.toChange(id)); url.append(Link.toChange(id));

View File

@@ -181,7 +181,7 @@ class Receive extends AbstractGitCommand {
if (m.matches()) { if (m.matches()) {
// The referenced change must exist and must still be open. // The referenced change must exist and must still be open.
// //
final Change.Id changeId = Change.Id.fromString(m.group(1)); final Change.Id changeId = Change.Id.parse(m.group(1));
parseNewPatchSetCommand(cmd, changeId); parseNewPatchSetCommand(cmd, changeId);
continue; continue;
} }