]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
a7977bd08e0e209eb7d70c3269da3fdd1db07dc8
[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 package org.collectionspace.ecm.platform.quote.listener;
21
22 import java.util.List;
23
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;
36
37 public class DocumentRemovedQuoteEventListener extends
38         AbstractQuoteListener implements PostCommitEventListener {
39
40     private static final Log log = LogFactory.getLog(DocumentRemovedQuoteEventListener.class);
41
42     @Override
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);
48     }
49
50     private static void onDocumentRemoved(CoreSession coreSession,
51             RelationManager relationManager, QuoteServiceConfig config,
52             DocumentModel docMessage) throws ClientException {
53
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");
59             return;
60         }
61         Statement pattern = new StatementImpl(null, null, documentRes);
62         List<Statement> statementList = relationManager.getStatements(
63                 config.graphName, pattern);
64
65         // remove comments
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);
71
72             if (docModel != null) {
73                 try {
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);
78                 }
79             } else {
80                 log.warn("quote/comment not found: id=" + commentId);
81             }
82         }
83         coreSession.save();
84         // remove relations
85         relationManager.remove(config.graphName, statementList);
86     }
87
88 }