1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package coras.riskanalysis;
22
23 import java.io.BufferedOutputStream;
24 import java.io.ByteArrayInputStream;
25 import java.io.ByteArrayOutputStream;
26 import java.io.FileNotFoundException;
27 import java.io.IOException;
28 import java.util.Observable;
29 import java.util.Observer;
30
31 import javax.xml.bind.JAXBContext;
32 import javax.xml.bind.JAXBException;
33 import javax.xml.bind.Marshaller;
34 import javax.xml.bind.Unmarshaller;
35
36 import no.sintef.assetrepository.Asset;
37 import no.sintef.lock.LockException;
38 import no.sintef.lock.LockRequiredException;
39 import no.sintef.xml.XmlException;
40 import no.sintef.xml.XmlHelper;
41
42 import org.apache.log4j.Logger;
43 import org.w3c.dom.Node;
44 import org.xml.sax.InputSource;
45
46 import coras.common.CorasRepresentation;
47 import coras.representations.RepresentationFactory;
48 import coras.representations.RepresentationTypeEnum;
49 import coras.representations.TableRepresentation;
50 import coras.structure.ConcernEnum;
51 import coras.structure.ViewpointEnum;
52 import coras.table.CorasTableModel;
53 import coras.table.jaxb.TableType;
54 import coras.types.ElementTypeEnum;
55 import coras.types.ITable;
56 import coras.types.SubtypeEnum;
57
58 /***
59 * @author fvr
60 *
61 * TODO To change the template for this generated type comment go to Window -
62 * Preferences - Java - Code Style - Code Templates
63 */
64 public final class TableResult extends RiskAnalysisResult implements ITable {
65
66 private static final Logger LOGGER = Logger.getLogger(TableRepresentation.class);
67
68 private Unmarshaller unmarshaller = null;
69 private Marshaller marshaller = null;
70
71 private CorasTableModel tableModel = new CorasTableModel();
72 private TableRepresentation representation = null;
73
74 /***
75 * @param project
76 * @param name
77 * @param shortDescription
78 * @param concern
79 * @param viewpoint
80 * @return
81 * @throws LockException
82 * @throws XmlException
83 * @throws FileNotFoundException
84 */
85 protected static TableResult create(RiskAnalysisProject project,
86 SubtypeEnum subtype, String name, String shortDescription,
87 String fullDescription, ConcernEnum concern, ViewpointEnum viewpoint,
88 TableRepresentation table)
89 throws LockException, FileNotFoundException, XmlException {
90 if (table == null) {
91 table = RepresentationFactory.createTable(subtype);
92 }
93 CorasRepresentation[] representations = new CorasRepresentation[] {table };
94 Asset asset = createAsset(project, ElementTypeEnum.TABLE, subtype,
95 name, shortDescription, fullDescription, concern, viewpoint,
96 representations);
97 return new TableResult(asset);
98 }
99
100 /***
101 * @param asset
102 */
103 protected TableResult(Asset asset) {
104 super(asset);
105 init();
106 updateTableModel(false);
107 addObserver(new Observer() {
108 public void update(Observable o, Object arg) {
109 updateTableModel(false);
110 }
111 });
112 }
113
114
115 public boolean isDirty() {
116 return super.isDirty() || tableModel.isDirty();
117 }
118
119 public CorasTableModel getTableModel() {
120 return tableModel;
121 }
122
123
124
125
126
127 public void unCheckOut() throws LockException {
128 super.unCheckOut();
129 updateTableModel(true);
130
131 tableModel.makeClean();
132 }
133
134 private void updateTableModel(boolean force) {
135 TableRepresentation newRepresentation = getTableRepresentation();
136 if (!force && representation == newRepresentation) {
137
138
139 return;
140 }
141 synchronized (this) {
142 representation = newRepresentation;
143 TableType table = null;
144
145
146
147
148 if (representation != null) {
149 Node n = getTableRepresentation().getXmlContent();
150
151
152
153
154
155
156
157
158 if (n != null) {
159 try {
160 table = (TableType) unmarshaller.unmarshal(n);
161 tableModel.setTable(table);
162 } catch (JAXBException e) {
163
164 e.printStackTrace();
165 }
166 }
167 }
168 tableModel.setTable(table);
169 }
170 }
171
172 public synchronized void updateRepresentation() throws LockRequiredException {
173 try {
174 Node n = null;
175 TableType table = tableModel.getTable();
176
177
178
179
180
181
182
183
184 if (table != null) {
185 ByteArrayOutputStream baos = new ByteArrayOutputStream();
186 marshaller.marshal(table, new BufferedOutputStream(baos));
187 byte[] bytes = baos.toByteArray();
188 n = XmlHelper.parse(new InputSource(new ByteArrayInputStream(bytes)));
189 }
190
191 TableRepresentation newTable = RepresentationFactory.createTable(n, getName());
192 setTableRepresentation(newTable);
193
194 tableModel.makeClean();
195 } catch (JAXBException e) {
196
197 e.printStackTrace();
198 } catch (XmlException e) {
199
200 e.printStackTrace();
201 } catch (IOException e) {
202
203 e.printStackTrace();
204 }
205 }
206
207
208
209
210
211
212 private TableRepresentation getTableRepresentation() {
213 return (TableRepresentation) getRepresentation(RepresentationTypeEnum.TABLE);
214 }
215
216 private void setTableRepresentation(TableRepresentation newTable) throws LockRequiredException {
217 TableRepresentation oldTable = getTableRepresentation();
218 removeRepresentation(oldTable);
219 addRepresentation(newTable);
220 }
221
222 private void init() {
223
224 try {
225 JAXBContext jc = JAXBContext.newInstance("coras.table.jaxb");
226 marshaller = jc.createMarshaller();
227 marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
228
229
230
231
232
233
234
235
236
237
238
239 unmarshaller = jc.createUnmarshaller();
240 } catch (JAXBException e) {
241 LOGGER.fatal("Unable to initialize Java-to-XML binding for context coras.table.xml: " + e.getMessage());
242 }
243 }
244
245
246 }