001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Nuxeo - initial API and implementation
011 *
012 * $Id$
013 */
014package org.nuxeo.ecm.platform.audit.api.comment;
015
016import java.util.List;
017
018import org.nuxeo.common.utils.IdUtils;
019import org.nuxeo.ecm.core.api.CoreSession;
020import org.nuxeo.ecm.core.api.DocumentModel;
021import org.nuxeo.ecm.core.api.DocumentNotFoundException;
022import org.nuxeo.ecm.core.api.DocumentRef;
023import org.nuxeo.ecm.core.api.IdRef;
024import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
025import org.nuxeo.ecm.platform.audit.api.LogEntry;
026import org.nuxeo.ecm.platform.util.RepositoryLocation;
027
028/**
029 * Helper to manage {@link LogEntry} comment processing (code was moved from the Seam bean)
030 *
031 * @author Tiry (tdelprat@nuxeo.com)
032 * @since 5.4.2
033 */
034public class CommentProcessorHelper {
035
036    protected CoreSession documentManager;
037
038    public CommentProcessorHelper(CoreSession documentManager) {
039        this.documentManager = documentManager;
040    }
041
042    public void processComments(List<LogEntry> logEntries) {
043        if (logEntries == null) {
044            return;
045        }
046        for (LogEntry entry : logEntries) {
047            String comment = getLogComment(entry);
048            LinkedDocument linkedDoc = getLogLinkedDocument(entry);
049            entry.setPreprocessedComment(new UIAuditComment(comment, linkedDoc));
050        }
051    }
052
053    public String getLogComment(LogEntry entry) {
054        String oldComment = entry.getComment();
055        if (oldComment == null) {
056            return null;
057        }
058
059        String newComment = oldComment;
060        boolean targetDocExists = false;
061        String[] split = oldComment.split(":");
062        if (split.length >= 2) {
063            String strDocRef = split[1];
064            DocumentRef docRef = new IdRef(strDocRef);
065            targetDocExists = documentManager.exists(docRef);
066        }
067
068        if (targetDocExists) {
069            String eventId = entry.getEventId();
070            // update comment
071            if (DocumentEventTypes.DOCUMENT_DUPLICATED.equals(eventId)) {
072                newComment = "audit.duplicated_to";
073            } else if (DocumentEventTypes.DOCUMENT_CREATED_BY_COPY.equals(eventId)) {
074                newComment = "audit.copied_from";
075            } else if (DocumentEventTypes.DOCUMENT_MOVED.equals(eventId)) {
076                newComment = "audit.moved_from";
077            }
078        }
079
080        return newComment;
081    }
082
083    public LinkedDocument getLogLinkedDocument(LogEntry entry) {
084        String oldComment = entry.getComment();
085        if (oldComment == null) {
086            return null;
087        }
088
089        LinkedDocument linkedDoc = null;
090
091        String[] split = oldComment.split(":");
092        if (split.length >= 2) {
093            String repoName = split[0];
094            String strDocRef = split[1];
095
096            // test if strDocRef is a document uuid to continue
097            if (IdUtils.isValidUUID(strDocRef)) {
098                DocumentRef docRef = new IdRef(strDocRef);
099                RepositoryLocation repoLoc = new RepositoryLocation(repoName);
100
101                // create linked doc, broken by default
102                linkedDoc = new LinkedDocument();
103                linkedDoc.setDocumentRef(docRef);
104                linkedDoc.setRepository(repoLoc);
105
106                // try to resolve target document
107                // XXX multi-repository management
108                try {
109                    DocumentModel targetDoc = documentManager.getDocument(docRef);
110                    linkedDoc.setDocument(targetDoc);
111                    linkedDoc.setBrokenDocument(false);
112                } catch (DocumentNotFoundException e) {
113                    // not the expected format or broken document
114                }
115            }
116        }
117
118        return linkedDoc;
119    }
120
121}