package com.test.visio.main;
public class ExportVisioMap {
public static void main(String[] args) throws Exception {
String dirPath = Utils.getSharedDataDir(ExportVisioMap.class) ;
String shapeDir = Utils.getSharedDataDir(ExportVisioMap.class) + "shapes/";
// String shapeDir = “shapes/”;
License license = new License();
license.setLicense(dirPath+ “license/” + “Aspose.Diagram.lic”);
// license.setLicense(“license/” + “Aspose.Diagram.lic”);
// Create directory if it is not already present.
File file = new File(dirPath);
if (!file.exists())
file.mkdir();
// initialize a new Diagram
Diagram diagram = new Diagram();
int maxPageId = GetMaxPageID(diagram);
// Initialize a new page object
Page newPage = new Page();
// Set name
newPage.setName("spectre map");
// Set page ID
newPage.setID(maxPageId + 1);
// Add a new blank page
diagram.getPages().add(newPage);
// master shapes
String generalappliance = "generalappliance", serverswitch = "serverswitch", bridge = "bridge",
commserver = "commserver", firewall = "firewall", universalgateway ="universalgateway",
hub = "hub", router = "router", kvmDevice = "kvmdevice", layer3switch = "layer3switch",
virtuallayerswitch = "virtuallayerswitch", netsonar = "netsonar", printer = "printer",
storagearray = "storagearray", terminal = "terminal", ups= "ups", bug = "bug",
dynamicConnector = "Dynamic connector", cellularphone = "cellularphone",unknown = "unknown";
diagram.addMaster(shapeDir + "Test.vss", generalappliance);
diagram.addMaster(shapeDir + "Test.vss", serverswitch);
diagram.addMaster(shapeDir + "Test.vss", bridge);
diagram.addMaster(shapeDir + "Test.vss", commserver);
diagram.addMaster(shapeDir + "Test.vss", firewall);
diagram.addMaster(shapeDir + "Test.vss", universalgateway);
diagram.addMaster(shapeDir + "Test.vss", hub);
diagram.addMaster(shapeDir + "Test.vss", router);
diagram.addMaster(shapeDir + "Test.vss", kvmDevice);
diagram.addMaster(shapeDir + "Test.vss", layer3switch);
diagram.addMaster(shapeDir + "Test.vss", virtuallayerswitch);
diagram.addMaster(shapeDir + "Test.vss", netsonar);
diagram.addMaster(shapeDir + "Test.vss", printer);
diagram.addMaster(shapeDir + "Test.vss", storagearray);
diagram.addMaster(shapeDir + "Test.vss", terminal);
diagram.addMaster(shapeDir + "Test.vss", ups);
diagram.addMaster(shapeDir + "Test.vss", bug);
diagram.addMaster(shapeDir + "Test.vss", dynamicConnector);
diagram.addMaster(shapeDir + "Test.vss", cellularphone);
diagram.addMaster(shapeDir + "Test.vss", unknown);
//removing default page
Page page = diagram.getPages().getPage(0);
diagram.getPages().remove(diagram.getPages().get(page.getID()));
int PageIndex = 0;
Page p = diagram.getPages().getPage("spectre map");
p.getPageSheet().getPageProps().getDrawingResizeType().setValue(DrawingResizeTypeValue.DEPENDS_ON_DRAWING_SIZE_TYPE);
// page orientation
p.getPageSheet().getPrintProps().getPrintPageOrientation().setValue(PrintPageOrientationValue.PORTRAIT);
//iterating json data
JSONParser nodeParser = new JSONParser();
JSONParser edgeParser = new JSONParser();
Map<Source, Target> sourceTargetMap = null;
ArrayList<Nodes> totalNodesList = new ArrayList<Nodes>();
long highestX = 0;
long highestY = 0;
try {
Object nodesobj = nodeParser.parse(new FileReader(dirPath + "//json//esi_nodes.json"));
Object edgeobj = edgeParser.parse(new FileReader(dirPath + "//json//esi_edges.json"));
/* Object nodesobj = nodeParser.parse(new FileReader("json/nodes.json"));
Object edgeobj = edgeParser.parse(new FileReader("json/edges.json"));
*/
JSONArray nodesList = (JSONArray) nodesobj;
JSONArray edgeList = (JSONArray) edgeobj;
for (int i = 0; i < nodesList.size(); i++) {
JSONObject jsonObject = (JSONObject) nodesList.get(i);
Nodes nodes = new Nodes();
long node_id = (Long) jsonObject.get("node_id");
String ip_address = (String) jsonObject.get("ip_address".trim());
long xAxis = (Long) jsonObject.get("x");
long yAxis = (Long) jsonObject.get("y");
String device_type = (String) jsonObject.get("device type");
nodes.setNode_id(node_id);
nodes.setIp_address(ip_address);
nodes.setDevice_type(device_type);
nodes.setX(xAxis);
nodes.setY(yAxis);
totalNodesList.add(nodes);
long highestXtemp = 0;
long highestYtemp = 0;
// to find highest value of X in a Nodes
if(xAxis > highestX)
highestXtemp = xAxis;
if(highestXtemp > highestX)
highestX = highestXtemp;
// to find highest value of Y in a Nodes
if(yAxis > highestY)
highestYtemp = yAxis;
if(highestYtemp > highestY)
highestY = highestYtemp;
}
//converting X and Y axis to Inches
if(highestX != 0 && highestY != 0) {
highestX = (long) (highestX*0.1042/10);
highestY = (long) (highestY*0.1042/10);
} else {
highestX = 100;
highestY = 100;
}
System.out.println("highest X axis :: "+highestX);
System.out.println("highest Y axis :: "+highestY);
//setting highest X and Y as Page width and height
p.getPageSheet().getPageProps().getPageWidth().setValue(highestX+1);
p.getPageSheet().getPageProps().getPageHeight().setValue(highestY+1);
System.out.println("Number of Nodes :: "+totalNodesList.size());
sourceTargetMap = new HashMap<Source, Target>();
for (int i = 0; i < edgeList.size(); i++) {
JSONObject jsonObject = (JSONObject) edgeList.get(i);
long source = (Long) jsonObject.get("source");
long target = (Long) jsonObject.get("target");
Source sourceObj = new Source();
sourceObj.setSource(source);
Target targetObj = new Target();
targetObj.setTarget(target);
sourceTargetMap.put(sourceObj, targetObj);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
ArrayList<Positions> positionList = new ArrayList<Positions>();
for (Nodes nodes : totalNodesList) {
double sourceWidth = (double)nodes.getX();
double sourceHeight =(double)nodes.getY();
if(sourceWidth != 0) {
sourceWidth = sourceWidth*0.1042/10;
}
if(sourceHeight != 0) {
sourceHeight = sourceHeight*0.1042/10;
}
long sourcePosition = diagram.addShape(sourceWidth, sourceHeight, nodes.getDevice_type(), PageIndex);
Shape shape = p.getShapes().getShape(sourcePosition);
shape.getTextXForm().getTxtWidth().setValue(1.5);
shape.getParas().add(new Para());
shape.getParas().getPara(0).getHorzAlign().setValue(HorzAlignValue.JUSTIFY);
//set index for para
shape.getParas().getPara(0).setIX(0);
// mark character run and add text
shape.getText().getValue().add(new Cp(0));
shape.getText().getValue().add(new Txt(nodes.getIp_address()));
Prop prop1 = new Prop();
prop1.setName("IP");
prop1.getLabel().setValue("IP");
// prop1.getValue().setVal(nodes.getIp_address().toString());
prop1.getType().setValue(PropType.STRING);
prop1.getValue().setVal(nodes.getIp_address().toString());
Prop prop2 = new Prop();
prop2.setName("XAxis");
prop2.getLabel().setValue("XAxis");
prop2.getValue().setVal(sourceWidth+"");
Prop prop3 = new Prop();
prop3.setName("YAxis");
prop3.getLabel().setValue("YAxis");
prop3.getValue().setVal(sourceHeight+"");
Prop prop4 = new Prop();
TypeProp typeProp4 = new TypeProp(TypePropValue.STRING);
prop4.setName("DeviceType");
prop4.getLabel().setValue("DeviceType");
String deviceType = nodes.getDevice_type();
// System.out.println("typeProp4========"+typeProp4.getValue());
prop4.getType().setValue(typeProp4.getValue());
// prop4.getValue().setVal(typeProp4.getValue());
// System.err.println("type==="+nodes.getDevice_type());
shape.getProps().add(prop1);
shape.getProps().add(prop2);
shape.getProps().add(prop3);
shape.getProps().add(prop4);
// System.err.println("count==="+shape.getProps().getCount());
/*for (Prop prop :(Iterable<Prop>) shape.getProps())
{
if (prop.getLabel().getValue() == "DeviceType")
prop.getValue().setVal(shape.getName());
}*/
Positions position = new Positions();
position.setNode_id(nodes.getNode_id());
position.setPosition(sourcePosition);
positionList.add(position);
}
long connectorPosition = 0;
Set<Source> keySet = sourceTargetMap.keySet();
for (Source keys : keySet) {
Source sourceId = keys;
Target targetId = (Target)sourceTargetMap.get(keys);
long sourcePosition=0;
long targetPosition=0;
for (Positions pos : positionList) {
if(sourceId.getSource() == pos.getNode_id()) {
sourcePosition = pos.getPosition();
}if(targetId.getTarget() == pos.getNode_id()) {
targetPosition = pos.getPosition();
}
}
Shape connectorShape = new Shape();
// set line weight, default in PT
connectorShape.getLine().getLineWeight().setValue(0.01388);
// set color of the shape's line
connectorShape.getLine().getLineColor().setValue("#3058a7");
// set line rounding, default in inch
connectorShape.getLine().getRounding().setValue(0);
// set line caps
connectorShape.getLine().getLineCap().setValue(BOOL.TRUE);
// set line color transparency in percent
connectorShape.getLine().getLineColorTrans().setValue(50);
connectorPosition = diagram.addShape(connectorShape, dynamicConnector, PageIndex);
p.connectShapesViaConnector(sourcePosition, ConnectionPointPlace.RIGHT, targetPosition, ConnectionPointPlace.LEFT, connectorPosition);
}
/*Shape shape1 = p.getShapes().getShape(1);
for (Prop prop :(Iterable<Prop>) shape1.getProps())
{
System.out.println("Label Name=="+prop.getLabel().getValue());
System.out.println("Label Value=="+prop.getValue().getVal());
if (prop.getLabel().getValue() == "DeviceType")
prop.getValue().setVal(prop.getValue().getVal());
}*/
// Save diagram
SimpleDateFormat localDateFormat = new SimpleDateFormat("MMddYYYY-HHmmss");
String fileNameWithDataAndTime = "spectre_digram-"+localDateFormat.format(new Date())+".vsdx";
System.out.println(fileNameWithDataAndTime);
diagram.save(dirPath + "diagrams/" +fileNameWithDataAndTime , SaveFileFormat.VSDX);
// diagram.save(“diagrams/” +fileNameWithDataAndTime , SaveFileFormat.VSDX);
}
private static int GetMaxPageID(Diagram diagram)
{
int max = diagram.getPages().getPage(0).getID();
for (int i = 1; i < diagram.getPages().getCount(); i++)
{
if (max < diagram.getPages().getPage(i).getID())
max = diagram.getPages().getPage(i).getID();
}
return max;
}
}