Search in sources :

Example 1 with XWikiAttachment

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;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) GlobalAction(org.xwiki.extension.xar.question.ConflictQuestion.GlobalAction) DocumentReference(org.xwiki.model.reference.DocumentReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) ConflictQuestion(org.xwiki.extension.xar.question.ConflictQuestion)

Example 2 with XWikiAttachment

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;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment)

Example 3 with XWikiAttachment

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);
}
Also used : HashMap(java.util.HashMap) MessagingException(javax.mail.MessagingException) ArrayList(java.util.ArrayList) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Attachment(com.xpn.xwiki.api.Attachment) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) MessagingException(javax.mail.MessagingException) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with XWikiAttachment

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;
}
Also used : ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) Attachment(com.xpn.xwiki.api.Attachment) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Document(com.xpn.xwiki.api.Document)

Example 5 with XWikiAttachment

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;
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) ObjectPropertyReference(org.xwiki.model.reference.ObjectPropertyReference) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) BaseObject(com.xpn.xwiki.objects.BaseObject)

Aggregations

XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)133 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)71 DocumentReference (org.xwiki.model.reference.DocumentReference)51 Test (org.junit.Test)40 XWikiContext (com.xpn.xwiki.XWikiContext)35 XWikiException (com.xpn.xwiki.XWikiException)25 ByteArrayInputStream (java.io.ByteArrayInputStream)20 Attachment (com.xpn.xwiki.api.Attachment)18 Date (java.util.Date)17 IOException (java.io.IOException)15 ArrayList (java.util.ArrayList)15 Document (com.xpn.xwiki.api.Document)14 XWiki (com.xpn.xwiki.XWiki)13 BaseObject (com.xpn.xwiki.objects.BaseObject)13 AttachmentReference (org.xwiki.model.reference.AttachmentReference)13 InputStream (java.io.InputStream)11 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)10 File (java.io.File)10 URL (java.net.URL)7 List (java.util.List)7