001/*
002 * (C) Copyright 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 *     matic
018 */
019package org.nuxeo.ecm.platform.annotations.repository.service;
020
021import java.net.URI;
022import java.util.List;
023
024import org.nuxeo.ecm.core.api.CoreSession;
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.core.api.NuxeoPrincipal;
027import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
028import org.nuxeo.ecm.core.event.Event;
029import org.nuxeo.ecm.core.event.EventContext;
030import org.nuxeo.ecm.platform.annotations.api.Annotation;
031import org.nuxeo.ecm.platform.annotations.api.AnnotationsService;
032import org.nuxeo.ecm.platform.annotations.repository.URNDocumentViewTranslator;
033import org.nuxeo.runtime.api.Framework;
034
035/**
036 * @author matic
037 */
038public class ResetCheckedoutGraphListener implements GraphManagerEventListener {
039
040    protected final URNDocumentViewTranslator translator = new URNDocumentViewTranslator();
041
042    protected final GraphManagerEventListener copyManager = new DocumentVersionnedGraphManager();
043
044    public void manage(Event event) {
045
046        copyManager.manage(event);
047
048        if (!DocumentEventTypes.DOCUMENT_CHECKEDIN.equals(event.getName())) {
049            return;
050        }
051
052        // reset checked-out graph
053
054        final EventContext context = event.getContext();
055        final CoreSession session = context.getCoreSession();
056
057        final DocumentModel doc = (DocumentModel) context.getArguments()[0];
058        final String repo = doc.getRepositoryName();
059
060        removeGraphFor(session, repo, doc, (NuxeoPrincipal) context.getPrincipal());
061    }
062
063    protected void removeGraphFor(CoreSession session, String repositoryName, DocumentModel doc, NuxeoPrincipal user)
064            {
065        URI uri = translator.getNuxeoUrn(repositoryName, doc.getId());
066        AnnotationsService service = Framework.getLocalService(AnnotationsService.class);
067
068        List<Annotation> annotations = service.queryAnnotations(uri, user);
069        for (Annotation annotation : annotations) {
070            AnnotationsRepositoryComponent.instance.injector.removeAnnotationText(doc, annotation.getId());
071            session.saveDocument(doc);
072            service.deleteAnnotationFor(uri, annotation, user);
073        }
074    }
075
076}