001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors.
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 *     <a href="mailto:grenard@nuxeo.com">Guillaume Renard</a>
016 *
017 */
018
019package org.nuxeo.ecm.platform.routing.core.impl.jsongraph;
020
021import java.io.IOException;
022import java.io.StringWriter;
023import java.util.ArrayList;
024import java.util.HashMap;
025import java.util.List;
026import java.util.Locale;
027import java.util.Map;
028import java.util.MissingResourceException;
029
030import org.apache.commons.logging.Log;
031import org.apache.commons.logging.LogFactory;
032import org.codehaus.jackson.map.ObjectMapper;
033import org.nuxeo.common.utils.i18n.I18NUtils;
034import org.nuxeo.ecm.core.api.CoreSession;
035import org.nuxeo.ecm.core.api.DocumentModel;
036import org.nuxeo.ecm.core.api.IdRef;
037import org.nuxeo.ecm.core.api.NuxeoException;
038import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner;
039import org.nuxeo.ecm.platform.routing.core.impl.GraphNode;
040import org.nuxeo.ecm.platform.routing.core.impl.GraphRoute;
041import org.nuxeo.ecm.platform.routing.core.impl.GraphNode.Transition;
042
043/**
044 * @since 7.2
045 */
046public class JsonGraphRoute extends UnrestrictedSessionRunner {
047
048    public static Map<String, Object> getGraphElementsAsMap(GraphRoute route, Locale locale) {
049        Map<String, Object> graph = new HashMap<String, Object>();
050        List<NodeView> nodeViews = new ArrayList<NodeView>();
051        List<TransitionView> tranViews = new ArrayList<TransitionView>();
052
053        for (GraphNode node : route.getNodes()) {
054            nodeViews.add(new NodeView(node, locale));
055            List<Transition> transitions = node.getOutputTransitions();
056            for (Transition transition : transitions) {
057                GraphNode targetNode = route.getNode(transition.getTarget());
058                tranViews.add(new TransitionView(node.getId(), targetNode.getId(), transition, locale));
059            }
060        }
061        graph.put("nodes", nodeViews);
062        graph.put("transitions", tranViews);
063        return graph;
064    }
065
066    public static String getI18nLabel(String label, Locale locale) {
067        if (label == null) {
068            label = "";
069        }
070        try {
071            return I18NUtils.getMessageString("messages", label, null, locale);
072        } catch (MissingResourceException e) {
073            log.warn(e.getMessage());
074            return label;
075        }
076    }
077
078    private static Log log = LogFactory.getLog(JsonGraphRoute.class);
079
080    protected String docId;
081
082    protected GraphRoute graphRoute;
083
084    protected Map<String, Object> graphElements;
085
086    protected String json;
087
088    protected Locale locale;
089
090    public JsonGraphRoute(CoreSession session, GraphRoute graphRoute, Locale locale) {
091        super(session);
092        this.graphRoute = graphRoute;
093        this.locale = locale;
094    }
095
096    public JsonGraphRoute(CoreSession session, String docId, Locale locale) {
097        super(session);
098        this.docId = docId;
099        this.locale = locale;
100    }
101
102    /**
103     * @since 7.2
104     */
105    public Map<String, Object> getGraphElements() {
106        if (graphElements == null) {
107            runUnrestricted();
108        }
109        return graphElements;
110    }
111
112    public String getJSON() {
113        if (json == null) {
114            runUnrestricted();
115        }
116        return json;
117    }
118
119    @Override
120    public void run() {
121        if (graphRoute == null) {
122            DocumentModel doc = session.getDocument(new IdRef(docId));
123            graphRoute = doc.getAdapter(GraphRoute.class);
124        }
125        try {
126            graphElements = getGraphElementsAsMap(graphRoute, locale);
127            ObjectMapper mapper = new ObjectMapper();
128            StringWriter writer = new StringWriter();
129            mapper.writeValue(writer, graphElements);
130            json = writer.toString();
131        } catch (IOException e) {
132            throw new NuxeoException(e);
133        }
134    }
135
136    @Override
137    public String toString() {
138        return getJSON();
139    }
140}