use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.
the class DocumentMergeImporter method askDocumentToSave.
private XWikiDocument askDocumentToSave(XWikiDocument currentDocument, XWikiDocument previousDocument, XWikiDocument nextDocument, XWikiDocument mergedDocument, PackageConfiguration configuration, MergeResult documentMergeResult) {
// Indicate future author to whoever is going to answer the question
if (currentDocument != null) {
nextDocument.setCreatorReference(currentDocument.getCreatorReference());
}
if (mergedDocument != null) {
mergedDocument.setCreatorReference(currentDocument.getCreatorReference());
}
DocumentReference userReference = configuration != null ? configuration.getUserReference() : null;
if (userReference != null) {
nextDocument.setAuthorReference(userReference);
nextDocument.setContentAuthorReference(userReference);
for (XWikiAttachment attachment : nextDocument.getAttachmentList()) {
attachment.setAuthorReference(nextDocument.getAuthorReference());
}
if (mergedDocument != null) {
mergedDocument.setAuthorReference(userReference);
mergedDocument.setContentAuthorReference(userReference);
for (XWikiAttachment attachment : mergedDocument.getAttachmentList()) {
if (attachment.isContentDirty()) {
attachment.setAuthorReference(mergedDocument.getAuthorReference());
}
}
}
}
// Calculate the conflict type
ConflictQuestion.ConflictType type;
if (previousDocument == null) {
type = ConflictQuestion.ConflictType.CURRENT_EXIST;
} else if (currentDocument == null) {
type = ConflictQuestion.ConflictType.CURRENT_DELETED;
} else if (documentMergeResult != null) {
if (!documentMergeResult.getLog().getLogs(LogLevel.ERROR).isEmpty()) {
type = ConflictQuestion.ConflictType.MERGE_FAILURE;
} else {
type = ConflictQuestion.ConflictType.MERGE_SUCCESS;
}
} else {
type = null;
}
// Create a question
ConflictQuestion question = new ConflictQuestion(currentDocument, previousDocument, nextDocument, mergedDocument, type);
// Find the answer
GlobalAction contextAction = getMergeConflictAnswer(question.getType(), configuration);
if (contextAction != null && contextAction != GlobalAction.ASK) {
question.setGlobalAction(contextAction);
} else if (configuration != null && configuration.getJobStatus() != null && configuration.isInteractive()) {
try {
// Ask what to do
configuration.getJobStatus().ask(question);
if (question.isAlways()) {
setMergeConflictAnswer(question.getType(), question.getGlobalAction());
}
} catch (InterruptedException e) {
// TODO: log something ?
}
}
// Find the XWikiDocument to save
XWikiDocument documentToSave;
switch(question.getGlobalAction()) {
case CURRENT:
documentToSave = currentDocument;
break;
case NEXT:
documentToSave = nextDocument;
break;
case PREVIOUS:
documentToSave = previousDocument;
break;
case CUSTOM:
documentToSave = question.getCustomDocument() != null ? question.getCustomDocument() : mergedDocument;
break;
default:
documentToSave = mergedDocument;
break;
}
return documentToSave;
}
use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.
the class AttachmentsTreeNode method getChildren.
@Override
protected List<String> getChildren(DocumentReference documentReference, int offset, int limit) throws Exception {
List<String> children = new ArrayList<String>();
if (offset == 0 && showAddAttachment(documentReference)) {
children.add("addAttachment:" + this.defaultEntityReferenceSerializer.serialize(documentReference));
}
XWikiContext xcontext = this.xcontextProvider.get();
XWikiDocument document = xcontext.getWiki().getDocument(documentReference, xcontext);
List<XWikiAttachment> attachments = document.getAttachmentList();
for (XWikiAttachment attachment : subList(attachments, offset, limit)) {
children.add(serialize(attachment.getReference()));
}
return children;
}
use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.
the class AbstractTemplateMimeBodyPartFactory method create.
@Override
public MimeBodyPart create(DocumentReference documentReference, Map<String, Object> parameters) throws MessagingException {
Map<String, Object> velocityVariables = (Map<String, Object>) parameters.get("velocityVariables");
Object localeValue = parameters.get("language");
String textContent = getTemplateManager().evaluate(documentReference, "text", velocityVariables, localeValue);
String htmlContent = getTemplateManager().evaluate(documentReference, "html", velocityVariables, localeValue);
Map<String, Object> htmlParameters = new HashMap<>();
htmlParameters.put("alternate", textContent);
// Handle attachments:
// - if the user has passed an "attachments" property with a list of attachment then add them
// - if the user has set the "includeTemplateAttachments" property then add all attachments found in the
// template document too
List<Attachment> attachments = new ArrayList<>();
List<Attachment> parameterAttachments = (List<Attachment>) parameters.get(ATTACHMENT_PROPERTY_NAME);
if (parameterAttachments != null) {
attachments.addAll(parameterAttachments);
}
Boolean includeTemplateAttachments = (Boolean) parameters.get(INCLUDE_TEMPLATE_ATTACHMENTS_PROPERTY_NAME);
if (includeTemplateAttachments != null && includeTemplateAttachments) {
try {
List<XWikiAttachment> xwikiAttachments = ((XWikiDocument) this.bridge.getDocumentInstance(documentReference)).getAttachmentList();
attachments.addAll(this.attachmentConverter.convert(xwikiAttachments));
} catch (Exception e) {
throw new MessagingException(String.format("Failed to include attachments from the Mail Template [%s]", documentReference), e);
}
}
if (!attachments.isEmpty()) {
htmlParameters.put(ATTACHMENT_PROPERTY_NAME, attachments);
}
return this.htmlBodyPartFactory.create(htmlContent, htmlParameters);
}
use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.
the class DefaultAttachmentConverter method convert.
@Override
public List<Attachment> convert(List<XWikiAttachment> attachments) {
XWikiContext xwikiContext = getXWikiContext();
List<Attachment> attachmentList = new ArrayList<>();
for (XWikiAttachment attachment : attachments) {
attachmentList.add(new Attachment(new Document(attachment.getDoc(), xwikiContext), attachment, xwikiContext));
}
return attachmentList;
}
use of com.xpn.xwiki.doc.XWikiAttachment in project xwiki-platform by xwiki.
the class WikiSkinUtils method getSkinResourceFromDocumentSkin.
private Resource<?> getSkinResourceFromDocumentSkin(String resource, XWikiDocument skinDocument, Skin skin) {
if (skinDocument != null) {
// Try to find a XWikiSkinFileOverrideClass object
BaseObject obj = skinDocument.getXObject(XWikiSkinFileOverrideClassDocumentInitializer.DOCUMENT_REFERENCE, XWikiSkinFileOverrideClassDocumentInitializer.PROPERTY_PATH, resource, false);
if (obj != null) {
ObjectPropertyReference reference = new ObjectPropertyReference(XWikiSkinFileOverrideClassDocumentInitializer.PROPERTY_CONTENT, obj.getReference());
return new ObjectPropertyWikiResource(getPath(reference), skin, reference, skinDocument.getAuthorReference(), this.xcontextProvider, obj.getLargeStringValue(XWikiSkinFileOverrideClassDocumentInitializer.PROPERTY_CONTENT));
}
// Try parsing the object property
BaseProperty<ObjectPropertyReference> property = getSkinResourceProperty(resource, skinDocument);
if (property != null) {
ObjectPropertyReference reference = property.getReference();
return new ObjectPropertyWikiResource(getPath(reference), skin, reference, skinDocument.getAuthorReference(), this.xcontextProvider, (String) property.getValue());
}
// Try parsing a document attachment
// Convert "/" into "." in order to support wiki skin attachments to override some resources located in
// subdirectories.
String normalizedResourceName = StringUtils.replaceChars(resource, '/', '.');
XWikiAttachment attachment = skinDocument.getAttachment(normalizedResourceName);
if (attachment != null) {
AttachmentReference reference = attachment.getReference();
return new AttachmentWikiResource(getPath(reference), skin, reference, attachment.getAuthorReference(), this.xcontextProvider);
}
}
return null;
}
Aggregations