View Javadoc

1   /*
2    *  Copyright (C) 2003-2005 SINTEF
3    *  Author:  Fredrik Vraalsen (fredrik dot vraalsen at sintef dot no)
4    *  Webpage: http://coras.sourceforge.net/
5    *
6    *  This program is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public License
8    *  as published by the Free Software Foundation; either version 2.1
9    *  of the License, or (at your option) any later version.
10   *
11   *  This program is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this program; if not, write to the Free
18   *  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19   *  02111-1307 USA
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 	/* (non-Javadoc)
125 	 * @see coras.common.CorasAsset#unCheckOut()
126 	 */
127 	public void unCheckOut() throws LockException {
128 		super.unCheckOut();
129 		updateTableModel(true);
130 		// FIXME move makeClean call into updateTableModel?
131 		tableModel.makeClean();
132 	}
133 	
134     private void updateTableModel(boolean force) {
135         TableRepresentation newRepresentation = getTableRepresentation();
136         if (!force && representation == newRepresentation) {
137             // the table model will only change upon a reload, which is the only
138             // time the representation will change (confirm this?)
139             return;
140         }
141         synchronized (this) {
142             representation = newRepresentation;
143             TableType table = null;
144 
145             // What to do if representation == null? Recreate table!?
146             // representation = RepresentationFactory.createTable();
147 
148             if (representation != null) {
149                 Node n = getTableRepresentation().getXmlContent();
150                 /*
151                 try {
152                     System.out.println("table data: " + new String(XmlHelper.xmlToUtf8(n), "UTF-8"));
153                 } catch (UnsupportedEncodingException e1) {
154                     // TODO Auto-generated catch block
155                     e1.printStackTrace();
156                 }
157                 */
158                 if (n != null) {
159                     try {
160                         table = (TableType) unmarshaller.unmarshal(n);
161                         tableModel.setTable(table);
162                     } catch (JAXBException e) {
163                         // TODO Auto-generated catch block
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              DOMResult result = new DOMResult();
179              marshaller.marshal(tableModel, result);
180              Node n = result.getNode();
181              */
182             
183             // FIXME JaxMe bugs ???
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     		// FIXME move makeClean call into updateTableModel?
194             tableModel.makeClean();
195         } catch (JAXBException e) {
196             // TODO Auto-generated catch block
197             e.printStackTrace();
198         } catch (XmlException e) {
199             // TODO Auto-generated catch block
200             e.printStackTrace(); 
201         } catch (IOException e) { 
202             // TODO Auto-generated catch block 
203             e.printStackTrace();
204         }
205     }
206 
207     /*
208      * (non-Javadoc)
209      * 
210      * @see coras.types.ITable#getTableRepresentation()
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 	    // _tableModel.addTableModelListener(this);
224 		try {
225 			JAXBContext jc = JAXBContext.newInstance("coras.table.jaxb");
226 			marshaller = jc.createMarshaller();
227 			marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
228 			
229 			// TODO: Handle Jaxme2?
230 			/*
231              * marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper",
232              * new NamespacePrefixMapper() { public String
233              * getPreferredPrefix(String namespaceUri, String suggestion,
234              * boolean requirePrefix) { if
235              * (CorasTable.NAMESPACE_URI.equals(namespaceUri)) return
236              * CorasTable.PREFIX; return suggestion; } });
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 }