001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     <a href="mailto:grenard@nuxeo.com">Guillaume Renard</a>
018 *
019 */
020
021package org.nuxeo.ecm.platform.routing.core.impl.jsongraph;
022
023import java.io.IOException;
024import java.io.StringWriter;
025import java.util.ArrayList;
026import java.util.HashMap;
027import java.util.List;
028import java.util.Locale;
029import java.util.Map;
030import java.util.MissingResourceException;
031
032import org.apache.commons.logging.Log;
033import org.apache.commons.logging.LogFactory;
034import org.codehaus.jackson.map.ObjectMapper;
035import org.nuxeo.common.utils.i18n.I18NUtils;
036import org.nuxeo.ecm.core.api.CoreSession;
037import org.nuxeo.ecm.core.api.DocumentModel;
038import org.nuxeo.ecm.core.api.IdRef;
039import org.nuxeo.ecm.core.api.NuxeoException;
040import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner;
041import org.nuxeo.ecm.platform.routing.core.impl.GraphNode;
042import org.nuxeo.ecm.platform.routing.core.impl.GraphRoute;
043import org.nuxeo.ecm.platform.routing.core.impl.GraphNode.Transition;
044
045/**
046 * @since 7.2
047 */
048public class JsonGraphRoute extends UnrestrictedSessionRunner {
049
050    public static Map<String, Object> getGraphElementsAsMap(GraphRoute route, Locale locale) {
051        Map<String, Object> graph = new HashMap<String, Object>();
052        List<NodeView> nodeViews = new ArrayList<NodeView>();
053        List<TransitionView> tranViews = new ArrayList<TransitionView>();
054
055        for (GraphNode node : route.getNodes()) {
056            nodeViews.add(new NodeView(node, locale));
057            List<Transition> transitions = node.getOutputTransitions();
058            for (Transition transition : transitions) {
059                GraphNode targetNode = route.getNode(transition.getTarget());
060                tranViews.add(new TransitionView(node.getId(), targetNode.getId(), transition, locale));
061            }
062        }
063        graph.put("nodes", nodeViews);
064        graph.put("transitions", tranViews);
065        return graph;
066    }
067
068    public static String getI18nLabel(String label, Locale locale) {
069        if (label == null) {
070            label = "";
071        }
072        try {
073            return I18NUtils.getMessageString("messages", label, null, locale);
074        } catch (MissingResourceException e) {
075            log.warn(e.getMessage());
076            return label;
077        }
078    }
079
080    private static Log log = LogFactory.getLog(JsonGraphRoute.class);
081
082    protected String docId;
083
084    protected GraphRoute graphRoute;
085
086    protected Map<String, Object> graphElements;
087
088    protected String json;
089
090    protected Locale locale;
091
092    public JsonGraphRoute(CoreSession session, GraphRoute graphRoute, Locale locale) {
093        super(session);
094        this.graphRoute = graphRoute;
095        this.locale = locale;
096    }
097
098    public JsonGraphRoute(CoreSession session, String docId, Locale locale) {
099        super(session);
100        this.docId = docId;
101        this.locale = locale;
102    }
103
104    /**
105     * @since 7.2
106     */
107    public Map<String, Object> getGraphElements() {
108        if (graphElements == null) {
109            runUnrestricted();
110        }
111        return graphElements;
112    }
113
114    public String getJSON() {
115        if (json == null) {
116            runUnrestricted();
117        }
118        return json;
119    }
120
121    @Override
122    public void run() {
123        if (graphRoute == null) {
124            DocumentModel doc = session.getDocument(new IdRef(docId));
125            graphRoute = doc.getAdapter(GraphRoute.class);
126        }
127        try {
128            graphElements = getGraphElementsAsMap(graphRoute, locale);
129            ObjectMapper mapper = new ObjectMapper();
130            StringWriter writer = new StringWriter();
131            mapper.writeValue(writer, graphElements);
132            json = writer.toString();
133        } catch (IOException e) {
134            throw new NuxeoException(e);
135        }
136    }
137
138    @Override
139    public String toString() {
140        return getJSON();
141    }
142}