Module: Muml_Class

Includes:
DAO
Defined in:
lib/ontomde-uml2-jpa/jpa.rb,
lib/ontomde-uml2-jpa/finder.rb,
lib/ontomde-uml2-jpa/redefined.rb,
lib/ontomde-uml2-jpa/configuration.rb

Constant Summary collapse

JPA_TX_BEGIN =
""
JPA_TX_COMMIT =
""
JAVA_NOT_PERSISTENT_NAMESPACE =
["java","javax"]
DB_Context =
"xmda.jpa.DbContext"
DB_MAX_TABLE_NAME_LENGTH =

Maximum allowed length for a sgbd table name cf:

Mysql: http://dev.mysql.com/doc/refman/5.0/en/identifiers.html
64
DB_TABLE_NAME_OVERRIDE =

table name override

Hash.new
DB_URI_ALIAS =
Hash.new
DB_DISCRIMINATOR =
Hash.new

Instance Method Summary collapse

Methods included from DAO

#jpa_addDAO!, #jpa_addGetDao!, #jpa_addMerge!, #jpa_addPersist!, #jpa_addPersistenceContext!, #jpa_addRemove!

Instance Method Details

#db_getFindAllCallObject

def db_daoClassName jpa_daoClassName end def jpa_daoClassName return “#java_qualifiedNameDao” end



32
33
34
35
# File 'lib/ontomde-uml2-jpa/redefined.rb', line 32

def db_getFindAllCall
  param=context[:db_useExtraTypeParameterForFindAll] ? "(#{self.java_qualifiedName})null" : ""
  return "#{java_DAOClass_one.java_qualifiedName}.findAll(#{param})"
end

#db_tableNameObject

Table name for this element



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/ontomde-uml2-jpa/jpa.rb', line 344

def db_tableName
  r=java_uniqueName
  if r.length<=DB_MAX_TABLE_NAME_LENGTH
    #ok. nothing to do
  elsif ! DB_TABLE_NAME_OVERRIDE[r].nil?
    #no check performed on overriden name
    r=DB_TABLE_NAME_OVERRIDE[r]
  elsif context[:silentlyTruncateTableName,false]
    log.debug { "silently truncating table name #{r}" }
    r=r[0,DB_MAX_TABLE_NAME_LENGTH]
  else
    log_error_db_tableName_too_long(r)
  end
  return r
end

#db_tx(java_code) ⇒ Object

returns java_code enclosed in begin/commit with catch cleanup



244
245
246
# File 'lib/ontomde-uml2-jpa/jpa.rb', line 244

def db_tx(java_code)
  jpa_tx(java_code)
end

#java_writeSuggesterBody(oa) ⇒ Object

Method redefined from ontomde-uml2-java alias :original_java_writeSuggesterBody :java_writeSuggesterBody



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ontomde-uml2-jpa/redefined.rb', line 39

def java_writeSuggesterBody(oa)
  #log.debug {"adding suggester to #{self} for #{oa}"}
  if oa.uml_type_one.jpa_isPersistent?
    #TODO: recherche du composite commun
    #TODO: transient object do not have a valid uniq id

    #if oa.umlx_oneSide?
    if !oa.umlx_manySide?
      write( %{
/* TPL:0001jpa_one #{oa.umlx_oneSide?} */
java.util.List<#{oa.uml_type_one.java_qualifiedName}> ret=#{oa.uml_type_one.db_getFindAllCall};
#{oa.uml_type_one.java_qualifiedName} elt=get#{oa.java_NameBean}();
if(elt!=null && !ret.contains(elt)) {
	ret.add(elt);
}
return ret;\n})
    else
      write( %{
/* TPL:0001jpa_many #{oa.umlx_oneSide?} */
java.util.List<#{oa.uml_type_one.java_qualifiedName}> ret=#{oa.uml_type_one.db_getFindAllCall};

#{oa.uml_type_one.java_qualifiedName} item=null;
java.util.Iterator<?> iter=get#{oa.java_NameBean}().iterator();
while(iter.hasNext()){
	item=(#{oa.uml_type_one.java_qualifiedName}) iter.next();
        if(!ret.contains(item)) {
        ret.add(item);
        }
	}
return ret;\n})

    end
  else
    super(oa)#original_java_writeSuggesterBody(oa)
  end
end

#jpa_addEntityManager!Object



258
259
260
261
262
263
264
265
266
267
268
# File 'lib/ontomde-uml2-jpa/jpa.rb', line 258

def jpa_addEntityManager!

  gem=umlx_createAndAddOperation(rdf_uri+"getEntityManager","getEntityManager")
  gem.uml_isStatic=RDF_TRUE
  ret=gem.umlx_createAndAddReturnParameter(DB_Context+"_ret")
  ret.uml_upperValue=umlx_literal(1)
  ret.uml_lowerValue=umlx_literal(0)
  ret.uml_type=umlx_getOrCreateClass(DB::EntityManager)
  gem.java_code="return #{DB_Context}.getEntityManager();"

end

#jpa_addFindAll!(eao) ⇒ Object

adds a finder to eao self is supposed to be the bean and eao the “dao”.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ontomde-uml2-jpa/finder.rb', line 4

def jpa_addFindAll!(eao)
  # java_NameBean is added to avoid name clash with inheritance
  m=eao.umlx_createAndAddOperation(rdf_uri+"_findAll","findAll")#+self.java_NameBean)
  m.uml_isStatic=RDF_TRUE
  #m.uml_class=self
  m.uml_visibility=Cuml_VisibilityKind::Public
  #m.uml_isStatic=RDF_TRUE

  #em=m.umlx_createAndAddParameter(m.rdf_uri+"_entityManager","entityManager")
  #em.uml_type=umlx_getOrCreateClass(JPA::EntityManager)
  #em.uml_direction=Cuml_ParameterDirectionKind::In
  #em.uml_upperValue=umlx_literal(1)
  #em.uml_lowerValue=umlx_literal(1)

  rp=m.umlx_createAndAddParameter(m.rdf_uri+"_ret_param","return")
  rp.uml_direction=Cuml_ParameterDirectionKind::Return
  rp.uml_upperValue=umlx_literal(-1)
  rp.uml_lowerValue=umlx_literal(0)
  rp.uml_type=self
  rp.uml_isOrdered=RDF_TRUE

  #m.java_code="return  (#{self.java_qualifiedName})entityManager.find( #{self.java_qualifiedName}.class, #{ip.uml_name});"

  m.java_code=%{return (java.util.List<#{self.java_qualifiedName}>) getEntityManager().createQuery( "select Object(c) from  #{java_Name} as c").getResultList();}
  m.java_annotation_add(%{@SuppressWarnings("unchecked")})

  # because A<-B is not Set<A> <- Set<B>
  # we need to have an extra parameter
  # so method overloading is OK.
  # (this is not necessary when A is return)

  if context[:db_useExtraTypeParameterForFindAll]
    t=m.umlx_createAndAddParameter(m.rdf_uri+"_class","nullCast")
    t.uml_type=self
  end
  return m
end

#jpa_addFindByPrimaryKey!(eao) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ontomde-uml2-jpa/finder.rb', line 41

def jpa_addFindByPrimaryKey!(eao)
  m=eao.umlx_createAndAddOperation(rdf_uri+"_finderop","find")
  m.uml_isStatic=RDF_TRUE
  #m.uml_class=self
  m.uml_visibility=Cuml_VisibilityKind::Public
  #m.uml_isStatic=RDF_TRUE

  #em=m.umlx_createAndAddParameter(m.rdf_uri+"_entityManager","entityManager")
  #em.uml_type=umlx_getOrCreateClass(JPA::EntityManager)
  #em.uml_direction=Cuml_ParameterDirectionKind::In
  #em.uml_upperValue=umlx_literal(1)
  #em.uml_lowerValue=umlx_literal(1)

  ip=m.umlx_createAndAddParameter(m.rdf_uri+"_id_param","primaryKey")
  ip.uml_direction=Cuml_ParameterDirectionKind::In
  ip.uml_upperValue=umlx_literal(1)
  ip.uml_lowerValue=umlx_literal(1)
  ip.uml_type=umlx_dataType_long
  #ip.uml_isOrdered=RDF_FALSE

  rp=m.umlx_createAndAddParameter(m.rdf_uri+"_ret_param","return")
  rp.uml_direction=Cuml_ParameterDirectionKind::Return
  rp.uml_upperValue=umlx_literal(1)
  rp.uml_lowerValue=umlx_literal(0)
  rp.uml_type=self
  #rp.uml_isOrdered=RDF_FALSE

  m.java_code="return  (#{self.java_qualifiedName})getEntityManager().find( #{self.java_qualifiedName}.class, #{ip.uml_name});"

  return m
end

#jpa_addPersistence!Object



362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/ontomde-uml2-jpa/jpa.rb', line 362

def jpa_addPersistence!
  #jpa_addDAO!

  #Ajout l'interface XmdaEntity
  ent=umlx_getOrCreateInterface(DB::EntityInterface)
  ent.java_isGeneric=RDF_TRUE
  umlx_createAndAddImplementation(ent)

  jpa_addEntityManager! # required for query, persist,...
  uml_ownedOperation.each { |o|
    o.jpa_addPersistence!
  }
  java_makeSerializable!
  java_annotation_add("@Entity")
  java_uniqueName # this call enforces uniqueness
  java_annotation_add("@Table (name=\"#{db_tableName}\")") if jpa_hasPersistenceId?

  if context[:dbTypeDiscriminator]==:int
    k=rdf_uri.to_s
    disc=DB_DISCRIMINATOR[k]
    if disc.nil?
      disc=0
      DB_DISCRIMINATOR.each { |kx,v|
        disc=v+1 if disc<=v
      }
      DB_DISCRIMINATOR[k]=disc
    end
    #TODO: generate only when necessary
    java_annotation_add(%{@DiscriminatorColumn(discriminatorType = DiscriminatorType.INTEGER)})
    java_annotation_add(%{@DiscriminatorValue(value = "#{disc}" )})
  end
  jpa_addStdImports!
end

#jpa_addPersistenceIdAnnnotation!(p) ⇒ Object

com.francetelecom.rd.cil.commons.common.CommonException com.francetelecom.rd.cil.commons.model.IdentifiedObject java_packageQualifiedName.vo.#java_NameValue java_packageQualifiedName.vo.*



430
431
432
433
# File 'lib/ontomde-uml2-jpa/jpa.rb', line 430

def jpa_addPersistenceIdAnnnotation!(p)
  p.java_annotation_add("@Id")
  p.java_annotation_add("@GeneratedValue")
end

#jpa_addPersistOperation!Object



254
255
# File 'lib/ontomde-uml2-jpa/jpa.rb', line 254

def jpa_addPersistOperation!
end

#jpa_addRefreshOperation!Object



278
279
# File 'lib/ontomde-uml2-jpa/jpa.rb', line 278

def jpa_addRefreshOperation!
end

#jpa_addRemoveOperation!Object

xMDA does not generate generic DAO type remove method. object should be removed using their composite object



272
273
274
275
276
# File 'lib/ontomde-uml2-jpa/jpa.rb', line 272

def jpa_addRemoveOperation!
  # Possible implementation
  # get container
  # call remove method
end

#jpa_addStdImports!Object



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/ontomde-uml2-jpa/jpa.rb', line 396

def jpa_addStdImports!
  #java_import_add("java.util.ArrayList")
  #java_import_add("java.util.Set")
  #java_import_add("java.util.List")
  #java_import_add("javax.persistence.*")
  java_import_add("javax.persistence.Entity")
  java_import_add("javax.persistence.Table")
  java_import_add("javax.persistence.DiscriminatorColumn")
  java_import_add("javax.persistence.DiscriminatorValue")
  java_import_add("javax.persistence.DiscriminatorType")
  java_import_add("javax.persistence.Version")
  java_import_add("javax.persistence.Basic")
  java_import_add("javax.persistence.Column")
  java_import_add("javax.persistence.Lob")
  java_import_add("javax.persistence.Id")
  java_import_add("javax.persistence.GeneratedValue")
  java_import_add("javax.persistence.JoinTable")
  java_import_add("javax.persistence.OneToMany")
  java_import_add("javax.persistence.ManyToOne")
  java_import_add("javax.persistence.ManyToMany")
  java_import_add("javax.persistence.Temporal")
  java_import_add("javax.persistence.CascadeType")
  java_import_add("javax.persistence.FetchType")
  java_import_add("javax.persistence.OneToOne")
  java_import_add("javax.persistence.JoinColumn")
  java_import_add("javax.persistence.TemporalType")



  #com.francetelecom.rd.cil.commons.common.CommonException
  #com.francetelecom.rd.cil.commons.model.IdentifiedObject
  ##{java_packageQualifiedName}.vo.#{java_Name}Value
  ##{java_packageQualifiedName}.vo.*
end

#jpa_carryPersistence?Boolean

Returns:

  • (Boolean)


300
301
302
303
304
305
306
307
308
# File 'lib/ontomde-uml2-jpa/jpa.rb', line 300

def jpa_carryPersistence?
  qn=java_qualifiedName
  JAVA_NOT_PERSISTENT_NAMESPACE.each { |ns|
    return false if qn.index(ns)==0
  }
  #external is supposed not to carry persistence
  #return false if umlx_external?
  return true;
end

#jpa_deriveFromPersistent?Boolean

Returns:

  • (Boolean)


284
285
286
287
288
289
# File 'lib/ontomde-uml2-jpa/jpa.rb', line 284

def jpa_deriveFromPersistent?
  uml_generalization.each { |c|
    return true if c.uml_general_one.jpa_isPersistent?
  }
  return false
end

#jpa_hasPersistenceId?Boolean

Returns:

  • (Boolean)


310
311
312
313
314
# File 'lib/ontomde-uml2-jpa/jpa.rb', line 310

def jpa_hasPersistenceId?
  return false if jpa_deriveFromPersistent?
  return false unless jpa_carryPersistence?
  return true
end

#jpa_isPersistent?Boolean

Returns:

  • (Boolean)


290
291
292
293
294
295
296
297
298
299
# File 'lib/ontomde-uml2-jpa/jpa.rb', line 290

def jpa_isPersistent?
  #an external class is supposed to be not persistent
  #if umlx_external?
  #	return false
  #end
  return false if db_isTransient? || db_isDAO?
  return true if jpa_deriveFromPersistent?
  #return false unless uml_generalization.empty?
  return jpa_carryPersistence?
end

#jpa_tx(java_code) ⇒ Object

returns java_code enclosed in begin/commit with catch cleanup



248
249
250
# File 'lib/ontomde-uml2-jpa/jpa.rb', line 248

def jpa_tx(java_code)
  return "#{JPA_TX_BEGIN}#{java_code}#{JPA_TX_COMMIT}"
end

#jpa_writeConfigurationObject

writes jpa entry for this class if this class is persistent.



167
168
169
170
171
172
173
174
# File 'lib/ontomde-uml2-jpa/configuration.rb', line 167

def jpa_writeConfiguration()
  ignoreExternal=false
  return if java_ignoreMe?(ignoreExternal)
  return unless umlx_owner_one.kind_of?(Muml_Package)
  return unless jpa_isPersistent?
  return if db_isTransient?
  write %{<class>#{java_qualifiedName}</class>\n}
end

#log_error_db_tableName_too_long(tableName) ⇒ Object



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/ontomde-uml2-jpa/jpa.rb', line 315

def log_error_db_tableName_too_long(tableName)
  return if log_already_displayed?("#{rdf_uri}__tableName")
  log.error {
    <<END
********* SGBD TABLE NAME TOO LONG **********
** This software has detected a table name exceeding 63 character
** ( tableName(#{tableName.length})="#{tableName}" )
**
** This table name is related to element:
** #{mtk_object_message}
**
** Default Table name can be overidden using method call:
** db_addTableNameOverride("#{tableName}","MYSHORTTABLENAME")
**
** This model element is related to these other model elements:
#{mtk_related_message}********* SGBD TABLE NAME TOO LONG **********
END
  }
end