001/*
002 * (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Thomas Roger <troger@nuxeo.com>
016 */
017package org.nuxeo.ecm.mobile.webengine.adapter;
018
019import java.util.ArrayList;
020import java.util.Collections;
021import java.util.HashMap;
022import java.util.List;
023import java.util.Map;
024
025import javax.ws.rs.GET;
026
027import org.nuxeo.ecm.core.api.CoreSession;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.directory.Session;
030import org.nuxeo.ecm.directory.api.DirectoryService;
031import org.nuxeo.ecm.mobile.webengine.document.MobileDocument;
032import org.nuxeo.ecm.platform.relations.api.Node;
033import org.nuxeo.ecm.platform.relations.api.QNameResource;
034import org.nuxeo.ecm.platform.relations.api.RelationManager;
035import org.nuxeo.ecm.platform.relations.api.Resource;
036import org.nuxeo.ecm.platform.relations.api.ResourceAdapter;
037import org.nuxeo.ecm.platform.relations.api.Statement;
038import org.nuxeo.ecm.platform.relations.api.Subject;
039import org.nuxeo.ecm.platform.relations.api.impl.StatementImpl;
040import org.nuxeo.ecm.platform.relations.api.util.RelationConstants;
041import org.nuxeo.ecm.platform.relations.web.NodeInfo;
042import org.nuxeo.ecm.platform.relations.web.NodeInfoImpl;
043import org.nuxeo.ecm.platform.relations.web.StatementInfo;
044import org.nuxeo.ecm.platform.relations.web.StatementInfoImpl;
045import org.nuxeo.ecm.webengine.WebEngine;
046import org.nuxeo.ecm.webengine.WebException;
047import org.nuxeo.ecm.webengine.model.WebAdapter;
048import org.nuxeo.runtime.api.Framework;
049
050/**
051 * Adapter to retrieve annotations for the current {@code MobileDocument}.
052 * <p>
053 * Most of the code comes from the {@link org.nuxeo.ecm.platform.relations.web.listener.ejb.RelationActionsBean} class.
054 *
055 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
056 * @author <a href="mailto:bjalon@nuxeo.com">Benjamin JALON</a>
057 * @since 5.5
058 */
059@WebAdapter(name = "relation", type = "Relation", targetType = "MobileDocument")
060public class RelationAdapter extends DefaultMobileAdapter {
061
062    public static final String PREDICATES_DIRECTORY = "predicates";
063
064    @GET
065    public Object doGet() {
066        return getView("index");
067    }
068
069    public Map<String, List<StatementInfo>> getRelations() {
070        DocumentModel doc = getDocumentModel();
071        RelationManager relationManager = Framework.getLocalService(RelationManager.class);
072        Resource docResource = getDocumentResource(doc);
073
074        List<StatementInfo> allStatements = new ArrayList<StatementInfo>();
075        if (docResource != null) {
076            Statement pattern = new StatementImpl(docResource, null, null);
077            List<Statement> outgoingStatements = relationManager.getStatements(RelationConstants.GRAPH_NAME, pattern);
078            allStatements.addAll(getStatementsInfo(outgoingStatements));
079
080            pattern = new StatementImpl(null, null, docResource);
081            List<Statement> incomingStatements = relationManager.getStatements(RelationConstants.GRAPH_NAME, pattern);
082            allStatements.addAll(getStatementsInfo(incomingStatements));
083        }
084
085        Map<String, List<StatementInfo>> relations = new HashMap<String, List<StatementInfo>>();
086        for (StatementInfo statement : allStatements) {
087            String label = getPredicateLabel(PREDICATES_DIRECTORY, statement);
088            if (!relations.containsKey(label)) {
089                relations.put(label, new ArrayList<StatementInfo>());
090            }
091            List<StatementInfo> statements = relations.get(label);
092            statements.add(statement);
093            relations.put(label, statements);
094        }
095        return relations;
096    }
097
098    public boolean hasRelation() {
099        Map<String, List<StatementInfo>> relation = getRelations();
100        for (String key : relation.keySet()) {
101            if (relation.get(key).size() > 0) {
102                return true;
103            }
104        }
105        return false;
106    }
107
108    private QNameResource getDocumentResource(DocumentModel document) {
109        QNameResource documentResource = null;
110        if (document != null) {
111            documentResource = (QNameResource) Framework.getLocalService(RelationManager.class).getResource(
112                    RelationConstants.DOCUMENT_NAMESPACE, document, null);
113        }
114        return documentResource;
115    }
116
117    private List<StatementInfo> getStatementsInfo(List<Statement> statements) {
118        if (statements == null) {
119            return null;
120        }
121        List<StatementInfo> infoList = new ArrayList<StatementInfo>();
122        for (Statement statement : statements) {
123            Subject subject = statement.getSubject();
124            NodeInfo subjectInfo = new NodeInfoImpl(subject, getDocumentModel(subject), true);
125            Resource predicate = statement.getPredicate();
126            Node object = statement.getObject();
127            NodeInfo objectInfo = new NodeInfoImpl(object, getDocumentModel(object), true);
128            StatementInfo info = new StatementInfoImpl(statement, subjectInfo, new NodeInfoImpl(predicate), objectInfo);
129            infoList.add(info);
130        }
131        return infoList;
132    }
133
134    private DocumentModel getDocumentModel(Node node) {
135        if (node.isQNameResource()) {
136            QNameResource resource = (QNameResource) node;
137            CoreSession session = WebEngine.getActiveContext().getCoreSession();
138            Map<String, Object> context = Collections.<String, Object> singletonMap(
139                    ResourceAdapter.CORE_SESSION_CONTEXT_KEY, session);
140            Object o = Framework.getLocalService(RelationManager.class).getResourceRepresentation(
141                    resource.getNamespace(), resource, context);
142            if (o instanceof DocumentModel) {
143                return (DocumentModel) o;
144            }
145        }
146        return null;
147    }
148
149    /**
150     * Returns the predicate's label for a given {@code Statement}.
151     */
152    private String getPredicateLabel(String directoryName, StatementInfo statementInfo) {
153        DirectoryService directoryService = Framework.getLocalService(DirectoryService.class);
154        Session session = directoryService.open(directoryName);
155
156        DocumentModel entry = session.getEntry(statementInfo.getPredicate().getUri());
157        return (String) entry.getPropertyValue("vocabulary:label");
158    }
159
160}