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.FileNotFoundException;
24  import java.util.ArrayList;
25  import java.util.Collection;
26  import java.util.HashMap;
27  import java.util.Iterator;
28  import java.util.Map;
29  
30  import no.sintef.assetrepository.Asset;
31  import no.sintef.assetrepository.Representation;
32  import no.sintef.lock.LockException;
33  import no.sintef.xml.XmlException;
34  import coras.client.SwingWorker;
35  import coras.common.AssetTypeEnum;
36  import coras.common.CorasPackage;
37  import coras.representations.DiagramRepresentation;
38  import coras.representations.DocumentRepresentation;
39  import coras.representations.FaultTreeModel;
40  import coras.representations.TableRepresentation;
41  import coras.representations.UMLModelRepresentation;
42  import coras.reuse.DocumentExperience;
43  import coras.reuse.FaultTreeExperience;
44  import coras.reuse.UMLModelExperience;
45  import coras.structure.ConcernEnum;
46  import coras.structure.ViewpointEnum;
47  import coras.types.ElementTypeEnum;
48  import coras.types.SubtypeEnum;
49  
50  /***
51   * @author fvr
52   *
53   * TODO To change the template for this generated type comment go to
54   * Window - Preferences - Java - Code Style - Code Templates
55   */
56  public final class RiskAnalysisProject extends CorasPackage {
57  
58  	private Map children = new HashMap();
59  	
60      /***
61       * @return
62       */
63      public static Collection getProjects() {
64          Collection result = new ArrayList();
65          Collection assets = Asset.getAssetsByType(AssetTypeEnum.RISK_ANALYSIS_PROJECT.toString());
66          for (Iterator i = assets.iterator(); i.hasNext();) {
67              Asset asset = (Asset) i.next();
68              RiskAnalysisProject project = new RiskAnalysisProject(asset);
69              result.add(project);
70          }
71          return result;
72      }
73  
74      public static RiskAnalysisProject getProject(String projectId) {
75      	Asset asset = Asset.getAssetById(projectId);
76      	// FIXME check that this is really a risk analysis project!!!
77          return new RiskAnalysisProject(asset);
78      }
79      
80      public static RiskAnalysisProject createProject(String name, String shortDescription, String fullDescription) {
81          String type = AssetTypeEnum.RISK_ANALYSIS_PROJECT.toString();
82          Map properties = new HashMap();
83          if (shortDescription != null) properties.put(SHORT_DESCRIPTION, shortDescription);
84          String annotation = "";
85          Representation[] representations = new Representation[0];
86          Asset asset;
87          try {
88              asset = Asset.createRootAsset(type, name, fullDescription, properties, annotation, representations = new Representation[0]);
89              return new RiskAnalysisProject(asset);
90          } catch (LockException e) {
91              // FIXME This should never happen!
92              e.printStackTrace();
93              return null;
94          }
95      }
96      
97      /***
98       * @param assetVersion
99       */
100     private RiskAnalysisProject(Asset asset) {
101         super(asset);
102     }
103 
104     public Collection getResults(SwingWorker worker) {
105         Collection results = new ArrayList();
106         Collection childAssets = getChildAssets(worker);
107         for (Iterator i = childAssets.iterator(); i.hasNext();) {
108             Asset asset = (Asset) i.next();
109             RiskAnalysisResult result = (RiskAnalysisResult) children.get(asset);
110             if (result == null) {
111                 ElementTypeEnum type = RiskAnalysisResult.getType(asset);
112                 if (type == ElementTypeEnum.DOCUMENT) {
113                     result = new DocumentResult(asset);
114                 } else if (type == ElementTypeEnum.FAULT_TREE) {
115                     result = new FaultTreeResult(asset);
116                 } else if (type == ElementTypeEnum.TABLE) {
117                     result = new TableResult(asset);
118                 } else if (type == ElementTypeEnum.UML_MODEL) {
119                     result = new UMLModelResult(asset);
120                 } else {
121                     System.err.println("ERROR: unexpected result type " + asset.getProperty("type"));
122                     continue;
123                 }
124             	children.put(asset, result);
125             }
126             results.add(result);
127         }
128         return results;
129     }
130     
131     public RiskAnalysisResult getResult(String resultId) {
132     	if (resultId == null) {
133     		return null;
134     	}
135     	Collection results = getResults(null);
136     	for (Iterator i = results.iterator(); i.hasNext();) {
137 			RiskAnalysisResult r = (RiskAnalysisResult) i.next();
138 			if (resultId.equals(r.getId())) {
139 				return r;
140 			}
141     	}
142     	return null;
143     }
144     
145     public RiskAnalysisResult getResult(ElementTypeEnum type, SubtypeEnum subtype) {
146     	Collection results = getResults(null);
147     	for (Iterator i = results.iterator(); i.hasNext();) {
148     		RiskAnalysisResult r = (RiskAnalysisResult) i.next();
149     		if (r.getType() == type && r.getSubtype() == subtype) {
150     			return r;
151     		}
152     	}
153     	return null;
154     }
155     
156     public UMLModelResult createUMLModel(SubtypeEnum subtype, String name, String shortDescription, String fullDescription, ConcernEnum concern, ViewpointEnum viewpoint, UMLModelRepresentation umlModel, DiagramRepresentation diagram) throws LockException {
157         return UMLModelResult.create(this, subtype, name, shortDescription, fullDescription, concern, viewpoint, umlModel, diagram);
158     }
159     
160     public UMLModelResult createUMLModel(UMLModelExperience experience) throws LockException {
161         if (experience == null) {
162             return null;
163         }
164         SubtypeEnum subtype = experience.getSubtype();
165         String name = experience.getName();
166         String shortDescription = experience.getShortDescription();
167         String fullDescription = experience.getFullDescription();
168         ConcernEnum concern = experience.getConcern();
169         ViewpointEnum[] viewpoints = experience.getViewpoints();
170         ViewpointEnum viewpoint = (viewpoints == null || viewpoints.length == 0 ? null : viewpoints[0]);
171         UMLModelRepresentation umlModel = experience.getUMLModel();
172         DiagramRepresentation diagram = experience.getUMLDiagram();
173         return createUMLModel(subtype, name, shortDescription, fullDescription, concern, viewpoint, umlModel, diagram);
174     }
175     
176     public TableResult createTable(SubtypeEnum subtype, String name, String shortDescription, String fullDescription, ConcernEnum concern, ViewpointEnum viewpoint, TableRepresentation table) throws FileNotFoundException, LockException, XmlException {
177         return TableResult.create(this, subtype, name, shortDescription, fullDescription, concern, viewpoint, table);
178     }
179     
180     public FaultTreeResult createFaultTree(String name, String shortDescription, String fullDescription, ConcernEnum concern, ViewpointEnum viewpoint, FaultTreeModel faultTreeModel, DiagramRepresentation diagram) throws LockException {
181         return FaultTreeResult.create(this, name, shortDescription, fullDescription, concern, viewpoint, faultTreeModel, diagram);
182     }
183     
184     public FaultTreeResult createFaultTree(FaultTreeExperience experience) throws LockException {
185         if (experience == null) {
186             return null;
187         }
188         String name = experience.getName();
189         String shortDescription = experience.getShortDescription();
190         String fullDescription = experience.getFullDescription();
191         ConcernEnum concern = experience.getConcern();
192         ViewpointEnum[] viewpoints = experience.getViewpoints();
193         ViewpointEnum viewpoint = (viewpoints == null || viewpoints.length == 0 ? null : viewpoints[0]);
194         FaultTreeModel faultTreeModel = experience.getFaultTreeModel();
195         DiagramRepresentation diagram = experience.getFaultTreeDiagram();
196         return createFaultTree(name, shortDescription, fullDescription, concern, viewpoint, faultTreeModel, diagram);
197     }
198     
199     public DocumentResult createDocument(String name, String shortDescription, String fullDescription, ConcernEnum concern, ViewpointEnum viewpoint, DocumentRepresentation content) throws LockException {
200         return DocumentResult.create(this, name, shortDescription, fullDescription, concern, viewpoint, content);
201     }
202     
203     public DocumentResult createDocument(DocumentExperience experience) throws LockException {
204         if (experience == null) {
205             return null;
206         }
207         String name = experience.getName();
208         String shortDescription = experience.getShortDescription();
209         String fullDescription = experience.getFullDescription();
210         ConcernEnum concern = experience.getConcern();
211         ViewpointEnum[] viewpoints = experience.getViewpoints();
212         ViewpointEnum viewpoint = (viewpoints == null || viewpoints.length == 0 ? null : viewpoints[0]);
213         DocumentRepresentation document = experience.getDocumentContent();
214         return createDocument(name, shortDescription, fullDescription, concern, viewpoint, document);
215     }
216     
217 }