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.common;
22  
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import no.sintef.assetrepository.Asset;
27  import no.sintef.assetrepository.Representation;
28  import no.sintef.lock.LockException;
29  import no.sintef.lock.LockRequiredException;
30  import coras.structure.ConcernEnum;
31  import coras.structure.ViewpointEnum;
32  import coras.types.ElementTypeEnum;
33  import coras.types.SubtypeEnum;
34  
35  
36  /***
37   * @author fvr
38   *
39   * TODO To change the template for this generated type comment go to
40   * Window - Preferences - Java - Code Style - Code Templates
41   */
42  public abstract class CorasElement extends CorasAsset {
43  
44      private static final String CONCERN = "concern";
45      private static final String VIEWPOINTS = "viewpoints";
46      private static final String VIEWPOINT_SEPARATOR = ":";
47      private static final String TYPE = "type";
48      private static final String SUBTYPE = "subtype";
49      private static final ViewpointEnum[] EMPTY_VIEWPOINTS = new ViewpointEnum[0];
50      
51      protected CorasElement(Asset asset) {
52          super(asset);
53      }
54      
55      protected static Asset createAsset(CorasPackage parent, AssetTypeEnum assetType, ElementTypeEnum type, SubtypeEnum subtype, String name, String shortDescription, String fullDescription, ConcernEnum concern, ViewpointEnum[] viewpoints, CorasRepresentation[] corasRepresentations) throws LockException {
56          return createAsset(parent, assetType, type, subtype, name, shortDescription, fullDescription, concern, viewpoints, null, corasRepresentations);
57      }
58      
59      protected static Asset createAsset(CorasPackage parent, AssetTypeEnum assetType, ElementTypeEnum type, SubtypeEnum subtype, String name, String shortDescription, String fullDescription, ConcernEnum concern, ViewpointEnum[] viewpoints, Map properties, CorasRepresentation[] corasRepresentations) throws LockException {
60          if (properties == null) {
61              properties = new HashMap();
62          }
63          properties.put(TYPE, type.toString());
64          if (subtype != null) properties.put(SUBTYPE, subtype.toString());
65          if (shortDescription != null) properties.put(SHORT_DESCRIPTION, shortDescription);
66          if (concern != null) properties.put(CONCERN, concern.toString());
67          properties.put(VIEWPOINTS, viewpointsToString(viewpoints));
68          String annotation = "";
69          Representation[] representations = new Representation[corasRepresentations != null ? corasRepresentations.length : 0];
70          for (int i = 0; i < representations.length; i++) {
71              representations[i] = corasRepresentations[i].getRepresentation();
72          }
73          return parent.createChildAsset(assetType, name, fullDescription, properties, annotation, representations);
74      }
75      
76      public ConcernEnum getConcern() {
77          return ConcernEnum.forName(getProperty(CONCERN));
78      }
79      public void setConcern(ConcernEnum newConcern) throws LockRequiredException {
80          setProperty(CONCERN, newConcern != null ? newConcern.toString() : null);
81      }
82      
83      public ViewpointEnum[] getViewpoints() {
84          String viewpointsString = getProperty(VIEWPOINTS);
85          if (viewpointsString == null || viewpointsString.trim().equals("")) {
86              return EMPTY_VIEWPOINTS;
87          }
88          String[] viewpointStrings = viewpointsString.split(VIEWPOINT_SEPARATOR);
89          ViewpointEnum[] result = new ViewpointEnum[viewpointStrings.length];
90          for (int i = 0; i < viewpointStrings.length; i++) {
91              result[i] = ViewpointEnum.forName(viewpointStrings[i]);
92          }
93          return result;
94      }
95      public void setViewpoints(ViewpointEnum[] viewpoints) throws LockRequiredException {
96          setProperty(VIEWPOINTS, viewpointsToString(viewpoints));
97      }
98  
99      public ElementTypeEnum getType() {
100         return ElementTypeEnum.forName(getProperty(TYPE));
101     }
102     public SubtypeEnum getSubtype() {
103         return SubtypeEnum.forName(getProperty(SUBTYPE));
104     }
105 
106     /***
107      * @param child
108      * @return
109      */
110     protected static ElementTypeEnum getType(Asset child) {
111         return ElementTypeEnum.forName(child.getProperty(TYPE));
112     }
113 
114     private static String viewpointsToString(ViewpointEnum[] viewpoints) {
115         if (viewpoints == null) {
116             viewpoints = EMPTY_VIEWPOINTS;
117         }
118         StringBuffer sb = new StringBuffer();
119         for (int i = 0; i < viewpoints.length; i++) {
120             if (i > 0) {
121                 sb.append(VIEWPOINT_SEPARATOR);
122             }
123             ViewpointEnum viewpoint = viewpoints[i];
124             if (viewpoint != null) {
125                 sb.append(viewpoint.toString());
126             }
127         }
128         return sb.toString();
129     }
130     
131 }