/*
* JBoss, Home of Professional Open Source.
* Copyright 2006, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.bpm.console.client.model;
import com.google.gwt.json.client.*;
import org.jboss.bpm.console.client.util.ConsoleLog;
import org.jboss.bpm.console.client.model.jbpm3.TokenReference;
import org.jboss.bpm.console.client.util.JSONWalk;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* TODO: run integrated (gwt-console-server) test
*
* @author Heiko.Braun <heiko.braun@jboss.com>
*/
public class DTOParser
{
public static ProcessDefinitionRef parseProcessDefinition(String json)
{
JSONValue root = JSONParser.parse(json);
String id = JSONWalk.on(root).next("id").asString();
Long version = JSONWalk.on(root).next("version").asLong();
String name = JSONWalk.on(root).next("name").asString();
return new ProcessDefinitionRef(id, name, version);
}
public static List<String> parseRolesAssigned(String json)
{
// parse roles
List<String> roles = new ArrayList<String>();
// {"roles":[{"isAssigned":true,"role":"admin"},{"isAssigned":true,"role":"user"}]}
JSONValue root = JSONParser.parse(json);
JSONArray array = JSONWalk.on(root).next("roles").asArray();
for (int i = 0; i < array.size(); ++i)
{
JSONObject item = array.get(i).isObject();
boolean assigned = JSONWalk.on(item).next("assigned").asBool();
String roleName = JSONWalk.on(item).next("role").asString();
if (assigned)
{
roles.add(roleName);
}
}
return roles;
}
public static List<TaskRef> parseTaskReferenceList(String json)
{
List<TaskRef> results = new ArrayList<TaskRef>();
// {"tasks":[{"id":14,"name":"manager evaluation","isSignalling":true,
// "isBlocking":false,"tokenId":6,"processInstanceId":4,"actor":"manager",
// "transitionNames":["reject","approve"]}]}
JSONValue root = JSONParser.parse(json);
JSONArray array = JSONWalk.on(root).next("tasks").asArray();
for (int i = 0; i < array.size(); ++i)
{
JSONObject item = array.get(i).isObject();
TaskRef ref = parseTaskReference(item);
results.add(ref);
}
return results;
}
public static TaskRef parseTaskReference(JSONObject item)
{
long id = JSONWalk.on(item).next("id").asLong();
String executionId = JSONWalk.on(item).next("processInstanceId").asString();
String processId = JSONWalk.on(item).next("processId").asString();
String name = JSONWalk.on(item).next("name").asString();
String assignee = JSONWalk.on(item).next("assignee").asString();
boolean isBlocking = JSONWalk.on(item).next("isBlocking").asBool();
boolean isSignalling = JSONWalk.on(item).next("isSignalling").asBool();
TaskRef ref = new TaskRef(
id, executionId,
processId,
name, assignee,
isSignalling, isBlocking
);
// participant users
JSONArray arrUsers = JSONWalk.on(item).next("participantUsers").asArray();
for (int k = 0; k < arrUsers.size(); ++k)
{
JSONValue jsonValue = arrUsers.get(k);
ParticipantRef p = parseParticipant(jsonValue, k);
ref.getParticipantUsers().add(p);
}
JSONArray arrGroups = JSONWalk.on(item).next("participantGroups").asArray();
for (int k = 0; k < arrGroups.size(); ++k)
{
JSONValue jsonValue = arrGroups.get(k);
ParticipantRef p = parseParticipant(jsonValue, k);
ref.getParticipantGroups().add(p);
}
if (isSignalling)
{
JSONArray arr = JSONWalk.on(item).next("outcomes").asArray();
for (int k = 0; k < arr.size(); ++k)
{
JSONValue jsonValue = arr.get(k);
if (jsonValue.toString().equals("null"))
{
ConsoleLog.debug("FIXME JBPM-1828: Null value on outcomes:" + arr.toString());
continue; // TODO: JBPM-1828
}
JSONString t = jsonValue.isString();
ref.getOutcomes().add(t.stringValue());
}
}
return ref;
}
private static ParticipantRef parseParticipant(JSONValue jsonValue, int k)
{
String type = JSONWalk.on(jsonValue).next("type").asString();
String idRef = JSONWalk.on(jsonValue).next("idRef").asString();
ParticipantRef p = new ParticipantRef(type, idRef);
boolean isGroup = JSONWalk.on(jsonValue).next("isGroup").asBool();
p.setGroup(isGroup);
return p;
}
public static ProcessInstanceRef parseProcessInstance(JSONObject root)
{
ConsoleLog.debug("parse " + root);
// {"id":"StateSequence/8", "definitionId":"StateSequence:1",
// "startDate":"2009-02-19 14:36:50", "suspended":false}
String id = JSONWalk.on(root).next("id").asString();
String definitionId = JSONWalk.on(root).next("definitionId").asString();
Date start = JSONWalk.on(root).next("startDate").asDate();
JSONWalk.JSONWrapper endDateJSON = JSONWalk.on(root).next("endDate");
Date end = null;
if (endDateJSON != null)
end = endDateJSON.asDate();
boolean suspended = JSONWalk.on(root).next("suspended").asBool();
ProcessInstanceRef processInstance = new ProcessInstanceRef(
id, definitionId,
start, end,
suspended
);
// tokens
JSONObject tokJso = JSONWalk.on(root).next("rootToken").asObject();
TokenReference rootToken = parseTokenReference(tokJso);
processInstance.setRootToken(rootToken);
return processInstance;
}
public static TokenReference parseTokenReference(JSONObject jso)
{
String rootTokenId = JSONWalk.on(jso).next("id").asString();
//String name = JSONWalk.on(jso).next("name").asString();
String nodeName = JSONWalk.on(jso).next("currentNodeName").asString();
// TDOD: Fix token name
TokenReference rt = new TokenReference(rootTokenId, "", nodeName);
boolean canBeSignaled = JSONWalk.on(jso).next("canBeSignaled").asBool();
rt.setCanBeSignaled(canBeSignaled);
JSONArray signals = JSONWalk.on(jso).next("availableSignals").asArray();
for (int i = 0; i < signals.size(); i++)
{
JSONValue jsonValue = signals.get(i);
if (jsonValue.toString().equals("null"))
{
ConsoleLog.debug("FIXME JBPM-1828: Null value on availableSignals:" + signals.toString());
continue; // TODO: JBPM-1828
}
JSONString item = jsonValue.isString();
rt.getAvailableSignals().add(item.stringValue());
}
JSONArray childArr = JSONWalk.on(jso).next("children").asArray();
for (int i = 0; i < childArr.size(); i++)
{
JSONObject item = childArr.get(i).isObject();
rt.getChildren().add(parseTokenReference(item));
}
return rt;
}
public static List<String> parseStringArray(JSONValue jso)
{
List<String> result = new ArrayList<String>();
JSONArray jsonArray = jso.isArray();
if (null == jsonArray)
throw new IllegalArgumentException("Not an array: " + jso);
for (int i = 0; i < jsonArray.size(); i++)
{
JSONValue jsonValue = jsonArray.get(i);
if (jsonValue.toString().equals("null"))
{
ConsoleLog.debug("FIXME JBPM-1828: Null value on string array:" + jsonArray.toString());
continue; // TODO: JBPM-1828
}
JSONString item = jsonValue.isString();
result.add(item.stringValue());
}
return result;
}
}