All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.servicerocket.confluence.randombits.metadata.migration.AttachmentReferenceMigrator Maven / Gradle / Ivy

There is a newer version: 2.5.12
Show newest version
package com.servicerocket.confluence.randombits.metadata.migration;

import com.atlassian.confluence.pages.Attachment;
import com.google.common.base.Function;
import com.servicerocket.confluence.randombits.metadata.reference.AttachmentReference;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.ListIterator;
import java.util.Map;

/**
 * @author Mohd Faiz Hasim
 * @author LongYC
 * @since 6.1.0
 */
@Component
public class AttachmentReferenceMigrator {

    /**
     * Append attachmentId into AttachmentReference
     * so that the ID can be used as identifier instead of filename.
     * AttachmentReference must be inside a List as of current Metadata implementation.
     *
     * @param stringObjectMap String-Object map
     * @param attachment Confluence Attachment object
     * @return map
     */
    public Map process(Map stringObjectMap, Attachment attachment) {
        for (Map.Entry entry : stringObjectMap.entrySet()) {
            processIfList(attachment).apply(entry);
            processIfMap(attachment).apply(entry);
        }

        return stringObjectMap;
    }

    private Function, Map.Entry> processIfList(final Attachment attachment) {
        return new Function, Map.Entry>() {
            @Override
            public Map.Entry apply(Map.Entry entry) {
                if (entry.getValue() instanceof List) {
                    updateAttachmentReference((List) entry.getValue(), attachment);
                }
                return entry;
            }
        };
    }

    private Function, Map.Entry> processIfMap(final Attachment attachment) {
        return new Function, Map.Entry>() {
            @Override public Map.Entry apply(Map.Entry entry) {
                if (entry.getValue() instanceof Map) {
                    process((Map) entry.getValue(), attachment);
                }
                return entry;
            }
        };
    }

    private void updateAttachmentReference(List valueObjectList, Attachment attachment) {
        ListIterator iterator = valueObjectList.listIterator();

        while (iterator.hasNext()) {
            Object valueObject = iterator.next();
            if (valueObject instanceof AttachmentReference) {
                AttachmentReference attachmentReference = (AttachmentReference) valueObject;
                if (attachmentReference.getFileName().equals(attachment.getFileName())) {
                    iterator.set(new AttachmentReference(attachment));
                }
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy