boolean asDraft,
boolean validateOnly)
throws Exception {
// initialize
MessageBroker msgBroker = extractMessageBroker();
Schema schema = null;
// find the metadata editor form
UIViewRoot root = getContextBroker().extractViewRoot();
UIComponent editorForm = root.findComponent("mdEditor");
if (editorForm == null) {
String sMsg = "Programming error: The mdEditor form connot be located.";
throw new SchemaException(sMsg);
}
// determine the currently open schema
String sOpenSchemaKey = getOpenSchemaKey();
if (sOpenSchemaKey.length() == 0) {
String sMsg = "Programming error: The openSchemaKey was not specified.";
throw new SchemaException(sMsg);
} else {
MetadataDocument document = new MetadataDocument();
schema = document.prepareForCreate(context,sOpenSchemaKey);
}
// un-bind editor values, validate the input
UiContext uiContext = new UiContext();
schema.unBind(uiContext,editorForm);
try {
if (!asDraft) schema.validate();
} catch (ValidationException e) {
for (ValidationError error: e.getValidationErrors()) {
if (error.getSection() != null) {
if (!error.getSection().getOpen()) {
error.getSection().forceOpen(editorForm);
}
}
}
throw e;
}
// update the document
Document dom = schema.loadTemplate();
schema.update(dom);
String sXml = XmlIoUtil.domToString(dom);
getLogger().finer("Updated template xml:\n"+sXml);
// prepare the publisher
getSelectablePublishers().setSelectedKey(getOnBehalfOf());
Publisher publisher = getSelectablePublishers().selectedAsPublisher(context,false);
// the document
if (validateOnly) {
// handle a validation only request
ValidationRequest request = new ValidationRequest(context,null,sXml);
request.verify();
msgBroker.addSuccessMessage("catalog.publication.success.validated");
} else {
// publish the document
EditorRequest request = new EditorRequest(context,publisher,sXml);
request.getPublicationRecord().setUuid(getOpenDocumentUuid());
if (asDraft) {
request.getPublicationRecord().setApprovalStatus(MmdEnums.ApprovalStatus.draft.toString());
}
request.publish();
setOpenDocumentUuid(request.getPublicationRecord().getUuid());
if (asDraft) {
msgBroker.addSuccessMessage("catalog.publication.success.draftSaved");
} else {
if (request.getPublicationRecord().getWasDocumentReplaced()) {
msgBroker.addSuccessMessage("catalog.publication.success.replaced");
} else {
msgBroker.addSuccessMessage("catalog.publication.success.created");
}
}
}
}