1 2 3 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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
public class CreateApplication
{
private EntityManagerFactory emf = null ;
private EntityManagerFactory setEMF()
{
if(this.emf == null) emf = Persistence.createEntityManagerFactory("user");
return this.emf;
}
public boolean createApplication(String label, String description, Resource represents)
{
label = StringOp.deleteBlanks(label);
if(!StringOp.isNull(label))
{
if(description != null) description = StringOp.deleteBlanks(description);
Application _app = new Application();
_app.setDescription(description);
_app.setInscription(new Date());
_app.setLabel(label);
_app.setRepresents(represents);
EntityManagerFactory emf = this.setEMF();
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
try{
tx.begin();
if(represents.getId() != null)
{
Resource _synchro_resource = em.find(Resource.class, represents.getId());
if(_synchro_resource != null) _app.setRepresents(_synchro_resource);
}
em.persist(_app);
tx.commit();
//em.close();
return true ;
}
catch(Exception e)
{
tx.rollback();
System.out.println("[CreateApplication.createApplication] fails to create application"
+ " label : " + label
+ " description : " + description
+ " cause : " + e.getMessage());
//em.close();
return false;
}
}
else
{
System.out.println("[CreateApplication.createApplication] fails to create application"
+ " label : " + label
+ " cause : this value is not correct" );
return false ;
}
}
} |
Partager