* @param site May be {@code null}.
* @param user May be {@code null}.
* @return Can be used to revert all changes. May be {@code null}.
*/
public static History publish(Object object, Site site, ToolUser user) {
State state = State.getInstance(object);
ObjectModification contentData = state.as(ObjectModification.class);
Site.ObjectModification siteData = state.as(Site.ObjectModification.class);
if (object instanceof Site) {
site = (Site) object;
siteData.setOwner(site);
} else if (state.isNew() &&
siteData.getOwner() == null) {
siteData.setOwner(site);
}
Date now = new Date();
Date publishDate = contentData.getPublishDate();
ToolUser publishUser = contentData.getPublishUser();
if (publishDate == null) {
contentData.setPublishDate(now);
}
if (publishUser == null) {
contentData.setPublishUser(user);
}
contentData.setUpdateDate(now);
contentData.setUpdateUser(user);
if (object instanceof Draft) {
state.save();
return null;
} else {
try {
History history = new History(user, object);
state.beginWrites();
state.save();
history.save();
state.commitWrites();
return history;
} finally {
state.endWrites();
}
}
}