View Javadoc

1   package coras.table;
2   
3   import java.io.UnsupportedEncodingException;
4   
5   import javax.xml.bind.JAXBContext;
6   import javax.xml.bind.JAXBException;
7   import javax.xml.bind.Marshaller;
8   import javax.xml.transform.dom.DOMResult;
9   
10  import no.sintef.xml.XmlHelper;
11  
12  import org.w3c.dom.Node;
13  
14  import coras.table.jaxb.CellType;
15  import coras.table.jaxb.ColumnDefType;
16  import coras.table.jaxb.ColumnDefsType;
17  import coras.table.jaxb.ObjectFactory;
18  import coras.table.jaxb.RowType;
19  import coras.table.jaxb.RowsType;
20  import coras.table.jaxb.TableType;
21  import coras.types.ITable;
22  
23  public class RiskMatrixGenerator {
24  
25  	private static final String FREQUENCY_COLUMN_ID = "riskMatrixFrequency";
26  	private static final String RISK_VALUE_COLUMN_ID = "riskValue";
27  
28  	private static final int FREQUENCY_ROW = 1;
29  	private static final int CONSEQUENCE_ROW = 2;
30  	private static final int VALUES_COLUMN = 2;
31  	private static final String VALUES_SPLIT_PATTERN = "//s*,//s*";
32  
33  	public static void main(String[] args) throws UnsupportedEncodingException {
34  		String[] consequenceValues = new String[] { "Low", "Medium", "High"};
35  		String[] frequencyValues = new String[] { "Rare", "Possible", "Certain"};
36  		Node n = generateRiskMatrix(consequenceValues, frequencyValues);
37  		System.out.println(new String(XmlHelper.xmlToUtf8(n, true), "UTF-8"));
38  	}
39  	
40  	public static Node generateRiskMatrix(ITable valueDefinitionTable) {
41  		CorasTableModel tableModel = valueDefinitionTable.getTableModel();
42  		String frequencyValues = (String) tableModel.getValueAt(FREQUENCY_ROW, VALUES_COLUMN);
43  		String consequenceValues = (String) tableModel.getValueAt(CONSEQUENCE_ROW, VALUES_COLUMN);
44  		return generateRiskMatrix(consequenceValues.split(VALUES_SPLIT_PATTERN), 
45  				frequencyValues.split(VALUES_SPLIT_PATTERN));
46  	}
47  	
48  	private static Node generateRiskMatrix(String[] consequenceValues, String[] frequencyValues) {
49  		if (consequenceValues == null 
50  				|| consequenceValues.length == 0
51  				|| (consequenceValues.length == 1 && consequenceValues[0].equals(""))) {
52  			throw new IllegalArgumentException("No consequence values defined.");
53  		}
54  		if (frequencyValues == null 
55  				|| frequencyValues.length == 0
56  				|| (frequencyValues.length == 1 && frequencyValues[0].equals(""))) {
57  			throw new IllegalArgumentException("No frequency values defined.");
58  		}
59  		try {
60  			Marshaller m = JAXBContext.newInstance("coras.table.jaxb").createMarshaller();
61  			ObjectFactory of = new ObjectFactory();
62  			ColumnDefsType columnDefs = of.createColumnDefsType();
63  			ColumnDefType frequencyCol = of.createColumnDefType();
64  			frequencyCol.setId(FREQUENCY_COLUMN_ID);
65  			frequencyCol.setName("Frequency");
66  			columnDefs.getColumnDef().add(frequencyCol);
67  			for (int i = 0; i < consequenceValues.length; i++) {
68  				String consequenceValue = consequenceValues[i];
69  				ColumnDefType consequenceCol = of.createColumnDefType();
70  				consequenceCol.setId(RISK_VALUE_COLUMN_ID + i);
71  				consequenceCol.setName(consequenceValue);
72  				columnDefs.getColumnDef().add(consequenceCol);
73  			}
74  			RowsType rows = of.createRowsType();
75  			for (int i = frequencyValues.length - 1; i >= 0; i--) {
76  				String frequencyValue = frequencyValues[i];
77  				CellType cell = of.createCellType();
78  				cell.setColumnId(FREQUENCY_COLUMN_ID);
79  				cell.setValue(frequencyValue);
80  				RowType row = of.createRowType();
81  				row.getCell().add(cell);
82  				rows.getRow().add(row);
83  			}
84  			TableType table = of.createTable();
85  			table.setType("Risk Matrix");
86  			table.setFixedStructure(true);
87  			table.setColumnDefs(columnDefs);
88  			table.setRows(rows);
89  			DOMResult result = new DOMResult();
90  			m.marshal(table, result);
91  			return result.getNode();
92  		} catch (JAXBException e) {
93  			// This should never happen!
94  			e.printStackTrace();
95  			return null;
96  		}
97  	}
98  	
99  }