]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
7983d1d16399a6b89bdb54e9611806b1c74fdca8
[tmp/jakarta-migration.git] /
1 /*
2  * (C) Copyright 2006-2009 Nuxeo SAS (http://nuxeo.com/) and contributors.
3  *
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
8  *
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.
13  *
14  * Contributors:
15  *     Nuxeo - initial API and implementation
16  *
17  * $Id$
18  */
19
20 /*
21  * An example Nuxeo event listener. 
22  */
23
24 package org.collectionspace.ecm.platform.quote.listener;
25
26
27
28 import java.util.List;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
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;
43
44 public class DocumentRemovedQuoteEventListener extends
45         AbstractQuoteListener implements PostCommitEventListener {
46
47     private static final Log log = LogFactory.getLog(DocumentRemovedQuoteEventListener.class);
48
49     @Override
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);
55     }
56
57     private static void onDocumentRemoved(CoreSession coreSession,
58             RelationManager relationManager, QuoteServiceConfig config,
59             DocumentModel docMessage) throws ClientException {
60
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");
66             return;
67         }
68         Statement pattern = new StatementImpl(null, null, documentRes);
69         List<Statement> statementList = relationManager.getStatements(
70                 config.graphName, pattern);
71
72         // remove comments
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);
78
79             if (docModel != null) {
80                 try {
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);
85                 }
86             } else {
87                 log.warn("quote/comment not found: id=" + commentId);
88             }
89         }
90         coreSession.save();
91         // remove relations
92         relationManager.remove(config.graphName, statementList);
93     }
94
95 }