KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > finalist > jag > uml > UML2JagGenerator


1 /* Copyright (C) 2003 Finalist IT Group
2  *
3  * This file is part of JAG - the Java J2EE Application Generator
4  *
5  * JAG is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * JAG is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with JAG; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */

17
18 package com.finalist.jag.uml;
19
20
21 import com.finalist.jaggenerator.modules.*;
22 import com.finalist.jaggenerator.*;
23 import com.finalist.uml14.simpleuml.*;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
28 import javax.xml.parsers.DocumentBuilder JavaDoc;
29 import javax.swing.*;
30 import java.io.*;
31 import java.util.*;
32 import java.net.URL JavaDoc;
33 import java.net.MalformedURLException JavaDoc;
34 import org.w3c.dom.Document JavaDoc;
35
36 /**
37  * Generate a JAG skelet configuration file based on an UML1.4 XMI model.
38  *
39  * @author Rudie Ekkelenkamp, Michael O'Connor - Finalist IT Group
40  * @version $Revision: 1.26 $, $Date: 2005/12/24 13:35:48 $
41  * @todo Plug in a generic java class field type --> SQL column type mapping, instead of DEFAULT_SQL_TYPE..
42  */

43 public class UML2JagGenerator {
44    private ConsoleLogger logger;
45    private static SimpleModel model;
46    static Log log = LogFactory.getLog(UML2JagGenerator.class);
47
48    private static final String JavaDoc DEFAULT_SQL_TYPE = "VARCHAR(255)"; // a safe generic column type for most databases
49
private static final String JavaDoc CHARACTER_PRIMITIVE = "char";
50    private static final String JavaDoc CHARACTER_CLASS = "java.lang.Character";
51    private static final String JavaDoc JAVA_LANG_PACKAGE_PREFIX = "java.lang.";
52    private static final char DOT = '.';
53    private static final String JavaDoc EMPTY_STRING = "";
54    private static final String JavaDoc INTEGER_PRIMITIVE = "int";
55    private static final String JavaDoc INTEGER_CLASS = "java.lang.Integer";
56    private static final String JavaDoc DEFAULT_JDBC_TYPE = "VARCHAR";
57    private static final String JavaDoc NUMBER_SQL_TYPE = "NUMBER";
58    private static final String JavaDoc TIMESTAMP_SQL_TYPE = "DATE";
59    private static final String JavaDoc BIGINT_JDBC_TYPE = "BIGINT";
60
61    /**
62     * Makes a UML2JagGenerator with an external logger.
63     *
64     * @param logger somewhere to redirect output, other than System.out
65     */

66    public UML2JagGenerator(ConsoleLogger logger) {
67       this.logger = logger;
68       model = new SimpleModel();
69    }
70
71    /**
72     * Makes a UML2JagGenerator.
73     */

74    public UML2JagGenerator() {
75       model = new SimpleModel();
76    }
77
78
79    /**
80     * Convert the UML model into a JAG readable XML application file.
81     *
82     * @param xmiFileName
83     * @param outputDir directory where the XML file will be stored.
84     * @return the xml File created, if any.
85     */

86    public synchronized File generateXML(String JavaDoc xmiFileName, String JavaDoc outputDir) {
87       URL JavaDoc url = null;
88       try {
89          url = new URL JavaDoc("FILE", EMPTY_STRING, xmiFileName);
90       } catch (MalformedURLException JavaDoc e) {
91          e.printStackTrace();
92       }
93
94       model.readModel(url);
95
96       try {
97          checkInterruptStatus();
98          return generateXML(model, outputDir);
99
100       } catch (InterruptedException JavaDoc e) {
101          e.printStackTrace();
102       }
103
104       return null;
105    }
106
107    private File generateXML(SimpleModel model, String JavaDoc outputDir) throws InterruptedException JavaDoc {
108       File file = null;
109       Root root = generateConfig(model);
110       checkInterruptStatus();
111
112       try {
113          DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
114          DocumentBuilder JavaDoc builder = dbf.newDocumentBuilder();
115          Document JavaDoc doc = builder.newDocument();
116          org.w3c.dom.Element JavaDoc skelet = doc.createElement("skelet");
117          doc.appendChild(skelet);
118          root.getXML(skelet);
119          String JavaDoc XMLDoc = com.finalist.jaggenerator.JagGenerator.outXML(doc);
120          log.debug("The generated xml project file is: ");
121          log.debug(XMLDoc);
122          file = new File(outputDir, model.getName() + ".xml");
123          checkInterruptStatus();
124
125          OutputStream outputStream = new FileOutputStream(file);
126          outputStream.write(XMLDoc.getBytes());
127          outputStream.flush();
128          outputStream.close();
129
130       } catch (Exception JavaDoc e) {
131          e.printStackTrace();
132          log("Error writing the file.");
133       }
134
135       return file;
136    }
137
138    /**
139     * Generate a config file readable by JAG from the UML model
140     *
141     * @param model UML Model
142     * @return Root conifguration
143     */

144    private Root generateConfig(SimpleModel model) throws InterruptedException JavaDoc {
145       Root root = new Root();
146       createDataSource(model, root.datasource);
147       createConfig(model, root.config, root.app, root.paths);
148       checkInterruptStatus();
149
150       // Create the Session EJBs
151
HashMap sessionEJBMap = createSessionEJBs(model);
152       if (sessionEJBMap != null && sessionEJBMap.size() > 0) {
153          root.setSessionEjbs(new ArrayList(sessionEJBMap.values()));
154       }
155       checkInterruptStatus();
156
157       // Create the entity EJBs.
158
HashMap entityEJBMap = createEntityEJBs(model);
159       if (entityEJBMap != null && entityEJBMap.size() > 0) {
160          root.setEntityEjbs(new ArrayList(entityEJBMap.values()));
161       }
162       checkInterruptStatus();
163
164       // Create the container-managed relations
165
createContainerManagedRelations(entityEJBMap, model);
166
167       return root;
168    }
169
170    private void checkInterruptStatus() throws InterruptedException JavaDoc {
171       if (Thread.interrupted()) {
172          throw new InterruptedException JavaDoc();
173       }
174    }
175
176    /**
177     * Create all entity EJBs in the uml model and put them in a hashmap
178     *
179     * @param model the uml model.
180     * @return HashMap with all Entity classes.
181     */

182    private HashMap createEntityEJBs(SimpleModel model) {
183       HashMap map = new HashMap();
184       // Get a list of all packages in the model.
185
Collection pkList = model.getAllSimpleUmlPackages(model);
186       for (Iterator pkIterator = pkList.iterator(); pkIterator.hasNext();) {
187          SimpleUmlPackage simpleUmlPackage = (SimpleUmlPackage) pkIterator.next();
188          Collection list;
189          list = simpleUmlPackage.getSimpleClassifiers();
190          for (Iterator pkit = list.iterator(); pkit.hasNext();) {
191             SimpleModelElement el = (SimpleModelElement) pkit.next();
192             if ((el instanceof SimpleUmlClass) &&
193                   model.getStereoType(el) != null &&
194                   model.getStereoType(el).equalsIgnoreCase(JagUMLProfile.STEREOTYPE_CLASS_ENTITY)) {
195                // We got a winner, it's a class with the right stereotype.
196

197                SimpleUmlClass suc = (SimpleUmlClass) el;
198
199                String JavaDoc rootPackage = simpleUmlPackage.getFullPackageName();
200                String JavaDoc tableName = getTaggedValue(model,
201                      JagUMLProfile.TAGGED_VALUE_CLASS_TABLE_NAME, suc,
202                      Utils.unformat(Utils.firstToLowerCase(suc.getName())));
203
204                Entity entity = new Entity(EMPTY_STRING, tableName, EMPTY_STRING);
205                entity.setRootPackage(rootPackage);
206                entity.setName(suc.getName());
207                entity.setDescription(model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_DOCUMENTATION, suc));
208                if (model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_CLASS_DISPLAY_NAME, suc) != null) {
209                   entity.setDisplayName(model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_CLASS_DISPLAY_NAME, suc));
210                }
211                if (model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_CLASS_IS_ASSOCIATION, suc) != null) {
212                   entity.isAssociationEntity.setSelectedItem(model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_CLASS_IS_ASSOCIATION, suc));
213                }
214                
215                entity.setRefName(entity.getName().toString());
216
217                // Now iterate over the fields of the model element and Create fields that will be added to the entity.
218
int pkCount = 0;
219                Collection attributes = suc.getSimpleAttributes();
220                Field primaryKeyField = null;
221                for (Iterator iterator = attributes.iterator(); iterator.hasNext();) {
222                   SimpleAttribute att = (SimpleAttribute) iterator.next();
223                   boolean isPK = equal(model.getStereoType(att), JagUMLProfile.STEREOTYPE_ATTRIBUTE_PRIMARY_KEY);
224                   boolean required = false;
225                   if (isPK) {
226                      required = true;
227                   } else {
228                      // Only set required to true if a stereotype has been defined.
229
required = equal(model.getStereoType(att), JagUMLProfile.STEREOTYPE_ATTRIBUTE_REQUIRED);
230                   }
231
232                   Column col = new Column();
233                   String JavaDoc colName = model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_ATTRIBUTE_COLUMN_NAME, att);
234
235                   if (colName == null) {
236                      //make a column name based on the attribute name
237
colName = Utils.unformat(att.getName());
238                   }
239                   col.setName(colName);
240                   String JavaDoc sqlType = getTaggedValue(model,
241                         JagUMLProfile.TAGGED_VALUE_ATTRIBUTE_SQL_TYPE, att, null);
242                   String JavaDoc jdbcType = getTaggedValue(model,
243                         JagUMLProfile.TAGGED_VALUE_ATTRIBUTE_JDBC_TYPE, att, null);
244                   col.setPrimaryKey(isPK);
245                   col.setNullable(!required);
246
247                   SimpleClassifier theClassifier = att.getType();
248                   String JavaDoc fieldType = theClassifier.getOwner().getFullPackageName();
249                   if (fieldType != null && !Character.isLowerCase(theClassifier.getName().charAt(0))) {
250                      fieldType = fieldType + DOT + theClassifier.getName();
251                   } else {
252                      //JAG doesn't support primitive entity bean persistant field types..
253
String JavaDoc primitiveType = theClassifier.getName();
254                      if (CHARACTER_PRIMITIVE.equals(primitiveType)) {
255                         fieldType = CHARACTER_CLASS;
256                      }
257                      else if (INTEGER_PRIMITIVE.equals(primitiveType)) {
258                         fieldType = INTEGER_CLASS;
259                      }
260                      else {
261                         fieldType = JAVA_LANG_PACKAGE_PREFIX +
262                               Character.toUpperCase(primitiveType.charAt(0)) +
263                               primitiveType.substring(1);
264                      }
265                   }
266                   if (sqlType == null) {
267                      String JavaDoc[] mappedTypes = getDatabaseColumnTypesForClass(fieldType);
268                      sqlType = mappedTypes[0];
269                      jdbcType = mappedTypes[1];
270                   }
271                   col.setSqlType(sqlType);
272                   String JavaDoc autoGeneratedPrimaryKey = model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_ATTRIBUTE_AUTO_PRIMARY_KEY, att);
273                   boolean generate = false;
274                   if ("true".equalsIgnoreCase(autoGeneratedPrimaryKey)) {
275                      generate = true;
276                   }
277                   Field field = new Field(entity, col);
278                   field.setName(att.getName());
279                   field.setType(fieldType); // Overrule the automatic setting by the Field constructor.
280
if (isPK) {
281                      field.setPrimaryKey(isPK);
282                   }
283                   field.setSqlType(sqlType);
284                   field.setJdbcType(jdbcType);
285                   field.setHasAutoGenPrimaryKey(generate);
286
287
288                   if (isPK) {
289                      pkCount++;
290                      primaryKeyField = field;
291                   }
292                   entity.add(field);
293                }
294
295                if (pkCount > 1) {
296                   // It's a composite primary key.
297
entity.setIsComposite("true");
298                   // Set the primary key class....
299
String JavaDoc compositePK = model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_CLASS_COMPOSITE_PRIMARY_KEY, suc);
300                   entity.setPKeyType(compositePK);
301                   // set the composite key here//
302
} else {
303                   if (primaryKeyField != null) {
304                      entity.setPrimaryKey(primaryKeyField);
305                   }
306                }
307
308                if (pkCount == 0) {
309                   log("UML Error! Entity '" + entity.getName() + "' has no primary key! At least one attribute " +
310                         "in an entity bean must have the stereotype \"PrimaryKey\".");
311                   JOptionPane.showMessageDialog(null,
312                               "Entity '" + entity.getName() + "' has no primary key! At least one attribute " +
313                               "in an entity bean must have the stereotype \"PrimaryKey\".",
314                               "UML Error!",
315                               JOptionPane.ERROR_MESSAGE);
316                } else {
317                   // Put the entity in the hashmap entity
318
map.put(entity.getRefName(), entity);
319                }
320             }
321          }
322       }
323       return map;
324    }
325
326    /** yet another mapping...
327     *
328     * These mappings are as far as possible database-generic, using Oracle specifics where genericity (is that a word?)
329     * is not possible (Oracle-specific types are automatically re-typed in JAG if the user selects a non-Oracle database
330     * in the 'Datasource' settings).
331     * NOTE: 'Genericity' is more important than accuracy here..
332     *
333     * @return a two-length String array [sql type, jdbc type]
334     */

335    private String JavaDoc[] getDatabaseColumnTypesForClass(String JavaDoc javaClass) {
336       if (Byte JavaDoc.class.getName().equals(javaClass) ||
337           Short JavaDoc.class.getName().equals(javaClass) ||
338           Integer JavaDoc.class.getName().equals(javaClass) ||
339           Long JavaDoc.class.getName().equals(javaClass) ||
340           Double JavaDoc.class.getName().equals(javaClass)) {
341          return new String JavaDoc[] {NUMBER_SQL_TYPE, BIGINT_JDBC_TYPE};
342       }
343       if (java.sql.Timestamp JavaDoc.class.getName().equals(javaClass) ||
344           java.sql.Date JavaDoc.class.getName().equals(javaClass) ||
345           java.util.Date JavaDoc.class.getName().equals(javaClass)) {
346          return new String JavaDoc[] {TIMESTAMP_SQL_TYPE, TIMESTAMP_SQL_TYPE};
347       }
348
349       return new String JavaDoc[] {DEFAULT_SQL_TYPE, DEFAULT_JDBC_TYPE};
350    }
351
352    private String JavaDoc getTaggedValue(SimpleModel model, String JavaDoc taggedValueAttributeSqlType,
353                                  SimpleModelElement att, String JavaDoc defaultValue) {
354       String JavaDoc value = model.getTaggedValue(taggedValueAttributeSqlType, att);
355       return value == null ? defaultValue : value;
356    }
357
358    /** Create the session EJBs on the simpleModel. */
359    private HashMap createSessionEJBs(SimpleModel model) {
360       HashMap map = new HashMap();
361
362       // Get a list of all packages in the model.
363
Collection pkList = model.getAllSimpleUmlPackages(model);
364       for (Iterator pkIterator = pkList.iterator(); pkIterator.hasNext();) {
365          SimpleUmlPackage simpleUmlPackage = (SimpleUmlPackage) pkIterator.next();
366
367          Collection list = simpleUmlPackage.getSimpleClassifiers();
368          for (Iterator it = list.iterator(); it.hasNext();) {
369             SimpleModelElement el = (SimpleModelElement) it.next();
370             if ((el instanceof SimpleUmlClass) &&
371                   model.getStereoType(el) != null &&
372                   model.getStereoType(el).equals(JagUMLProfile.STEREOTYPE_CLASS_SERVICE)) {
373                // We got a winner, it's a class with the right stereotype.
374
ArrayList businessMethods = new ArrayList();
375
376                SimpleUmlClass suc = (SimpleUmlClass) el;
377                Collection operations = suc.getSimpleOperations();
378                for (Iterator oit = operations.iterator(); oit.hasNext();) {
379                    BusinessMethod bm = new BusinessMethod();
380                    SimpleOperation operation = (SimpleOperation) oit.next();
381                    log.info("The operation name is: " + operation.getName());
382
383                    bm.setMethodName(operation.getName());
384                    String JavaDoc desc = model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_DOCUMENTATION, operation);
385                    if (desc == null) {
386                      bm.setDescription("");
387                    } else {
388                       bm.setDescription(desc);
389                    }
390
391                    ArrayList argList = new ArrayList();
392                    log.info("The number of parameters: " + operation.getSimpleParameters().size());
393                    for (Iterator pit = operation.getSimpleParameters().iterator(); pit.hasNext();) {
394                        SimpleParameter param = (SimpleParameter) pit.next();
395                        BusinessArgument arg = new BusinessArgument();
396                        String JavaDoc type;
397                       // inout of return.
398
log.debug("Param kind: " + param.getKind());
399                        if (param.getType() != null) {
400                           if (param.getType().getName() != null && param.getType().getName().equalsIgnoreCase("void")) {
401                              type = "void";
402                           } else {
403                               String JavaDoc packageName = param.getType().getOwner().getFullPackageName();
404                               if (packageName == null) {
405                                  packageName = "";
406                               } else {
407                                  packageName = param.getType().getOwner().getFullPackageName() + ".";
408                               }
409                               String JavaDoc typeName = param.getType().getName();
410                               if (typeName != null) {
411                                  if (typeName.startsWith("java::lang::")) {
412                                     // This is prepended by poseidon 3.
413
typeName = typeName.substring(12);
414                                  }
415                               }
416                               type = "" + packageName + typeName;
417                           }
418                        } else {
419                           type = "void";
420                        }
421                        if (param.getKind().equalsIgnoreCase(SimpleParameter.RETURN)) {
422                            log.info("Found a return type");
423                            bm.setReturnType(type);
424                        } else {
425                            log.info("The param name is: " + param.getName());
426                            arg.setName(param.getName());
427                            log.info("The param type is: " + type);
428                            arg.setType(type);
429                            argList.add(arg);
430                        }
431                    }
432                   bm.setArgumentList(argList);
433                   businessMethods.add(bm);
434                }
435
436                String JavaDoc rootPackage = simpleUmlPackage.getFullPackageName();
437                Session session = new Session(rootPackage);
438                session.setName(suc.getName());
439                session.setRootPackage(rootPackage);
440                session.setRefName(suc.getName());
441                session.setDescription(model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_DOCUMENTATION, suc));
442                session.setBusinessMethods(businessMethods);
443                // Now add the entity refs to the session.
444
Collection deps = model.getSimpleDependencies();
445                for (Iterator iterator = deps.iterator(); iterator.hasNext();) {
446                   SimpleDependency sd = (SimpleDependency) iterator.next();
447                    // session.addRef(entityRef);
448
if (sd.getClient().getName().equals(session.getName().toString())) {
449                      // This dependency is from the current Session. Add a ref.
450
String JavaDoc entityRef = sd.getSupplier().getName();
451                      session.addRef(entityRef);
452                   }
453                }
454                // Put the entity in the hashmap entity
455
map.put(session.getRefName(), session);
456             }
457          }
458       }
459       return map;
460    }
461
462    /**
463     * Determine the relations that are defined for an entity EJB.
464     *
465     * @param entityEJBMap Current classes in the model.
466     * @param simpleModel The UML model.
467     */

468    private void createContainerManagedRelations(HashMap entityEJBMap, SimpleModel simpleModel) {
469       Iterator assocs = simpleModel.getSimpleAssociations().iterator();
470       while (assocs.hasNext()) {
471          SimpleAssociation assoc = (SimpleAssociation) assocs.next();
472
473          String JavaDoc sourceEntityName = assoc.getSource().getSimpleClassifier().getName();
474          String JavaDoc destinationEntityName = assoc.getDestination().getSimpleClassifier().getName();
475          Entity sourceEntity = (Entity) entityEJBMap.get(sourceEntityName);
476          Entity destinationEntity = (Entity) entityEJBMap.get(destinationEntityName);
477          boolean multiplicity;
478          boolean bidirectional;
479          if (JagUMLProfile.TAGGED_VALUE_ASSOCIATION_MULTIPLICITY_ONE_TO_ONE.equals(simpleModel.getTaggedValue(JagUMLProfile.TAGGED_VALUE_ASSOCIATION_MULTIPLICITY, assoc))) {
480             multiplicity = false;
481          } else {
482             // This is the default.
483
multiplicity = true;
484          }
485          if ("true".equals(simpleModel.getTaggedValue(JagUMLProfile.TAGGED_VALUE_ASSOCIATION_BIDIRECTIONAL, assoc))) {
486             bidirectional = true;
487          } else {
488             // This is the default.
489
bidirectional = false;
490          }
491
492          if (sourceEntity == null || destinationEntity == null) {
493             log("The relation named '" + assoc.getName() +
494                   "' has 1 or more 'association ends' " +
495                   "whose names do not correspond to entity bean class names");
496             continue;
497          }
498
499          ForeignKey info = new ForeignKey();
500          String JavaDoc fkFieldName = assoc.getName();
501          String JavaDoc fkColumnName = null;
502          Iterator i = sourceEntity.getFields().iterator();
503          while (i.hasNext()) {
504             Field field = (Field) i.next();
505             if (field.getName().equals(fkFieldName)) {
506                fkColumnName = field.getColumnName();
507             }
508          }
509          info.setPkTableName(destinationEntity.getLocalTableName().toString());
510          info.setPkColumnName(destinationEntity.getPrimaryKey().getColumnName());
511          info.setFkColumnName(fkColumnName);
512          info.setFkName(fkFieldName);
513
514          Relation relation = new Relation(sourceEntity, info, false);
515          relation.setBidirectional(bidirectional);
516          relation.setTargetMultiple(multiplicity);
517          sourceEntity.addRelation(relation);
518          log("Added relation: " + relation);
519       }
520    }
521
522    private void log(String JavaDoc message) {
523       log.info(message);
524       if (logger != null) {
525          logger.log(message);
526       }
527    }
528
529    /** Create a class with the datasource definition */
530    private void createDataSource(SimpleModel model, Datasource ds) {
531       boolean datasourceFound = false;
532       // Get a list of all packages in the model.
533
Collection pkList = model.getAllSimpleUmlPackages(model);
534       for (Iterator pkIterator = pkList.iterator(); pkIterator.hasNext();) {
535          SimpleUmlPackage simpleUmlPackage = (SimpleUmlPackage) pkIterator.next();
536          Collection list = simpleUmlPackage.getSimpleClassifiers();
537          for (Iterator it = list.iterator(); it.hasNext();) {
538             SimpleModelElement el = (SimpleModelElement) it.next();
539             if ((el instanceof SimpleUmlClass) &&
540                   model.getStereoType(el) != null &&
541                   model.getStereoType(el).equals(JagUMLProfile.STEREOTYPE_CLASS_DATA_SOURCE)) {
542                // We got a winner, it's a class with the right stereotype.
543
datasourceFound = true;
544                ds.setJndi(model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_CLASS_DATA_SOURCE_JNDI_NAME, el));
545                ds.setMapping(model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_CLASS_DATA_SOURCE_MAPPING, el));
546                ds.setJdbcUrl(model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_CLASS_DATA_SOURCE_JDBC_URL, el));
547                ds.setUserName(model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_CLASS_DATA_SOURCE_USER_NAME, el));
548                ds.setPassword(model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_CLASS_DATA_SOURCE_PASSWORD, el));
549             }
550          }
551       }
552
553       if (!datasourceFound) {
554          ds.setJndi("jdbc/" + model.getName());
555
556       }
557    }
558
559     /** Create a class with the datasource definition */
560     private void createConfig(SimpleModel model, Config config, App app, Paths paths) {
561        // Get a list of all packages in the model.
562
Collection pkList = model.getAllSimpleUmlPackages(model);
563        for (Iterator pkIterator = pkList.iterator(); pkIterator.hasNext();) {
564           SimpleUmlPackage simpleUmlPackage = (SimpleUmlPackage) pkIterator.next();
565           Collection list = simpleUmlPackage.getSimpleClassifiers();
566           for (Iterator it = list.iterator(); it.hasNext();) {
567              SimpleModelElement el = (SimpleModelElement) it.next();
568              if ((el instanceof SimpleUmlClass) &&
569                    model.getStereoType(el) != null &&
570                    model.getStereoType(el).equals(JagUMLProfile.STEREOTYPE_CLASS_JAG_CONFIG)) {
571                 // We got a winner, it's a class with the right stereotype.
572

573                  config.setAuthor(model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_MODEL_AUTHOR, el));
574                  config.setVersion(model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_MODEL_VERSION, el));
575                  config.setCompany(model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_MODEL_COMPANY, el));
576                  String JavaDoc templateDir = model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_MODEL_TEMPLATE, el);
577                  if (templateDir != null) {
578                     File dir = new File(templateDir);
579                     config.getTemplate().setTemplateDir(dir);
580                  }
581                  HashMap map = new HashMap();
582                  map.put(JagGenerator.TEMPLATE_APPLICATION_SERVER, model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_CONFIG_APPSERVER, el));
583                  map.put(JagGenerator.TEMPLATE_USE_RELATIONS, model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_CONFIG_USE_RELATIONS, el));
584                  map.put(JagGenerator.TEMPLATE_USE_JAVA5, model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_CONFIG_USE_JAVA5, el));
585                  map.put(JagGenerator.TEMPLATE_USE_MOCK, model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_CONFIG_USE_MOCK, el));
586                  map.put(JagGenerator.TEMPLATE_WEB_TIER, model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_CONFIG_WEB_TIER, el));
587                  map.put(JagGenerator.TEMPLATE_BUSINESS_TIER, model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_CONFIG_BUSINESS_TIER, el));
588                  map.put(JagGenerator.TEMPLATE_SERVICE_TIER, model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_CONFIG_SERVICE_TIER, el));
589                  config.setTemplateSettings(map);
590
591                  app.setName(model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_MODEL_APPLICATION_NAME, el));
592                  app.setDescription(model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_MODEL_DESCRIPTION, el));
593                  app.setVersion(model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_MODEL_APPLICATION_VERSION, el));
594                  app.setRootPackage(model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_MODEL_ROOT_PACKAGE, el));
595                  app.setLogFramework(model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_MODEL_LOGGING, el));
596                  app.setDateFormat(model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_MODEL_DATE_FORMAT, el));
597                  app.setTimestampFormat(model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_MODEL_TIMESTAMP_FORMAT, el));
598
599                  paths.setServiceOutput(model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_MODEL_SERVICE_PATH, el));
600                  paths.setEjbOutput(model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_MODEL_EJB_PATH, el));
601                  paths.setWebOutput(model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_MODEL_WEB_PATH, el));
602                  paths.setJspOutput(model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_MODEL_JSP_PATH, el));
603                  paths.setTestOutput(model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_MODEL_TEST_PATH, el));
604                  paths.setConfigOutput(model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_MODEL_CONFIG_PATH, el));
605                  paths.setSwingOutput(model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_MODEL_SWING_PATH, el));
606                  paths.setMockOutput(model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_MODEL_MOCK_PATH, el));
607                  paths.setHibernateOutput(model.getTaggedValue(JagUMLProfile.TAGGED_VALUE_MODEL_HIBERNATE_PATH, el));
608              }
609           }
610        }
611     }
612
613
614    private boolean equal(String JavaDoc a, String JavaDoc b) {
615       if (a != null && b != null && a.equals(b)) {
616          return true;
617       } else
618          return false;
619    }
620
621    /**
622     *
623     * @param args
624     */

625    public static void main(String JavaDoc[] args) {
626       if (args.length == 1) {
627          new UML2JagGenerator().generateXML(args[0], ".");
628       } else {
629          System.out.println("Pass an xmi file as argument!");
630       }
631    }
632 }
633
634 /*
635         $Log: UML2JagGenerator.java,v $
636         Revision 1.26 2005/12/24 13:35:48 ekkelenkamp
637         added new tagged values.
638
639         Revision 1.25 2005/09/29 17:03:32 ekkelenkamp
640         Only refs from the current dependency
641
642         Revision 1.24 2005/09/29 14:48:06 ekkelenkamp
643         Bug fix for required field.
644
645         Revision 1.23 2005/09/23 07:23:58 ekkelenkamp
646         export service tier selection
647
648         Revision 1.22 2005/07/29 07:47:05 ekkelenkamp
649         Don't use EntityRef
650
651         Revision 1.21 2005/07/15 21:27:50 ekkelenkamp
652         no spring path
653
654         Revision 1.20 2005/06/09 19:09:54 ekkelenkamp
655         java5 support.
656
657         Revision 1.19 2005/03/04 21:07:22 ekkelenkamp
658         Business method support for UML
659
660         Revision 1.18 2005/03/04 15:17:33 ekkelenkamp
661         business methods are in progress now.
662
663         Revision 1.17 2005/02/04 09:35:06 ekkelenkamp
664         Use constants where possible
665
666         Revision 1.16 2005/02/04 08:20:43 ekkelenkamp
667         UML synchronize up-to-date.
668
669         Revision 1.15 2005/01/19 21:44:58 ekkelenkamp
670         uml support for many-to-one relations and bidirectionality.
671
672         Revision 1.14 2004/12/27 12:55:16 ekkelenkamp
673         Support for business methods.
674
675         Revision 1.13 2004/12/23 12:25:52 ekkelenkamp
676         Support for business methods.
677
678         Revision 1.12 2004/12/22 11:23:01 ekkelenkamp
679         initial support for business methods.
680
681         Revision 1.11 2004/12/05 23:27:44 ekkelenkamp
682         Fixes for relation fields update.
683
684         Revision 1.10 2004/11/27 23:50:54 ekkelenkamp
685         Improved UML/JAG synchronization.
686
687         Revision 1.9 2004/11/27 19:30:07 ekkelenkamp
688         Improved UML/JAG synchronization.
689
690         Revision 1.8 2004/11/27 07:50:04 ekkelenkamp
691         Improved UML/JAG synchronization.
692
693         Revision 1.7 2004/03/28 12:34:03 ekkelenkamp
694         Set root package
695
696         Revision 1.6 2004/03/28 11:55:34 ekkelenkamp
697         tagged values on model
698
699         Revision 1.5 2003/11/27 17:54:13 oconnor_m
700         no message
701
702         Revision 1.4 2003/11/25 15:10:30 oconnor_m
703         support for importing/exporting from/to UML model via XMI
704
705         Revision 1.3 2003/11/14 09:44:39 ekkelenkamp
706         Functional version, only container managed relations are missing
707
708         Revision 1.2 2003/11/03 12:32:42 ekkelenkamp
709         Initial support for Session EJBs.
710
711         Revision 1.1 2003/11/02 14:01:30 ekkelenkamp
712         Initial version of UML support in Jag
713
714 */
Popular Tags