2 * (C) Copyright 2006-2009 Nuxeo SAS (http://nuxeo.com/) and contributors.
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the GNU Lesser General Public License
6 * (LGPL) version 2.1 which accompanies this distribution, and is available at
7 * http://www.gnu.org/licenses/lgpl.html
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
15 * Nuxeo - initial API and implementation
21 * An example Nuxeo event listener.
24 package org.collectionspace.ecm.platform.quote.listener;
28 import java.util.List;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
33 import org.nuxeo.ecm.core.api.ClientException;
34 import org.nuxeo.ecm.core.api.CoreSession;
35 import org.nuxeo.ecm.core.api.DocumentModel;
36 import org.nuxeo.ecm.core.event.PostCommitEventListener;
37 import org.collectionspace.ecm.platform.quote.service.QuoteServiceConfig;
38 import org.nuxeo.ecm.platform.relations.api.QNameResource;
39 import org.nuxeo.ecm.platform.relations.api.RelationManager;
40 import org.nuxeo.ecm.platform.relations.api.Resource;
41 import org.nuxeo.ecm.platform.relations.api.Statement;
42 import org.nuxeo.ecm.platform.relations.api.impl.StatementImpl;
44 public class DocumentRemovedQuoteEventListener extends
45 AbstractQuoteListener implements PostCommitEventListener {
47 private static final Log log = LogFactory.getLog(DocumentRemovedQuoteEventListener.class);
50 protected void doProcess(CoreSession coreSession,
51 RelationManager relationManager, QuoteServiceConfig config,
52 DocumentModel docMessage) throws Exception {
53 log.debug("Processing relations cleanup on Document removal");
54 onDocumentRemoved(coreSession, relationManager, config, docMessage);
57 private static void onDocumentRemoved(CoreSession coreSession,
58 RelationManager relationManager, QuoteServiceConfig config,
59 DocumentModel docMessage) throws ClientException {
61 Resource documentRes = relationManager.getResource(
62 config.documentNamespace, docMessage, null);
63 if (documentRes == null) {
64 log.error("Could not adapt document model to relation resource ; "
65 + "check the service relation adapters configuration");
68 Statement pattern = new StatementImpl(null, null, documentRes);
69 List<Statement> statementList = relationManager.getStatements(
70 config.graphName, pattern);
73 for (Statement stmt : statementList) {
74 QNameResource resource = (QNameResource) stmt.getSubject();
75 String commentId = resource.getLocalName();
76 DocumentModel docModel = (DocumentModel) relationManager.getResourceRepresentation(
77 config.commentNamespace, resource, null);
79 if (docModel != null) {
81 coreSession.removeDocument(docModel.getRef());
82 log.debug("quote removal succeded for id: " + commentId);
83 } catch (Exception e) {
84 log.error("quote removal failed", e);
87 log.warn("quote/comment not found: id=" + commentId);
92 relationManager.remove(config.graphName, statementList);