001/*
002 * (C) Copyright 2009 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 *     Thomas Roger
018 */
019
020package org.nuxeo.ecm.webapp.note;
021
022import static org.jboss.seam.ScopeType.CONVERSATION;
023
024import java.io.Serializable;
025import java.util.regex.Matcher;
026import java.util.regex.Pattern;
027
028import org.jboss.seam.annotations.In;
029import org.jboss.seam.annotations.Name;
030import org.jboss.seam.annotations.Scope;
031import org.nuxeo.ecm.core.api.CoreSession;
032import org.nuxeo.ecm.core.api.DocumentModel;
033import org.nuxeo.ecm.core.api.IdRef;
034import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner;
035import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
036
037/**
038 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
039 */
040@Scope(CONVERSATION)
041@Name("noteActions")
042public class NoteActions implements Serializable {
043
044    private static final long serialVersionUID = 1L;
045
046    protected static final Pattern PATTERN_TO_CHECK = Pattern.compile("(.*<img.*/files:files/.*/>.*)+", Pattern.DOTALL);
047
048    protected static final String PATTERN_TO_REPLACE = "(<img.*?)%s(/files:files/.*?/>)";
049
050    @In(create = true, required = false)
051    protected transient CoreSession documentManager;
052
053    @In(create = true, required = false)
054    protected transient NavigationContext navigationContext;
055
056    private static class LiveDocumentRefFinder extends UnrestrictedSessionRunner {
057
058        private String liveDocumentRef;
059
060        private final DocumentModel proxy;
061
062        public LiveDocumentRefFinder(DocumentModel proxy) {
063            super(proxy.getRepositoryName());
064            this.proxy = proxy;
065        }
066
067        @Override
068        public void run() {
069            liveDocumentRef = proxy.getRef().toString();
070            if (proxy.getSourceId() != null) {
071                liveDocumentRef = proxy.getSourceId();
072                DocumentModel version = session.getDocument(new IdRef(proxy.getSourceId()));
073                if (version.getSourceId() != null) {
074                    liveDocumentRef = version.getSourceId();
075                }
076            }
077        }
078
079        public String getLiveDocumentRef() {
080            if (liveDocumentRef == null) {
081                runUnrestricted();
082            }
083            return liveDocumentRef;
084        }
085
086    }
087
088    /**
089     * Translate the image links referencing attached files to use the docId of the current proxy or version. Do not
090     * translate anything if we are on a live document.
091     *
092     * @param note the note content
093     * @return the translated note content
094     */
095    public String translateImageLinks(String note) {
096        DocumentModel currentDocument = navigationContext.getCurrentDocument();
097        if (!(currentDocument.isProxy() || currentDocument.isVersion())) {
098            return note;
099        }
100
101        if (!hasImageLinksToTranslate(note)) {
102            return note;
103        }
104
105        String docIdToReplace = null;
106        if (currentDocument.isVersion()) {
107            docIdToReplace = currentDocument.getSourceId();
108        } else if (currentDocument.isProxy()) {
109            docIdToReplace = new LiveDocumentRefFinder(currentDocument).getLiveDocumentRef();
110        }
111
112        return translateImageLinks(note, docIdToReplace, currentDocument.getId());
113    }
114
115    protected boolean hasImageLinksToTranslate(String note) {
116        Matcher matcher = PATTERN_TO_CHECK.matcher(note);
117        return matcher.matches();
118    }
119
120    protected String translateImageLinks(String note, String fromDocRef, String toDocRef) {
121        String patternToReplace = String.format(PATTERN_TO_REPLACE, fromDocRef);
122        Pattern pattern = Pattern.compile(patternToReplace);
123        Matcher matcher = pattern.matcher(note);
124        String replacement = "$1" + toDocRef + "$2";
125        return matcher.replaceAll(replacement);
126    }
127
128}