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