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