createDiscussionDocument(db, null, users, new int[] { 0 }, disc_rootDocs);
}
void createDiscussionDocument(Database db, Document parent, ArrayList<String> users, int[] pos, int nDoc)
throws IOException {
DateTime date = db.getParent().createDateTime(new Date());
String[] loremIpsum = SampleDataUtil.readLoremIpsum();
for (int j = 0; j < nDoc; j++) {
pos[pos.length - 1] = j + 1;
Document doc = db.createDocument();
doc.replaceItemValue("Form", "Discussion");
StringBuilder b = new StringBuilder();
for (int i = 0; i < pos.length; i++) {
if (i > 0) {
b.append("/");
}
b.append(pos[i]);
}
int idx = (int) (Math.random() * (loremIpsum.length - 1));
String body = loremIpsum[idx];
int dot = body.indexOf('.');
if (dot < 0) {
dot = body.length() - 1;
}
int coma = body.indexOf(',');
if (coma < 0) {
coma = body.length() - 1;
}
String title = body.substring(0, Math.min(dot, coma));
// Get a random author
int x = Math.min((int) (Math.random() * (users.size())), users.size());
String author = users.get(x);
doc.replaceItemValue("Title", title);
doc.replaceItemValue("Body", body);
doc.replaceItemValue("Author", author);
doc.replaceItemValue("Date", date);
if (parent != null) {
doc.makeResponse(parent);
}
doc.computeWithForm(false, false);
doc.save();
if (pos.length < disc_maxDepth) {
double r = Math.random();
if (r <= (1.0 / pos.length)) {
int[] newPos = new int[pos.length + 1];
System.arraycopy(pos, 0, newPos, 0, pos.length);
int n = (int) (Math.random() * 5);
createDiscussionDocument(db, doc, users, newPos, n);
}
}
// Move the date to the day before if requested
boolean mvd = Math.random() <= 0.40;
if (mvd) {
// Previous day...
date.adjustDay(-1);
}
}
}