001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021package org.nuxeo.ecm.platform.audit.api.comment;
022
023import java.util.List;
024
025import org.nuxeo.common.utils.IdUtils;
026import org.nuxeo.ecm.core.api.CoreSession;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.DocumentNotFoundException;
029import org.nuxeo.ecm.core.api.DocumentRef;
030import org.nuxeo.ecm.core.api.IdRef;
031import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
032import org.nuxeo.ecm.platform.audit.api.LogEntry;
033import org.nuxeo.ecm.platform.util.RepositoryLocation;
034
035/**
036 * Helper to manage {@link LogEntry} comment processing (code was moved from the Seam bean)
037 *
038 * @author Tiry (tdelprat@nuxeo.com)
039 * @since 5.4.2
040 */
041public class CommentProcessorHelper {
042
043    protected CoreSession documentManager;
044
045    public CommentProcessorHelper(CoreSession documentManager) {
046        this.documentManager = documentManager;
047    }
048
049    public void processComments(List<LogEntry> logEntries) {
050        if (logEntries == null) {
051            return;
052        }
053        for (LogEntry entry : logEntries) {
054            String comment = getLogComment(entry);
055            LinkedDocument linkedDoc = getLogLinkedDocument(entry);
056            entry.setPreprocessedComment(new UIAuditComment(comment, linkedDoc));
057        }
058    }
059
060    public String getLogComment(LogEntry entry) {
061        String oldComment = entry.getComment();
062        if (oldComment == null) {
063            return null;
064        }
065
066        String newComment = oldComment;
067        boolean targetDocExists = false;
068        String[] split = oldComment.split(":");
069        if (split.length >= 2) {
070            String strDocRef = split[1];
071            DocumentRef docRef = new IdRef(strDocRef);
072            targetDocExists = documentManager.exists(docRef);
073        }
074
075        if (targetDocExists) {
076            String eventId = entry.getEventId();
077            // update comment
078            if (DocumentEventTypes.DOCUMENT_DUPLICATED.equals(eventId)) {
079                newComment = "audit.duplicated_to";
080            } else if (DocumentEventTypes.DOCUMENT_CREATED_BY_COPY.equals(eventId)) {
081                newComment = "audit.copied_from";
082            } else if (DocumentEventTypes.DOCUMENT_MOVED.equals(eventId)) {
083                newComment = "audit.moved_from";
084            }
085        }
086
087        return newComment;
088    }
089
090    public LinkedDocument getLogLinkedDocument(LogEntry entry) {
091        String oldComment = entry.getComment();
092        if (oldComment == null) {
093            return null;
094        }
095
096        LinkedDocument linkedDoc = null;
097
098        String[] split = oldComment.split(":");
099        if (split.length >= 2) {
100            String repoName = split[0];
101            String strDocRef = split[1];
102
103            // test if strDocRef is a document uuid to continue
104            if (IdUtils.isValidUUID(strDocRef)) {
105                DocumentRef docRef = new IdRef(strDocRef);
106                RepositoryLocation repoLoc = new RepositoryLocation(repoName);
107
108                // create linked doc, broken by default
109                linkedDoc = new LinkedDocument();
110                linkedDoc.setDocumentRef(docRef);
111                linkedDoc.setRepository(repoLoc);
112
113                // try to resolve target document
114                // XXX multi-repository management
115                try {
116                    DocumentModel targetDoc = documentManager.getDocument(docRef);
117                    linkedDoc.setDocument(targetDoc);
118                    linkedDoc.setBrokenDocument(false);
119                } catch (DocumentNotFoundException e) {
120                    // not the expected format or broken document
121                }
122            }
123        }
124
125        return linkedDoc;
126    }
127
128}