Boolean zazl = req.getParameter("zazl") != null;
String[] emails = emailsStr.split(",");
List<Reviewer> reviewers = new ArrayList<Reviewer>();
IDesignerUser du = ReviewManager.getReviewManager().getDesignerUser(user);
if (!isUpdate) {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(Constants.DATE_PATTERN_SHORT);
formatter.setCalendar(Calendar.getInstance(new SimpleTimeZone(0, "GMT")));
String timeVersion = formatter.format(currentTime);
String id = null;
int latestVersionID = 1;
if (du.getLatestVersion() == null|| du.getVersion(du.getLatestVersion().getTime()) == null) {
List<Version> versions = du.getVersions();
for(Version temp: versions){
int i = Integer.parseInt(temp.getVersionID());
if (i > latestVersionID) {
latestVersionID = i;
}
}
id=latestVersionID+"";
} else {
int latestID = Integer.parseInt(du.getLatestVersion().getVersionID());
id = latestID + 1 + "";
}
version = new Version(id, timeVersion, savingDraft, dueDate,
desireWidth, desireHeight);
du.addVersion(version);
du.setLatestVersion(version);
} else {
version = du.getVersion(vTime);
//NOTE: In theory, it would be good to remove the review any reviewers
//no longer part of the review and persist the changes
}
//Deal with reviewers the designer has added to the review
ReviewerVersion reviewerVersion = new ReviewerVersion(user.getUserID(), version.getTime());
Reviewer tmpReviewer = null;
for (int i = 0; i < emails.length; i++) {
String email = emails[i];
if (!email.equals(user.getPerson().getEmail())) {
tmpReviewer = ReviewManager.getReviewManager().getReviewer("", email);
tmpReviewer.addReviewerVersion(reviewerVersion);
reviewers.add(tmpReviewer);
}
}
//Add the designer as a reviewer
tmpReviewer = ReviewManager.getReviewManager().getReviewer(user.getUserID(), user.getPerson().getEmail());
tmpReviewer.addReviewerVersion(reviewerVersion);
reviewers.add(tmpReviewer);
//Handle fake reviewer (if necessary)
String fakeReviewer = ServerManager.getServerManager().getDavinciProperty(Constants.FAKE_REVIEWER);
if (fakeReviewer != null) {
tmpReviewer = ReviewManager.getReviewManager().getReviewer("fakeReviewer", fakeReviewer);
tmpReviewer.addReviewerVersion(reviewerVersion);
reviewers.add(tmpReviewer);
}
version.setDraft(savingDraft);
version.setDueDate(dueDate);
version.setDesireWidth(desireWidth);
version.setDesireHeight(desireHeight);
version.setReviewers(reviewers);
version.setVersionTitle(versionTitle);
if (resources != null) {
version.setResource(resources);
}
version.setHasClosedManually(false);
version.setDescription(message);
version.setReceiveEmail(receiveEmail);
if (isRestart) {
version.setRestartFrom(vTime);
du.getVersion(vTime).setHasRestarted(true);
DavinciProject project = new DavinciProject();
project.setOwnerId(du.getName());
ReviewCacheManager.$.republish(project,vTime, version);
}
JSONWriter writer = new JSONWriter(true);
writer.startObject();
writer.addField("result", "OK");
writer.addField("version", version.getTime());
writer.addField("designer", du.getName());
if (savingDraft) {
ReviewManager.getReviewManager().saveDraft(du, version);
} else {
ReviewManager.getReviewManager().publish(du, version);