View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2009 Ordina and committers to Mod4j
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *     Ordina - initial implementation
10   *******************************************************************************/
11  package org.mod4j.crossx.broker;
12  
13  import java.util.ArrayList;
14  import java.util.List;
15  
16  import org.apache.log4j.Logger;
17  import org.mod4j.crossx.mm.crossx.LiteralSymbolProperty;
18  import org.mod4j.crossx.mm.crossx.ModelInfo;
19  import org.mod4j.crossx.mm.crossx.ReferenceSymbolProperty;
20  import org.mod4j.crossx.mm.crossx.Symbol;
21  import org.mod4j.crossx.mm.crossx.SymbolProperty;
22  
23  /**
24   * This class contains all operations that are needed in the oAW xTend, xPand and Checks files.
25   * Therefore all methods need to be static.
26   * 
27   * @author jwa11799
28   * 
29   */
30  public class CrossxBroker {
31  
32      private static final Logger logger = Logger.getLogger(CrossxBroker.class.getName());
33      
34      /**
35       * Private constructor, this class contains static members only.
36       */
37      private CrossxBroker() {
38      }
39      
40      public static Symbol lookupReference(ReferenceSymbolProperty ref){
41          if( ref == null ) return null;
42          return lookupSymbol(ref.getModelname(), ref.getSymbolname(), ref.getType());
43      }
44  
45      /**
46       * Lookup a symbol in a model of a certain type
47       * 
48       * @param model
49       *            The model in which to look for the symbol
50       * @param symbolname
51       *            The name of the symbol to find
52       * @param symbolType
53       *            The type of the symbol to find
54       * @return the symbol found, or null if no such symbol could be found
55       */
56      public static Symbol lookupSymbol(String model, String symbolname, String symbolType) {
57          return CrossxEnvironment.lookupSymbol(model, symbolname, symbolType);
58      }
59  
60      /**
61       * Find all symbol names with type 'symbolType' within models named 'modelname'
62       * 
63       * @param modelname
64       * @param symbolType
65       * @return
66       */
67      public static List<Symbol> findAllFromModel(String modelname, String symbolType) {
68          return CrossxEnvironment.findAllFromModel(modelname, symbolType);
69      }
70  
71      /**
72       * Find all symbol names with type in 'symbolTypes' within models named 'modelname'
73       * 
74       * @param modelname
75       * @param symbolTypes
76       * @return
77       */
78      public static List<Symbol> findAllFromModel(String modelname, List<String> symbolTypes) {
79      	List<Symbol> result = new ArrayList<Symbol>();
80      	for (String symbolType : symbolTypes) {
81      		result.addAll(CrossxEnvironment.findAllFromModel(modelname, symbolType));
82      	}
83          return result;
84      }
85  
86      /**
87       * Find all symbols with type in 'symbolTypes' within models named 'modelname' with name 'symbolName'
88       * 
89       * @param modelname
90       * @param symbolName
91       * @param symbolTypes
92       * @return
93       */
94      public static List<Symbol> findAllFromModel(String modelname, String symbolName, List<String> symbolTypes) {
95          List<Symbol> result = new ArrayList<Symbol>();
96          for (String symbolType : symbolTypes) {
97              Symbol symbol = CrossxEnvironment.lookupSymbol(modelname, symbolName, symbolType);
98              if (symbol != null) {
99                  result.add(symbol);
100             }
101         }
102         return result;
103     }
104     
105     /**
106      * Find all symbol names with type 'symbolType' within models named 'modelname' and within the project named 'project'
107      * 
108      * @param project
109      * @param modelname
110      * @param symbolType
111      * @return
112      */
113     public static List<Symbol> findAllSymbolsFromModel(String project, String modelname, String symbolType) {
114         return CrossxEnvironment.findAllSymbolsFromModelInProject(project, modelname, symbolType);
115     }
116 
117     /**
118      * Find all models within the workspace
119      * 
120      * @return
121      */
122 
123     public static List<ModelInfo> findAllModels() {
124         return CrossxEnvironment.findAllModels();
125     }
126 
127     public static List<ModelInfo> findAllModels(List<String> types) {
128         List<ModelInfo> all = CrossxEnvironment.findAllModels();
129         List<ModelInfo> filtered = new ArrayList<ModelInfo>();
130         for (ModelInfo modelInfo : all) {
131             if (types.contains(modelInfo.getDslname())) {
132                 filtered.add(modelInfo);
133             }
134         }
135         return filtered;
136     }
137     
138     /**
139      * Find all models within project with name 'project'
140      * 
141      * @param project
142      * @return
143      */
144     public static List<ModelInfo> findAllModelsInProject(String project) {
145         return CrossxEnvironment.findAllModelsInProject(project);
146     }
147 
148     /**
149      * Find all models within project with name 'project'
150      * 
151      * @param project
152      * @return
153      */
154     public static List<ModelInfo> findAllModelsInProject(String project, List<String> types) {
155         System.out.println("findAllModelsInProject[" + project + "]");
156         List<ModelInfo> all = CrossxEnvironment.findAllModelsInProject(project);
157         List<ModelInfo> result = new ArrayList<ModelInfo>();
158         for (ModelInfo modelInfo : all) {
159             if( types.contains( modelInfo.getDslname()) ) {
160                 System.out.println("add [" + modelInfo.getModelname()+ "]");
161                 result.add(modelInfo);
162             } else {
163                 System.out.println("NOT add [" + modelInfo.getModelname()+ "]");
164             }
165         }
166         return result;
167     }
168 
169     /**
170       */
171     public static Symbol lookupSymbolWithProperty(String model, String symbolname, String type, String propertyName,
172             String propertyValue) {
173         Symbol symbol = CrossxEnvironment.lookupSymbol(model, symbolname, type);
174         if (hasProperty(symbol, propertyName, propertyValue)) {
175             return symbol;
176         }
177         return null;
178     }
179 
180     public static Symbol lookupSymbolWithSubsymbol(String model, String symbolname, String symboltype,
181             String subsymbolType, String subsymbolName) {
182         Symbol symbol = CrossxEnvironment.lookupSymbol(model, symbolname, symboltype);
183         if (symbol == null) {
184             return null;
185         }
186         for (Symbol subsym : symbol.getSubSymbols()) {
187             if (subsym.getType().equals(subsymbolType) && subsym.getName().equals(subsymbolName)) {
188                 return symbol;
189             }
190         }
191         return null;
192     }
193 
194     /**
195      * @param classname
196      * @return Returns true of a BusinessClass with the name path exists in any model in the workspace
197      */
198     public static boolean hasProperty(Symbol symbol, String propertyName, String propertyValue) {
199         for (SymbolProperty property : symbol.getProperties()) {
200             if (property instanceof LiteralSymbolProperty) {
201                 LiteralSymbolProperty l = (LiteralSymbolProperty) property;
202                 if (l.getName().equals(propertyName) && l.getValue().equals(propertyValue))
203                     return true;
204             }
205         }
206         return false;
207     }
208 
209     /**
210      *  Get the value of the property name 'propertyname' or null if no such property exists.
211      *  Only returns values of literal symbol properties, not of regference properties.
212      * @param symbol        The symbolf for which the property is looked up
213      * @param propertyName  The name of the property too find
214      * @return
215      */
216     static public String getPropertyValue(Symbol symbol, String propertyName) {
217         if( ( symbol == null) || (propertyName == null) ) { return null; }
218 
219         for (SymbolProperty prop : symbol.getProperties()) {
220             if (prop.getName().equals(propertyName)) {
221                 if (prop instanceof LiteralSymbolProperty) {
222                     return ((LiteralSymbolProperty) prop).getValue();
223                 }
224             }
225         }
226         return null;
227     }
228 
229     /**
230      *  Get the property named 'propertyname' or null if no such property exists
231      * @param symbol        The symbol for which the property is looked up
232      * @param propertyName  The name of the property too find
233      * @return
234      */
235     static public SymbolProperty getProperty(Symbol symbol, String propertyName) {
236         if( ( symbol == null) || (propertyName == null) ) { return null; }
237         
238         for (SymbolProperty prop : symbol.getProperties()) {
239             if (prop.getName().equals(propertyName)) {
240                 return prop;
241             }
242         }
243         return null;
244     }
245 
246     /**
247      *  Get the property named 'propertyname' or null if no such property exists
248      * @param symbol        The symbol for which the property is looked up
249      * @param propertyName  The name of the property too find
250      * @return
251      */
252     static public ReferenceSymbolProperty getReferenceProperty(Symbol symbol, String propertyName) {
253         SymbolProperty prop = getProperty(symbol, propertyName);
254         if( prop !=  null ){
255             if (prop instanceof ReferenceSymbolProperty) {
256                 return ((ReferenceSymbolProperty) prop);
257             }
258         }
259         return null;
260     }
261 
262     /**
263      *  Get the property named 'propertyname' or null if no such property exists
264      * @param symbol        The symbol for which the property is looked up
265      * @param propertyName  The name of the property too find
266      * @return
267      */
268     static public LiteralSymbolProperty getLiteralProperty(Symbol symbol, String propertyName) {
269         SymbolProperty prop = getProperty(symbol, propertyName);
270         if( prop !=  null ){
271             if (prop instanceof LiteralSymbolProperty) {
272                 return ((LiteralSymbolProperty) prop);
273             }
274         }
275         return null;
276     }
277 
278     static public Symbol getSubSymbol(Symbol mainSymbol, String symbolName) {
279        if( ( mainSymbol == null) || (symbolName == null) ) { return null; }
280         
281         for (Symbol sub : mainSymbol.getSubSymbols() ) {
282             if (sub.getName().equals(symbolName)) {
283                 return sub;
284             }
285         }
286         return null;
287     }
288 
289     /**
290      * Find all subsymbols of 'symbol' that have the type 'symbolType'
291      * @param symbol
292      * @param symbolType
293      * @return
294      */
295     static public List<Symbol> findAllSubSymbols(Symbol symbol, String symbolType) {
296         List<Symbol> result = new ArrayList<Symbol>();
297         if( (symbol == null)  || (symbolType == null)){ return result; }
298         
299         for (Symbol sub : symbol.getSubSymbols()) {
300             if (sub.getType().equals(symbolType)) {
301                 result.add(sub);
302             }
303         }
304         return result;
305     }
306 
307     
308     /**
309      * Find all symbols of type 'symboltype' in project 'project'
310      * 
311      * @param project
312      * @param symboltype
313      * @return
314      */
315     public static List<Symbol>  findAllSymbols(String project, String symboltype) {
316         List<Symbol> result = new ArrayList<Symbol>(); 
317         List<Symbol> tmp = CrossxEnvironment.findAllSymbols(project, symboltype);
318         if (tmp != null) {
319             logger.info("CrossxCheck ["+ project + "] findAllSymbols " + symboltype + " [" + tmp.toString() + "]");
320             result.addAll(tmp);
321         } else {
322             logger.info("CrossxCheck ["+ project + "] findAllSymbols " + symboltype + " returns null");
323         }
324         return result;
325     }
326 }