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
20 package org.collectionspace.ecm.platform.quote.listener;
22 import java.util.List;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.nuxeo.ecm.core.api.ClientException;
27 import org.nuxeo.ecm.core.api.CoreSession;
28 import org.nuxeo.ecm.core.api.DocumentModel;
29 import org.nuxeo.ecm.core.event.PostCommitEventListener;
30 import org.collectionspace.ecm.platform.quote.service.QuoteServiceConfig;
31 import org.nuxeo.ecm.platform.relations.api.QNameResource;
32 import org.nuxeo.ecm.platform.relations.api.RelationManager;
33 import org.nuxeo.ecm.platform.relations.api.Resource;
34 import org.nuxeo.ecm.platform.relations.api.Statement;
35 import org.nuxeo.ecm.platform.relations.api.impl.StatementImpl;
37 public class DocumentRemovedQuoteEventListener extends
38 AbstractQuoteListener implements PostCommitEventListener {
40 private static final Log log = LogFactory.getLog(DocumentRemovedQuoteEventListener.class);
43 protected void doProcess(CoreSession coreSession,
44 RelationManager relationManager, QuoteServiceConfig config,
45 DocumentModel docMessage) throws Exception {
46 log.debug("Processing relations cleanup on Document removal");
47 onDocumentRemoved(coreSession, relationManager, config, docMessage);
50 private static void onDocumentRemoved(CoreSession coreSession,
51 RelationManager relationManager, QuoteServiceConfig config,
52 DocumentModel docMessage) throws ClientException {
54 Resource documentRes = relationManager.getResource(
55 config.documentNamespace, docMessage, null);
56 if (documentRes == null) {
57 log.error("Could not adapt document model to relation resource ; "
58 + "check the service relation adapters configuration");
61 Statement pattern = new StatementImpl(null, null, documentRes);
62 List<Statement> statementList = relationManager.getStatements(
63 config.graphName, pattern);
66 for (Statement stmt : statementList) {
67 QNameResource resource = (QNameResource) stmt.getSubject();
68 String commentId = resource.getLocalName();
69 DocumentModel docModel = (DocumentModel) relationManager.getResourceRepresentation(
70 config.commentNamespace, resource, null);
72 if (docModel != null) {
74 coreSession.removeDocument(docModel.getRef());
75 log.debug("quote removal succeded for id: " + commentId);
76 } catch (Exception e) {
77 log.error("quote removal failed", e);
80 log.warn("quote/comment not found: id=" + commentId);
85 relationManager.remove(config.graphName, statementList);